Import of working code base
Signed-off-by: martin f. krafft <madduck@madduck.net>
diff --git a/mergers/list/__init__.py b/mergers/list/__init__.py
new file mode 100644
index 0000000..51ee818
--- /dev/null
+++ b/mergers/list/__init__.py
@@ -0,0 +1,10 @@
+#
+# -*- coding: utf-8 -*-
+#
+# This file is part of reclass (http://github.com/madduck/reclass)
+#
+# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+from extend import ListExtend
+from set import SetExtend
diff --git a/mergers/list/base.py b/mergers/list/base.py
new file mode 100644
index 0000000..a7bbc17
--- /dev/null
+++ b/mergers/list/base.py
@@ -0,0 +1,16 @@
+#
+# -*- coding: utf-8 -*-
+#
+# This file is part of reclass (http://github.com/madduck/reclass)
+#
+# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+from mergers.base import BaseMerger
+
+class BaseListMerger(BaseMerger):
+
+ def merge(self, first, second):
+ first = [first] if not isinstance(first, list) else first[:]
+ second = [second] if not isinstance(second, list) else second[:]
+ return self._combine(first, second)
diff --git a/mergers/list/extend.py b/mergers/list/extend.py
new file mode 100644
index 0000000..f4acc35
--- /dev/null
+++ b/mergers/list/extend.py
@@ -0,0 +1,18 @@
+#
+# -*- coding: utf-8 -*-
+#
+# This file is part of reclass (http://github.com/madduck/reclass)
+#
+# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+from base import BaseListMerger
+
+class ListExtend(BaseListMerger):
+
+ def _combine(self, first, second):
+ if isinstance(second, list):
+ first.extend(second)
+ else:
+ first.append(second)
+ return first
diff --git a/mergers/list/set.py b/mergers/list/set.py
new file mode 100644
index 0000000..ddb0eb4
--- /dev/null
+++ b/mergers/list/set.py
@@ -0,0 +1,17 @@
+#
+# -*- coding: utf-8 -*-
+#
+# This file is part of reclass (http://github.com/madduck/reclass)
+#
+# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+from extend import BaseListMerger
+
+class SetExtend(BaseListMerger):
+
+ def _combine(self, first, second):
+ for i in second:
+ if i not in first:
+ first.append(i)
+ return first
diff --git a/mergers/list/tests/test_extend.py b/mergers/list/tests/test_extend.py
new file mode 100644
index 0000000..bdc40ca
--- /dev/null
+++ b/mergers/list/tests/test_extend.py
@@ -0,0 +1,66 @@
+#
+# -*- coding: utf-8 -*-
+#
+# This file is part of reclass (http://github.com/madduck/reclass)
+#
+# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+from mergers.list import ListExtend
+
+class TestListExtend:
+
+ def setUp(self):
+ self.merger = ListExtend()
+
+ def _test_merge(self, one, two, target):
+ res = self.merger.merge(one, two)
+ print res, target
+ return res == target
+
+ def test_merge_scalars(self):
+ assert self._test_merge(1, 2, [1,2])
+
+ def test_merge_tuples(self):
+ t1 = (1,2,3)
+ t2 = (6,5,4)
+ target = [t1, t2]
+ assert self._test_merge(t1, t2, target)
+
+ def test_merge_lists(self):
+ l1 = [1,2,3]
+ l2 = [6,5,4]
+ target = l1 + l2
+ assert self._test_merge(l1, l2, target)
+
+ def test_merge_scalar_tuple(self):
+ s = 'one'
+ t = (2,3)
+ target = [s, t]
+ assert self._test_merge(s, t, target)
+
+ def test_merge_scalar_list(self):
+ s = 'foo'
+ l = [1,2,3]
+ target = [s]
+ target.extend(l)
+ assert self._test_merge(s, l, target)
+
+ def test_merge_list_scalar(self):
+ l = [1,2,3]
+ s = 'bar'
+ target = l[:]
+ target.append(s)
+ assert self._test_merge(l, s, target)
+
+ def test_merge_duplicates_scalar(self):
+ s1 = 2
+ s2 = 2
+ target = [2,2]
+ assert self._test_merge(s1, s2, target)
+
+ def test_merge_duplicates_list(self):
+ l1 = [1,2,3]
+ l2 = [3,2,1]
+ target = l1 + l2
+ assert self._test_merge(l1, l2, target)
diff --git a/mergers/list/tests/test_set.py b/mergers/list/tests/test_set.py
new file mode 100644
index 0000000..d3e8183
--- /dev/null
+++ b/mergers/list/tests/test_set.py
@@ -0,0 +1,27 @@
+#
+# -*- coding: utf-8 -*-
+#
+# This file is part of reclass (http://github.com/madduck/reclass)
+#
+# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+from test_extend import TestListExtend
+from mergers.list import SetExtend
+
+class TestSetExtend(TestListExtend):
+
+ def setUp(self):
+ self.merger = SetExtend()
+
+ def test_merge_duplicates_scalar(self):
+ s1 = 2
+ s2 = 2
+ target = [2]
+ assert self._test_merge(s1, s2, target)
+
+ def test_merge_duplicates_list(self):
+ l1 = [1,2,3]
+ l2 = [3,2,1]
+ target = l1
+ assert self._test_merge(l1, l2, target)