Minor improvement of cnfvar diff output
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 6 Sep 2023 07:32:23 +0000 (09:32 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 6 Sep 2023 07:32:23 +0000 (09:32 +0200)
Add unittest for new output

src/cnfvar/model.py
test/cnfvar/test_model.py

index 7944d78..030b05c 100644 (file)
@@ -963,8 +963,11 @@ class CnfDiff(list):
         :return: Iterator over text lines
         """
         for diff_type, cnf, ancestry in self:
-            ancestral_string = ' > '.join(f"{anc.name}={anc.value}" for anc in ancestry)
-            output_func(f"cnf diff: {diff_type} {cnf.name} ({cnf.instance}) = {cnf.value!r} in "
+            if ancestry:
+                ancestral_string = 'in ' + ' > '.join(f"{anc.name}={anc.value}" for anc in ancestry)
+            else:
+                ancestral_string = ''
+            output_func(f"cnf diff: {diff_type} {cnf.name} ({cnf.instance}) = {cnf.value!r} "
                         + ancestral_string)
 
 
index e09587c..0367a94 100644 (file)
@@ -231,8 +231,8 @@ class TestCompare(unittest.TestCase):
         """Compare data to itself."""
         self.assertEqual(CnfDiff(), self.DATA.compare(self.DATA))
 
-    def test_remove(self):
-        """Remove entry from data, check it is found."""
+    def test_remove_child(self):
+        """Remove child entry from data, check it is found."""
         other = deepcopy(self.DATA)
         removed = other.single_with_value("Star Trek").children\
             .single_with_value("The Original Series").children\
@@ -248,6 +248,19 @@ class TestCompare(unittest.TestCase):
                  "in franchise=Star Trek > series=The Original Series > ship=Enterprise (original)"
         self.assertEqual(expect, diff_text[0])
 
+    def test_remove_root(self):
+        """Remove entry from data at root level, check it is found and properly textified"""
+        other = deepcopy(self.DATA)
+        removed = other.remove_where(lambda cnf: cnf.value == "Babylon 5")
+        self.assertEqual(1, len(removed))
+        diff = self.DATA.compare(other)
+        self.assertEqual(1, len(diff))
+        diff_text = []
+        diff.print(output_func=lambda s: diff_text.append(s))
+        self.assertEqual(1, len(diff_text))
+        expect = "cnf diff: - franchise (4) = 'Babylon 5' "
+        self.assertEqual(expect, diff_text[0])
+
     def test_add(self):
         """Add entry to data, check it is found."""
         other = deepcopy(self.DATA)