forked from Velocidex/ordereddict
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordereddict_test.go
More file actions
126 lines (100 loc) · 3.36 KB
/
ordereddict_test.go
File metadata and controls
126 lines (100 loc) · 3.36 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
package ordereddict
import (
"encoding/json"
"reflect"
"testing"
"time"
"github.com/alecthomas/repr"
"github.com/stretchr/testify/assert"
)
type dictSerializationTest struct {
dict *Dict
serialized string
}
var (
dictSerializationTests = []dictSerializationTest{
{NewDict().Set("Foo", "Bar"), `{"Foo":"Bar"}`},
// Test an unserilizable member - This should not prevent the
// entire dict from serializing - only that member should be
// ignored.
{NewDict().Set("Foo", "Bar").
Set("Time", time.Unix(3000000000000000, 0)),
`{"Foo":"Bar","Time":null}`},
// Recursive dict
{NewDict().Set("Foo",
NewDict().Set("Bar", 2).
Set("Time", time.Unix(3000000000000000, 0))),
`{"Foo":{"Bar":2,"Time":null}}`},
// Ensure key order is preserved.
{NewDict().Set("A", 1).Set("B", 2), `{"A":1,"B":2}`},
{NewDict().Set("B", 1).Set("A", 2), `{"B":1,"A":2}`},
// Serialize with quotes
{NewDict().Set("foo\\'s quote", 1), `{"foo\\'s quote":1}`},
}
// Check that serialization decodes to the object.
dictUnserializationTest = []dictSerializationTest{
// Preserve order of keys on deserialization.
{NewDict().Set("A", uint64(1)).Set("B", uint64(2)), `{"A":1,"B":2}`},
{NewDict().Set("B", uint64(1)).Set("A", uint64(2)), `{"B":1,"A":2}`},
// Handle arrays, ints floats and bools
{NewDict().Set("B", uint64(1)).Set("A", uint64(2)), `{"B":1,"A":2}`},
{NewDict().Set("B", float64(1)).Set("A", uint64(2)), `{"B":1.0,"A":2}`},
{NewDict().Set("B", []interface{}{uint64(1)}).Set("A", uint64(2)), `{"B":[1],"A":2}`},
{NewDict().Set("B", true).Set("A", uint64(2)), `{"B":true,"A":2}`},
{NewDict().Set("B", nil).Set("A", uint64(2)), `{"B":null,"A":2}`},
// Embedded dicts decode into ordered dicts.
{NewDict().
Set("B", NewDict().Set("Zoo", "X").Set("Baz", "Y")).
Set("A", "Z"), `{"B":{"Zoo":"X","Baz":"Y"},"A":"Z"}`},
// Make sure we properly preserve uint64 (overflows int64)
{NewDict().
Set("Uint64", uint64(9223372036854775808)),
`{"Uint64": 9223372036854775808}`},
// We prefer uint64 but int64 is needed for negative numbers
{NewDict().
Set("Int64", int64(-500)),
`{"Int64": -500}`},
}
)
func TestDictSerialization(t *testing.T) {
for _, test := range dictSerializationTests {
serialized, err := json.Marshal(test.dict)
if err != nil {
t.Fatalf("Failed to serialize %v: %v", repr.String(test.dict), err)
}
assert.Equal(t, test.serialized, string(serialized))
}
}
func TestDictDeserialization(t *testing.T) {
for _, test := range dictUnserializationTest {
value := NewDict()
err := json.Unmarshal([]byte(test.serialized), value)
if err != nil {
t.Fatalf("Failed to serialize %v: %v", test.serialized, err)
}
// Make sure the keys are the same.
assert.Equal(t, test.dict.Keys(), value.Keys())
assert.Equal(t, test.dict.ToDict(), value.ToDict())
assert.True(t, reflect.DeepEqual(test.dict, value))
}
}
func TestOrder(t *testing.T) {
test := NewDict().
Set("A", 1).
Set("B", 2)
assert.Equal(t, []string{"A", "B"}, test.Keys())
test = NewDict().
Set("B", 1).
Set("A", 2)
assert.Equal(t, []string{"B", "A"}, test.Keys())
}
func TestCaseInsensitive(t *testing.T) {
test := NewDict().SetCaseInsensitive()
test.Set("FOO", 1)
value, pres := test.Get("foo")
assert.True(t, pres)
assert.Equal(t, 1, value)
test = NewDict().Set("FOO", 1)
value, pres = test.Get("foo")
assert.False(t, pres)
}