-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_test.go
More file actions
372 lines (342 loc) · 9.87 KB
/
object_test.go
File metadata and controls
372 lines (342 loc) · 9.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
package anytype_test
import (
"sync"
"testing"
"github.com/DanielSvub/anytype"
)
func TestObject(t *testing.T) {
t.Run("basics", func(t *testing.T) {
l := List()
o := Object(
"first", 1,
"second", 2,
"third", l,
)
if !o.Keys().Contains("second") {
t.Error("key list should contain the key")
}
if !o.Values().Contains(2) {
t.Error("value list should contain the value")
}
if !o.Contains(l) {
t.Error("list should contain the list")
}
if !o.Contains(1) {
t.Error("object should not contain value 4")
}
if o.Contains(4) {
t.Error("object should not contain value 4")
}
if o.Count() != 3 {
t.Error("object should have 3 fields")
}
if o.KeyOf(2) != "second" {
t.Error("key for value 2 should be 'second'")
}
if o.Equals(Object()) {
t.Error("object should not be equal to empty object")
}
if o.Pluck("first", "second").Count() != 2 {
t.Error("plucked object should have 2 fields")
}
o.Unset("third")
if !Object("first", 1).Merge(Object("second", 2)).Equals(o) {
t.Error("merge does not work properly")
}
json := o.String()
if json != `{"first":1,"second":2}` && json != `{"second":2,"first":1}` {
t.Error("serialization does not work properly")
}
o.Clear()
if !o.Empty() {
t.Error("object should be empty")
}
})
t.Run("types", func(t *testing.T) {
o := Object(
"object", Object("test", 0),
"list", List(0),
"string", "test",
"bool", true,
"int", 1,
"float", 3.14,
"nil", nil,
)
if o.TypeOf("object") != anytype.TypeObject {
t.Error("field should be an object")
}
if o.TypeOf("list") != anytype.TypeList {
t.Error("field should be a list")
}
if o.TypeOf("string") != anytype.TypeString {
t.Error("field should be a string")
}
if o.TypeOf("bool") != anytype.TypeBool {
t.Error("field should be a bool")
}
if o.TypeOf("int") != anytype.TypeInt {
t.Error("field should be an int")
}
if o.TypeOf("float") != anytype.TypeFloat {
t.Error("field should be a float")
}
if o.TypeOf("nil") != anytype.TypeNil {
t.Error("field should be nil")
}
if o.TypeOf("undefined") != anytype.TypeUndefined {
t.Error("field should be undefined")
}
})
t.Run("equality", func(t *testing.T) {
if Object("first", 1).Equals(Object("second", 2)) {
t.Error("equality check does not work properly")
}
})
t.Run("cloning", func(t *testing.T) {
o := Object(
"object", Object("test", 0),
"list", List(0),
"string", "test",
"bool", true,
"int", 1,
"float", 3.14,
"nil", nil,
)
if !o.Equals(o.Clone()) {
t.Error("object should be equal to itself")
}
})
t.Run("dictionaries", func(t *testing.T) {
if !ObjectFrom(Object("str", "test", "int", 1).Dict()).Equals(Object("str", "test", "int", 1)) {
t.Error("conversion to map[string]any does not work")
}
if !ObjectFrom(map[string]object{"obj": Object()}).Equals(Object("obj", Object())) {
t.Error("conversion from map[string]object does not work")
}
if !ObjectFrom(map[string]list{"list": List()}).Equals(Object("list", List())) {
t.Error("conversion from map[string]object does not work")
}
if !ObjectFrom(map[string]string{"str": "test"}).Equals(Object("str", "test")) {
t.Error("conversion from map[string]object does not work")
}
if !ObjectFrom(map[string]bool{"bool": true}).Equals(Object("bool", true)) {
t.Error("conversion from map[string]object does not work")
}
if !ObjectFrom(map[string]int{"int": 1}).Equals(Object("int", 1)) {
t.Error("conversion from map[string]object does not work")
}
if !ObjectFrom(map[string]float64{"float": 3.14}).Equals(Object("float", 3.14)) {
t.Error("conversion from map[string]object does not work")
}
})
t.Run("getters", func(t *testing.T) {
o := Object(
"object", Object("test", 0),
"list", List(0),
"string", "test",
"bool", true,
"int", 1,
"float", 3.14,
"nil", nil,
)
if o.Get("int").(int) != 1 {
t.Error("Int field should be 1")
}
if !o.GetObject("object").Equals(Object("test", 0)) {
t.Error("cannot acquire an object")
}
if !o.GetList("list").Equals(List(0)) {
t.Error("cannot acquire a list")
}
if o.GetString("string") != "test" {
t.Error("cannot acquire a string")
}
if !o.GetBool("bool") {
t.Error("cannot acquire a bool")
}
if o.GetInt("int") != 1 {
t.Error("cannot acquire an int")
}
if o.GetFloat("float") != 3.14 {
t.Error("cannot acquire a float")
}
})
t.Run("foreaches", func(t *testing.T) {
o := Object(
"object", Object("test", 0),
"list", List(0),
"string", "test",
"bool", true,
"int", 1,
"float", 3.14,
"nil", nil,
)
t1 := Object()
o.ForEach(func(key string, value any) { t1.Set(key, value) })
if !t1.Equals(o) {
t.Error("standard ForEach does not work properly")
}
t2 := Object()
o.ForEachValue(func(value any) { t2.Set(o.KeyOf(value), value) })
if !t2.Equals(o) {
t.Error("ForEachValue does not work properly")
}
t3 := Object()
o.ForEachObject(func(value object) { t3.Set(o.KeyOf(value), value) })
if !t3.Equals(Object("object", Object("test", 0))) {
t.Error("ForEachObject does not work properly")
}
t4 := Object()
o.ForEachList(func(value list) { t4.Set(o.KeyOf(value), value) })
if !t4.Equals(Object("list", List(0))) {
t.Error("ForEachList does not work properly")
}
t5 := Object()
o.ForEachString(func(value string) { t5.Set(o.KeyOf(value), value) })
if !t5.Equals(Object("string", "test")) {
t.Error("ForEachString does not work properly")
}
t6 := Object()
o.ForEachBool(func(value bool) { t6.Set(o.KeyOf(value), value) })
if !t6.Equals(Object("bool", true)) {
t.Error("ForEachBool does not work properly")
}
t7 := Object()
o.ForEachInt(func(value int) { t7.Set(o.KeyOf(value), value) })
if !t7.Equals(Object("int", 1)) {
t.Error("ForEachInt does not work properly")
}
t8 := Object()
o.ForEachFloat(func(value float64) { t8.Set(o.KeyOf(value), value) })
if !t8.Equals(Object("float", 3.14)) {
t.Error("ForEachFloat does not work properly")
}
t9 := Object()
var mutex sync.Mutex
o.ForEachAsync(func(key string, value any) {
mutex.Lock()
t9.Set(key, value)
mutex.Unlock()
})
if !t1.Equals(o) {
t.Error("async ForEach does not work properly")
}
})
t.Run("mappings", func(t *testing.T) {
o := Object(
"object", Object("test", 0),
"list", List(0),
"string", "test",
"bool", true,
"int", 1,
"float", 3.14,
"nil", nil,
)
if !o.Map(func(key string, value any) any { return value }).Equals(o) {
t.Error("standard map does not work properly")
}
if !o.MapValues(func(value any) any { return value }).Equals(o) {
t.Error("MapValues does not work properly")
}
if !o.MapObjects(func(value object) any { return value }).Equals(Object(
"object", Object("test", 0),
)) {
t.Error("MapObjects does not work properly")
}
if !o.MapLists(func(value list) any { return value }).Equals(Object(
"list", List(0),
)) {
t.Error("MapLists does not work properly")
}
if !o.MapStrings(func(value string) any { return value }).Equals(Object(
"string", "test",
)) {
t.Error("MapStrings does not work properly")
}
if !o.MapBools(func(value bool) any { return value }).Equals(Object(
"bool", true,
)) {
t.Error("MapBools does not work properly")
}
if !o.MapInts(func(value int) any { return value }).Equals(Object(
"int", 1,
)) {
t.Error("MapInts does not work properly")
}
if !o.MapFloats(func(value float64) any { return value }).Equals(Object(
"float", 3.14,
)) {
t.Error("MapFloats does not work properly")
}
if !o.MapAsync(func(key string, value any) any { return value }).Equals(o) {
t.Error("async map does not work properly")
}
})
}
func TestObjectPanics(t *testing.T) {
catch := func(msg string) {
if r := recover(); r == nil {
t.Error(msg)
}
}
t.Run("unsupportedMap", func(t *testing.T) {
defer catch("creating object from string did not cause panic")
ObjectFrom("unsupported")
})
t.Run("invalidSet", func(t *testing.T) {
defer catch("invalid setting did not cause panic")
Object("first", 1, "second")
})
t.Run("invalidKey", func(t *testing.T) {
defer catch("invalid key did not cause panic")
Object(1, 1)
})
t.Run("invalidGet", func(t *testing.T) {
defer catch("getting non-existing field did not cause panic")
Object().Get("test")
})
t.Run("invalidObjectGet", func(t *testing.T) {
defer catch("getting non-object field as object did not cause panic")
Object("test", false).GetObject("test")
})
t.Run("invalidListGet", func(t *testing.T) {
defer catch("getting non-list field as list did not cause panic")
Object("test", false).GetList("test")
})
t.Run("invalidStringGet", func(t *testing.T) {
defer catch("getting non-string field as string did not cause panic")
Object("test", false).GetString("test")
})
t.Run("invalidBoolGet", func(t *testing.T) {
defer catch("getting non-bool field as bool did not cause panic")
Object("test", 0).GetBool("test")
})
t.Run("invalidIntGet", func(t *testing.T) {
defer catch("getting non-int field as int did not cause panic")
Object("test", false).GetInt("test")
})
t.Run("invalidFloatGet", func(t *testing.T) {
defer catch("getting non-float field as float did not cause panic")
Object("test", false).GetFloat("test")
})
t.Run("invalidIndentation", func(t *testing.T) {
defer catch("invalid indentation did not cause panic")
Object().FormatString(-1)
})
t.Run("invalidValue", func(t *testing.T) {
defer catch("non-existing value did not cause panic")
Object().KeyOf("test")
})
t.Run("invalidGetTF", func(t *testing.T) {
defer catch("getting invalid tree form did not cause panic")
Object().GetTF("")
})
t.Run("invalidSetTF", func(t *testing.T) {
defer catch("setting invalid tree form did not cause panic")
Object().SetTF("", 0)
})
t.Run("invalidUnsetTF", func(t *testing.T) {
defer catch("unsetting invalid tree form did not cause panic")
Object().UnsetTF("")
})
}