-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtoon_test.go
More file actions
384 lines (357 loc) · 7.87 KB
/
toon_test.go
File metadata and controls
384 lines (357 loc) · 7.87 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package gotoon
import (
"testing"
"time"
)
func TestEncodePrimitives(t *testing.T) {
tests := []struct {
name string
input interface{}
expected string
}{
{
name: "null",
input: nil,
expected: "null",
},
{
name: "boolean true",
input: true,
expected: "true",
},
{
name: "boolean false",
input: false,
expected: "false",
},
{
name: "integer",
input: 42,
expected: "42",
},
{
name: "float",
input: 3.14,
expected: "3.14",
},
{
name: "string",
input: "hello",
expected: "hello",
},
{
name: "string with spaces",
input: "hello world",
expected: "hello world",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Encode(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestEncodeObject(t *testing.T) {
tests := []struct {
name string
input interface{}
expected string
}{
{
name: "simple object",
input: map[string]interface{}{
"id": 123,
"name": "Ada",
},
expected: "id: 123\nname: Ada",
},
{
name: "nested object",
input: map[string]interface{}{
"user": map[string]interface{}{
"id": 123,
"name": "Ada",
},
},
expected: "user:\n id: 123\n name: Ada",
},
{
name: "object with boolean",
input: map[string]interface{}{
"active": true,
"name": "test",
},
expected: "active: true\nname: test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Encode(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", tt.expected, result)
}
})
}
}
func TestEncodePrimitiveArray(t *testing.T) {
tests := []struct {
name string
input interface{}
expected string
}{
{
name: "empty array",
input: []interface{}{},
expected: "[0]:",
},
{
name: "array of strings",
input: []string{"reading", "gaming"},
expected: "[2]: reading,gaming",
},
{
name: "array of numbers",
input: []int{1, 2, 3},
expected: "[3]: 1,2,3",
},
{
name: "object with array",
input: map[string]interface{}{
"tags": []string{"admin", "ops", "dev"},
},
expected: "tags[3]: admin,ops,dev",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Encode(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", tt.expected, result)
}
})
}
}
func TestEncodeTabularArray(t *testing.T) {
tests := []struct {
name string
input interface{}
expected string
}{
{
name: "array of objects with same keys",
input: map[string]interface{}{
"items": []map[string]interface{}{
{"id": 1, "name": "Alice", "role": "admin"},
{"id": 2, "name": "Bob", "role": "user"},
},
},
expected: "items[2]{id,name,role}:\n 1,Alice,admin\n 2,Bob,user",
},
{
name: "array of objects with numbers",
input: map[string]interface{}{
"items": []map[string]interface{}{
{"sku": "A1", "qty": 2, "price": 9.99},
{"sku": "B2", "qty": 1, "price": 14.5},
},
},
expected: "items[2]{price,qty,sku}:\n 9.99,2,A1\n 14.5,1,B2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Encode(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", tt.expected, result)
}
})
}
}
func TestEncodeMixedArray(t *testing.T) {
tests := []struct {
name string
input interface{}
expected string
}{
{
name: "mixed array",
input: map[string]interface{}{
"items": []interface{}{
1,
"text",
map[string]interface{}{"a": 1},
},
},
expected: "items[3]:\n - 1\n - text\n - a: 1",
},
{
name: "array of objects with different keys",
input: map[string]interface{}{
"items": []map[string]interface{}{
{"id": 1, "name": "First"},
{"id": 2, "name": "Second", "extra": true},
},
},
expected: "items[2]:\n - id: 1\n name: First\n - extra: true\n id: 2\n name: Second",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Encode(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", tt.expected, result)
}
})
}
}
func TestEncodeWithOptions(t *testing.T) {
input := map[string]interface{}{
"tags": []string{"a", "b", "c"},
}
t.Run("custom indent", func(t *testing.T) {
result, err := Encode(input, WithIndent(4))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "tags[3]: a,b,c"
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
})
t.Run("tab delimiter", func(t *testing.T) {
result, err := Encode(input, WithDelimiter("\t"))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "tags[3\t]: a\tb\tc"
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
})
t.Run("length marker", func(t *testing.T) {
result, err := Encode(input, WithLengthMarker())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "tags[#3]: a,b,c"
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
})
}
func TestEncodeStruct(t *testing.T) {
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
}
input := map[string]interface{}{
"users": []User{
{ID: 1, Name: "Alice", Role: "admin"},
{ID: 2, Name: "Bob", Role: "user"},
},
}
result, err := Encode(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "users[2]{id,name,role}:\n 1,Alice,admin\n 2,Bob,user"
if result != expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", expected, result)
}
}
func TestEncodeTime(t *testing.T) {
tm := time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC)
input := map[string]interface{}{
"timestamp": tm,
}
result, err := Encode(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "timestamp: \"2025-01-15T10:30:00Z\""
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
func TestEncodeQuoting(t *testing.T) {
tests := []struct {
name string
input interface{}
expected string
}{
{
name: "string with comma",
input: map[string]interface{}{
"note": "hello, world",
},
expected: "note: \"hello, world\"",
},
{
name: "string looking like boolean",
input: map[string]interface{}{
"items": []string{"true", "false"},
},
expected: "items[2]: \"true\",\"false\"",
},
{
name: "empty string",
input: map[string]interface{}{
"name": "",
},
expected: "name: \"\"",
},
{
name: "string with quotes",
input: map[string]interface{}{
"text": "say \"hi\"",
},
expected: "text: \"say \\\"hi\\\"\"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Encode(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", tt.expected, result)
}
})
}
}
func TestREADMEExample(t *testing.T) {
data := map[string]interface{}{
"users": []map[string]interface{}{
{"id": 1, "name": "Alice", "role": "admin"},
{"id": 2, "name": "Bob", "role": "user"},
},
}
result, err := Encode(data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "users[2]{id,name,role}:\n 1,Alice,admin\n 2,Bob,user"
if result != expected {
t.Errorf("expected:\n%s\n\ngot:\n%s", expected, result)
}
}