-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
263 lines (231 loc) · 5.2 KB
/
main_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"testing"
)
func TestBytesAsJsonString(t *testing.T) {
value, err := bytesAsJson([]byte(`"qwerty"`))
if err != nil {
t.Error(err)
}
if value.(string) != "qwerty" {
t.Errorf("'%v' is not querty", value)
}
}
func TestBytesAsJsonMap(t *testing.T) {
value, err := bytesAsJson([]byte(`{"qwerty":"uiopas"}`))
if err != nil {
t.Error(err)
}
m := value.(map[string]interface{})
if len(m) != 1 {
t.Error("not length 1")
}
v := m["qwerty"]
if v.(string) != "uiopas" {
t.Errorf("'%v' is not uiopas", v)
}
}
func TestBytesAsJsonArray(t *testing.T) {
_, err := bytesAsJson([]byte(`["qwerty","uiopas","dfghjkl"]`))
if err != nil {
t.Error(err)
}
}
func equivalentString(value string, expected interface{}, t *testing.T) {
switch v2 := expected.(type) {
case string:
if value != v2 {
t.Errorf("%s is not an equivalent string to %s", value, expected)
}
default:
t.Errorf("%v is not a string", expected)
}
}
func equivalentMap(value map[string]interface{}, expected interface{}, t *testing.T) {
switch v2 := expected.(type) {
case map[string]interface{}:
if len(value) != len(v2) {
t.Errorf("len(%v) != len(%v)", value, v2)
}
for k, v := range value {
equivalentJson(v, v2[k], t)
}
default:
t.Errorf("%v is not a map", expected)
}
}
func equivalentArray(value []interface{}, expected interface{}, t *testing.T) {
switch v2 := expected.(type) {
case []interface{}:
if len(value) != len(v2) {
t.Errorf("len(%v) != len(%v)", value, v2)
}
for i, o := range value {
equivalentJson(o, v2[i], t)
}
default:
t.Errorf("%v is not an array", expected)
}
}
func equivalentJson(value interface{}, expected interface{}, t *testing.T) {
switch v := value.(type) {
case string:
equivalentString(v, expected, t)
case map[string]interface{}:
equivalentMap(v, expected, t)
case []interface{}:
equivalentArray(v, expected, t)
default:
t.Errorf("unexpected types for %v and %v", value, expected)
}
}
func asJson(raw []byte, t *testing.T) interface{} {
v, err := bytesAsJson(raw)
if err != nil {
t.Error(err)
}
return v
}
func TestEquivalent(t *testing.T) {
equivalentJson(
asJson([]byte(`"asdf"`), t),
asJson([]byte(`"asdf"`), t),
t,
)
equivalentJson(
asJson([]byte(`{"foo":"bar"}`), t),
asJson([]byte(`{"foo":"bar"}`), t),
t,
)
equivalentJson(
asJson([]byte(`["a1","a2"]`), t),
asJson([]byte(`["a1","a2"]`), t),
t,
)
}
func resolver(t *testing.T) Resolver {
return func(name string) (interface{}, error) {
return asJson([]byte(`{"a":"thing"}`), t), nil
}
}
func TestIdentityReplaceString(t *testing.T) {
replaced, err := replace(asJson([]byte(`"zxcvb"`), t), resolver(t))
if err != nil {
t.Error(err)
}
equivalentJson(
replaced,
asJson([]byte(`"zxcvb"`), t),
t,
)
}
func TestIdentityReplaceAMap(t *testing.T) {
template := `{"asdf":"qwerty"}`
replaced, err := replace(asJson([]byte(template), t), resolver(t))
if err != nil {
t.Error(err)
}
expected := asJson([]byte(template), t)
equivalentJson(
replaced,
expected,
t,
)
}
func TestReplaceMapIdentity(t *testing.T) {
template := map[string]interface{} { "asdf": "qwerty" }
replaced, err := replaceMap(template, resolver(t))
if err != nil {
t.Error(err)
}
expected := asJson([]byte(`{"asdf":"qwerty"}`), t)
equivalentJson(
replaced,
expected,
t,
)
}
func TestReplaceMapRefIdentity(t *testing.T) {
template := map[string]interface{} { "Ref": "foo" }
replaced, err := replaceMap(template, resolver(t))
if err != nil {
t.Error(err)
}
expected := asJson([]byte(`{"Ref":"foo"}`), t)
equivalentJson(
replaced,
expected,
t,
)
}
func TestReplaceMapRefURI(t *testing.T) {
template := map[string]interface{} { "Ref": map[string]interface{} { "URI": "somecomponent" } }
replaced, err := replaceMap(template, resolver(t))
if err != nil {
t.Error(err)
}
expected := asJson([]byte(`{"a":"thing"}`), t)
equivalentJson(
replaced,
expected,
t,
)
}
func TestIdentityRef(t *testing.T) {
template := `{"asdf":{"Ref": "NamedValue"}}`
replaced, err := replace(asJson([]byte(template), t), resolver(t))
if err != nil {
t.Error(err)
}
expected := asJson([]byte(template), t)
equivalentJson(
replaced,
expected,
t,
)
}
func TestReplaceArray(t *testing.T) {
template := []interface{}{ "a1", "a2", "a3" }
replaced, err := replaceArray(template, resolver(t))
if err != nil {
t.Error(err)
}
expected := asJson([]byte(`["a1","a2","a3"]`), t)
equivalentJson(
replaced,
expected,
t,
)
}
func TestReplace(t *testing.T) {
template := `{"asdf":{"Ref": {"URI":"componentName"}}, "bsdf":[{"Ref": {"URI":"otherComponent"}}]}`
replaced, err := replace(asJson([]byte(template), t), resolver(t))
if err != nil {
t.Error(err)
}
expected := asJson([]byte(`{"asdf":{"a":"thing"},"bsdf":[{"a":"thing"}]}`), t)
equivalentJson(
replaced,
expected,
t,
)
}
func TestRecursiveReplace(t *testing.T) {
template := `{"Ref": {"URI":"a"}}`
r := func(name string) (interface{}, error) {
if name == "a" {
return map[string]interface{}{"Ref":map[string]interface{}{"URI":"b"}}, nil
} else {
return "poiuyt", nil
}
}
replaced, err := replaceBytes([]byte(template), r)
if err != nil {
t.Error(err)
}
equivalentJson(
replaced,
"poiuyt",
t,
)
}