From 892e7501086669a950e2baa2fcc2f5d6d5b4e24e Mon Sep 17 00:00:00 2001 From: Fred Cheng Date: Mon, 16 Jun 2014 16:41:53 -0700 Subject: [PATCH] if we can't merge the diffs, favor the diff being transformed --- dist/python/jsondiff/__init__.py | 8 ++++++-- dist/python/jsondiff/tests/test_differ.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/dist/python/jsondiff/__init__.py b/dist/python/jsondiff/__init__.py index 9767f0e..4ac4e43 100644 --- a/dist/python/jsondiff/__init__.py +++ b/dist/python/jsondiff/__init__.py @@ -348,8 +348,12 @@ def transform_object_diff(a, b, s, policy=None): del ac[k] a_patches = DMP.patch_make(sk, DMP.diff_fromDelta(sk, op['v'])) b_patches = DMP.patch_make(sk, DMP.diff_fromDelta(sk, b[k]['v'])) - b_text = DMP.patch_apply(b_patches, sk)[0] - ab_text = DMP.patch_apply(a_patches, b_text)[0] + b_text, b_applied = DMP.patch_apply(b_patches, sk) + ab_text, a_applied = DMP.patch_apply(a_patches, b_text) + + if False in a_applied or False in b_applied: + ab_text, a_applied = DMP.patch_apply(a_patches, sk) + diffs = DMP.diff_main(b_text, ab_text) if len(diffs) > 2: DMP.diff_cleanupEfficiency(diffs) diff --git a/dist/python/jsondiff/tests/test_differ.py b/dist/python/jsondiff/tests/test_differ.py index 66f4d8e..7177f9e 100644 --- a/dist/python/jsondiff/tests/test_differ.py +++ b/dist/python/jsondiff/tests/test_differ.py @@ -301,6 +301,24 @@ def test_object_diffs(self): b = {'blah':{'hi':'there'}, 'num':-10, 'new':{'list':['a', 'b', 'c']}} self.assertTrue(object_equals(b, apply_object_diff(a, object_diff(a, b)))) + def test_bad_transform(self): + o = {'log' : 'Original Captains Log'} + a = {'log' : 'Local Captains Log'} + b = {'log' : 'Remote Captains Log'} + + a_diff = diff(o, a)['v'] + b_diff = diff(o, b)['v'] + + a_prime_diff = transform_object_diff(a_diff, b_diff, o) + + ob = apply_object_diff(o, b_diff) + + self.assertTrue(object_equals(ob, b)) + + oba = apply_object_diff(ob, a_prime_diff) + + self.assertFalse(object_equals(oba, ob)) + self.assertTrue(object_equals(oba, a)) if __name__ == '__main__': unittest.main()