forked from weaveworks/weave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
116 lines (111 loc) · 2.16 KB
/
json_test.go
File metadata and controls
116 lines (111 loc) · 2.16 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
package proxy
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLookupObject(t *testing.T) {
tests := []struct {
root jsonObject
key string
result jsonObject
err error
}{
{
jsonObject{},
"a",
jsonObject{},
nil,
},
{
jsonObject{"a": map[string]interface{}{"b": int(1)}},
"a",
jsonObject{"b": int(1)},
nil,
},
{
jsonObject{"nonObject": int(1)},
"nonObject",
nil,
&UnmarshalWrongTypeError{Field: "nonObject", Expected: "object", Got: 1},
},
}
for _, test := range tests {
gotResult, gotErr := test.root.Object(test.key)
msg := fmt.Sprintf("%q.Object(%q) => %q, %q", test.root, test.key, gotResult, gotErr)
assert.Equal(t, test.result, gotResult, msg)
assert.Equal(t, test.err, gotErr, msg)
}
}
func TestLookupString(t *testing.T) {
tests := []struct {
root jsonObject
key string
result string
err error
}{
{
jsonObject{},
"a",
"",
nil,
},
{
jsonObject{"nonString": int(1)},
"nonString",
"",
&UnmarshalWrongTypeError{Field: "nonString", Expected: "string", Got: 1},
},
}
for _, test := range tests {
gotResult, gotErr := test.root.String(test.key)
msg := fmt.Sprintf("%q.String(%q) => %q, %q", test.root, test.key, gotResult, gotErr)
assert.Equal(t, test.result, gotResult, msg)
assert.Equal(t, test.err, gotErr, msg)
}
}
func TestLookupStringArray(t *testing.T) {
tests := []struct {
root jsonObject
key string
result []string
err error
}{
{
jsonObject{},
"a",
nil,
nil,
},
{
jsonObject{"a": []string{"foo"}},
"a",
[]string{"foo"},
nil,
},
{
jsonObject{"a": []string{}},
"a",
[]string{},
nil,
},
{
jsonObject{"a": "foo"},
"a",
[]string{"foo"},
nil,
},
{
jsonObject{"int": 5},
"int",
nil,
&UnmarshalWrongTypeError{Field: "int", Expected: "string or array of strings", Got: 5},
},
}
for _, test := range tests {
gotResult, gotErr := test.root.StringArray(test.key)
msg := fmt.Sprintf("%q.String(%q) => %q, %q", test.root, test.key, gotResult, gotErr)
assert.Equal(t, test.result, gotResult, msg)
assert.Equal(t, test.err, gotErr, msg)
}
}