forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes_test.go
154 lines (141 loc) · 7.78 KB
/
types_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package codegen
import (
"testing"
"goa.design/goa/v3/expr"
)
func TestGoTypeDef(t *testing.T) {
var (
simpleArray = &expr.AttributeExpr{
Type: &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.Boolean}}}
simpleMap = &expr.AttributeExpr{
Type: &expr.Map{
KeyType: &expr.AttributeExpr{Type: expr.Int},
ElemType: &expr.AttributeExpr{Type: expr.String},
}}
requiredObj = &expr.AttributeExpr{
Type: &expr.Object{
{Name: "IntField", Attribute: &expr.AttributeExpr{Type: expr.Int}},
{Name: "StringField", Attribute: &expr.AttributeExpr{Type: expr.String}},
},
Validation: &expr.ValidationExpr{Required: []string{"IntField", "StringField"}}}
defaultObj = &expr.AttributeExpr{
Type: &expr.Object{
{Name: "IntField", Attribute: &expr.AttributeExpr{Type: expr.Int, DefaultValue: 1}},
{Name: "StringField", Attribute: &expr.AttributeExpr{Type: expr.String, DefaultValue: "foo"}},
}}
ut = &expr.UserTypeExpr{AttributeExpr: &expr.AttributeExpr{Type: expr.Boolean}, TypeName: "UserType"}
rt = &expr.ResultTypeExpr{UserTypeExpr: &expr.UserTypeExpr{AttributeExpr: &expr.AttributeExpr{Type: expr.Boolean}, TypeName: "ResultType"}, Identifier: "application/vnd.goa.example", Views: nil}
userType = &expr.AttributeExpr{Type: ut}
resultType = &expr.AttributeExpr{Type: rt}
stringMetaType = expr.MetaExpr{"struct:field:type": []string{"string"}}
jsonWithImportMetaType = expr.MetaExpr{"struct:field:type": []string{"json.RawMessage", "encoding/json"}}
jsonWithRenameMetaType = expr.MetaExpr{"struct:field:type": []string{"jason.RawMessage", "encoding/json", "jason"}}
structPkgPathMetaType = expr.MetaExpr{"struct:pkg:path": []string{"types"}}
utPkgPathMeta = &expr.UserTypeExpr{AttributeExpr: &expr.AttributeExpr{Type: expr.Boolean, Meta: structPkgPathMetaType}, TypeName: "UserType"}
mixedObj = &expr.AttributeExpr{
Type: &expr.Object{
{Name: "IntField", Attribute: &expr.AttributeExpr{Type: expr.Int}},
{Name: "ArrayField", Attribute: simpleArray},
{Name: "MapField", Attribute: simpleMap},
{Name: "UserTypeField", Attribute: userType},
{Name: "MetaTypeField", Attribute: &expr.AttributeExpr{Type: expr.Int, Meta: jsonWithImportMetaType}},
{Name: "QualifiedMetaTypeField", Attribute: &expr.AttributeExpr{Type: expr.Int, Meta: jsonWithRenameMetaType}},
{Name: "StructPkgPath", Attribute: &expr.AttributeExpr{Type: utPkgPathMeta}},
},
Validation: &expr.ValidationExpr{Required: []string{"IntField", "ArrayField", "MapField", "UserTypeField", "MetaTypeField", "QualifiedMetaTypeField"}}}
)
cases := map[string]struct {
att *expr.AttributeExpr
pointer bool
usedefault bool
expected string
}{
"BooleanKind": {&expr.AttributeExpr{Type: expr.Boolean}, false, true, "bool"},
"IntKind": {&expr.AttributeExpr{Type: expr.Int}, false, true, "int"},
"Int32Kind": {&expr.AttributeExpr{Type: expr.Int32}, false, true, "int32"},
"Int64Kind": {&expr.AttributeExpr{Type: expr.Int64}, false, true, "int64"},
"UIntKind": {&expr.AttributeExpr{Type: expr.UInt}, false, true, "uint"},
"UInt32Kind": {&expr.AttributeExpr{Type: expr.UInt32}, false, true, "uint32"},
"UInt64Kind": {&expr.AttributeExpr{Type: expr.UInt64}, false, true, "uint64"},
"Float32Kind": {&expr.AttributeExpr{Type: expr.Float32}, false, true, "float32"},
"Float64Kind": {&expr.AttributeExpr{Type: expr.Float64}, false, true, "float64"},
"StringKind": {&expr.AttributeExpr{Type: expr.String}, false, true, "string"},
"BytesKind": {&expr.AttributeExpr{Type: expr.Bytes}, false, true, "[]byte"},
"AnyKind": {&expr.AttributeExpr{Type: expr.Any}, false, true, "any"},
"Array": {simpleArray, false, true, "[]bool"},
"Map": {simpleMap, false, true, "map[int]string"},
"UserTypeExpr": {userType, false, true, "UserType"},
"ResultTypeExpr": {resultType, false, true, "ResultType"},
"Object": {requiredObj, false, true, "struct {\n\tIntField int\n\tStringField string\n}"},
"ObjDefault": {defaultObj, false, true, "struct {\n\tIntField int\n\tStringField string\n}"},
"ObjDefaultNoDef": {defaultObj, false, false, "struct {\n\tIntField *int\n\tStringField *string\n}"},
"ObjMixed": {mixedObj, false, true, "struct {\n\tIntField int\n\tArrayField []bool\n\tMapField map[int]string\n\tUserTypeField UserType\n\tMetaTypeField json.RawMessage\n\tQualifiedMetaTypeField jason.RawMessage\n\tStructPkgPath *types.UserType\n}"},
"ObjMixedPointer": {mixedObj, true, true, "struct {\n\tIntField *int\n\tArrayField []bool\n\tMapField map[int]string\n\tUserTypeField *UserType\n\tMetaTypeField *json.RawMessage\n\tQualifiedMetaTypeField *jason.RawMessage\n\tStructPkgPath *types.UserType\n}"},
"MetaTypeSameAsDesign": {&expr.AttributeExpr{Type: expr.String, Meta: stringMetaType}, false, true, "string"},
"MetaTypeOverrideDesign": {&expr.AttributeExpr{Type: expr.String, Meta: jsonWithImportMetaType}, false, true, "json.RawMessage"},
"MetaTypeOverrideDesignWithQualifiedImport": {&expr.AttributeExpr{Type: expr.String, Meta: jsonWithRenameMetaType}, false, true, "jason.RawMessage"},
}
for k, tc := range cases {
scope := NewNameScope()
actual := scope.GoTypeDef(tc.att, tc.pointer, tc.usedefault)
if actual != tc.expected {
t.Errorf("%s: got %#v, expected %#v", k, actual, tc.expected)
}
}
}
func TestGoNativeTypeName(t *testing.T) {
cases := map[string]struct {
dataType expr.DataType
expected string
}{
"BooleanKind": {expr.Boolean, "bool"},
"IntKind": {expr.Int, "int"},
"Int32Kind": {expr.Int32, "int32"},
"Int64Kind": {expr.Int64, "int64"},
"UIntKind": {expr.UInt, "uint"},
"UInt32Kind": {expr.UInt32, "uint32"},
"UInt64Kind": {expr.UInt64, "uint64"},
"Float32Kind": {expr.Float32, "float32"},
"Float64Kind": {expr.Float64, "float64"},
"StringKind": {expr.String, "string"},
"BytesKind": {expr.Bytes, "[]byte"},
"AnyKind": {expr.Any, "any"},
}
for k, tc := range cases {
actual := GoNativeTypeName(tc.dataType)
if actual != tc.expected {
t.Errorf("%s: got %#v, expected %#v", k, actual, tc.expected)
}
}
}
func TestGoify(t *testing.T) {
cases := map[string]struct {
str string
firstUpper bool
expected string
}{
"empty": {"", false, ""},
"first upper false": {"blue_id", false, "blueID"},
"first upper false normal identifier all lower": {"blue", false, "blue"},
"first upper false and UUID": {"blue_uuid", false, "blueUUID"},
"first upper true": {"blue_id", true, "BlueID"},
"first upper true and UUID": {"blue_uuid", true, "BlueUUID"},
"first upper true normal identifier all lower": {"blue", true, "Blue"},
"first upper false normal identifier": {"Blue", false, "blue"},
"first upper true normal identifier": {"Blue", true, "Blue"},
"invalid identifier": {"Blue%50", true, "Blue50"},
"invalid identifier firstupper false": {"Blue%50", false, "blue50"},
"only UUID and firstupper false": {"UUID", false, "uuid"},
"consecutives invalid identifiers firstupper false": {"[[fields___type]]", false, "fieldsType"},
"consecutives invalid identifiers": {"[[fields___type]]", true, "FieldsType"},
"invalid identifiers": {"[[", false, "val"},
"middle upper firstupper false": {"MiddleUpper", false, "middleUpper"},
"middle upper": {"MiddleUpper", true, "MiddleUpper"},
}
for k, tc := range cases {
actual := Goify(tc.str, tc.firstUpper)
if actual != tc.expected {
t.Errorf("%s: got %#v, expected %#v", k, actual, tc.expected)
}
}
}