Skip to content

Commit f8f1d9e

Browse files
committed
Add XML and JSON matcher tests
1 parent b10baaa commit f8f1d9e

2 files changed

Lines changed: 121 additions & 15 deletions

File tree

pkg/matcher/matcher.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ func (m JSONMatcher) Match(got interface{}, expected interface{}) MatcherResult
173173
json := expected.(map[string]string)
174174
for q, e := range json {
175175
r := gjson.Get(got.(string), q)
176-
if r.Value() != e {
176+
if r.Value() == nil {
177+
result.Success = false
178+
result.Diff = fmt.Sprintf(`Query "%s" did not match a path`, q)
179+
break
180+
}
181+
182+
if fmt.Sprintf("%v", r.Value()) != e {
177183
result.Success = false
178184
result.Diff = fmt.Sprintf(`Expected json path "%s" with result
179185
@@ -182,6 +188,7 @@ func (m JSONMatcher) Match(got interface{}, expected interface{}) MatcherResult
182188
to be equal to
183189
184190
%s`, q, e, r.Value())
191+
break
185192
}
186193
}
187194

@@ -204,25 +211,32 @@ func (m XMLMatcher) Match(got interface{}, expected interface{}) MatcherResult {
204211
log.Fatal(err)
205212
}
206213

207-
if r := xmlquery.FindOne(doc, q); r != nil {
208-
if r.InnerText() != e {
209-
result.Success = false
210-
result.Diff = fmt.Sprintf(`Expected xml path "%s" with result
214+
node, err := xmlquery.Query(doc, q)
215+
if err != nil {
216+
return MatcherResult{
217+
Success: false,
218+
Diff: fmt.Sprintf("Error occured: %s", err),
219+
}
220+
}
221+
222+
if node == nil {
223+
return MatcherResult{
224+
Success: false,
225+
Diff: fmt.Sprintf(`Query "%s" did not match a path`, q),
226+
}
227+
}
228+
229+
if node.InnerText() != e {
230+
result.Success = false
231+
result.Diff = fmt.Sprintf(`Expected xml path "%s" with result
211232
212233
%s
213234
214235
to be equal to
215236
216-
%s`, q, e, r.InnerText())
217-
}
218-
} else {
219-
result = MatcherResult{
220-
Success: false,
221-
Diff: fmt.Sprintf(`Query "%s" did not match a path`, q),
222-
}
223-
}
224-
225-
}
237+
%s`, q, e, node.InnerText())
238+
}
239+
}
226240

227241
return result
228242
}

pkg/matcher/matcher_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,95 @@ func TestNotContainsMatcher_Fails(t *testing.T) {
9292
diffText := "\nExpected\n\nlore ipsum donor\n\nto not contain\n\ndonor\n"
9393
assert.Equal(t, diffText, r.Diff)
9494
}
95+
96+
func TestXMLMatcher_Match(t *testing.T) {
97+
m := XMLMatcher{}
98+
r := m.Match("<book>test</book>", map[string]string{"/book": "test"})
99+
100+
assert.True(t, r.Success)
101+
assert.Equal(t, "", r.Diff)
102+
}
103+
104+
func TestXMLMatcher_DoesNotMatch(t *testing.T) {
105+
m := XMLMatcher{}
106+
r := m.Match("<book>another</book>", map[string]string{"/book": "test"})
107+
108+
diff := `Expected xml path "/book" with result
109+
110+
test
111+
112+
to be equal to
113+
114+
another`
115+
assert.False(t, r.Success)
116+
assert.Equal(t, diff, r.Diff)
117+
}
118+
119+
func TestXMLMatcher_DoesNotFindPath(t *testing.T) {
120+
m := XMLMatcher{}
121+
r := m.Match("<comic>test</comic>", map[string]string{"/book": "test"})
122+
123+
assert.False(t, r.Success)
124+
assert.Equal(t, `Query "/book" did not match a path`, r.Diff)
125+
}
126+
127+
func TestXMLMatcher_MatchArray(t *testing.T) {
128+
m := NewMatcher(XML)
129+
html := "<books><book>test1</book><book>test2</book></books>"
130+
131+
r := m.Match(html, map[string]string{"/books": "test1test2"})
132+
133+
assert.True(t, r.Success)
134+
assert.Equal(t, "", r.Diff)
135+
}
136+
137+
func TestXMLMatcher_WithXPATHError(t *testing.T) {
138+
m := XMLMatcher{}
139+
r := m.Match("<book>test</book>", map[string]string{"<!/book": "test"})
140+
141+
assert.False(t, r.Success)
142+
assert.Equal(t, `Error occured: expression must evaluate to a node-set`, r.Diff)
143+
}
144+
145+
func TestJSONMatcher_Match(t *testing.T) {
146+
m := JSONMatcher{}
147+
r := m.Match(`{"book": "test"}`, map[string]string{"book": "test"})
148+
149+
assert.True(t, r.Success)
150+
assert.Equal(t, "", r.Diff)
151+
}
152+
153+
func TestJSONMatcher_DoesNotFindPath(t *testing.T) {
154+
m := JSONMatcher{}
155+
r := m.Match(`{"comic": "test"}`, map[string]string{"book": "test"})
156+
157+
assert.False(t, r.Success)
158+
assert.Equal(t, `Query "book" did not match a path`, r.Diff)
159+
}
160+
161+
func TestJSONMatcher_MatchArray(t *testing.T) {
162+
m := JSONMatcher{}
163+
json := `{"books": [ "test1", "test2" ]}`
164+
165+
r := m.Match(json, map[string]string{"books": "[test1 test2]"})
166+
167+
assert.True(t, r.Success)
168+
assert.Equal(t, "", r.Diff)
169+
}
170+
171+
172+
func TestJSONMatcher_DoesNotMatch(t *testing.T) {
173+
m := NewMatcher(JSON)
174+
r := m.Match(`{"book": "another"}`, map[string]string{"book": "test"})
175+
176+
diff := `Expected json path "book" with result
177+
178+
test
179+
180+
to be equal to
181+
182+
another`
183+
184+
assert.False(t, r.Success)
185+
assert.Equal(t, diff, r.Diff)
186+
}

0 commit comments

Comments
 (0)