unambiguous array indices make pointers comparable

This commit is contained in:
Stefan Kögl
2013-01-06 10:57:13 +01:00
parent 2537956503
commit 55198aa4cb
3 changed files with 40 additions and 4 deletions

View File

@@ -5,7 +5,7 @@ Resolve JSON Pointers in Python
-------------------------------
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
* Website: https://github.com/stefankoegl/python-json-pointer

View File

@@ -30,9 +30,9 @@
# 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
__author__ = 'Stefan Kögl <stefan@skoegl.net>'
@@ -205,6 +205,23 @@ class JsonPointer(object):
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):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)

View File

@@ -4,7 +4,8 @@
import doctest
import unittest
import sys
from jsonpointer import resolve_pointer, EndOfList, JsonPointerException
from jsonpointer import resolve_pointer, EndOfList, JsonPointerException, \
JsonPointer
class SpecificationTests(unittest.TestCase):
""" Tests all examples from the JSON Pointer specification """
@@ -46,12 +47,30 @@ class SpecificationTests(unittest.TestCase):
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']
coverage_modules = []
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SpecificationTests))
suite.addTest(unittest.makeSuite(ComparisonTests))
for module in modules:
m = __import__(module, fromlist=[module])