-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathorderedmapof_test.go
More file actions
248 lines (236 loc) · 4.89 KB
/
orderedmapof_test.go
File metadata and controls
248 lines (236 loc) · 4.89 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
//go:build go1.18
// +build go1.18
package orderedmap
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
"testing"
)
func TestOrderedMapOf(t *testing.T) {
o := NewOf[string, any]()
// number
o.Set("number", 3)
v, _ := o.Get("number")
if v.(int) != 3 {
t.Error("Set number")
}
// string
o.Set("string", "x")
v, _ = o.Get("string")
if v.(string) != "x" {
t.Error("Set string")
}
// string slice
o.Set("strings", []string{
"t",
"u",
})
v, _ = o.Get("strings")
if v.([]string)[0] != "t" {
t.Error("Set strings first index")
}
if v.([]string)[1] != "u" {
t.Error("Set strings second index")
}
// mixed slice
o.Set("mixed", []interface{}{
1,
"1",
})
v, _ = o.Get("mixed")
if v.([]interface{})[0].(int) != 1 {
t.Error("Set mixed int")
}
if v.([]interface{})[1].(string) != "1" {
t.Error("Set mixed string")
}
// overriding existing key
o.Set("number", 4)
v, _ = o.Get("number")
if v.(int) != 4 {
t.Error("Override existing key")
}
// Keys method
keys := o.Keys()
expectedKeys := []string{
"number",
"string",
"strings",
"mixed",
}
for i, key := range keys {
if key != expectedKeys[i] {
t.Error("Keys method", key, "!=", expectedKeys[i])
}
}
// Values method
values := o.Values()
expectedValues := map[string]interface{}{
"number": 4,
"string": "x",
"strings": []string{"t", "u"},
"mixed": []interface{}{1, "1"},
}
if !reflect.DeepEqual(values, expectedValues) {
t.Error("Values method returned unexpected map")
}
// delete
o.Delete("strings")
o.Delete("not a key being used")
if len(o.Keys()) != 3 {
t.Error("Delete method")
}
_, ok := o.Get("strings")
if ok {
t.Error("Delete did not remove 'strings' key")
}
}
func TestOrderedMapOf_SortKeys(t *testing.T) {
o := NewOf[string, int]()
o.Set("b", 2)
o.Set("a", 1)
o.Set("c", 3)
o.SortKeys(sort.Strings)
// Check the root keys
expectedKeys := []string{
"a",
"b",
"c",
}
k := o.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("SortKeys root key order", i, k[i], "!=", expectedKeys[i])
}
}
}
func TestOrderedMapOf_Sort(t *testing.T) {
o := NewOf[string, float64]()
o.Set("b", 2.1)
o.Set("a", 1.1)
o.Set("c", 3)
o.Sort(func(a *PairOf[string, float64], b *PairOf[string, float64]) bool {
return a.value > b.value
})
// Check the root keys
expectedKeys := []string{
"c",
"b",
"a",
}
k := o.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Sort root key order", i, k[i], "!=", expectedKeys[i])
}
}
}
func TestOrderedMapOf_BlankMarshalJSON(t *testing.T) {
o := NewOf[int, int]()
// blank map
b, err := json.Marshal(o)
if err != nil {
t.Error("Marshalling blank map to json", err)
}
s := string(b)
// check json is correctly ordered
if s != `{}` {
t.Error("JSON Marshaling blank map value is incorrect", s)
}
// convert to indented json
bi, err := json.MarshalIndent(o, "", " ")
if err != nil {
t.Error("Marshalling indented json for blank map", err)
}
si := string(bi)
ei := `{}`
if si != ei {
fmt.Println(ei)
fmt.Println(si)
t.Error("JSON MarshalIndent blank map value is incorrect", si)
}
}
func TestOrderedMapOf_MarshalJSON(t *testing.T) {
o := NewOf[string, any]()
// number
o.Set("number", 3)
// string
o.Set("string", "x")
// string
o.Set("specialstring", "\\.<>[]{}_-")
// new value keeps key in old position
o.Set("number", 4)
// keys not sorted alphabetically
o.Set("z", 1)
o.Set("a", 2)
o.Set("b", 3)
// slice
o.Set("slice", []interface{}{
"1",
1,
})
// orderedmap
v := New()
v.Set("e", 1)
v.Set("a", 2)
o.Set("orderedmap", v)
// escape key
o.Set("test\n\r\t\\\"ing", 9)
// convert to json
b, err := json.Marshal(o)
if err != nil {
t.Error("Marshalling json", err)
}
s := string(b)
// check json is correctly ordered
if s != `{"number":4,"string":"x","specialstring":"\\.\u003c\u003e[]{}_-","z":1,"a":2,"b":3,"slice":["1",1],"orderedmap":{"e":1,"a":2},"test\n\r\t\\\"ing":9}` {
t.Error("JSON Marshal value is incorrect", s)
}
// convert to indented json
bi, err := json.MarshalIndent(o, "", " ")
if err != nil {
t.Error("Marshalling indented json", err)
}
si := string(bi)
ei := `{
"number": 4,
"string": "x",
"specialstring": "\\.\u003c\u003e[]{}_-",
"z": 1,
"a": 2,
"b": 3,
"slice": [
"1",
1
],
"orderedmap": {
"e": 1,
"a": 2
},
"test\n\r\t\\\"ing": 9
}`
if si != ei {
fmt.Println(ei)
fmt.Println(si)
t.Error("JSON MarshalIndent value is incorrect", si)
}
}
func TestOrderedMapOf_MarshalJSONNoEscapeHTML(t *testing.T) {
o := NewOf[string, string]()
o.SetEscapeHTML(false)
// string special characters
o.Set("specialstring", "\\.<>[]{}_-")
// convert to json
b, err := o.MarshalJSON()
if err != nil {
t.Error("Marshalling json", err)
}
s := strings.Replace(string(b), "\n", "", -1)
// check json is correctly ordered
if s != `{"specialstring":"\\.<>[]{}_-"}` {
t.Error("JSON Marshal value is incorrect", s)
}
}