-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathuser_type_test.go
93 lines (88 loc) Β· 1.75 KB
/
user_type_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
package expr
import "testing"
func TestUserTypeExprName(t *testing.T) {
var (
userTypeExprWithoutAttribute = UserTypeExpr{
TypeName: "foo",
}
userTypeExprHasMeta = UserTypeExpr{
TypeName: "foo",
AttributeExpr: &AttributeExpr{
Meta: MetaExpr{
"struct:type:name": []string{"bar"},
},
},
}
userTypeExprHasAnotherMeta = UserTypeExpr{
TypeName: "foo",
AttributeExpr: &AttributeExpr{
Meta: MetaExpr{
"struct:field:name": []string{"baz"},
},
},
}
)
cases := map[string]struct {
userType UserTypeExpr
expected string
}{
"attribute in user type is nill": {
userType: userTypeExprWithoutAttribute,
expected: "foo",
},
"user type has meta": {
userType: userTypeExprHasMeta,
expected: "bar",
},
"user type has another meta": {
userType: userTypeExprHasAnotherMeta,
expected: "foo",
},
}
for k, tc := range cases {
t.Run(k, func(t *testing.T) {
if actual := tc.userType.Name(); actual != tc.expected {
t.Errorf("got %#v, expected %#v", actual, tc.expected)
}
})
}
}
func TestUserTypeExprIsCompatible(t *testing.T) {
var (
b = true
i = 1
)
cases := map[string]struct {
typ DataType
values []any
expected bool
}{
"compatible": {
typ: Int,
values: []any{i},
expected: true,
},
"not compatible": {
typ: Int,
values: []any{b},
expected: false,
},
"type is nil": {
typ: nil,
values: []any{b, i},
expected: true,
},
}
for k, tc := range cases {
u := UserTypeExpr{
AttributeExpr: &AttributeExpr{
Type: tc.typ,
},
}
for _, value := range tc.values {
if actual := u.IsCompatible(value); tc.expected != actual {
t.Errorf("%s: got %#v, expected %#v", k, actual, tc.expected)
}
}
}
}