@@ -92,3 +92,95 @@ func TestNotContainsMatcher_Fails(t *testing.T) {
9292 diffText := "\n Expected\n \n lore ipsum donor\n \n to not contain\n \n donor\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