-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonobj_test.go
163 lines (129 loc) · 3.22 KB
/
jsonobj_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
package jsonobj
import (
"encoding/json"
"testing"
"github.com/0xrawsec/toast"
)
type MyStruct struct {
Field1 string
Field2 int
EmbeddedStruct
}
type Dummy struct {
A string
B int64
}
type EmbeddedStruct struct {
Field3 float64
IntSlice []int
StructSlice []Dummy
M map[string]int
}
func jsonStr(v any) string {
if b, err := json.Marshal(v); err != nil {
panic(err)
} else {
return string(b)
}
}
func TestObject(t *testing.T) {
t.Parallel()
tt := toast.FromT(t)
o := New()
tt.Assert(o.IsEmpty())
o.SetField("test", 42)
o.SetField("toast", 42)
tt.Assert(o.GetField("test").(int) == 42)
_, err := json.Marshal(&o)
tt.CheckErr(err)
// the field is already there so SetField will modify it
o.SetField("test", 43)
tt.Assert(o.HasField("test"))
tt.Assert(o.GetField("test").(int) == 43)
tt.Assert(!o.HasField("unknown"))
}
func TestNestedObject(t *testing.T) {
t.Parallel()
tt := toast.FromT(t)
s := MyStruct{
Field1: "test",
Field2: 42,
EmbeddedStruct: EmbeddedStruct{Field3: 42, IntSlice: []int{42}, StructSlice: []Dummy{{
A: "Ola",
B: 4242,
}},
M: map[string]int{"test": 42},
},
}
o := FromStruct(s)
b, err := json.Marshal(&o)
tt.CheckErr(err)
tt.Log(string(b))
js, err := json.Marshal(&s)
tt.CheckErr(err)
tt.Log(string(js))
// json object should be equal with json serialization
tt.Assert(string(b) == string(js))
new := MyStruct{}
tt.CheckErr(json.Unmarshal(b, &new))
// serialization after deserialization should not change anything
tt.Assert(jsonStr(new) == jsonStr(s))
}
func TestNestedObjectLowercase(t *testing.T) {
t.Parallel()
tt := toast.FromT(t)
s := MyStruct{
Field1: "test",
Field2: 42,
EmbeddedStruct: EmbeddedStruct{Field3: 42, IntSlice: []int{42}, StructSlice: []Dummy{{
A: "Ola",
B: 4242,
}},
M: map[string]int{"test": 42},
},
}
o := FromStructWithOptions(s, Options{LowerCase})
b, err := json.Marshal(&o)
tt.CheckErr(err)
tt.Log(string(b))
new := MyStruct{}
tt.CheckErr(json.Unmarshal(b, &new))
// serialization after deserialization should not change anything
tt.Assert(jsonStr(new) == jsonStr(s))
}
func TestConvertSlice(t *testing.T) {
t.Parallel()
tt := toast.FromT(t)
s := []interface{}{
[]int{42},
map[string]string{"toast": "test"},
}
o := New()
tt.Log(jsonStr(o.ConvertSlice(s)))
}
func TestFromMap(t *testing.T) {
t.Parallel()
tt := toast.FromT(t)
m := map[string]any{
"B": "test",
"A": 42,
"C": []int{1, 2, 3}}
o := FromMap(m)
// key order is not guaranteed
s := jsonStr(o)
tt.Log(s)
new := new(map[string]any)
json.Unmarshal([]byte(s), &new)
tt.Assert(jsonStr(m) == jsonStr(new))
}
func TestSnakeCase(t *testing.T) {
t.Parallel()
tt := toast.FromT(t)
tt.Assert(camelToSnake("TestTest") == "test_test", "Unexpected snake case")
tt.Assert(camelToSnake("TestTEST") == "test_test", "Unexpected snake case")
tt.Assert(camelToSnake("OneTWOThree") == "one_two_three", "Unexpected snake case")
tt.Assert(camelToSnake("One2Three") == "one_2_three", "Unexpected snake case")
tt.Assert(camelToSnake("One23") == "one_23", "Unexpected snake case")
tt.Assert(camelToSnake("1Step2Step") == "1_step_2_step", "Unexpected snake case")
tt.Assert(camelToSnake("123") == "123", "Unexpected snake case")
}