unambiguous array indices make pointers comparable
This commit is contained in:
@@ -5,7 +5,7 @@ Resolve JSON Pointers in Python
|
|||||||
-------------------------------
|
-------------------------------
|
||||||
|
|
||||||
Library to resolve JSON Pointers according to
|
Library to resolve JSON Pointers according to
|
||||||
http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-04
|
http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-08
|
||||||
|
|
||||||
See Sourcecode for Examples
|
See Sourcecode for Examples
|
||||||
* Website: https://github.com/stefankoegl/python-json-pointer
|
* Website: https://github.com/stefankoegl/python-json-pointer
|
||||||
|
|||||||
@@ -30,9 +30,9 @@
|
|||||||
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
#
|
#
|
||||||
|
|
||||||
""" Identify specific nodes in a JSON document (according to draft 07) """
|
""" Identify specific nodes in a JSON document (according to draft 08) """
|
||||||
|
|
||||||
# http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07
|
# http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-08
|
||||||
|
|
||||||
# Will be parsed by setup.py to determine package metadata
|
# Will be parsed by setup.py to determine package metadata
|
||||||
__author__ = 'Stefan Kögl <stefan@skoegl.net>'
|
__author__ = 'Stefan Kögl <stefan@skoegl.net>'
|
||||||
@@ -205,6 +205,23 @@ class JsonPointer(object):
|
|||||||
self.parts[:len(ptr.parts)] == ptr.parts
|
self.parts[:len(ptr.parts)] == ptr.parts
|
||||||
|
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
""" compares a pointer to another object
|
||||||
|
|
||||||
|
Pointers can be compared by comparing their strings (or splitted
|
||||||
|
strings), because no two different parts can point to the same
|
||||||
|
structure in an object (eg no different number representations) """
|
||||||
|
|
||||||
|
if not isinstance(other, JsonPointer):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return self.parts == other.parts
|
||||||
|
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(tuple(self.parts))
|
||||||
|
|
||||||
|
|
||||||
def pairwise(iterable):
|
def pairwise(iterable):
|
||||||
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
|
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
|
||||||
a, b = tee(iterable)
|
a, b = tee(iterable)
|
||||||
|
|||||||
21
tests.py
21
tests.py
@@ -4,7 +4,8 @@
|
|||||||
import doctest
|
import doctest
|
||||||
import unittest
|
import unittest
|
||||||
import sys
|
import sys
|
||||||
from jsonpointer import resolve_pointer, EndOfList, JsonPointerException
|
from jsonpointer import resolve_pointer, EndOfList, JsonPointerException, \
|
||||||
|
JsonPointer
|
||||||
|
|
||||||
class SpecificationTests(unittest.TestCase):
|
class SpecificationTests(unittest.TestCase):
|
||||||
""" Tests all examples from the JSON Pointer specification """
|
""" Tests all examples from the JSON Pointer specification """
|
||||||
@@ -46,12 +47,30 @@ class SpecificationTests(unittest.TestCase):
|
|||||||
self.assertRaises(JsonPointerException, resolve_pointer, doc, "/foo/-/1")
|
self.assertRaises(JsonPointerException, resolve_pointer, doc, "/foo/-/1")
|
||||||
|
|
||||||
|
|
||||||
|
class ComparisonTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_eq_hash(self):
|
||||||
|
p1 = JsonPointer("/something/1/b")
|
||||||
|
p2 = JsonPointer("/something/1/b")
|
||||||
|
p3 = JsonPointer("/something/1.0/b")
|
||||||
|
|
||||||
|
self.assertEqual(p1, p2)
|
||||||
|
self.assertNotEqual(p1, p3)
|
||||||
|
self.assertNotEqual(p2, p3)
|
||||||
|
|
||||||
|
self.assertEqual(hash(p1), hash(p2))
|
||||||
|
self.assertNotEqual(hash(p1), hash(p3))
|
||||||
|
self.assertNotEqual(hash(p2), hash(p3))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
modules = ['jsonpointer']
|
modules = ['jsonpointer']
|
||||||
coverage_modules = []
|
coverage_modules = []
|
||||||
|
|
||||||
suite = unittest.TestSuite()
|
suite = unittest.TestSuite()
|
||||||
suite.addTest(unittest.makeSuite(SpecificationTests))
|
suite.addTest(unittest.makeSuite(SpecificationTests))
|
||||||
|
suite.addTest(unittest.makeSuite(ComparisonTests))
|
||||||
|
|
||||||
for module in modules:
|
for module in modules:
|
||||||
m = __import__(module, fromlist=[module])
|
m = __import__(module, fromlist=[module])
|
||||||
|
|||||||
Reference in New Issue
Block a user