@@ -2,6 +2,7 @@ package jsonmap_test
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"sort"
6
7
"testing"
7
8
@@ -25,6 +26,19 @@ const jsonTest = `
25
26
}
26
27
`
27
28
29
+ type Person struct {
30
+ FirstName string
31
+ Name string
32
+ Age int
33
+ }
34
+
35
+ func (p * Person ) JSON () * jsonmap.Json {
36
+ j := jsonmap .New ()
37
+ j .Set ("name" , fmt .Sprintf ("%s %s" , p .FirstName , p .Name ))
38
+ j .Set ("age" , p .Age )
39
+ return j
40
+ }
41
+
28
42
func TestNewJson (t * testing.T ) {
29
43
j := jsonmap .New ()
30
44
assert .False (t , j .IsNil ())
@@ -116,42 +130,102 @@ func TestGetPath(t *testing.T) {
116
130
}
117
131
118
132
func TestSet (t * testing.T ) {
119
- j := jsonmap .New ()
120
- assert .True (t , j .Set ("" , 3.14 ))
121
- assert .Equal (t , 3.14 , j .AsFloat ())
122
-
123
- // Collision test => try to sets object on value
124
- assert .True (t , j .Set ("hello" , "world" ))
125
- assert .Equal (t , "world" , j .Get ("hello" ).AsString ())
126
-
127
- // Can reinject new value
128
- assert .True (t , j .Set ("" , "3.14" ))
129
- assert .Equal (t , "3.14" , j .AsString ())
130
-
131
- // Create auto map
132
- j = jsonmap .New ()
133
- assert .True (t , j .Set ("hello" , "world" ))
134
- assert .Equal (t , "world" , j .Get ("hello" ).AsString ())
135
- assert .True (t , j .Set ("the.number.pi.is" , 3.14 ))
136
- assert .Equal (t , 3.14 , j .Get ("the.number.pi.is" ).AsFloat ())
137
-
138
- // Can sets array
139
- j = jsonmap .FromString (jsonTest )
140
- assert .Equal (t , int64 (2 ), j .Get ("array[1]" ).AsInt ())
141
- assert .True (t , j .Set ("array[1]" , 3.14 ))
142
- assert .Equal (t , 3.14 , j .Get ("array[1]" ).AsFloat ())
143
- assert .Equal (t , "b" , j .Get ("object.sub[1].1" ).AsString ())
144
- assert .True (t , j .Set ("object.sub[1].1" , 3.14 ))
145
- assert .Equal (t , 3.14 , j .Get ("object.sub[1].1" ).AsFloat ())
133
+ t .Run ("can set root path" , func (t * testing.T ) {
134
+ j := jsonmap .New ()
135
+ assert .True (t , j .Set ("" , 3.14 ))
136
+ assert .JSONEq (t , "3.14" , j .Stringify ())
137
+ assert .Equal (t , 3.14 , j .AsFloat ())
138
+ })
139
+
140
+ t .Run ("can replace a value by an object" , func (t * testing.T ) {
141
+ j := jsonmap .New ()
142
+ assert .True (t , j .Set ("" , 3.14 ))
143
+ assert .True (t , j .Set ("hello" , "world" ))
144
+ assert .JSONEq (t , `{ "hello": "world" }` , j .Stringify ())
145
+ })
146
+
147
+ t .Run ("can replace an object by a value" , func (t * testing.T ) {
148
+ j := jsonmap .New ()
149
+ assert .True (t , j .Set ("hello" , "world" ))
150
+ assert .True (t , j .Set ("" , 3.14 ))
151
+ assert .JSONEq (t , "3.14" , j .Stringify ())
152
+ })
153
+
154
+ t .Run ("can create sub path" , func (t * testing.T ) {
155
+ j := jsonmap .New ()
156
+ assert .True (t , j .Set ("hello" , "world" ))
157
+ assert .True (t , j .Set ("the.number.pi.is" , 3.14 ))
158
+ assert .JSONEq (t , `{ "hello": "world", "the": { "number": { "pi": { "is": 3.14 }}}}` , j .Stringify ())
159
+ })
160
+
161
+ t .Run ("can create path with escape '.'" , func (t * testing.T ) {
162
+ j := jsonmap .New ()
163
+ assert .True (t , j .Set ("test\\ .machin" , 45 ))
164
+ assert .True (t , j .Set ("choux\\ .machin.truc" , "bidule" ))
165
+ assert .True (t , j .Set ("choux.machin\\ .truc" , "bidule" ))
166
+ assert .JSONEq (t , `{ "test.machin": 45, "choux.machin": { "truc": "bidule" }, "choux": { "machin.truc": "bidule" }}` , j .Stringify ())
167
+ })
146
168
147
- // can create auto array
148
- assert .True (t , j .Set ("hello" , 1 , 2 , 3 , 4 , 5 ))
149
- assert .Subset (t , []int {1 , 2 , 3 , 4 , 5 }, j .Get ("hello" ).AsArray ())
169
+ t .Run ("can set array" , func (t * testing.T ) {
170
+ j := jsonmap .New ()
171
+ assert .True (t , j .Set ("items" , []int {1 , 2 , 3 , 4 , 5 }))
172
+ assert .JSONEq (t , `{ "items": [1, 2, 3, 4, 5] }` , j .Stringify ())
150
173
151
- assert .True (t , j .Set ("test\\ .machin" , 45 ))
152
- assert .Equal (t , int64 (45 ), j .Get ("test\\ .machin" ).AsInt ())
153
- assert .True (t , j .Set ("choux\\ .machin.truc" , "bidule" ))
154
- assert .Equal (t , "bidule" , j .Get ("choux\\ .machin.truc" ).AsString ())
174
+ assert .True (t , j .Set ("items[2]" , 3.14 ))
175
+ assert .JSONEq (t , `{ "items": [1, 2, 3.14, 4, 5] }` , j .Stringify ())
176
+
177
+ assert .True (t , j .Set ("items[3]" , & Person {FirstName : "Thomas" , Name : "CHARLOT" , Age : 36 }))
178
+ assert .JSONEq (t , `{ "items": [1, 2, 3.14, {"name": "Thomas CHARLOT", "age": 36 }, 5] }` , j .Stringify ())
179
+
180
+ assert .True (t , j .Set ("items[3].age" , 37 ))
181
+ assert .JSONEq (t , `{ "items": [1, 2, 3.14, {"name": "Thomas CHARLOT", "age": 37 }, 5] }` , j .Stringify ())
182
+
183
+ assert .False (t , j .Set ("items[7]" , 11 ))
184
+ assert .JSONEq (t , `{ "items": [1, 2, 3.14, {"name": "Thomas CHARLOT", "age": 37 }, 5] }` , j .Stringify ())
185
+ })
186
+
187
+ t .Run ("can set nil" , func (t * testing.T ) {
188
+ j := jsonmap .New ()
189
+ assert .True (t , j .Set ("nil" , nil ))
190
+ assert .JSONEq (t , `{ "nil": null }` , j .Stringify ())
191
+ })
192
+
193
+ t .Run ("can set a sub json" , func (t * testing.T ) {
194
+ j := jsonmap .New ()
195
+ sub := jsonmap .New ()
196
+ assert .True (t , sub .Set ("pi" , 3.14 ))
197
+ assert .True (t , j .Set ("wrapped" , sub ))
198
+ assert .JSONEq (t , `{ "wrapped": { "pi": 3.14 }}` , j .Stringify ())
199
+ })
200
+
201
+ t .Run ("can set a json array" , func (t * testing.T ) {
202
+ j := jsonmap .New ()
203
+ items := []* jsonmap.Json {
204
+ jsonmap .FromString (`{ "string": "hello" }` ),
205
+ jsonmap .FromString (`{ "bool": true }` ),
206
+ jsonmap .FromString (`{ "number": 3.14 }` ),
207
+ jsonmap .FromString (`{ "array": [1,2,3,4,5] }` ),
208
+ jsonmap .FromString (`{ "object": { "a": 4, "1": "a" }}` ),
209
+ jsonmap .Nil (),
210
+ }
211
+
212
+ assert .True (t , j .Set ("items" , items ))
213
+ assert .JSONEq (t ,
214
+ `{ "items": [{ "string": "hello" }, { "bool": true }, { "number": 3.14 }, { "array": [1,2,3,4,5] }, { "object": { "a": 4, "1": "a" }}, null ]}` ,
215
+ j .Stringify (),
216
+ )
217
+ })
218
+
219
+ t .Run ("can set a jsonizer" , func (t * testing.T ) {
220
+ j := jsonmap .New ()
221
+ person := & Person {FirstName : "Thomas" , Name : "CHARLOT" , Age : 36 }
222
+ assert .True (t , j .Set ("person" , person ))
223
+ assert .JSONEq (t ,
224
+ `{ "person": { "name": "Thomas CHARLOT", "age": 36 }}` ,
225
+ j .Stringify (),
226
+ )
227
+
228
+ })
155
229
}
156
230
157
231
func TestWrap (t * testing.T ) {
0 commit comments