Beispiel #1
0
    def test_list_equal_length(self):
        diff = Diff([1, 2, 3], [4, 5, 6])
        self.assertFalse(diff)
        self.assertEqual(list(diff.modified()), [
            ([0], 1, 4),
            ([1], 2, 5),
            ([2], 3, 6),
        ])

        diff = Diff([1, 2, 3], [3, 2, 1])
        self.assertFalse(diff)
        self.assertEqual(list(diff.modified()), [
            ([0], 1, 3),
            ([2], 3, 1),
        ])
Beispiel #2
0
 def test_dict_types(self):
     diff = Diff({
         'a': 4.2,
         'b': False,
         'c': 'a',
         'd': 1,
         'e': None
     }, {
         'a': 4.1,
         'b': True,
         'c': 'b',
         'd': 2,
         'e': 1
     })
     self.assertFalse(diff)
     self.assertEqual(list(diff.modified()), [(['a'], 4.2, 4.1),
                                              (['b'], False, True),
                                              (['c'], 'a', 'b'),
                                              (['d'], 1, 2),
                                              (['e'], None, 1)])
     self.assertEqual(
         str(diff),
         dedent('''
         +++ expected
         --- got
         ============
          {
         ? a: 4.2 => 4.1,
         ? b: False => True,
         ? c: a => b,
         ? d: 1 => 2,
         ? e: None => 1
          }
         ''').strip() + '\n')
Beispiel #3
0
 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')
Beispiel #4
0
    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()), [])
Beispiel #5
0
 def test_dict_modified(self):
     for this, that in [
             ({'a': 1}, {'a': 2}),
             ({'a': 1, 'b': 2}, {'a': 2, 'b': 2}),
     ]:
         with self.subTest((this, that)):
             diff = Diff(this, that)
             self.assertFalse(diff)
             self.assertEqual(
                 list(diff.modified()),
                 [(['a'], 1, 2)]
             )
Beispiel #6
0
 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'
     )
Beispiel #7
0
 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')