-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_test.go
More file actions
88 lines (80 loc) · 1.9 KB
/
object_test.go
File metadata and controls
88 lines (80 loc) · 1.9 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
package orderedobject
import (
"encoding/json"
"testing"
)
func TestMarshal(t *testing.T) {
testCases := []struct {
name string
object *Object[any]
expected string
}{
{
name: "Empty object",
object: func() *Object[any] {
return NewObject[any](0)
}(),
expected: `{}`,
},
{
name: "Single key-value pair",
object: func() *Object[any] {
obj := NewObject[any](1)
obj.Set("key", "value")
return obj
}(),
expected: `{"key":"value"}`,
},
{
name: "Multiple key-value pairs",
object: func() *Object[any] {
obj := NewObject[any](3)
obj.Set("name", "John")
obj.Set("age", 30)
obj.Set("city", "New York")
return obj
}(),
expected: `{"name":"John","age":30,"city":"New York"}`,
},
{
name: "Nested objects",
object: func() *Object[any] {
address := NewObject[any](2)
address.Set("street", "123 Main St")
address.Set("city", "London")
person := NewObject[any](3)
person.Set("name", "Alice")
person.Set("age", 28)
person.Set("address", address)
return person
}(),
expected: `{"name":"Alice","age":28,"address":{"street":"123 Main St","city":"London"}}`,
},
{
name: "Array of objects",
object: func() *Object[any] {
person1 := NewObject[any](2)
person1.Set("name", "Bob")
person1.Set("age", 35)
person2 := NewObject[any](2)
person2.Set("name", "Charlie")
person2.Set("age", 40)
people := NewObject[any](1)
people.Set("people", []any{person1, person2})
return people
}(),
expected: `{"people":[{"name":"Bob","age":35},{"name":"Charlie","age":40}]}`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
encoded, err := json.Marshal(tc.object)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if string(encoded) != tc.expected {
t.Errorf("Expected %s, got %s", tc.expected, string(encoded))
}
})
}
}