From 55198aa4cb5c7d3d503e8cb9e717433688af8d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=B6gl?= Date: Sun, 6 Jan 2013 10:57:13 +0100 Subject: unambiguous array indices make pointers comparable --- README.md | 2 +- jsonpointer.py | 21 +++++++++++++++++++-- tests.py | 21 ++++++++++++++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4a5fa45..ee0cd42 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/jsonpointer.py b/jsonpointer.py index cbb9137..e6ddb4a 100644 --- a/jsonpointer.py +++ b/jsonpointer.py @@ -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 ' @@ -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) diff --git a/tests.py b/tests.py index 2d9c8bc..5f15a25 100755 --- a/tests.py +++ b/tests.py @@ -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]) -- cgit v1.2.1