@@ -75,6 +75,50 @@ def test_round_trip(self):
75
75
new_ptr = JsonPointer .from_parts (parts )
76
76
self .assertEqual (ptr , new_ptr )
77
77
78
+ def test_str_and_repr (self ):
79
+ paths = [
80
+ ("" , "" , "JsonPointer({u}'')" ),
81
+ ("/foo" , "/foo" , "JsonPointer({u}'/foo')" ),
82
+ ("/foo/0" , "/foo/0" , "JsonPointer({u}'/foo/0')" ),
83
+ ("/" , "/" , "JsonPointer({u}'/')" ),
84
+ ("/a~1b" , "/a~1b" , "JsonPointer({u}'/a~1b')" ),
85
+ ("/c%d" , "/c%d" , "JsonPointer({u}'/c%d')" ),
86
+ ("/e^f" , "/e^f" , "JsonPointer({u}'/e^f')" ),
87
+ ("/g|h" , "/g|h" , "JsonPointer({u}'/g|h')" ),
88
+ ("/i\\ j" , "/i\\ j" , "JsonPointer({u}'/i\\ \\ j')" ),
89
+ ("/k\" l" , "/k\" l" , "JsonPointer({u}'/k\" l')" ),
90
+ ("/ " , "/ " , "JsonPointer({u}'/ ')" ),
91
+ ("/m~0n" , "/m~0n" , "JsonPointer({u}'/m~0n')" ),
92
+ ]
93
+ for path , ptr_str , ptr_repr in paths :
94
+ ptr = JsonPointer (path )
95
+ self .assertEqual (path , ptr .path )
96
+
97
+ if sys .version_info [0 ] == 2 :
98
+ u_str = "u"
99
+ else :
100
+ u_str = ""
101
+ self .assertEqual (ptr_str , str (ptr ))
102
+ self .assertEqual (ptr_repr .format (u = u_str ), repr (ptr ))
103
+
104
+ if sys .version_info [0 ] == 2 :
105
+ path = "/\xee "
106
+ ptr_str = b"/\xee "
107
+ ptr_repr = "JsonPointer(u'/\\ xee')"
108
+ else :
109
+ path = "/\xee "
110
+ ptr_str = "/\xee "
111
+ ptr_repr = "JsonPointer('/\xee ')"
112
+ ptr = JsonPointer (path )
113
+ self .assertEqual (path , ptr .path )
114
+
115
+ self .assertEqual (ptr_str , str (ptr ))
116
+ self .assertEqual (ptr_repr , repr (ptr ))
117
+
118
+ # should not be unicode in Python 2
119
+ self .assertIsInstance (str (ptr ), str )
120
+ self .assertIsInstance (repr (ptr ), str )
121
+
78
122
def test_parts (self ):
79
123
paths = [
80
124
("" , []),
@@ -131,6 +175,42 @@ def test_contains_magic(self):
131
175
self .assertTrue (self .ptr1 in self .ptr1 )
132
176
self .assertFalse (self .ptr3 in self .ptr1 )
133
177
178
+ def test_join (self ):
179
+
180
+ ptr12a = self .ptr1 .join (self .ptr2 )
181
+ self .assertEqual (ptr12a .path , "/a/b/c/a/b" )
182
+
183
+ ptr12b = self .ptr1 .join (self .ptr2 .parts )
184
+ self .assertEqual (ptr12b .path , "/a/b/c/a/b" )
185
+
186
+ ptr12c = self .ptr1 .join (self .ptr2 .parts [0 :1 ])
187
+ self .assertEqual (ptr12c .path , "/a/b/c/a" )
188
+
189
+ ptr12d = self .ptr1 .join ("/a/b" )
190
+ self .assertEqual (ptr12d .path , "/a/b/c/a/b" )
191
+
192
+ ptr12e = self .ptr1 .join (["a" , "b" ])
193
+ self .assertEqual (ptr12e .path , "/a/b/c/a/b" )
194
+
195
+ self .assertRaises (JsonPointerException , self .ptr1 .join , 0 )
196
+
197
+ def test_join_magic (self ):
198
+
199
+ ptr12a = self .ptr1 / self .ptr2
200
+ self .assertEqual (ptr12a .path , "/a/b/c/a/b" )
201
+
202
+ ptr12b = self .ptr1 / self .ptr2 .parts
203
+ self .assertEqual (ptr12b .path , "/a/b/c/a/b" )
204
+
205
+ ptr12c = self .ptr1 / self .ptr2 .parts [0 :1 ]
206
+ self .assertEqual (ptr12c .path , "/a/b/c/a" )
207
+
208
+ ptr12d = self .ptr1 / "/a/b"
209
+ self .assertEqual (ptr12d .path , "/a/b/c/a/b" )
210
+
211
+ ptr12e = self .ptr1 / ["a" , "b" ]
212
+ self .assertEqual (ptr12e .path , "/a/b/c/a/b" )
213
+
134
214
class WrongInputTests (unittest .TestCase ):
135
215
136
216
def test_no_start_slash (self ):
@@ -193,6 +273,12 @@ def test_set(self):
193
273
newdoc = set_pointer (doc , "/foo/1" , "cod" , inplace = False )
194
274
self .assertEqual (resolve_pointer (newdoc , "/foo/1" ), "cod" )
195
275
276
+ self .assertEqual (len (doc ["foo" ]), 2 )
277
+ newdoc = set_pointer (doc , "/foo/-" , "xyz" , inplace = False )
278
+ self .assertEqual (resolve_pointer (newdoc , "/foo/2" ), "xyz" )
279
+ self .assertEqual (len (doc ["foo" ]), 2 )
280
+ self .assertEqual (len (newdoc ["foo" ]), 3 )
281
+
196
282
newdoc = set_pointer (doc , "/" , 9 , inplace = False )
197
283
self .assertEqual (resolve_pointer (newdoc , "/" ), 9 )
198
284
@@ -209,6 +295,11 @@ def test_set(self):
209
295
set_pointer (doc , "/foo/1" , "cod" )
210
296
self .assertEqual (resolve_pointer (doc , "/foo/1" ), "cod" )
211
297
298
+ self .assertEqual (len (doc ["foo" ]), 2 )
299
+ set_pointer (doc , "/foo/-" , "xyz" )
300
+ self .assertEqual (resolve_pointer (doc , "/foo/2" ), "xyz" )
301
+ self .assertEqual (len (doc ["foo" ]), 3 )
302
+
212
303
set_pointer (doc , "/" , 9 )
213
304
self .assertEqual (resolve_pointer (doc , "/" ), 9 )
214
305
0 commit comments