-
Notifications
You must be signed in to change notification settings - Fork 42
/
orderedmap_test.go
140 lines (115 loc) · 3.19 KB
/
orderedmap_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
package hjson
import (
"encoding/json"
"reflect"
"testing"
)
func verifyContent(t *testing.T, om *OrderedMap, txtExpected string) {
bOut, err := json.Marshal(om)
if err != nil {
t.Error(err)
}
if string(bOut) != txtExpected {
t.Errorf("Expected:\n%s\n\nGot:\n%s\n\n", txtExpected, string(bOut))
}
}
func TestAppend(t *testing.T) {
om := NewOrderedMap()
om.Set("B", "first")
om.Set("A", 2)
verifyContent(t, om, `{"B":"first","A":2}`)
}
func TestInsert(t *testing.T) {
om := NewOrderedMapFromSlice([]KeyValue{
{"B", "first"},
{"A", 2},
})
_, ok := om.Insert(1, "C", 1)
if ok {
t.Error("Insert returned true for non-existing key")
}
verifyContent(t, om, `{"B":"first","C":1,"A":2}`)
oldVal, ok := om.Insert(3, "C", 3)
if !ok {
t.Error("Insert returned false for existing key")
}
if oldVal != 1 {
t.Errorf("Expected old value 1, got: '%v'", oldVal)
}
verifyContent(t, om, `{"B":"first","C":3,"A":2}`)
if om.Len() != 3 {
t.Errorf("Expected length: 3 Got length: %d\n", om.Len())
}
if om.AtIndex(1) != 3 {
t.Errorf("Expected value 3 at index 1. Got value: %d\n", om.AtIndex(3))
}
if om.Map["C"] != 3 {
t.Errorf("Expected value 3 for key C. Got value: %d\n", om.AtIndex(3))
}
oldVal, found := om.DeleteKey("XYZ")
if found {
t.Errorf("DeleteKey returned true for non-existing key.")
}
oldVal, found = om.DeleteKey("C")
if !found {
t.Errorf("DeleteKey returned false for existing key.")
}
if oldVal != 3 {
t.Errorf("Expected old value 3, got: '%v'", oldVal)
}
verifyContent(t, om, `{"B":"first","A":2}`)
key, oldVal := om.DeleteIndex(1)
if key != "A" {
t.Errorf("Expected key 'A', got: '%v'", key)
}
if oldVal != 2 {
t.Errorf("Expected old value 2, got: '%v'", oldVal)
}
verifyContent(t, om, `{"B":"first"}`)
key, oldVal = om.DeleteIndex(0)
if key != "B" {
t.Errorf("Expected key 'B', got: '%v'", key)
}
if oldVal != "first" {
t.Errorf("Expected old value 'first', got: '%v'", oldVal)
}
verifyContent(t, om, `{}`)
}
func TestUnmarshalJSON(t *testing.T) {
var om *OrderedMap
err := json.Unmarshal([]byte(`{"B":"first","C":3,"sub":{"z":7,"y":8},"A":2}`), &om)
if err != nil {
t.Error(err)
}
verifyContent(t, om, `{"B":"first","C":3,"sub":{"z":7,"y":8},"A":2}`)
if _, ok := om.Map["C"].(float64); !ok {
t.Errorf("Expected type float64, got type %v", reflect.TypeOf(om.Map["C"]))
}
}
func TestUnmarshalJSON_2(t *testing.T) {
var om OrderedMap
err := json.Unmarshal([]byte(`{"B":"first","C":3,"sub":{"z":7,"y":8},"A":2}`), &om)
if err != nil {
t.Error(err)
}
verifyContent(t, &om, `{"B":"first","C":3,"sub":{"z":7,"y":8},"A":2}`)
}
func TestUnmarshalHJSON(t *testing.T) {
var om *OrderedMap
err := Unmarshal([]byte(`{"B":"first","C":3,"sub":{"z":7,"y":8},"A":2}`), &om)
if err != nil {
t.Error(err)
}
verifyContent(t, om, `{"B":"first","C":3,"sub":{"z":7,"y":8},"A":2}`)
if _, ok := om.Map["C"].(float64); !ok {
t.Errorf("Expected type float64, got type %v", reflect.TypeOf(om.Map["C"]))
}
}
func TestUnmarshalHJSON_2(t *testing.T) {
var om OrderedMap
err := Unmarshal([]byte(`{"B":"first","C":3,"sub":{"z":7,"y":8},"A":2}`), &om)
if err != nil {
t.Error(err)
}
verifyContent(t, &om, `{"B":"first","C":3,"sub":{"z":7,"y":8},"A":2}`)
}