forked from kinbiko/jsonassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
192 lines (183 loc) · 7.98 KB
/
integration_test.go
File metadata and controls
192 lines (183 loc) · 7.98 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
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
package jsonassert_test
import (
"fmt"
"testing"
"github.com/mx51/jsonassert"
)
func TestAssertf(t *testing.T) {
tt := []struct {
name string
act string
exp string
msgs []string
}{
{name: "different types", act: `"true"`, exp: `true`, msgs: []string{
`actual JSON (string) and expected JSON (boolean) were of different types at '$'`,
}},
{name: "empty", act: "", exp: "", msgs: []string{}},
{name: "empty v null", act: "", exp: "null", msgs: []string{`'actual' JSON is not valid JSON: unable to identify JSON type of ""`}},
{name: "null v empty", act: "null", exp: "", msgs: []string{`'expected' JSON is not valid JSON: unable to identify JSON type of ""`}},
{name: "null", act: "null", exp: "null", msgs: []string{}},
{name: "true", act: `true`, exp: `true`, msgs: []string{}},
{name: "false", act: `false`, exp: `false`, msgs: []string{}},
{name: "true v false", act: `true`, exp: `false`, msgs: []string{`expected boolean at '$' to be false but was true`}},
{name: "false v true", act: `false`, exp: `true`, msgs: []string{`expected boolean at '$' to be true but was false`}},
{name: "identical floats", act: `12.45`, exp: `12.45`, msgs: []string{}},
{name: "identical negative ints", act: `-1245`, exp: `-1245`, msgs: []string{}},
{name: "different floats", act: `12.45`, exp: `1.245`, msgs: []string{`expected number at '$' to be '1.2450000' but was '12.4500000'`}},
{name: "different ints", act: `1245`, exp: `-1245`, msgs: []string{`expected number at '$' to be '-1245.0000000' but was '1245.0000000'`}},
{name: "identical strings", act: `"hello world"`, exp: `"hello world"`, msgs: []string{}},
{name: "identical empty strings", act: `""`, exp: `""`, msgs: []string{}},
{name: "different strings", act: `"hello"`, exp: `"world"`, msgs: []string{`expected string at '$' to be 'world' but was 'hello'`}},
{name: "different strings", act: `"lorem ipsum dolor sit amet lorem ipsum dolor sit amet"`, exp: `"lorem ipsum dolor sit amet lorem ipsum dolor sit amet why do I have to be the test string?"`, msgs: []string{
`expected string at '$' to be
'lorem ipsum dolor sit amet lorem ipsum dolor sit amet why do I have to be the test string?'
but was
'lorem ipsum dolor sit amet lorem ipsum dolor sit amet'`,
}},
{name: "empty v non-empty string", act: `""`, exp: `"world"`, msgs: []string{`expected string at '$' to be 'world' but was ''`}},
{name: "identical objects", act: `{"hello": "world"}`, exp: `{"hello":"world"}`, msgs: []string{}},
{name: "different keys in objects", act: `{"world": "hello"}`, exp: `{"hello":"world"}`, msgs: []string{
`unexpected object key(s) ["world"] found at '$'`,
`expected object key(s) ["hello"] missing at '$'`,
}},
{name: "different values in objects", act: `{"foo": "hello"}`, exp: `{"foo": "world" }`, msgs: []string{
`expected string at '$.foo' to be 'world' but was 'hello'`,
}},
{name: "different keys in nested objects", act: `{"foo": {"world": "hello"}}`, exp: `{"foo": {"hello":"world"} }`, msgs: []string{
`unexpected object key(s) ["world"] found at '$.foo'`,
`expected object key(s) ["hello"] missing at '$.foo'`,
}},
{name: "different values in nested objects", act: `{"foo": {"hello": "world"}}`, exp: `{"foo": {"hello":"世界"} }`, msgs: []string{
`expected string at '$.foo.hello' to be '世界' but was 'world'`,
}},
{name: "only one object is nested", act: `{}`, exp: `{"foo": {"hello":"世界"} }`, msgs: []string{
`expected 1 keys at '$' but got 0 keys`,
`expected object key(s) ["foo"] missing at '$'`,
}},
{name: "empty array v empty array", act: `[]`, exp: `[ ]`, msgs: []string{}},
{name: "non-empty array v empty array", act: `[null]`, exp: `[ ]`, msgs: []string{
`length of arrays at '$' were different. Expected array to be of length 0, but contained 1 element(s)`,
`actual JSON at '$' was: [null], but expected JSON was: []`,
}},
{name: "non-empty array v empty array", act: `[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]`, exp: `[1,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]`, msgs: []string{
`length of arrays at '$' were different. Expected array to be of length 22, but contained 30 element(s)`,
`actual JSON at '$' was:
[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
but expected JSON was:
[1,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]`,
}},
{name: "identical non-empty arrays", act: `["hello"]`, exp: `["hello"]`, msgs: []string{}},
{name: "different non-empty arrays", act: `["hello"]`, exp: `["world"]`, msgs: []string{
`expected string at '$[0]' to be 'world' but was 'hello'`,
}},
{name: "different length non-empty arrays", act: `["hello", "world"]`, exp: `["world"]`, msgs: []string{
`length of arrays at '$' were different. Expected array to be of length 1, but contained 2 element(s)`,
`actual JSON at '$' was: ["hello","world"], but expected JSON was: ["world"]`,
}},
{name: "presence against null", act: `{"foo": null}`, exp: `{"foo": "<<PRESENCE>>"}`, msgs: []string{
`expected the presence of any value at '$.foo', but was absent`,
}},
{name: "presence against boolean", act: `{"foo": true}`, exp: `{"foo": "<<PRESENCE>>"}`, msgs: []string{}},
{name: "presence against number", act: `{"foo": 1234}`, exp: `{"foo": "<<PRESENCE>>"}`, msgs: []string{}},
{name: "presence against string", act: `{"foo": "hello world"}`, exp: `{"foo": "<<PRESENCE>>"}`, msgs: []string{}},
{name: "presence against object", act: `{"foo": {"bar": "baz"}}`, exp: `{"foo": "<<PRESENCE>>"}`, msgs: []string{}},
{name: "presence against object", act: `{"foo": ["bar", "baz"]}`, exp: `{"foo": "<<PRESENCE>>"}`, msgs: []string{}},
}
for _, tc := range tt {
t.Run(tc.name, func(st *testing.T) {
tp, ja := setup()
ja.Assertf(tc.act, tc.exp)
if got := len(tp.messages); got != len(tc.msgs) {
st.Errorf("expected %d assertion message(s) but got %d", len(tc.msgs), got)
if len(tc.msgs) > 0 {
st.Errorf("Expected the following messages:")
for _, msg := range tc.msgs {
st.Errorf(" - %s", msg)
}
}
if len(tp.messages) > 0 {
st.Errorf("Got the following messages:")
for _, msg := range tp.messages {
st.Errorf(" - %s", msg)
}
}
return
}
for i := range tc.msgs {
if exp, got := tc.msgs[i], tp.messages[i]; got != exp {
st.Errorf("expected assertion message:\n'%s'\nbut got\n'%s'", exp, got)
}
}
})
}
}
func TestAssertContainsf(t *testing.T) {
tt := []struct {
name string
act string
exp string
msgs []string
}{
{
name: "partial_matching_on_object",
act: `{"foo": "bar", "baz": "ok"}`,
exp: `{"baz": "ok"}`,
msgs: []string{},
},
{
name: "object_value_not_found",
act: `{"foo": "bar"}`,
exp: `{"baz": "ok"}`,
msgs: []string{
`expected object key(s) ["baz"] missing at '$'`,
},
},
{
name: "partial_matching_on_array",
act: `{"foo": ["bar", "baz", "ok"]}`,
exp: `{"foo": ["bar", "<<STRING>>", "ok"]}`,
msgs: []string{},
},
}
for _, tc := range tt {
t.Run(tc.name, func(st *testing.T) {
tp, ja := setup()
ja.AssertContainsf(tc.act, tc.exp)
if got := len(tp.messages); got != len(tc.msgs) {
st.Errorf("expected %d assertion message(s) but got %d", len(tc.msgs), got)
if len(tc.msgs) > 0 {
st.Errorf("Expected the following messages:")
for _, msg := range tc.msgs {
st.Errorf(" - %s", msg)
}
}
if len(tp.messages) > 0 {
st.Errorf("Got the following messages:")
for _, msg := range tp.messages {
st.Errorf(" - %s", msg)
}
}
return
}
for i := range tc.msgs {
if exp, got := tc.msgs[i], tp.messages[i]; got != exp {
st.Errorf("expected assertion message:\n'%s'\nbut got\n'%s'", exp, got)
}
}
})
}
}
func setup() (*testPrinter, *jsonassert.Asserter) {
tp := &testPrinter{}
return tp, jsonassert.New(tp)
}
type testPrinter struct {
messages []string
}
func (tp *testPrinter) Errorf(msg string, args ...interface{}) {
tp.messages = append(tp.messages, fmt.Sprintf(msg, args...))
}
func (tp *testPrinter) Helper() {
// Do nothing in tests
}