diff --git a/.gitignore b/.gitignore index 179770e..56a7db7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build dist *.egg-info/ doc/_build +.hypothesis/ diff --git a/.travis.yml b/.travis.yml index 77f8b19..c95cbfc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ python: install: - travis_retry pip install -r requirements.txt + - if [ "$TRAVIS_PYTHON_VERSION" != "2.6" -a "$TRAVIS_PYTHON_VERSION" != "3.2" -a "$TRAVIS_PYTHON_VERSION" != "3.3" ]; then travis_retry pip install hypothesis; fi - if [ "$TRAVIS_PYTHON_VERSION" == "3.2" ]; then travis_retry pip install 'coverage<4'; fi - travis_retry pip install coveralls diff --git a/requirements-dev.txt b/requirements-dev.txt index 21daf9a..744c41c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,2 +1,3 @@ wheel pypandoc +hypothesis >= 3 diff --git a/tests.py b/tests.py index 51d9517..7e79202 100755 --- a/tests.py +++ b/tests.py @@ -9,6 +9,18 @@ import jsonpatch import jsonpointer import sys +import string + +try: + import hypothesis + from hypothesis import strategies as st +except ImportError: + hypothesis = None + +try: + unicode +except NameError: + unicode = str class ApplyPatchTestCase(unittest.TestCase): @@ -518,6 +530,28 @@ def test_replace_missing(self): self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj) +if hypothesis is not None: + json_st = st.recursive( + st.one_of([ + st.none(), + st.booleans(), + st.floats(allow_nan=False), + st.text(string.printable)]), + lambda children: + st.lists(children) + | st.dictionaries(st.text(string.printable), children)) + + class RoundtripTests(unittest.TestCase): + @hypothesis.example({}, {unicode('%20'): None}) + @hypothesis.example({unicode('%20'): None}, {}) + @hypothesis.given(json_st, json_st) + def test_roundtrip(self, src, dst): + patch = jsonpatch.JsonPatch.from_diff(src, dst, False) + hypothesis.note('Patch: %s' % (patch,)) + res = patch.apply(src) + self.assertEqual(res, dst) + + if __name__ == '__main__': modules = ['jsonpatch'] @@ -531,6 +565,8 @@ def get_suite(): suite.addTest(unittest.makeSuite(InvalidInputTests)) suite.addTest(unittest.makeSuite(ConflictTests)) suite.addTest(unittest.makeSuite(OptimizationTests)) + if hypothesis is not None: + suite.addTest(unittest.makeSuite(RoundtripTests)) return suite