-
Notifications
You must be signed in to change notification settings - Fork 0
/
typedorderedmap_test.go
95 lines (67 loc) · 1.94 KB
/
typedorderedmap_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
package orderedmapjson
import (
"encoding/json"
"testing"
)
func TestTypedOrderedMapInt(t *testing.T) {
const input = `{"nine":9,"eight":8,"seven":7,"six":6,"five":5,"four":4,"three":3,"two":2,"one":1,"zero":0}`
m := NewTypedOrderedMap[int]()
if err := json.Unmarshal([]byte(input), m); err != nil {
t.Fatalf("unexpected error: %v", err)
}
data, err := json.Marshal(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
output := string(data)
if output != input {
t.Fatalf("expected %s, got %s", input, output)
}
}
func TestTypedOrderedMapOrderedMapString(t *testing.T) {
const input = `{"one":{"foo":"bar"},"two":{"foo":"bar"},"three":{"foo":"bar"}}`
m := NewTypedOrderedMap[*TypedOrderedMap[string]]()
if err := json.Unmarshal([]byte(input), m); err != nil {
t.Fatalf("unexpected error: %v", err)
}
data, err := json.Marshal(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
output := string(data)
if output != input {
t.Fatalf("expected %s, got %s", input, output)
}
}
func TestTypedOrderedMapOrderedMapInt(t *testing.T) {
const input = `{"one":{"foo":1},"two":{"foo":2},"three":{"foo":3}}`
m := NewTypedOrderedMap[*TypedOrderedMap[int]]()
if err := json.Unmarshal([]byte(input), m); err != nil {
t.Fatalf("unexpected error: %v", err)
}
data, err := json.Marshal(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
output := string(data)
if output != input {
t.Fatalf("expected %s, got %s", input, output)
}
}
func TestTypedOrderedMapIntWrongType(t *testing.T) {
const input = `{"one":1,"two":2,"three":"3"}`
m := NewTypedOrderedMap[int]()
if err := json.Unmarshal([]byte(input), m); err == nil {
t.Fatal("expected error")
}
}
func TestTypedOrderedMapCopy(t *testing.T) {
m := NewTypedOrderedMap[string]()
m.Set("foo", "bar")
m.Set("123", "true")
m.Set("abc", "nil")
mm := m.Copy()
if mm.String() != m.String() {
t.Fatalf("expected %s, got %s", m.String(), mm.String())
}
}