@@ -32,15 +32,6 @@ from mwparserfromhell.wikicode import Wikicode | |||||
wrap = lambda L: Wikicode(SmartList(L)) | wrap = lambda L: Wikicode(SmartList(L)) | ||||
wraptext = lambda *args: wrap([Text(t) for t in args]) | wraptext = lambda *args: wrap([Text(t) for t in args]) | ||||
def getnodes(code): | |||||
"""Iterate over all child nodes of a given parent node. | |||||
Imitates Wikicode._get_all_nodes(). | |||||
""" | |||||
for node in code.nodes: | |||||
for context, child in node.__iternodes__(getnodes): | |||||
yield child | |||||
class TreeEqualityTestCase(TestCase): | class TreeEqualityTestCase(TestCase): | ||||
"""A base test case with support for comparing the equality of node trees. | """A base test case with support for comparing the equality of node trees. | ||||
@@ -26,7 +26,7 @@ import unittest | |||||
from mwparserfromhell.compat import str | from mwparserfromhell.compat import str | ||||
from mwparserfromhell.nodes import Argument, Text | from mwparserfromhell.nodes import Argument, Text | ||||
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext | |||||
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext | |||||
class TestArgument(TreeEqualityTestCase): | class TestArgument(TreeEqualityTestCase): | ||||
"""Test cases for the Argument node.""" | """Test cases for the Argument node.""" | ||||
@@ -38,20 +38,15 @@ class TestArgument(TreeEqualityTestCase): | |||||
node2 = Argument(wraptext("foo"), wraptext("bar")) | node2 = Argument(wraptext("foo"), wraptext("bar")) | ||||
self.assertEqual("{{{foo|bar}}}", str(node2)) | self.assertEqual("{{{foo|bar}}}", str(node2)) | ||||
def test_iternodes(self): | |||||
"""test Argument.__iternodes__()""" | |||||
node1n1 = Text("foobar") | |||||
node2n1, node2n2, node2n3 = Text("foo"), Text("bar"), Text("baz") | |||||
node1 = Argument(wrap([node1n1])) | |||||
node2 = Argument(wrap([node2n1]), wrap([node2n2, node2n3])) | |||||
gen1 = node1.__iternodes__(getnodes) | |||||
gen2 = node2.__iternodes__(getnodes) | |||||
self.assertEqual((None, node1), next(gen1)) | |||||
self.assertEqual((None, node2), next(gen2)) | |||||
self.assertEqual((node1.name, node1n1), next(gen1)) | |||||
self.assertEqual((node2.name, node2n1), next(gen2)) | |||||
self.assertEqual((node2.default, node2n2), next(gen2)) | |||||
self.assertEqual((node2.default, node2n3), next(gen2)) | |||||
def test_children(self): | |||||
"""test Argument.__children__()""" | |||||
node1 = Argument(wraptext("foobar")) | |||||
node2 = Argument(wraptext("foo"), wrap([Text("bar"), Text("baz")])) | |||||
gen1 = node1.__children__() | |||||
gen2 = node2.__children__() | |||||
self.assertIs(node1.name, gen1) | |||||
self.assertIs(node2.name, gen2) | |||||
self.assertIs(node2.default, gen2) | |||||
self.assertRaises(StopIteration, next, gen1) | self.assertRaises(StopIteration, next, gen1) | ||||
self.assertRaises(StopIteration, next, gen2) | self.assertRaises(StopIteration, next, gen2) | ||||
@@ -36,11 +36,10 @@ class TestComment(TreeEqualityTestCase): | |||||
node = Comment("foobar") | node = Comment("foobar") | ||||
self.assertEqual("<!--foobar-->", str(node)) | self.assertEqual("<!--foobar-->", str(node)) | ||||
def test_iternodes(self): | |||||
"""test Comment.__iternodes__()""" | |||||
def test_children(self): | |||||
"""test Comment.__children__()""" | |||||
node = Comment("foobar") | node = Comment("foobar") | ||||
gen = node.__iternodes__(None) | |||||
self.assertEqual((None, node), next(gen)) | |||||
gen = node.__children__() | |||||
self.assertRaises(StopIteration, next, gen) | self.assertRaises(StopIteration, next, gen) | ||||
def test_strip(self): | def test_strip(self): | ||||
@@ -26,7 +26,7 @@ import unittest | |||||
from mwparserfromhell.compat import str | from mwparserfromhell.compat import str | ||||
from mwparserfromhell.nodes import ExternalLink, Text | from mwparserfromhell.nodes import ExternalLink, Text | ||||
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext | |||||
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext | |||||
class TestExternalLink(TreeEqualityTestCase): | class TestExternalLink(TreeEqualityTestCase): | ||||
"""Test cases for the ExternalLink node.""" | """Test cases for the ExternalLink node.""" | ||||
@@ -43,21 +43,16 @@ class TestExternalLink(TreeEqualityTestCase): | |||||
wraptext("Example Web Page")) | wraptext("Example Web Page")) | ||||
self.assertEqual("[http://example.com/ Example Web Page]", str(node4)) | self.assertEqual("[http://example.com/ Example Web Page]", str(node4)) | ||||
def test_iternodes(self): | |||||
"""test ExternalLink.__iternodes__()""" | |||||
node1n1 = Text("http://example.com/") | |||||
node2n1 = Text("http://example.com/") | |||||
node2n2, node2n3 = Text("Example"), Text("Page") | |||||
node1 = ExternalLink(wrap([node1n1]), brackets=False) | |||||
node2 = ExternalLink(wrap([node2n1]), wrap([node2n2, node2n3])) | |||||
gen1 = node1.__iternodes__(getnodes) | |||||
gen2 = node2.__iternodes__(getnodes) | |||||
self.assertEqual((None, node1), next(gen1)) | |||||
self.assertEqual((None, node2), next(gen2)) | |||||
self.assertEqual((node1.url, node1n1), next(gen1)) | |||||
self.assertEqual((node2.url, node2n1), next(gen2)) | |||||
self.assertEqual((node2.title, node2n2), next(gen2)) | |||||
self.assertEqual((node2.title, node2n3), next(gen2)) | |||||
def test_children(self): | |||||
"""test ExternalLink.__children__()""" | |||||
node1 = ExternalLink(wraptext("http://example.com/"), brackets=False) | |||||
node2 = ExternalLink(wraptext("http://example.com/"), | |||||
wrap([Text("Example"), Text("Page")])) | |||||
gen1 = node1.__children__() | |||||
gen2 = node2.__children__() | |||||
self.assertEqual(node1.url, next(gen1)) | |||||
self.assertEqual(node2.url, next(gen2)) | |||||
self.assertEqual(node2.title, next(gen2)) | |||||
self.assertRaises(StopIteration, next, gen1) | self.assertRaises(StopIteration, next, gen1) | ||||
self.assertRaises(StopIteration, next, gen2) | self.assertRaises(StopIteration, next, gen2) | ||||
@@ -26,7 +26,7 @@ import unittest | |||||
from mwparserfromhell.compat import str | from mwparserfromhell.compat import str | ||||
from mwparserfromhell.nodes import Heading, Text | from mwparserfromhell.nodes import Heading, Text | ||||
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext | |||||
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext | |||||
class TestHeading(TreeEqualityTestCase): | class TestHeading(TreeEqualityTestCase): | ||||
"""Test cases for the Heading node.""" | """Test cases for the Heading node.""" | ||||
@@ -38,14 +38,11 @@ class TestHeading(TreeEqualityTestCase): | |||||
node2 = Heading(wraptext(" zzz "), 5) | node2 = Heading(wraptext(" zzz "), 5) | ||||
self.assertEqual("===== zzz =====", str(node2)) | self.assertEqual("===== zzz =====", str(node2)) | ||||
def test_iternodes(self): | |||||
"""test Heading.__iternodes__()""" | |||||
text1, text2 = Text("foo"), Text("bar") | |||||
node = Heading(wrap([text1, text2]), 3) | |||||
gen = node.__iternodes__(getnodes) | |||||
self.assertEqual((None, node), next(gen)) | |||||
self.assertEqual((node.title, text1), next(gen)) | |||||
self.assertEqual((node.title, text2), next(gen)) | |||||
def test_children(self): | |||||
"""test Heading.__children__()""" | |||||
node = Heading(wrap([Text("foo"), Text("bar")]), 3) | |||||
gen = node.__children__() | |||||
self.assertEqual(node.title, next(gen)) | |||||
self.assertRaises(StopIteration, next, gen) | self.assertRaises(StopIteration, next, gen) | ||||
def test_strip(self): | def test_strip(self): | ||||
@@ -42,11 +42,10 @@ class TestHTMLEntity(TreeEqualityTestCase): | |||||
self.assertEqual("k", str(node3)) | self.assertEqual("k", str(node3)) | ||||
self.assertEqual("l", str(node4)) | self.assertEqual("l", str(node4)) | ||||
def test_iternodes(self): | |||||
"""test HTMLEntity.__iternodes__()""" | |||||
def test_children(self): | |||||
"""test HTMLEntity.__children__()""" | |||||
node = HTMLEntity("nbsp", named=True, hexadecimal=False) | node = HTMLEntity("nbsp", named=True, hexadecimal=False) | ||||
gen = node.__iternodes__(None) | |||||
self.assertEqual((None, node), next(gen)) | |||||
gen = node.__children__() | |||||
self.assertRaises(StopIteration, next, gen) | self.assertRaises(StopIteration, next, gen) | ||||
def test_strip(self): | def test_strip(self): | ||||
@@ -26,7 +26,7 @@ import unittest | |||||
from mwparserfromhell.compat import str | from mwparserfromhell.compat import str | ||||
from mwparserfromhell.nodes import Tag, Template, Text | from mwparserfromhell.nodes import Tag, Template, Text | ||||
from mwparserfromhell.nodes.extras import Attribute | from mwparserfromhell.nodes.extras import Attribute | ||||
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext | |||||
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext | |||||
agen = lambda name, value: Attribute(wraptext(name), wraptext(value)) | agen = lambda name, value: Attribute(wraptext(name), wraptext(value)) | ||||
agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), False) | agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), False) | ||||
@@ -64,37 +64,30 @@ class TestTag(TreeEqualityTestCase): | |||||
self.assertEqual("----", str(node8)) | self.assertEqual("----", str(node8)) | ||||
self.assertEqual("''italics!''", str(node9)) | self.assertEqual("''italics!''", str(node9)) | ||||
def test_iternodes(self): | |||||
"""test Tag.__iternodes__()""" | |||||
node1n1, node1n2 = Text("ref"), Text("foobar") | |||||
node2n1, node3n1, node3n2 = Text("bold text"), Text("img"), Text("id") | |||||
node3n3, node3n4, node3n5 = Text("foo"), Text("class"), Text("bar") | |||||
def test_children(self): | |||||
"""test Tag.__children__()""" | |||||
# <ref>foobar</ref> | # <ref>foobar</ref> | ||||
node1 = Tag(wrap([node1n1]), wrap([node1n2])) | |||||
node1 = Tag(wraptext("ref"), wraptext("foobar")) | |||||
# '''bold text''' | # '''bold text''' | ||||
node2 = Tag(wraptext("b"), wrap([node2n1]), wiki_markup="'''") | |||||
node2 = Tag(wraptext("b"), wraptext("bold text"), wiki_markup="'''") | |||||
# <img id="foo" class="bar" /> | # <img id="foo" class="bar" /> | ||||
node3 = Tag(wrap([node3n1]), | |||||
attrs=[Attribute(wrap([node3n2]), wrap([node3n3])), | |||||
Attribute(wrap([node3n4]), wrap([node3n5]))], | |||||
node3 = Tag(wraptext("img"), | |||||
attrs=[Attribute(wraptext("id"), wraptext("foo")), | |||||
Attribute(wraptext("class"), wraptext("bar"))], | |||||
self_closing=True, padding=" ") | self_closing=True, padding=" ") | ||||
gen1 = node1.__iternodes__(getnodes) | |||||
gen2 = node2.__iternodes__(getnodes) | |||||
gen3 = node3.__iternodes__(getnodes) | |||||
self.assertEqual((None, node1), next(gen1)) | |||||
self.assertEqual((None, node2), next(gen2)) | |||||
self.assertEqual((None, node3), next(gen3)) | |||||
self.assertEqual((node1.tag, node1n1), next(gen1)) | |||||
self.assertEqual((node3.tag, node3n1), next(gen3)) | |||||
self.assertEqual((node3.attributes[0].name, node3n2), next(gen3)) | |||||
self.assertEqual((node3.attributes[0].value, node3n3), next(gen3)) | |||||
self.assertEqual((node3.attributes[1].name, node3n4), next(gen3)) | |||||
self.assertEqual((node3.attributes[1].value, node3n5), next(gen3)) | |||||
self.assertEqual((node1.contents, node1n2), next(gen1)) | |||||
self.assertEqual((node2.contents, node2n1), next(gen2)) | |||||
self.assertEqual((node1.closing_tag, node1n1), next(gen1)) | |||||
gen1 = node1.__children__() | |||||
gen2 = node2.__children__() | |||||
gen3 = node3.__children__() | |||||
self.assertEqual(node1.tag, next(gen1)) | |||||
self.assertEqual(node3.tag, next(gen3)) | |||||
self.assertEqual(node3.attributes[0].name, next(gen3)) | |||||
self.assertEqual(node3.attributes[0].value, next(gen3)) | |||||
self.assertEqual(node3.attributes[1].name, next(gen3)) | |||||
self.assertEqual(node3.attributes[1].value, next(gen3)) | |||||
self.assertEqual(node1.contents, next(gen1)) | |||||
self.assertEqual(node2.contents, next(gen2)) | |||||
self.assertEqual(node1.closing_tag, next(gen1)) | |||||
self.assertRaises(StopIteration, next, gen1) | self.assertRaises(StopIteration, next, gen1) | ||||
self.assertRaises(StopIteration, next, gen2) | self.assertRaises(StopIteration, next, gen2) | ||||
self.assertRaises(StopIteration, next, gen3) | self.assertRaises(StopIteration, next, gen3) | ||||
@@ -26,7 +26,7 @@ import unittest | |||||
from mwparserfromhell.compat import str | from mwparserfromhell.compat import str | ||||
from mwparserfromhell.nodes import HTMLEntity, Template, Text | from mwparserfromhell.nodes import HTMLEntity, Template, Text | ||||
from mwparserfromhell.nodes.extras import Parameter | from mwparserfromhell.nodes.extras import Parameter | ||||
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext | |||||
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext | |||||
pgens = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=True) | pgens = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=True) | ||||
pgenh = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=False) | pgenh = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=False) | ||||
@@ -42,27 +42,21 @@ class TestTemplate(TreeEqualityTestCase): | |||||
[pgenh("1", "bar"), pgens("abc", "def")]) | [pgenh("1", "bar"), pgens("abc", "def")]) | ||||
self.assertEqual("{{foo|bar|abc=def}}", str(node2)) | self.assertEqual("{{foo|bar|abc=def}}", str(node2)) | ||||
def test_iternodes(self): | |||||
"""test Template.__iternodes__()""" | |||||
node1n1 = Text("foobar") | |||||
node2n1, node2n2, node2n3 = Text("foo"), Text("bar"), Text("abc") | |||||
node2n4, node2n5 = Text("def"), Text("ghi") | |||||
node2p1 = Parameter(wraptext("1"), wrap([node2n2]), showkey=False) | |||||
node2p2 = Parameter(wrap([node2n3]), wrap([node2n4, node2n5]), | |||||
def test_children(self): | |||||
"""test Template.__children__()""" | |||||
node2p1 = Parameter(wraptext("1"), wraptext("bar"), showkey=False) | |||||
node2p2 = Parameter(wraptext("abc"), wrap([Text("def"), Text("ghi")]), | |||||
showkey=True) | showkey=True) | ||||
node1 = Template(wrap([node1n1])) | |||||
node2 = Template(wrap([node2n1]), [node2p1, node2p2]) | |||||
node1 = Template(wraptext("foobar")) | |||||
node2 = Template(wraptext("foo"), [node2p1, node2p2]) | |||||
gen1 = node1.__iternodes__(getnodes) | |||||
gen2 = node2.__iternodes__(getnodes) | |||||
self.assertEqual((None, node1), next(gen1)) | |||||
self.assertEqual((None, node2), next(gen2)) | |||||
self.assertEqual((node1.name, node1n1), next(gen1)) | |||||
self.assertEqual((node2.name, node2n1), next(gen2)) | |||||
self.assertEqual((node2.params[0].value, node2n2), next(gen2)) | |||||
self.assertEqual((node2.params[1].name, node2n3), next(gen2)) | |||||
self.assertEqual((node2.params[1].value, node2n4), next(gen2)) | |||||
self.assertEqual((node2.params[1].value, node2n5), next(gen2)) | |||||
gen1 = node1.__children__() | |||||
gen2 = node2.__children__() | |||||
self.assertEqual(node1.name, next(gen1)) | |||||
self.assertEqual(node2.name, next(gen2)) | |||||
self.assertEqual(node2.params[0].value, next(gen2)) | |||||
self.assertEqual(node2.params[1].name, next(gen2)) | |||||
self.assertEqual(node2.params[1].value, next(gen2)) | |||||
self.assertRaises(StopIteration, next, gen1) | self.assertRaises(StopIteration, next, gen1) | ||||
self.assertRaises(StopIteration, next, gen2) | self.assertRaises(StopIteration, next, gen2) | ||||
@@ -36,11 +36,10 @@ class TestText(unittest.TestCase): | |||||
node2 = Text("fóóbar") | node2 = Text("fóóbar") | ||||
self.assertEqual("fóóbar", str(node2)) | self.assertEqual("fóóbar", str(node2)) | ||||
def test_iternodes(self): | |||||
"""test Text.__iternodes__()""" | |||||
def test_children(self): | |||||
"""test Text.__children__()""" | |||||
node = Text("foobar") | node = Text("foobar") | ||||
gen = node.__iternodes__(None) | |||||
self.assertEqual((None, node), next(gen)) | |||||
gen = node.__children__() | |||||
self.assertRaises(StopIteration, next, gen) | self.assertRaises(StopIteration, next, gen) | ||||
def test_strip(self): | def test_strip(self): | ||||
@@ -26,7 +26,7 @@ import unittest | |||||
from mwparserfromhell.compat import str | from mwparserfromhell.compat import str | ||||
from mwparserfromhell.nodes import Text, Wikilink | from mwparserfromhell.nodes import Text, Wikilink | ||||
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext | |||||
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext | |||||
class TestWikilink(TreeEqualityTestCase): | class TestWikilink(TreeEqualityTestCase): | ||||
"""Test cases for the Wikilink node.""" | """Test cases for the Wikilink node.""" | ||||
@@ -38,20 +38,15 @@ class TestWikilink(TreeEqualityTestCase): | |||||
node2 = Wikilink(wraptext("foo"), wraptext("bar")) | node2 = Wikilink(wraptext("foo"), wraptext("bar")) | ||||
self.assertEqual("[[foo|bar]]", str(node2)) | self.assertEqual("[[foo|bar]]", str(node2)) | ||||
def test_iternodes(self): | |||||
"""test Wikilink.__iternodes__()""" | |||||
node1n1 = Text("foobar") | |||||
node2n1, node2n2, node2n3 = Text("foo"), Text("bar"), Text("baz") | |||||
node1 = Wikilink(wrap([node1n1])) | |||||
node2 = Wikilink(wrap([node2n1]), wrap([node2n2, node2n3])) | |||||
gen1 = node1.__iternodes__(getnodes) | |||||
gen2 = node2.__iternodes__(getnodes) | |||||
self.assertEqual((None, node1), next(gen1)) | |||||
self.assertEqual((None, node2), next(gen2)) | |||||
self.assertEqual((node1.title, node1n1), next(gen1)) | |||||
self.assertEqual((node2.title, node2n1), next(gen2)) | |||||
self.assertEqual((node2.text, node2n2), next(gen2)) | |||||
self.assertEqual((node2.text, node2n3), next(gen2)) | |||||
def test_children(self): | |||||
"""test Wikilink.__children__()""" | |||||
node1 = Wikilink(wraptext("foobar")) | |||||
node2 = Wikilink(wraptext("foo"), wrap([Text("bar"), Text("baz")])) | |||||
gen1 = node1.__children__() | |||||
gen2 = node2.__children__() | |||||
self.assertEqual(node1.title, next(gen1)) | |||||
self.assertEqual(node2.title, next(gen2)) | |||||
self.assertEqual(node2.text, next(gen2)) | |||||
self.assertRaises(StopIteration, next, gen1) | self.assertRaises(StopIteration, next, gen1) | ||||
self.assertRaises(StopIteration, next, gen2) | self.assertRaises(StopIteration, next, gen2) | ||||