def test_dict_nested(self): diff = Diff({'x': { 'y': { 'a': 1, 'b': 2, 'c': 3 } }}, {'x': { 'y': { 'a': 1, 'b': 3, 'd': 4 } }}) self.assertFalse(diff) self.assertEqual(list(diff.added()), [(['x', 'y', 'd'], 4)]) self.assertEqual(list(diff.removed()), [(['x', 'y', 'c'], 3)]) self.assertEqual(list(diff.modified()), [(['x', 'y', 'b'], 2, 3)]) self.assertEqual( str(diff), dedent(''' +++ expected --- got ============ { x: { y: { ? b: 2 => 3, + d: 4, - c: 3 } } } ''').strip() + '\n')
def test_list_different_length(self): that, this = [1, 2, 3, 4, 5, 6], [-1, 2, 3] diff = Diff(that, this) self.assertFalse(diff) self.assertEqual(list(diff.modified()), [ ([0], 1, -1), ]) self.assertEqual(list(diff.added()), []) self.assertEqual(list(diff.removed()), [([3], 4), ([4], 5), ([5], 6)]) diff = Diff(this, that) self.assertFalse(diff) self.assertEqual(list(diff.modified()), [ ([0], -1, 1), ]) self.assertEqual(list(diff.added()), [([3], 4), ([4], 5), ([5], 6)]) self.assertEqual(list(diff.removed()), [])
def test_dict_added(self): for this, that in [ ({}, {'b': 2}), ({'a': 1}, {'b': 2}), ({'a': 1}, {'a': 1, 'b': 2}) ]: with self.subTest((this, that)): diff = Diff(this, that) self.assertFalse(diff) self.assertEqual( list(diff.added()), [(['b'], 2)] )
def test_mixed(self): diff = Diff( {'x': {'y': [0, 2, {'a': 1, 'b': 2, 'c': 3}]}}, {'x': {'y': [1, 2, {'a': 1, 'b': 3, 'd': 4}, 5]}} ) self.assertFalse(diff) self.assertEqual( list(diff.added()), [ (['x', 'y', 2, 'd'], 4), (['x', 'y', 3], 5) ] ) self.assertEqual( list(diff.removed()), [ (['x', 'y', 2, 'c'], 3), ] ) self.assertEqual( list(diff.modified()), [ (['x', 'y', 0], 0, 1), (['x', 'y', 2, 'b'], 2, 3) ] ) self.assertEqual( str(diff), dedent(''' +++ expected --- got ============ { x: { y: [ ? 0: 0 => 1, 2: { ? b: 2 => 3, + d: 4, - c: 3 }, + 3: 5 ] } } ''').strip() + '\n' )
def test_list_nested(self): diff = Diff([1, [2, 3], 5, 6], [1, [2, 3, 4], 5]) self.assertFalse(diff) self.assertEqual(list(diff.removed()), [([3], 6)]) self.assertEqual(list(diff.added()), [([1, 2], 4)]) self.assertEqual(list(diff.modified()), []) self.assertEqual( str(diff), dedent(''' +++ expected --- got ============ [ 1: [ + 2: 4 ], - 3: 6 ] ''').strip() + '\n')