Ejemplo n.º 1
0
 def test_child_methods(self):
     """make sure _ListProxy's non-magic methods work, like append()"""
     self._test_list_methods(lambda L: SmartList(list(L))[:])
     self._test_list_methods(lambda L: SmartList([999] + list(L))[1:])
     self._test_list_methods(lambda L: SmartList(list(L) + [999])[:-1])
     builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
     self._test_list_methods(builder)
Ejemplo n.º 2
0
 def test_child_add(self):
     """make sure _ListProxy's add/radd/iadd work"""
     self._test_add_radd_iadd(lambda L: SmartList(list(L))[:])
     self._test_add_radd_iadd(lambda L: SmartList([999] + list(L))[1:])
     self._test_add_radd_iadd(lambda L: SmartList(list(L) + [999])[:-1])
     builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
     self._test_add_radd_iadd(builder)
Ejemplo n.º 3
0
 def test_child_get_set_del(self):
     """make sure _ListProxy's getitem/setitem/delitem work"""
     self._test_get_set_del_item(lambda L: SmartList(list(L))[:])
     self._test_get_set_del_item(lambda L: SmartList([999] + list(L))[1:])
     self._test_get_set_del_item(lambda L: SmartList(list(L) + [999])[:-1])
     builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
     self._test_get_set_del_item(builder)
Ejemplo n.º 4
0
 def test_child_other_magics(self):
     """make sure _ListProxy's other magically implemented features work"""
     self._test_other_magic_methods(lambda L: SmartList(list(L))[:])
     self._test_other_magic_methods(
         lambda L: SmartList([999] + list(L))[1:])
     self._test_other_magic_methods(
         lambda L: SmartList(list(L) + [999])[:-1])
     builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
     self._test_other_magic_methods(builder)
 def test_doctest(self):
     """make sure the test embedded in SmartList's docstring passes"""
     parent = SmartList([0, 1, 2, 3])
     self.assertEqual([0, 1, 2, 3], parent)
     child = parent[2:]
     self.assertEqual([2, 3], child)
     child.append(4)
     self.assertEqual([2, 3, 4], child)
     self.assertEqual([0, 1, 2, 3, 4], parent)
Ejemplo n.º 6
0
 def test_nodes(self):
     """test getter/setter for the nodes attribute"""
     code = parse("Have a {{template}}")
     self.assertEqual(["Have a ", "{{template}}"], code.nodes)
     L1 = SmartList([Text("foobar"), Template(wraptext("abc"))])
     L2 = [Text("barfoo"), Template(wraptext("cba"))]
     L3 = "abc{{def}}"
     code.nodes = L1
     self.assertIs(L1, code.nodes)
     code.nodes = L2
     self.assertIs(L2, code.nodes)
     code.nodes = L3
     self.assertEqual(["abc", "{{def}}"], code.nodes)
     self.assertRaises(ValueError, setattr, code, "nodes", object)
    def test_influence(self):
        """make sure changes are propagated from parents to children"""
        parent = SmartList([0, 1, 2, 3, 4, 5])
        child1 = parent[2:]
        child2 = parent[2:5]
        self.assertEqual([0, 1, 2, 3, 4, 5], parent)
        self.assertEqual([2, 3, 4, 5], child1)
        self.assertEqual([2, 3, 4], child2)
        self.assertEqual(2, len(parent._children))

        parent.append(6)
        child1.append(7)
        child2.append(4.5)
        self.assertEqual([0, 1, 2, 3, 4, 4.5, 5, 6, 7], parent)
        self.assertEqual([2, 3, 4, 4.5, 5, 6, 7], child1)
        self.assertEqual([2, 3, 4, 4.5], child2)

        parent.insert(0, -1)
        parent.insert(4, 2.5)
        parent.insert(10, 6.5)
        self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], parent)
        self.assertEqual([2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], child1)
        self.assertEqual([2, 2.5, 3, 4, 4.5], child2)

        self.assertEqual(7, parent.pop())
        self.assertEqual(6.5, child1.pop())
        self.assertEqual(4.5, child2.pop())
        self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 5, 6], parent)
        self.assertEqual([2, 2.5, 3, 4, 5, 6], child1)
        self.assertEqual([2, 2.5, 3, 4], child2)

        parent.remove(-1)
        child1.remove(2.5)
        self.assertEqual([0, 1, 2, 3, 4, 5, 6], parent)
        self.assertEqual([2, 3, 4, 5, 6], child1)
        self.assertEqual([2, 3, 4], child2)

        self.assertEqual(0, parent.pop(0))
        self.assertEqual([1, 2, 3, 4, 5, 6], parent)
        self.assertEqual([2, 3, 4, 5, 6], child1)
        self.assertEqual([2, 3, 4], child2)

        child2.reverse()
        self.assertEqual([1, 4, 3, 2, 5, 6], parent)
        self.assertEqual([4, 3, 2, 5, 6], child1)
        self.assertEqual([4, 3, 2], child2)

        parent.extend([7, 8])
        child1.extend([8.1, 8.2])
        child2.extend([1.9, 1.8])
        self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], parent)
        self.assertEqual([4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], child1)
        self.assertEqual([4, 3, 2, 1.9, 1.8], child2)

        child3 = parent[9:]
        self.assertEqual([8, 8.1, 8.2], child3)

        del parent[8:]
        self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6], parent)
        self.assertEqual([4, 3, 2, 1.9, 1.8, 5, 6], child1)
        self.assertEqual([4, 3, 2, 1.9, 1.8], child2)
        self.assertEqual([], child3)

        del child1
        self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6], parent)
        self.assertEqual([4, 3, 2, 1.9, 1.8], child2)
        self.assertEqual([], child3)
        self.assertEqual(2, len(parent._children))

        del child3
        self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6], parent)
        self.assertEqual([4, 3, 2, 1.9, 1.8], child2)
        self.assertEqual(1, len(parent._children))

        parent.remove(1.9)
        parent.remove(1.8)
        self.assertEqual([1, 4, 3, 2, 5, 6], parent)
        self.assertEqual([4, 3, 2], child2)

        parent.reverse()
        self.assertEqual([6, 5, 2, 3, 4, 1], parent)
        self.assertEqual([4, 3, 2], child2)
        self.assertEqual(0, len(parent._children))
 def _dispatch_test_for_children(self, meth):
     """Run a test method on various different types of children."""
     meth(lambda L: SmartList(list(L))[:])
     meth(lambda L: SmartList([999] + list(L))[1:])
     meth(lambda L: SmartList(list(L) + [999])[:-1])
     meth(lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2])
