-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_map_test.go
More file actions
125 lines (122 loc) · 2.46 KB
/
ordered_map_test.go
File metadata and controls
125 lines (122 loc) · 2.46 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
package sham
import (
"encoding/xml"
"reflect"
"testing"
)
func TestOrderedMap_MarshalJSON(t *testing.T) {
type fields struct {
Values map[string]interface{}
Keys []string
}
tests := []struct {
name string
fields fields
want []byte
wantErr bool
}{
{
name: "Simple map",
fields: fields{
Values: map[string]interface{}{
"a": 1,
"b": 2,
"c": 3,
},
Keys: []string{"b", "c", "a"},
},
want: []byte(`{"b":2,"c":3,"a":1}`),
wantErr: false,
},
{
name: "Nested maps",
fields: fields{
Values: map[string]interface{}{
"a": 1,
"b": 2,
"c": &OrderedMap{
Values: map[string]interface{}{
"d": 5,
"e": 6,
"f": 4,
},
Keys: []string{"f", "d", "e"},
},
},
Keys: []string{"b", "c", "a"},
},
want: []byte(`{"b":2,"c":{"f":4,"d":5,"e":6},"a":1}`),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &OrderedMap{
Values: tt.fields.Values,
Keys: tt.fields.Keys,
}
got, err := m.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("OrderedMap.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("OrderedMap.MarshalJSON() = %s, want %s", got, tt.want)
}
})
}
}
func TestOrderedMap_MarshalXML(t *testing.T) {
tests := []struct {
name string
val *OrderedMap
want []byte
wantErr bool
}{
{
name: "Simple map",
val: &OrderedMap{
Values: map[string]interface{}{
"a": 1,
"b": 2,
"c": 3,
},
Keys: []string{"b", "c", "a"},
},
want: []byte(`<b>2</b><c>3</c><a>1</a>`),
wantErr: false,
},
{
name: "Nested maps",
val: &OrderedMap{
Values: map[string]interface{}{
"a": 1,
"b": 2,
"c": &OrderedMap{
Values: map[string]interface{}{
"d": 5,
"e": 6,
"f": 4,
},
Keys: []string{"f", "d", "e"},
},
},
Keys: []string{"b", "c", "a"},
},
want: []byte(`<b>2</b><c><f>4</f><d>5</d><e>6</e></c><a>1</a>`),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := xml.Marshal(tt.val)
if (err != nil) != tt.wantErr {
t.Errorf("OrderedMap.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("OrderedMap.MarshalJSON() = %s, want %s", got, tt.want)
}
})
}
}