-
Notifications
You must be signed in to change notification settings - Fork 8
/
ordereddict_test.go
73 lines (57 loc) · 1.66 KB
/
ordereddict_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
package vfilter
import (
"encoding/json"
"testing"
"time"
"github.com/Velocidex/ordereddict"
"github.com/alecthomas/repr"
"github.com/stretchr/testify/assert"
)
type dictSerializationTest struct {
dict *ordereddict.Dict
serialized string
}
var dictSerializationTests = []dictSerializationTest{
{ordereddict.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.
{ordereddict.NewDict().Set("Foo", "Bar").
Set("Time", time.Unix(3000000000000000, 0)),
`{"Foo":"Bar","Time":null}`},
// Recursive dict
{ordereddict.NewDict().Set("Foo",
ordereddict.NewDict().Set("Bar", 2).
Set("Time", time.Unix(3000000000000000, 0))),
`{"Foo":{"Bar":2,"Time":null}}`},
}
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 TestOrder(t *testing.T) {
scope := NewScope()
test := ordereddict.NewDict().
Set("A", 1).
Set("B", 2)
assert.Equal(t, []string{"A", "B"}, scope.GetMembers(test))
test = ordereddict.NewDict().
Set("B", 1).
Set("A", 2)
assert.Equal(t, []string{"B", "A"}, scope.GetMembers(test))
}
func TestCaseInsensitive(t *testing.T) {
test := ordereddict.NewDict().SetCaseInsensitive()
test.Set("FOO", 1)
value, pres := test.Get("foo")
assert.True(t, pres)
assert.Equal(t, 1, value)
test = ordereddict.NewDict().Set("FOO", 1)
value, pres = test.Get("foo")
assert.False(t, pres)
}