-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_test.go
122 lines (104 loc) · 2.74 KB
/
extract_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
110
111
112
113
114
115
116
117
118
119
120
121
122
package textra_test
import (
"reflect"
"testing"
"time"
"github.com/ravsii/textra"
)
func TestExtract(t *testing.T) {
type Empty struct{}
type TesterFilled struct {
Tag1 struct{} `json:"tag1"`
Tag2 struct{} `json:"tag2" sql:"tag2, pk"`
}
expectedFilled := textra.Struct{
textra.Field{
Name: "Tag1",
Type: "struct",
Tags: textra.Tags{
{"json", "tag1", nil},
},
},
textra.Field{
Name: "Tag2",
Type: "struct",
Tags: textra.Tags{
{"json", "tag2", nil},
{"sql", "tag2", []string{"pk"}},
},
},
}
testCases := []struct {
name string
input interface{}
want textra.Struct
}{
{"Empty, non-pointer", Empty{}, nil},
{"Empty, pointer", &Empty{}, nil},
{"Empty, nil-check", (*Empty)(nil), nil},
{"Populated, non-pointer", TesterFilled{}, expectedFilled},
{"Populated, pointer", &TesterFilled{}, expectedFilled},
{"Populated, nil-check", (*TesterFilled)(nil), expectedFilled},
}
for _, testCase := range testCases {
testCase := testCase
got := textra.Extract(testCase.input)
// nil / empty checks
if len(got) == len(testCase.want) && reflect.DeepEqual(got, textra.Struct{}) {
continue
}
if !reflect.DeepEqual(got, testCase.want) {
t.Errorf("%s: got %v want %v", testCase.name, got, testCase.want)
}
}
}
func TestExtractNonSlice(t *testing.T) {
boolptr := true
testCases := []interface{}{
4,
uintptr(1),
"",
"test",
true,
false,
&boolptr,
}
for _, testCase := range testCases {
got := textra.Extract(testCase)
// nil / empty checks
if got != nil {
t.Errorf("%T: result should be nil", testCase)
}
}
}
func TestExtractFieldType(t *testing.T) {
testCases := []struct {
name string
str interface{}
field string
wantType string
}{
{"string", struct{ a string }{}, "a", "string"},
{"*string", struct{ a *string }{}, "a", "*string"},
{"interface", struct{ a interface{} }{}, "a", "interface"},
{"*interface {}", struct{ a *interface{} }{}, "a", "*interface {}"},
{"*textra.Field", struct{ a *textra.Field }{}, "a", "*textra.Field"},
{"*[]string", struct{ a *[]string }{}, "a", "*[]string"},
{"*[]*string", struct{ a *[]*string }{}, "a", "*[]*string"},
{"struct", struct{ a struct{} }{}, "a", "struct"},
{"*struct {}", struct{ a *struct{} }{}, "a", "*struct {}"},
{"time.Time", struct{ a time.Time }{}, "a", "time.Time"},
{"*time.Time", struct{ a *time.Time }{}, "a", "*time.Time"},
}
for _, testCase := range testCases {
testCase := testCase
field, ok := textra.Extract(testCase.str).Field(testCase.field)
if !ok {
t.Errorf("%s: field %s not found", testCase.name, testCase.field)
}
got := field.Type
if got != testCase.wantType {
t.Errorf("%s: got %s want %s", testCase.name, got, testCase.wantType)
}
}
}