changeset 2166:8870f6cc7884

make_text_diff() function
author Ana Balica <ana.balica@gmail.com>
date Sun, 28 Jul 2013 11:09:14 +0300
parents e3cba78bea69
children 5cb3fdc338c1
files MoinMoin/util/_tests/test_diff_datastruct.py MoinMoin/util/diff_datastruct.py
diffstat 2 files changed, 64 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/util/_tests/test_diff_datastruct.py	Fri Jul 26 17:09:01 2013 +0300
+++ b/MoinMoin/util/_tests/test_diff_datastruct.py	Sun Jul 28 11:09:14 2013 +0300
@@ -7,11 +7,16 @@
 
 import pytest
 
-from MoinMoin.util.diff_datastruct import diff, Undefined, INSERT, DELETE
+from MoinMoin.util.diff_datastruct import diff, make_text_diff, Undefined, INSERT, DELETE
 
 
 class TestDiffDatastruct(object):
 
+    def _test_make_text_diff(self, tests):
+        for changes, expected in tests:
+            for got in make_text_diff(changes):
+                assert got == expected
+
     def test_diff_no_change(self):
         datastruct = [None, True, 42, u"value", [1, 2, 3], dict(one=1, two=2)]
         for d in datastruct:
@@ -97,3 +102,47 @@
         for d1, d2 in tests:
             with pytest.raises(TypeError):
                 diff(d1, d2)
+
+    def test_make_text_diff_empty(self):
+        for got in make_text_diff([]):
+            assert got == u""
+
+    def test_make_text_diff_none(self):
+        tests = [([(INSERT, [], None)], u"+ None"),
+                 ([(DELETE, [], None)], u"- None")]
+        self._test_make_text_diff(tests)
+
+    def test_make_text_diff_bool(self):
+        tests = [([(INSERT, [], True)], u"+ True"),
+                 ([(DELETE, [], False)], u"- False")]
+        self._test_make_text_diff(tests)
+
+    def test_make_text_diff_int(self):
+        tests = [([(INSERT, [], 123)], u"+ 123"),
+                 ([(DELETE, [], 321)], u"- 321")]
+        self._test_make_text_diff(tests)
+
+    def test_make_text_diff_float(self):
+        tests = [([(INSERT, [], 1.2)], u"+ 1.2"),
+                 ([(DELETE, [], 3.4)], u"- 3.4")]
+        self._test_make_text_diff(tests)
+
+    def test_make_text_diff_unicode(self):
+        tests = [([(INSERT, [], u"new value")], u"+ new value"),
+                 ([(DELETE, [], u"old value")], u"- old value")]
+        self._test_make_text_diff(tests)
+
+    def test_make_text_diff_list(self):
+        tests = [([(INSERT, [], [1, 2, 3, ])], u"+ [1, 2, 3]"),
+                 ([(DELETE, [], [4, 5, ])], u"- [4, 5]")]
+        self._test_make_text_diff(tests)
+
+    def test_make_text_diff_one_key(self):
+        tests = [([(INSERT, ["key"], u"new value")], u"+ key: new value"),
+                 ([(DELETE, ["key"], u"old value")], u"- key: old value")]
+        self._test_make_text_diff(tests)
+
+    def test_make_text_diff_multiple_keys(self):
+        tests = [([(INSERT, ["key1", "key2"], 1)], u"+ key1.key2: 1"),
+                 ([(DELETE, ["key1", "key2", "key3"], 2)], u"- key1.key2.key3: 2")]
+        self._test_make_text_diff(tests)
--- a/MoinMoin/util/diff_datastruct.py	Fri Jul 26 17:09:01 2013 +0300
+++ b/MoinMoin/util/diff_datastruct.py	Sun Jul 28 11:09:14 2013 +0300
@@ -78,3 +78,17 @@
             "Unsupported diff between {0} and {1} data types".format(
                 type(d1), type(d2)))
     return changes
+
+
+def make_text_diff(changes):
+    """ Transform change tuples into text diffs
+
+    :param changes: a list of tuples of the format (<change type>, <basekeys>, <value>)
+                    that represent a diff
+    :return: a generator of text diffs
+    """
+    marker = {INSERT: u"+", DELETE: u"-"}
+    for change_type, keys, value in changes:
+        yield "{0} {1}{2}{3}".format(marker[change_type],
+                                     ".".join(unicode(key) for key in keys),
+                                     ": " if keys else "", unicode(value))