-
Notifications
You must be signed in to change notification settings - Fork 2
/
json_normalizer_test.go
90 lines (87 loc) · 2 KB
/
json_normalizer_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
package golden_test
import (
"github.com/franiglesias/golden"
"gotest.tools/v3/assert"
"testing"
)
/*
TestJsonNormalizer: given that JsonNormalizer relies on json.Marshal we can
trust that any object should be correctly marshaled. This test documents how to
use it, and that the output is cleaned of leading and trailing space chars, so
we can be reasonably sure that the normalized version will be consistent and the
snapshot can be compared with the subject without showing irrelevant differences.
*/
func TestJsonNormalizer(t *testing.T) {
tests := []struct {
name string
subject any
want string
}{
{
name: "should normalize string",
subject: "This is a string",
want: "This is a string",
},
{
name: "should normalize number to string",
subject: 123.45,
want: "123.45",
},
{
name: "should normalize slice",
subject: []string{
"Item 1",
"Item 2",
},
want: `[
"Item 1",
"Item 2"
]`,
},
{
name: "slice ordering",
subject: map[string]string{
"ZField": "content",
"OField": "content 2",
"AField": "123",
},
want: `{
"AField": "123",
"OField": "content 2",
"ZField": "content"
}`,
},
{
name: "should remove leading and trailing spaces",
subject: " This is a string ",
want: "This is a string",
},
{
name: "should remove leading and trailing new lines",
subject: "\nThis is a string\n",
want: "This is a string",
},
{
name: "should normalize json string prettifying it",
subject: `{"object":{"id":"12345", "name":"My Object", "count":1234, "validated": true, "other": {"remark": "accept"}}}`,
want: `{
"object": {
"count": 1234,
"id": "12345",
"name": "My Object",
"other": {
"remark": "accept"
},
"validated": true
}
}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
normalizer := golden.JsonNormalizer{}
normalized, _ := normalizer.Normalize(tt.subject)
assert.Equal(t, tt.want, normalized)
})
}
}