Пример #1
0
    def testDiff(self):
        """MultipleElement diff method"""

        elem = MultipleElement(SimpleElement(check='digit'))

        # (empty) <=> (empty)
        added, changed, removed = elem.diff(elem)
        self.assertTrue(len(added) == len(removed) == len(changed) == 0)

        elem.add(3)
        elem.add(9)
        elem.add(4)

        # itself
        added, changed, removed = elem.diff(elem)
        self.assertTrue(len(added) == len(removed) == len(changed) == 0)

        # elem <=> (empty)
        added, changed, removed = elem.diff(elem.emptycopy())
        self.assertTrue(len(added) == len(changed) == 0)
        self.assertEqual(removed.get(), [3, 9, 4])

        # (empty) <=> elem
        added, changed, removed = elem.emptycopy().diff(elem)
        self.assertTrue(len(removed) == len(changed) == 0)
        self.assertEqual(added.get(), [3, 9, 4])

        # Mixed
        other = elem.copy()
        other.remove(4)
        other.add(5)
        added, changed, removed = elem.diff(other)
        self.assertEqual(changed.get(), None)
        self.assertEqual(added.get(), [5])
        self.assertEqual(removed.get(), [4])
Пример #2
0
    def testDiff(self):
        """MultipleElement diff method"""

        elem = MultipleElement(SimpleElement(check='digit'))

        # (empty) <=> (empty)
        added, changed, removed = elem.diff(elem)
        self.assertTrue(len(added) == len(removed) == len(changed) == 0)

        elem.add(3)
        elem.add(9)
        elem.add(4)

        # itself
        added, changed, removed = elem.diff(elem)
        self.assertTrue(len(added) == len(removed) == len(changed) == 0)

        # elem <=> (empty)
        added, changed, removed = elem.diff(elem.emptycopy())
        self.assertTrue(len(added) == len(changed) == 0)
        self.assertEqual(removed.get(), [3, 9, 4])

        # (empty) <=> elem
        added, changed, removed = elem.emptycopy().diff(elem)
        self.assertTrue(len(removed) == len(changed) == 0)
        self.assertEqual(added.get(), [3, 9, 4])

        # Mixed
        other = elem.copy()
        other.remove(4)
        other.add(5)
        added, changed, removed = elem.diff(other)
        self.assertEqual(changed.get(), None)
        self.assertEqual(added.get(), [5])
        self.assertEqual(removed.get(), [4])
Пример #3
0
    def testBase(self):
        """MultipleElement base methods"""

        elem = MultipleElement(SimpleElement(check='string'))

        # Default
        self.assertEqual(elem.get([]), [])

        # First add()
        elem.add("3")
        self.assertEqual(elem.get(), ["3"])
        self.assertEqual(elem.content(), ["3"])
        self.assertEqual(elem[0], "3")
        self.assertEqual(str(elem), "3")
        self.assertEqual(len(elem), 1)
        # Second add()
        elem.add("5")
        elem.add("7")
        self.assertEqual(elem.get(), ["3", "5", "7"])
        self.assertEqual(elem.content(), ["3", "5", "7"])
        self.assertEqual(elem[1], "5")
        self.assertEqual(str(elem), "3\n5\n7")
        self.assertEqual(len(elem), 3)

        # Remove an item
        del elem[1]
        self.assertEqual(elem.get(), ["3", "7"])
        self.assertEqual(len(elem), 2)
        elem.remove("3")
        self.assertEqual(elem.get(), ["7"])
        self.assertEqual(len(elem), 1)
        self.assertRaises(KeyError, elem.remove, "6")

        # replace()
        elem.replace("12")
        self.assertEqual(elem.get(), ["12"])

        # clear()
        elem.clear()
        self.assertEqual(len(elem), 0)

        # parse()
        elem.parse("4")
        self.assertEqual(elem.get(), ["4"])
        self.assertEqual(str(elem), "4")
        self.assertEqual(len(elem), 1)

        # __eq__
        other = elem.emptycopy()
        self.assertNotEqual(elem, other)
        other.add("4")
        self.assertEqual(elem, other)
