-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
109 lines (106 loc) · 2.27 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package textra
import (
"errors"
"reflect"
"strings"
"testing"
)
func TestParseTags(t *testing.T) {
tests := []struct {
name string
tag reflect.StructTag
want Tags
}{
{
name: "Test with single tag",
tag: `json:"name"`,
want: []Tag{{Tag: "json", Value: "name"}},
},
{
name: "Test with multiple tags",
tag: `json:"name,omitempty" xml:"Name,v1,v2" sql:"-"`,
want: []Tag{
{Tag: "json", Value: "name", Optional: []string{"omitempty"}},
{Tag: "xml", Value: "Name", Optional: []string{"v1", "v2"}},
{Tag: "sql", Value: "-"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseTags(tt.tag); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseTags() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseType(t *testing.T) {
tests := []struct {
name string
typ reflect.Type
want string
}{
{
name: "Simple type",
typ: reflect.TypeOf(42),
want: "int",
},
{
name: "Struct type",
typ: reflect.TypeOf(struct{}{}),
want: "struct",
},
{
name: "Ptr type",
typ: reflect.TypeOf(&struct{}{}),
want: "*struct",
},
{
name: "Slice type",
typ: reflect.TypeOf([]bool{}),
want: "[]bool",
},
{
name: "Complex slice type",
typ: reflect.TypeOf([]*[]*string{}),
want: "[]*[]*string",
},
{
name: "Map type",
typ: reflect.TypeOf(map[string]int{}),
want: "map[string]int",
},
{
name: "Complex map type",
typ: reflect.TypeOf(map[*string]map[*bool]*reflect.Type{}),
want: "map[*string]map[*bool]*reflect.Type",
},
{
name: "Func type",
typ: reflect.TypeOf(func() {}),
want: "func()",
},
{
name: "Comples func type",
typ: reflect.TypeOf(func(int, map[*string]map[*bool]*reflect.Type, string) (*bool, error) { return nil, nil }),
want: "func(int, map[*string]map[*bool]*reflect.Type, string) *bool, error",
},
{
name: "Interface type",
typ: reflect.TypeOf((*interface{})(nil)),
want: "interface",
},
{
name: "Error type",
typ: reflect.TypeOf(errors.New("test")),
want: "error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseType(tt.typ); !strings.Contains(got, tt.want) {
t.Errorf("parseType() = %v, want %v", got, tt.want)
}
})
}
}