From: Christian Herdtweck Date: Wed, 6 Sep 2023 07:32:23 +0000 (+0200) Subject: Minor improvement of cnfvar diff output X-Git-Tag: v1.7.3~1^2~8 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=d37c9f448799d52dd81b334e03cd91099da5ac45;p=pyi2ncommon Minor improvement of cnfvar diff output Add unittest for new output --- diff --git a/src/cnfvar/model.py b/src/cnfvar/model.py index 7944d78..030b05c 100644 --- a/src/cnfvar/model.py +++ b/src/cnfvar/model.py @@ -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) diff --git a/test/cnfvar/test_model.py b/test/cnfvar/test_model.py index e09587c..0367a94 100644 --- a/test/cnfvar/test_model.py +++ b/test/cnfvar/test_model.py @@ -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)