Пример #4
0
    def testBase(self):
        """MultipleElement base methods"""

        elem = MultipleElement(SimpleElement(check='string'))

        # Default
        self.assertEqual(elem.get([]), [])

        # First add()
        elem.add("3")
        self.assertEqual(elem.get(), ["3"])
        self.assertEqual(elem.content(), ["3"])
        self.assertEqual(elem[0], "3")
        self.assertEqual(str(elem), "3")
        self.assertEqual(len(elem), 1)
        # Second add()
        elem.add("5")
        elem.add("7")
        self.assertEqual(elem.get(), ["3", "5", "7"])
        self.assertEqual(elem.content(), ["3", "5", "7"])
        self.assertEqual(elem[1], "5")
        self.assertEqual(str(elem), "3 5 7")
        self.assertEqual(len(elem), 3)

        # Remove an item
        del elem[1]
        self.assertEqual(elem.get(), ["3", "7"])
        self.assertEqual(len(elem), 2)
        elem.remove("3")
        self.assertEqual(elem.get(), ["7"])
        self.assertEqual(len(elem), 1)
        self.assertRaises(KeyError, elem.remove, "6")

        # replace()
        elem.replace("12")
        self.assertEqual(elem.get(), ["12"])

        # clear()
        elem.clear()
        self.assertEqual(len(elem), 0)

        # parse()
        elem.parse("4")
        self.assertEqual(elem.get(), ["4"])
        self.assertEqual(str(elem), "4")
        self.assertEqual(len(elem), 1)

        # __eq__
        other = elem.emptycopy()
        self.assertNotEqual(elem, other)
        other.add("4")
        self.assertEqual(elem, other)
Пример #5
0
    def testCopy(self):
        """MultipleElement copy methods"""
        elem = MultipleElement(SimpleElement(check='digit'))

        # Empty
        self.assertEqual(len(elem.emptycopy()), 0)
        self.assertEqual(elem.copy().as_dict(), elem.as_dict())

        # With some content
        elem.add(3)
        elem.add(5)
        self.assertEqual(len(elem.emptycopy()), 0)
        self.assertEqual(elem.copy().as_dict(), elem.as_dict())
Пример #6
0
    def testCopy(self):
        """MultipleElement copy methods"""
        elem = MultipleElement(SimpleElement(check='digit'))

        # Empty
        self.assertEqual(len(elem.emptycopy()), 0)
        self.assertEqual(elem.copy().as_dict(), elem.as_dict())

        # With some content
        elem.add(3)
        elem.add(5)
        self.assertEqual(len(elem.emptycopy()), 0)
        self.assertEqual(elem.copy().as_dict(), elem.as_dict())
Пример #7
0
    def test_diff_key_simple_element(self):
        """MultipleElement diff method with simple elements with a specific key"""

        class MyElement(SimpleElement):
            def __init__(self, check='digit', default=None, value=None):
                SimpleElement.__init__(self, check, default, value)
            def key(self):
                return self.get() / 2

        elem = MultipleElement(MyElement())
        elem.add(7)
        elem.add(4)

        other = elem.copy()
        other.remove(7)
        other.add(6)

        added, changed, removed = elem.diff(other)
        self.assertEqual(added.get(), None)
        self.assertEqual(removed.get(), None)
        self.assertEqual(changed.get(), [6])
        self.assertEqual(changed.elements()[0].old.get(), 7)
Пример #8
0
    def test_diff_key_simple_element(self):
        """MultipleElement diff method with simple elements with a specific key"""
        class MyElement(SimpleElement):
            def __init__(self, check='digit', default=None, value=None):
                SimpleElement.__init__(self, check, default, value)

            def key(self):
                return self.get() / 2

        elem = MultipleElement(MyElement())
        elem.add(7)
        elem.add(4)

        other = elem.copy()
        other.remove(7)
        other.add(6)

        added, changed, removed = elem.diff(other)
        self.assertEqual(added.get(), None)
        self.assertEqual(removed.get(), None)
        self.assertEqual(changed.get(), [6])
        self.assertEqual(changed.elements()[0].old.get(), 7)
Пример #9
0
    def test_str_fold(self):
        """MultipleElement uses range-based folding when format to str"""
        elem = MultipleElement(SimpleElement(check='string'))

        # By default, no folding is done when cast to str
        elem.add('3')
        elem.add('4')
        elem.add('5')
        self.assertEqual(str(elem), "3\n4\n5")

        # With enabled folding
        elem.fold = True
        self.assertEqual(str(elem), '[3-5]')

        # Folding keeps order
        elem.clear()
        elem.add('3')
        elem.add('5')
        elem.add('4')
        self.assertEqual(str(elem), "[3,5]\n4")
Пример #10
0
    def test_str_fold(self):
        """MultipleElement uses range-based folding when format to str"""
        elem = MultipleElement(SimpleElement(check='string'))

        # By default, no folding is done when cast to str
        elem.add('3')
        elem.add('4')
        elem.add('5')
        self.assertEqual(str(elem), "3\n4\n5")

        # With enabled folding
        elem.fold = True
        self.assertEqual(str(elem), '[3-5]')

        # Folding keeps order
        elem.clear()
        elem.add('3')
        elem.add('5')
        elem.add('4')
        self.assertEqual(str(elem), "[3,5]\n4")