-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_test.go
293 lines (270 loc) · 7.71 KB
/
generator_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package testparrot
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
type testStruct struct {
V1 string
V2 int
V3 *testStruct
V4 []string
}
func TestNewGenerator(t *testing.T) {
generator := NewGenerator(pkgPath, pkgName)
require.IsType(t, &Generator{}, generator)
require.Equal(t, pkgName, generator.pkgName)
require.Equal(t, pkgPath, generator.pkgPath)
}
func TestGenerate(t *testing.T) {
recorder := NewRecorder()
recorder.Load("recorder1", []Recording{
{"key1", "value1"},
{"key2", 1},
})
recorder.Load("recorder2", []Recording{
{"key1", "value1"},
{"key2", 1},
})
buf := &bytes.Buffer{}
generator := NewGenerator(pkgPath, pkgName)
expected :=
"// Code generated by testparrot. DO NOT EDIT.\n\npackage testparrot\n\nfunc init() {\n\trecorder.Load(\"recorder1\", []Recording{" +
"{\n\t\tKey: \"key1\",\n\t\tValue: \"value1\",\n\t}, {\n\t\tKey: \"key2\",\n\t\tValue: 1,\n\t}})\n\trecorder.Load(\"recorder2\", []Recording{" +
"{\n\t\tKey: \"key1\",\n\t\tValue: \"value1\",\n\t}, {\n\t\tKey: \"key2\",\n\t\tValue: 1,\n\t}})\n}\n"
t.Run("to string", func(t *testing.T) {
err := generator.Generate(recorder, GenOptions{RecorderVar: "recorder"}, buf)
require.NoError(t, err)
require.Equal(t, expected, buf.String())
})
t.Run("to file", func(t *testing.T) {
genPath := path.Join(t.TempDir(), "gen.go")
err := generator.GenerateToFile(recorder, GenOptions{RecorderVar: "recorder"}, genPath)
require.NoError(t, err)
_, err = os.Stat(genPath)
require.NoError(t, err)
contents, err := ioutil.ReadFile(genPath)
if err != nil {
panic(err)
}
require.Equal(t, expected, string(contents))
})
}
func TestValToCode(t *testing.T) {
type wrappedBytes []byte
type simpleStruct struct {
V1 string
V2 int
V3 *float64
V4 bool
}
type nestedStruct struct {
V1 string
V2 *nestedStruct
}
type Key struct {
K1 string
K2 string
}
type Value struct {
V1 string
V2 int
}
type Enum string
tests := []struct {
name string
value interface{}
expected string
}{
{
name: "nil",
value: nil,
expected: "nil",
},
{
name: "literal str",
value: "test",
expected: "\"test\"",
},
{
name: "multiline str",
value: "long\nmulti\nline\nstrings\nare\ngenerated\nas\nmuliline\nstrings\nand\nthis\nmakes\neverything\nmore\nreadable",
expected: "`long\nmulti\nline\nstrings\nare\ngenerated\nas\nmuliline\nstrings\nand\nthis\nmakes\neverything\nmore\nreadable`",
},
{
name: "multiline bakcticks escape",
value: "long\nmulti\nline\nstrings\nare\ngenerated`\nas\nmuliline`\nstrings\nand\nthis\nmakes\neverything\nmore\nreadable",
expected: "`long\nmulti\nline\nstrings\nare\ngenerated` + \"`\" + `\nas\nmuliline` + \"`\" + `\nstrings\nand\nthis\nmakes\neverything\nmore\nreadable`",
},
{
name: "ptr literal",
value: Ptr("test"),
expected: "gotestparrot.Ptr(\"test\").(*string)",
},
{
name: "ptr to ptr",
value: Ptr(Ptr("test")),
expected: "gotestparrot.Ptr(gotestparrot.Ptr(\"test\").(*string))",
},
{
name: "slice uint8",
value: []uint8{0x62, 0x79, 0x74, 0x65, 0x61},
expected: "[]uint8{uint8(0x62), uint8(0x79), uint8(0x74), uint8(0x65), uint8(0x61)}",
},
{
name: "wrapped slice ptr",
value: Ptr(wrappedBytes("test")).(*wrappedBytes),
expected: "gotestparrot.Ptr(wrappedBytes(\"test\")).(*gotestparrot.wrappedBytes)",
},
{
name: "simple map",
value: map[string]string{
"key": "value",
},
expected: "map[string]string{\"key\": \"value\"}",
},
{
name: "double kv map",
value: map[Key]Value{
{"k1", "k2"}: {"v1", 1},
{"k1", "k3"}: {"v2", 3},
},
expected: "map[Key]Value{\n\tKey{\n\t\tK1: \"k1\",\n\t\tK2: \"k2\",\n\t}: Value{\n\t\tV1: \"v1\",\n\t\tV2: 1,\n\t},\n\tKey{\n\t\tK1: \"k1\",\n\t\tK2: \"k3\",\n\t}: Value{\n\t\tV1: \"v2\",\n\t\tV2: 3,\n\t},\n}",
},
{
name: "simple slice",
value: []string{"val1", "val2"},
expected: "[]string{\"val1\", \"val2\"}",
},
{
name: "struct slice",
value: []Value{{"v1", 1}, {"v2", 2}},
expected: "[]Value{{\n\tV1: \"v1\",\n\tV2: 1,\n}, {\n\tV1: \"v2\",\n\tV2: 2,\n}}",
},
{
name: "struct interface",
value: []interface{}{1, "test", Value{"v1", 1}},
expected: "[]interface{}{1, \"test\", Value{\n\tV1: \"v1\",\n\tV2: 1,\n}}",
},
{
name: "interface map",
value: map[interface{}]interface{}{
"key": "value",
10: "value",
},
expected: "map[interface{}]interface{}{\n\t\"key\": \"value\",\n\t10: \"value\",\n}",
},
{
name: "simple struct",
value: simpleStruct{
V1: "test",
V2: 10,
V3: Ptr(1.1).(*float64),
},
expected: "simpleStruct{\n\tV1: \"test\",\n\tV2: 10,\n\tV3: gotestparrot.Ptr(1.1).(*float64),\n}",
},
{
name: "nested struct",
value: nestedStruct{
V1: "string",
V2: &nestedStruct{
V1: "value",
},
},
expected: "nestedStruct{\n\tV1: \"string\",\n\tV2: &nestedStruct{V1: \"value\"},\n}",
},
{
name: "time",
value: time.Date(1999, 1, 2, 3, 4, 5, 0, time.FixedZone("UTC-8", -8*60*60)),
expected: "gotestparrot.Decode(\"1999-01-02T03:04:05-08:00\", time.Time{}).(time.Time)",
},
{
name: "timeptr",
value: valToPtr(time.Date(1999, 1, 2, 3, 4, 5, 0, time.FixedZone("UTC-8", -8*60*60))).(*time.Time),
expected: "gotestparrot.Decode(\"1999-01-02T03:04:05-08:00\", &time.Time{}).(*time.Time)",
},
{
name: "wrapped slice bytes",
value: wrappedBytes{0xff, 0xfe, 0xfd},
expected: `wrappedBytes{uint8(0xff), uint8(0xfe), uint8(0xfd)}`,
},
{
name: "wrapped slice string",
value: wrappedBytes(`test`),
expected: `wrappedBytes("test")`,
},
{
name: "uuid",
value: uuid.MustParse("6ba7b814-9dad-11d1-80b4-00c04fd430c8"),
expected: "gotestparrot.Decode(\"6ba7b814-9dad-11d1-80b4-00c04fd430c8\", uuid.UUID{}).(uuid.UUID)",
},
{
name: "uuid ptr",
value: valToPtr(uuid.MustParse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")),
expected: "gotestparrot.Decode(\"6ba7b814-9dad-11d1-80b4-00c04fd430c8\", &uuid.UUID{}).(*uuid.UUID)",
},
{
name: "time interface slice",
value: []interface{}{
must(time.Parse(time.RFC3339, "1999-01-02T03:04:05Z")),
},
expected: "[]interface{}{gotestparrot.Decode(\"1999-01-02T03:04:05Z\", time.Time{}).(time.Time)}",
},
{
name: "enum",
value: Enum("test"),
expected: `Enum("test")`,
},
{
name: "enum ptr",
value: valToPtr(Enum("test")).(*Enum),
expected: `gotestparrot.Ptr(Enum("test")).(*Enum)`,
},
{
name: "anonymous struct",
value: struct {
Field1 string `json:"key1"`
Field2 struct {
Field3 string
Field4 int
} `json:"key2"`
Field3 []struct {
Field1 string
Field2 int
}
Field4 *struct {
Field1 int
}
Field5 Value
Field6 **[]Value
Field7 struct {
Value
Value2 Value
}
}{},
expected: "struct {\n\tField1 string `json:\"key1\"`\n\tField2 struct {\n\t\tField3 string\n\t\tField4 int\n\t} " +
"`json:\"key2\"`\n\tField3 []struct {\n\t\tField1 string\n\t\tField2 int\n\t}\n\tField4 *struct {\n\t\tField1 " +
"int\n\t}\n\tField5 Value\n\tField6 **[]Value\n\tField7 struct {\n\t\tValue\n\t\tValue2 Value\n\t}\n}{}",
},
{
name: "anonymous slice struct",
value: []struct{ Field1 string }{},
expected: "[]struct {\n\tField1 string\n}{}",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
g := NewGenerator(pkgPath, pkgName)
code, err := valToCode(g, reflect.ValueOf(test.value), reflect.Value{})
require.NoError(t, err)
require.Equal(t, test.expected, fmt.Sprintf("%#v", code))
})
}
}