1- package jsonpath
1+ package jsonpath_test
22
33import (
44 "bytes"
99
1010 "github.com/steinfletcher/apitest"
1111 "github.com/stretchr/testify/assert"
12+
13+ jsonpath "github.com/steinfletcher/apitest-jsonpath"
1214)
1315
1416func TestApiTest_Contains (t * testing.T ) {
@@ -26,7 +28,7 @@ func TestApiTest_Contains(t *testing.T) {
2628 Handler (handler ).
2729 Get ("/hello" ).
2830 Expect (t ).
29- Assert (Contains (`$.b[? @.key=="c"].value` , "result" )).
31+ Assert (jsonpath . Contains (`$.b[? @.key=="c"].value` , "result" )).
3032 End ()
3133}
3234
@@ -45,7 +47,7 @@ func TestApiTest_Equal_Numeric(t *testing.T) {
4547 Handler (handler ).
4648 Get ("/hello" ).
4749 Expect (t ).
48- Assert (Equal (`$.a` , float64 (12345 ))).
50+ Assert (jsonpath . Equal (`$.a` , float64 (12345 ))).
4951 End ()
5052}
5153
@@ -64,7 +66,7 @@ func TestApiTest_Equal_String(t *testing.T) {
6466 Handler (handler ).
6567 Get ("/hello" ).
6668 Expect (t ).
67- Assert (Equal (`$.a` , "12345" )).
69+ Assert (jsonpath . Equal (`$.a` , "12345" )).
6870 End ()
6971}
7072
@@ -83,60 +85,10 @@ func TestApiTest_Equal_Map(t *testing.T) {
8385 Handler (handler ).
8486 Get ("/hello" ).
8587 Expect (t ).
86- Assert (Equal (`$` , map [string ]interface {}{"a" : "hello" , "b" : float64 (12345 )})).
88+ Assert (jsonpath . Equal (`$` , map [string ]interface {}{"a" : "hello" , "b" : float64 (12345 )})).
8789 End ()
8890}
8991
90- func Test_IncludesElement (t * testing.T ) {
91- list1 := []string {"Foo" , "Bar" }
92- list2 := []int {1 , 2 }
93- simpleMap := map [interface {}]interface {}{"Foo" : "Bar" }
94-
95- ok , found := includesElement ("Hello World" , "World" )
96- assertTrue (t , ok )
97- assertTrue (t , found )
98-
99- ok , found = includesElement (list1 , "Foo" )
100- assertTrue (t , ok )
101- assertTrue (t , found )
102-
103- ok , found = includesElement (list1 , "Bar" )
104- assertTrue (t , ok )
105- assertTrue (t , found )
106-
107- ok , found = includesElement (list2 , 1 )
108- assertTrue (t , ok )
109- assertTrue (t , found )
110-
111- ok , found = includesElement (list2 , 2 )
112- assertTrue (t , ok )
113- assertTrue (t , found )
114-
115- ok , found = includesElement (list1 , "Foo!" )
116- assertTrue (t , ok )
117- assertFalse (t , found )
118-
119- ok , found = includesElement (list2 , 3 )
120- assertTrue (t , ok )
121- assertFalse (t , found )
122-
123- ok , found = includesElement (list2 , "1" )
124- assertTrue (t , ok )
125- assertFalse (t , found )
126-
127- ok , found = includesElement (simpleMap , "Foo" )
128- assertTrue (t , ok )
129- assertTrue (t , found )
130-
131- ok , found = includesElement (simpleMap , "Bar" )
132- assertTrue (t , ok )
133- assertFalse (t , found )
134-
135- ok , found = includesElement (1433 , "1" )
136- assertFalse (t , ok )
137- assertFalse (t , found )
138- }
139-
14092func TestApiTest_Len (t * testing.T ) {
14193 handler := http .NewServeMux ()
14294 handler .HandleFunc ("/hello" , func (w http.ResponseWriter , r * http.Request ) {
@@ -152,8 +104,8 @@ func TestApiTest_Len(t *testing.T) {
152104 Handler (handler ).
153105 Get ("/hello" ).
154106 Expect (t ).
155- Assert (Len (`$.a` , 3 )).
156- Assert (Len (`$.b` , 1 )).
107+ Assert (jsonpath . Len (`$.a` , 3 )).
108+ Assert (jsonpath . Len (`$.b` , 1 )).
157109 End ()
158110}
159111
@@ -172,8 +124,8 @@ func TestApiTest_GreaterThan(t *testing.T) {
172124 Handler (handler ).
173125 Get ("/hello" ).
174126 Expect (t ).
175- Assert (GreaterThan (`$.a` , 2 )).
176- Assert (GreaterThan (`$.b` , 0 )).
127+ Assert (jsonpath . GreaterThan (`$.a` , 2 )).
128+ Assert (jsonpath . GreaterThan (`$.b` , 0 )).
177129 End ()
178130}
179131
@@ -192,8 +144,8 @@ func TestApiTest_LessThan(t *testing.T) {
192144 Handler (handler ).
193145 Get ("/hello" ).
194146 Expect (t ).
195- Assert (LessThan (`$.a` , 4 )).
196- Assert (LessThan (`$.b` , 2 )).
147+ Assert (jsonpath . LessThan (`$.a` , 4 )).
148+ Assert (jsonpath . LessThan (`$.b` , 2 )).
197149 End ()
198150}
199151
@@ -212,8 +164,8 @@ func TestApiTest_Present(t *testing.T) {
212164 Handler (handler ).
213165 Get ("/hello" ).
214166 Expect (t ).
215- Assert (Present (`$.a` )).
216- Assert (NotPresent (`$.password` )).
167+ Assert (jsonpath . Present (`$.a` )).
168+ Assert (jsonpath . NotPresent (`$.password` )).
217169 End ()
218170}
219171
@@ -242,21 +194,21 @@ func TestApiTest_Matches(t *testing.T) {
242194 Handler (handler ).
243195 Get ("/hello" ).
244196 Expect (t ).
245- Assert (Matches (testCase [0 ], testCase [1 ])).
197+ Assert (jsonpath . Matches (testCase [0 ], testCase [1 ])).
246198 End ()
247199 })
248200 }
249201}
250202
251203func TestApiTest_Matches_FailCompile (t * testing.T ) {
252- willFailToCompile := Matches (`$.b[? @.key=="c"].value` , `\` )
204+ willFailToCompile := jsonpath . Matches (`$.b[? @.key=="c"].value` , `\` )
253205 err := willFailToCompile (nil , nil )
254206
255207 assert .EqualError (t , err , `invalid pattern: '\'` )
256208}
257209
258210func TestApiTest_Matches_FailForObject (t * testing.T ) {
259- matcher := Matches (`$.anObject` , `.+` )
211+ matcher := jsonpath . Matches (`$.anObject` , `.+` )
260212
261213 err := matcher (& http.Response {
262214 Body : ioutil .NopCloser (bytes .NewBuffer ([]byte (`{"anObject":{"aString":"lol"}}` ))),
@@ -266,7 +218,7 @@ func TestApiTest_Matches_FailForObject(t *testing.T) {
266218}
267219
268220func TestApiTest_Matches_FailForArray (t * testing.T ) {
269- matcher := Matches (`$.aSlice` , `.+` )
221+ matcher := jsonpath . Matches (`$.aSlice` , `.+` )
270222
271223 err := matcher (& http.Response {
272224 Body : ioutil .NopCloser (bytes .NewBuffer ([]byte (`{"aSlice":[1,2,3]}` ))),
@@ -276,23 +228,11 @@ func TestApiTest_Matches_FailForArray(t *testing.T) {
276228}
277229
278230func TestApiTest_Matches_FailForNilValue (t * testing.T ) {
279- matcher := Matches (`$.nothingHere` , `.+` )
231+ matcher := jsonpath . Matches (`$.nothingHere` , `.+` )
280232
281233 err := matcher (& http.Response {
282234 Body : ioutil .NopCloser (bytes .NewBuffer ([]byte (`{"aSlice":[1,2,3]}` ))),
283235 }, nil )
284236
285237 assert .EqualError (t , err , "no match for pattern: '$.nothingHere'" )
286238}
287-
288- func assertTrue (t * testing.T , v bool ) {
289- if ! v {
290- t .Error ("\n expected to be true but was false" )
291- }
292- }
293-
294- func assertFalse (t * testing.T , v bool ) {
295- if v {
296- t .Error ("\n expected to be false but was true" )
297- }
298- }
0 commit comments