-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
64 lines (51 loc) · 1.18 KB
/
map_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
package orderedmap
import (
"reflect"
"testing"
)
func TestJSON(t *testing.T) {
input := `{"423":"abc","231":"dbh","152":"xyz"}`
var m Map
if err := m.UnmarshalJSON([]byte(input)); err != nil {
t.Fatal(err)
}
output, err := m.MarshalJSON()
if err != nil {
t.Fatal(err)
}
got := string(output)
if input != got {
t.Errorf("expected: %v, got: %v", input, got)
}
}
func TestLen(t *testing.T) {
input := `{"423":"abc","231":"dbh","152":"xyz"}`
var m Map
if err := m.UnmarshalJSON([]byte(input)); err != nil {
t.Fatal(err)
}
if got, expected := m.Len(), 3; expected != got {
t.Errorf("expected: %v, got: %v", expected, got)
}
}
func TestRange(t *testing.T) {
input := `{"423":"abc","231":"dbh","152":"xyz"}`
var m Map
if err := m.UnmarshalJSON([]byte(input)); err != nil {
t.Fatal(err)
}
var items []map[string]interface{}
m.Range(func(key string, value interface{}) bool {
entry := map[string]interface{}{key: value}
items = append(items, entry)
return true
})
expected := []map[string]interface{}{
{"423": "abc"},
{"231": "dbh"},
{"152": "xyz"},
}
if !reflect.DeepEqual(items, expected) {
t.Errorf("expected: %v, got: %v", expected, items)
}
}