-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_test.go
168 lines (142 loc) · 3.67 KB
/
decode_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
164
165
166
167
168
package omap
import (
"strings"
"testing"
)
func TestOrderedMap_DecodeJSON(t *testing.T) {
data := `{"a": 1, "b": 2, "c": 3, "z": 4,
"embedded": { "g": 5, "h": 6, "i": 7 },
"arr": [8,9,10]}`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
t.Run("decode JSON", func(t *testing.T) {
if dataMap.Len() != 6 {
t.Errorf("Expected 6 elements, got %d", dataMap.Len())
}
val, ok := dataMap.Get("z")
if !ok {
t.Errorf("Expected key 'z' to be 4, got %v", val)
}
if v, ok := val.(float64); !ok || v != 4 {
t.Errorf("Expected value 4, got %v", val)
}
})
t.Run("check order of elements", func(t *testing.T) {
i := 1
for _, v := range dataMap.Iterator {
switch val := v.(type) {
case float64:
vint := int(val)
if vint != i {
t.Errorf("Expected value %d, got %v", i, v)
}
i++
case *OrderedMap[string, any]:
for _, vv := range val.Iterator {
vint := int(vv.(float64))
if vint != i {
t.Errorf("Expected value %d, got %v", i, vv)
}
i++
}
case []any:
for _, v := range val {
vint := int(v.(float64))
if vint != i {
t.Errorf("Expected value %d, got %v", i, v)
}
i++
}
default:
t.Errorf("Unexpected type %T", val)
}
}
})
}
func TestOrderedMap_DecodeJSONError(t *testing.T) {
t.Run("invalid JSON", func(t *testing.T) {
data := `a{"a": 1}`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
t.Run("missing closing bracket", func(t *testing.T) {
data := `{"a": 1`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
t.Run("start", func(t *testing.T) {
data := `[a]{"a": 1}`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
t.Run("delimiter", func(t *testing.T) {
data := `{"a"; 1}`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
t.Run("key", func(t *testing.T) {
data := `{a; 1}`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
t.Run("embedded object", func(t *testing.T) {
data := `{"a": 1, "obj": {"b"":2}}`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
t.Run("malformed array", func(t *testing.T) {
data := `{"a": 1, "ar": ["1"\]}`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
t.Run("missing array closing bracket", func(t *testing.T) {
data := `{"a": 1, "ar": ["1"}`
dataMap := NewOrderedMap[string, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
}
func TestOrderedMap_Types(t *testing.T) {
t.Run("mismatch key type", func(t *testing.T) {
data := `{"a": 1}`
dataMap := NewOrderedMap[int, any]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
t.Run("mismatch data type", func(t *testing.T) {
data := `{"a": 1}`
dataMap := NewOrderedMap[string, string]()
err := dataMap.Decode(strings.NewReader(data))
if err == nil {
t.Error("Expected error, got nil")
}
})
}