from __future__ import unicode_literals

try:
    from unittest2 import TestCase
except ImportError:
    from unittest import TestCase

from mwparserfromhell.compat import range
from mwparserfromhell.nodes import (Argument, Comment, Heading, HTMLEntity,
                                    Tag, Template, Text, Wikilink)
from mwparserfromhell.nodes.extras import Attribute, Parameter
from mwparserfromhell.smart_list import SmartList
from mwparserfromhell.wikicode import Wikicode

wrap = lambda L: Wikicode(SmartList(L))
wraptext = lambda *args: wrap([Text(t) for t in args])

class TreeEqualityTestCase(TestCase):
    """A base test case with support for comparing the equality of node trees.

    This adds a number of type equality functions, for Wikicode, Text,
    Templates, and Wikilinks.
    """

    def assertNodeEqual(self, expected, actual):
        """Assert that two Nodes have the same type and have the same data."""
        registry = {
            Argument: self.assertArgumentNodeEqual,
            Comment: self.assertCommentNodeEqual,
            Heading: self.assertHeadingNodeEqual,
Ejemplo n.º 10
0
    def test_influence(self):
        """make sure changes are propagated from parents to children"""
        parent = SmartList([0, 1, 2, 3, 4, 5])
        child1 = parent[2:]
        child2 = parent[2:5]

        parent.append(6)
        child1.append(7)
        child2.append(4.5)
        self.assertEqual([0, 1, 2, 3, 4, 4.5, 5, 6, 7], parent)
        self.assertEqual([2, 3, 4, 4.5, 5, 6, 7], child1)
        self.assertEqual([2, 3, 4, 4.5], child2)

        parent.insert(0, -1)
        parent.insert(4, 2.5)
        parent.insert(10, 6.5)
        self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], parent)
        self.assertEqual([2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], child1)
        self.assertEqual([2, 2.5, 3, 4, 4.5], child2)

        self.assertEqual(7, parent.pop())
        self.assertEqual(6.5, child1.pop())
        self.assertEqual(4.5, child2.pop())
        self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 5, 6], parent)
        self.assertEqual([2, 2.5, 3, 4, 5, 6], child1)
        self.assertEqual([2, 2.5, 3, 4], child2)

        parent.remove(-1)
        child1.remove(2.5)
        self.assertEqual([0, 1, 2, 3, 4, 5, 6], parent)
        self.assertEqual([2, 3, 4, 5, 6], child1)
        self.assertEqual([2, 3, 4], child2)

        self.assertEqual(0, parent.pop(0))
        self.assertEqual([1, 2, 3, 4, 5, 6], parent)
        self.assertEqual([2, 3, 4, 5, 6], child1)
        self.assertEqual([2, 3, 4], child2)

        child2.reverse()
        self.assertEqual([1, 4, 3, 2, 5, 6], parent)
        self.assertEqual([4, 3, 2, 5, 6], child1)
        self.assertEqual([4, 3, 2], child2)

        parent.extend([7, 8])
        child1.extend([8.1, 8.2])
        child2.extend([1.9, 1.8])
        self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], parent)
        self.assertEqual([4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], child1)
        self.assertEqual([4, 3, 2, 1.9, 1.8], child2)