Skip to content

Commit

Permalink
unambiguous array indices make pointers comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoegl committed Jan 6, 2013
1 parent 2537956 commit 55198aa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions jsonpointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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>'
Expand Down Expand Up @@ -205,6 +205,23 @@ def contains(self, ptr):
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)
Expand Down
21 changes: 20 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -46,12 +47,30 @@ def test_eol(self):
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])
Expand Down

0 comments on commit 55198aa

Please sign in to comment.