Browse Source

Condense usage of wrap([Text("foo")]) to just wraptext("foo").

tags/v0.2
Ben Kurtovic 11 years ago
parent
commit
3fe629f541
8 changed files with 114 additions and 129 deletions
  1. +1
    -1
      tests/_test_tree_equality.py
  2. +15
    -15
      tests/test_argument.py
  3. +40
    -45
      tests/test_builder.py
  4. +9
    -11
      tests/test_heading.py
  5. +14
    -14
      tests/test_parameter.py
  6. +9
    -12
      tests/test_parser.py
  7. +11
    -16
      tests/test_utils.py
  8. +15
    -15
      tests/test_wikilink.py

+ 1
- 1
tests/_test_tree_equality.py View File

@@ -30,7 +30,7 @@ from mwparserfromhell.smart_list import SmartList
from mwparserfromhell.wikicode import Wikicode from mwparserfromhell.wikicode import Wikicode


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


def getnodes(code): def getnodes(code):
"""Iterate over all child nodes of a given parent node. """Iterate over all child nodes of a given parent node.


+ 15
- 15
tests/test_argument.py View File

@@ -26,16 +26,16 @@ 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
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext


class TestArgument(TreeEqualityTestCase): class TestArgument(TreeEqualityTestCase):
"""Test cases for the Argument node.""" """Test cases for the Argument node."""


def test_unicode(self): def test_unicode(self):
"""test Argument.__unicode__()""" """test Argument.__unicode__()"""
node = Argument(wrap([Text("foobar")]))
node = Argument(wraptext("foobar"))
self.assertEqual("{{{foobar}}}", str(node)) self.assertEqual("{{{foobar}}}", str(node))
node2 = Argument(wrap([Text("foo")]), wrap([Text("bar")]))
node2 = Argument(wraptext("foo"), wraptext("bar"))
self.assertEqual("{{{foo|bar}}}", str(node2)) self.assertEqual("{{{foo|bar}}}", str(node2))


def test_iternodes(self): def test_iternodes(self):
@@ -57,8 +57,8 @@ class TestArgument(TreeEqualityTestCase):


def test_strip(self): def test_strip(self):
"""test Argument.__strip__()""" """test Argument.__strip__()"""
node = Argument(wrap([Text("foobar")]))
node2 = Argument(wrap([Text("foo")]), wrap([Text("bar")]))
node = Argument(wraptext("foobar"))
node2 = Argument(wraptext("foo"), wraptext("bar"))
for a in (True, False): for a in (True, False):
for b in (True, False): for b in (True, False):
self.assertIs(None, node.__strip__(a, b)) self.assertIs(None, node.__strip__(a, b))
@@ -70,8 +70,8 @@ class TestArgument(TreeEqualityTestCase):
getter, marker = object(), object() getter, marker = object(), object()
get = lambda code: output.append((getter, code)) get = lambda code: output.append((getter, code))
mark = lambda: output.append(marker) mark = lambda: output.append(marker)
node1 = Argument(wrap([Text("foobar")]))
node2 = Argument(wrap([Text("foo")]), wrap([Text("bar")]))
node1 = Argument(wraptext("foobar"))
node2 = Argument(wraptext("foo"), wraptext("bar"))
node1.__showtree__(output.append, get, mark) node1.__showtree__(output.append, get, mark)
node2.__showtree__(output.append, get, mark) node2.__showtree__(output.append, get, mark)
valid = [ valid = [
@@ -81,26 +81,26 @@ class TestArgument(TreeEqualityTestCase):


def test_name(self): def test_name(self):
"""test getter/setter for the name attribute""" """test getter/setter for the name attribute"""
name = wrap([Text("foobar")])
name = wraptext("foobar")
node1 = Argument(name) node1 = Argument(name)
node2 = Argument(name, wrap([Text("baz")]))
node2 = Argument(name, wraptext("baz"))
self.assertIs(name, node1.name) self.assertIs(name, node1.name)
self.assertIs(name, node2.name) self.assertIs(name, node2.name)
node1.name = "héhehé" node1.name = "héhehé"
node2.name = "héhehé" node2.name = "héhehé"
self.assertWikicodeEqual(wrap([Text("héhehé")]), node1.name)
self.assertWikicodeEqual(wrap([Text("héhehé")]), node2.name)
self.assertWikicodeEqual(wraptext("héhehé"), node1.name)
self.assertWikicodeEqual(wraptext("héhehé"), node2.name)


def test_default(self): def test_default(self):
"""test getter/setter for the default attribute""" """test getter/setter for the default attribute"""
default = wrap([Text("baz")])
node1 = Argument(wrap([Text("foobar")]))
node2 = Argument(wrap([Text("foobar")]), default)
default = wraptext("baz")
node1 = Argument(wraptext("foobar"))
node2 = Argument(wraptext("foobar"), default)
self.assertIs(None, node1.default) self.assertIs(None, node1.default)
self.assertIs(default, node2.default) self.assertIs(default, node2.default)
node1.default = "buzz" node1.default = "buzz"
node2.default = None node2.default = None
self.assertWikicodeEqual(wrap([Text("buzz")]), node1.default)
self.assertWikicodeEqual(wraptext("buzz"), node1.default)
self.assertIs(None, node2.default) self.assertIs(None, node2.default)


if __name__ == "__main__": if __name__ == "__main__":


+ 40
- 45
tests/test_builder.py View File

@@ -29,7 +29,7 @@ from mwparserfromhell.nodes.extras import Attribute, Parameter
from mwparserfromhell.parser import tokens from mwparserfromhell.parser import tokens
from mwparserfromhell.parser.builder import Builder from mwparserfromhell.parser.builder import Builder


from ._test_tree_equality import TreeEqualityTestCase, wrap
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext


class TestBuilder(TreeEqualityTestCase): class TestBuilder(TreeEqualityTestCase):
"""Tests for the builder, which turns tokens into Wikicode objects.""" """Tests for the builder, which turns tokens into Wikicode objects."""
@@ -40,10 +40,10 @@ class TestBuilder(TreeEqualityTestCase):
def test_text(self): def test_text(self):
"""tests for building Text nodes""" """tests for building Text nodes"""
tests = [ tests = [
([tokens.Text(text="foobar")], wrap([Text("foobar")])),
([tokens.Text(text="fóóbar")], wrap([Text("fóóbar")])),
([tokens.Text(text="foobar")], wraptext("foobar")),
([tokens.Text(text="fóóbar")], wraptext("fóóbar")),
([tokens.Text(text="spam"), tokens.Text(text="eggs")], ([tokens.Text(text="spam"), tokens.Text(text="eggs")],
wrap([Text("spam"), Text("eggs")])),
wraptext("spam", "eggs")),
] ]
for test, valid in tests: for test, valid in tests:
self.assertWikicodeEqual(valid, self.builder.build(test)) self.assertWikicodeEqual(valid, self.builder.build(test))
@@ -53,25 +53,24 @@ class TestBuilder(TreeEqualityTestCase):
tests = [ tests = [
([tokens.TemplateOpen(), tokens.Text(text="foobar"), ([tokens.TemplateOpen(), tokens.Text(text="foobar"),
tokens.TemplateClose()], tokens.TemplateClose()],
wrap([Template(wrap([Text("foobar")]))])),
wrap([Template(wraptext("foobar"))])),


([tokens.TemplateOpen(), tokens.Text(text="spam"), ([tokens.TemplateOpen(), tokens.Text(text="spam"),
tokens.Text(text="eggs"), tokens.TemplateClose()], tokens.Text(text="eggs"), tokens.TemplateClose()],
wrap([Template(wrap([Text("spam"), Text("eggs")]))])),
wrap([Template(wraptext("spam", "eggs"))])),


([tokens.TemplateOpen(), tokens.Text(text="foo"), ([tokens.TemplateOpen(), tokens.Text(text="foo"),
tokens.TemplateParamSeparator(), tokens.Text(text="bar"), tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
tokens.TemplateClose()], tokens.TemplateClose()],
wrap([Template(wrap([Text("foo")]), params=[
Parameter(wrap([Text("1")]), wrap([Text("bar")]),
showkey=False)])])),
wrap([Template(wraptext("foo"), params=[
Parameter(wraptext("1"), wraptext("bar"), showkey=False)])])),


([tokens.TemplateOpen(), tokens.Text(text="foo"), ([tokens.TemplateOpen(), tokens.Text(text="foo"),
tokens.TemplateParamSeparator(), tokens.Text(text="bar"), tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
tokens.TemplateParamEquals(), tokens.Text(text="baz"), tokens.TemplateParamEquals(), tokens.Text(text="baz"),
tokens.TemplateClose()], tokens.TemplateClose()],
wrap([Template(wrap([Text("foo")]), params=[
Parameter(wrap([Text("bar")]), wrap([Text("baz")]))])])),
wrap([Template(wraptext("foo"), params=[
Parameter(wraptext("bar"), wraptext("baz"))])])),


([tokens.TemplateOpen(), tokens.Text(text="foo"), ([tokens.TemplateOpen(), tokens.Text(text="foo"),
tokens.TemplateParamSeparator(), tokens.Text(text="bar"), tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
@@ -82,14 +81,12 @@ class TestBuilder(TreeEqualityTestCase):
tokens.TemplateParamEquals(), tokens.Text(text="buff"), tokens.TemplateParamEquals(), tokens.Text(text="buff"),
tokens.TemplateParamSeparator(), tokens.Text(text="baff"), tokens.TemplateParamSeparator(), tokens.Text(text="baff"),
tokens.TemplateClose()], tokens.TemplateClose()],
wrap([Template(wrap([Text("foo")]), params=[
Parameter(wrap([Text("bar")]), wrap([Text("baz")])),
Parameter(wrap([Text("1")]), wrap([Text("biz")]),
showkey=False),
Parameter(wrap([Text("2")]), wrap([Text("buzz")]),
showkey=False),
Parameter(wrap([Text("3")]), wrap([Text("buff")])),
Parameter(wrap([Text("3")]), wrap([Text("baff")]),
wrap([Template(wraptext("foo"), params=[
Parameter(wraptext("bar"), wraptext("baz")),
Parameter(wraptext("1"), wraptext("biz"), showkey=False),
Parameter(wraptext("2"), wraptext("buzz"), showkey=False),
Parameter(wraptext("3"), wraptext("buff")),
Parameter(wraptext("3"), wraptext("baff"),
showkey=False)])])), showkey=False)])])),
] ]
for test, valid in tests: for test, valid in tests:
@@ -100,23 +97,22 @@ class TestBuilder(TreeEqualityTestCase):
tests = [ tests = [
([tokens.ArgumentOpen(), tokens.Text(text="foobar"), ([tokens.ArgumentOpen(), tokens.Text(text="foobar"),
tokens.ArgumentClose()], tokens.ArgumentClose()],
wrap([Argument(wrap([Text("foobar")]))])),
wrap([Argument(wraptext("foobar"))])),


([tokens.ArgumentOpen(), tokens.Text(text="spam"), ([tokens.ArgumentOpen(), tokens.Text(text="spam"),
tokens.Text(text="eggs"), tokens.ArgumentClose()], tokens.Text(text="eggs"), tokens.ArgumentClose()],
wrap([Argument(wrap([Text("spam"), Text("eggs")]))])),
wrap([Argument(wraptext("spam", "eggs"))])),


([tokens.ArgumentOpen(), tokens.Text(text="foo"), ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
tokens.ArgumentSeparator(), tokens.Text(text="bar"), tokens.ArgumentSeparator(), tokens.Text(text="bar"),
tokens.ArgumentClose()], tokens.ArgumentClose()],
wrap([Argument(wrap([Text("foo")]), wrap([Text("bar")]))])),
wrap([Argument(wraptext("foo"), wraptext("bar"))])),


([tokens.ArgumentOpen(), tokens.Text(text="foo"), ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
tokens.Text(text="bar"), tokens.ArgumentSeparator(), tokens.Text(text="bar"), tokens.ArgumentSeparator(),
tokens.Text(text="baz"), tokens.Text(text="biz"), tokens.Text(text="baz"), tokens.Text(text="biz"),
tokens.ArgumentClose()], tokens.ArgumentClose()],
wrap([Argument(wrap([Text("foo"), Text("bar")]),
wrap([Text("baz"), Text("biz")]))])),
wrap([Argument(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
] ]
for test, valid in tests: for test, valid in tests:
self.assertWikicodeEqual(valid, self.builder.build(test)) self.assertWikicodeEqual(valid, self.builder.build(test))
@@ -126,23 +122,22 @@ class TestBuilder(TreeEqualityTestCase):
tests = [ tests = [
([tokens.WikilinkOpen(), tokens.Text(text="foobar"), ([tokens.WikilinkOpen(), tokens.Text(text="foobar"),
tokens.WikilinkClose()], tokens.WikilinkClose()],
wrap([Wikilink(wrap([Text("foobar")]))])),
wrap([Wikilink(wraptext("foobar"))])),


([tokens.WikilinkOpen(), tokens.Text(text="spam"), ([tokens.WikilinkOpen(), tokens.Text(text="spam"),
tokens.Text(text="eggs"), tokens.WikilinkClose()], tokens.Text(text="eggs"), tokens.WikilinkClose()],
wrap([Wikilink(wrap([Text("spam"), Text("eggs")]))])),
wrap([Wikilink(wraptext("spam", "eggs"))])),


([tokens.WikilinkOpen(), tokens.Text(text="foo"), ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
tokens.WikilinkSeparator(), tokens.Text(text="bar"), tokens.WikilinkSeparator(), tokens.Text(text="bar"),
tokens.WikilinkClose()], tokens.WikilinkClose()],
wrap([Wikilink(wrap([Text("foo")]), wrap([Text("bar")]))])),
wrap([Wikilink(wraptext("foo"), wraptext("bar"))])),


([tokens.WikilinkOpen(), tokens.Text(text="foo"), ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
tokens.Text(text="bar"), tokens.WikilinkSeparator(), tokens.Text(text="bar"), tokens.WikilinkSeparator(),
tokens.Text(text="baz"), tokens.Text(text="biz"), tokens.Text(text="baz"), tokens.Text(text="biz"),
tokens.WikilinkClose()], tokens.WikilinkClose()],
wrap([Wikilink(wrap([Text("foo"), Text("bar")]),
wrap([Text("baz"), Text("biz")]))])),
wrap([Wikilink(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
] ]
for test, valid in tests: for test, valid in tests:
self.assertWikicodeEqual(valid, self.builder.build(test)) self.assertWikicodeEqual(valid, self.builder.build(test))
@@ -172,11 +167,11 @@ class TestBuilder(TreeEqualityTestCase):
tests = [ tests = [
([tokens.HeadingStart(level=2), tokens.Text(text="foobar"), ([tokens.HeadingStart(level=2), tokens.Text(text="foobar"),
tokens.HeadingEnd()], tokens.HeadingEnd()],
wrap([Heading(wrap([Text("foobar")]), 2)])),
wrap([Heading(wraptext("foobar"), 2)])),


([tokens.HeadingStart(level=4), tokens.Text(text="spam"), ([tokens.HeadingStart(level=4), tokens.Text(text="spam"),
tokens.Text(text="eggs"), tokens.HeadingEnd()], tokens.Text(text="eggs"), tokens.HeadingEnd()],
wrap([Heading(wrap([Text("spam"), Text("eggs")]), 4)])),
wrap([Heading(wraptext("spam", "eggs"), 4)])),
] ]
for test, valid in tests: for test, valid in tests:
self.assertWikicodeEqual(valid, self.builder.build(test)) self.assertWikicodeEqual(valid, self.builder.build(test))
@@ -186,11 +181,11 @@ class TestBuilder(TreeEqualityTestCase):
tests = [ tests = [
([tokens.CommentStart(), tokens.Text(text="foobar"), ([tokens.CommentStart(), tokens.Text(text="foobar"),
tokens.CommentEnd()], tokens.CommentEnd()],
wrap([Comment(wrap([Text("foobar")]))])),
wrap([Comment(wraptext("foobar"))])),


([tokens.CommentStart(), tokens.Text(text="spam"), ([tokens.CommentStart(), tokens.Text(text="spam"),
tokens.Text(text="eggs"), tokens.CommentEnd()], tokens.Text(text="eggs"), tokens.CommentEnd()],
wrap([Comment(wrap([Text("spam"), Text("eggs")]))])),
wrap([Comment(wraptext("spam", "eggs"))])),
] ]
for test, valid in tests: for test, valid in tests:
self.assertWikicodeEqual(valid, self.builder.build(test)) self.assertWikicodeEqual(valid, self.builder.build(test))
@@ -214,10 +209,10 @@ class TestBuilder(TreeEqualityTestCase):
tokens.TemplateOpen(), tokens.Text(text="bin"), tokens.TemplateOpen(), tokens.Text(text="bin"),
tokens.TemplateClose(), tokens.TemplateClose()] tokens.TemplateClose(), tokens.TemplateClose()]
valid = wrap( valid = wrap(
[Template(wrap([Template(wrap([Template(wrap([Template(wrap([Text(
"foo")])), Text("bar")]), params=[Parameter(wrap([Text("baz")]),
wrap([Text("biz")]))]), Text("buzz")])), Text("usr")]), params=[
Parameter(wrap([Text("1")]), wrap([Template(wrap([Text("bin")]))]),
[Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
"foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
showkey=False)])]) showkey=False)])])
self.assertWikicodeEqual(valid, self.builder.build(test)) self.assertWikicodeEqual(valid, self.builder.build(test))


@@ -243,14 +238,14 @@ class TestBuilder(TreeEqualityTestCase):
tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(), tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
tokens.TemplateClose()] tokens.TemplateClose()]
valid = wrap( valid = wrap(
[Template(wrap([Text("a")]), params=[Parameter(wrap([Text("1")]),
wrap([Text("b")]), showkey=False), Parameter(wrap([Text("2")]),
wrap([Template(wrap([Text("c")]), params=[Parameter(wrap([Text("1")
]), wrap([Wikilink(wrap([Text("d")])), Argument(wrap([Text("e")]))]
), showkey=False)])]), showkey=False)]), Wikilink(wrap([Text("f")]
), wrap([Argument(wrap([Text("g")])), Comment(wrap([Text("h")]))])
), Template(wrap([Text("i")]), params=[Parameter(wrap([Text("j")]),
wrap([HTMLEntity("nbsp", named=True)]))])])
[Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
"b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
"g")), Comment(wraptext("h"))])), Template(wraptext("i"), params=[
Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
named=True)]))])])
self.assertWikicodeEqual(valid, self.builder.build(test)) self.assertWikicodeEqual(valid, self.builder.build(test))


if __name__ == "__main__": if __name__ == "__main__":


+ 9
- 11
tests/test_heading.py View File

@@ -26,16 +26,16 @@ 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
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext


class TestHeading(TreeEqualityTestCase): class TestHeading(TreeEqualityTestCase):
"""Test cases for the Heading node.""" """Test cases for the Heading node."""


def test_unicode(self): def test_unicode(self):
"""test Heading.__unicode__()""" """test Heading.__unicode__()"""
node = Heading(wrap([Text("foobar")]), 2)
node = Heading(wraptext("foobar"), 2)
self.assertEqual("==foobar==", str(node)) self.assertEqual("==foobar==", str(node))
node2 = Heading(wrap([Text(" zzz ")]), 5)
node2 = Heading(wraptext(" zzz "), 5)
self.assertEqual("===== zzz =====", str(node2)) self.assertEqual("===== zzz =====", str(node2))


def test_iternodes(self): def test_iternodes(self):
@@ -50,7 +50,7 @@ class TestHeading(TreeEqualityTestCase):


def test_strip(self): def test_strip(self):
"""test Heading.__strip__()""" """test Heading.__strip__()"""
node = Heading(wrap([Text("foobar")]), 3)
node = Heading(wraptext("foobar"), 3)
for a in (True, False): for a in (True, False):
for b in (True, False): for b in (True, False):
self.assertEqual("foobar", node.__strip__(a, b)) self.assertEqual("foobar", node.__strip__(a, b))
@@ -60,8 +60,8 @@ class TestHeading(TreeEqualityTestCase):
output = [] output = []
getter = object() getter = object()
get = lambda code: output.append((getter, code)) get = lambda code: output.append((getter, code))
node1 = Heading(wrap([Text("foobar")]), 3)
node2 = Heading(wrap([Text(" baz ")]), 4)
node1 = Heading(wraptext("foobar"), 3)
node2 = Heading(wraptext(" baz "), 4)
node1.__showtree__(output.append, get, None) node1.__showtree__(output.append, get, None)
node2.__showtree__(output.append, get, None) node2.__showtree__(output.append, get, None)
valid = ["===", (getter, node1.title), "===", valid = ["===", (getter, node1.title), "===",
@@ -70,20 +70,18 @@ class TestHeading(TreeEqualityTestCase):


def test_title(self): def test_title(self):
"""test getter/setter for the title attribute""" """test getter/setter for the title attribute"""
title = wrap([Text("foobar")])
title = wraptext("foobar")
node = Heading(title, 3) node = Heading(title, 3)
self.assertIs(title, node.title) self.assertIs(title, node.title)
node.title = "héhehé" node.title = "héhehé"
self.assertWikicodeEqual(wrap([Text("héhehé")]), node.title)
self.assertWikicodeEqual(wraptext("héhehé"), node.title)


def test_level(self): def test_level(self):
"""test getter/setter for the level attribute""" """test getter/setter for the level attribute"""
node = Heading(wrap([Text("foobar")]), 3)
node = Heading(wraptext("foobar"), 3)
self.assertEqual(3, node.level) self.assertEqual(3, node.level)
node.level = 5 node.level = 5
self.assertEqual(5, node.level) self.assertEqual(5, node.level)
node.level = True
self.assertEqual(1, node.level)
self.assertRaises(ValueError, setattr, node, "level", 0) self.assertRaises(ValueError, setattr, node, "level", 0)
self.assertRaises(ValueError, setattr, node, "level", 7) self.assertRaises(ValueError, setattr, node, "level", 7)
self.assertRaises(ValueError, setattr, node, "level", "abc") self.assertRaises(ValueError, setattr, node, "level", "abc")


+ 14
- 14
tests/test_parameter.py View File

@@ -27,43 +27,43 @@ from mwparserfromhell.compat import str
from mwparserfromhell.nodes import Text from mwparserfromhell.nodes import Text
from mwparserfromhell.nodes.extras import Parameter from mwparserfromhell.nodes.extras import Parameter


from ._test_tree_equality import TreeEqualityTestCase, wrap
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext


class TestParameter(TreeEqualityTestCase): class TestParameter(TreeEqualityTestCase):
"""Test cases for the Parameter node extra.""" """Test cases for the Parameter node extra."""


def test_unicode(self): def test_unicode(self):
"""test Parameter.__unicode__()""" """test Parameter.__unicode__()"""
node = Parameter(wrap([Text("1")]), wrap([Text("foo")]), showkey=False)
node = Parameter(wraptext("1"), wraptext("foo"), showkey=False)
self.assertEqual("foo", str(node)) self.assertEqual("foo", str(node))
node2 = Parameter(wrap([Text("foo")]), wrap([Text("bar")]))
node2 = Parameter(wraptext("foo"), wraptext("bar"))
self.assertEqual("foo=bar", str(node2)) self.assertEqual("foo=bar", str(node2))


def test_name(self): def test_name(self):
"""test getter/setter for the name attribute""" """test getter/setter for the name attribute"""
name1 = wrap([Text("1")])
name2 = wrap([Text("foobar")])
node1 = Parameter(name1, wrap([Text("foobar")]), showkey=False)
node2 = Parameter(name2, wrap([Text("baz")]))
name1 = wraptext("1")
name2 = wraptext("foobar")
node1 = Parameter(name1, wraptext("foobar"), showkey=False)
node2 = Parameter(name2, wraptext("baz"))
self.assertIs(name1, node1.name) self.assertIs(name1, node1.name)
self.assertIs(name2, node2.name) self.assertIs(name2, node2.name)
node1.name = "héhehé" node1.name = "héhehé"
node2.name = "héhehé" node2.name = "héhehé"
self.assertWikicodeEqual(wrap([Text("héhehé")]), node1.name)
self.assertWikicodeEqual(wrap([Text("héhehé")]), node2.name)
self.assertWikicodeEqual(wraptext("héhehé"), node1.name)
self.assertWikicodeEqual(wraptext("héhehé"), node2.name)


def test_value(self): def test_value(self):
"""test getter/setter for the value attribute""" """test getter/setter for the value attribute"""
value = wrap([Text("bar")])
node = Parameter(wrap([Text("foo")]), value)
value = wraptext("bar")
node = Parameter(wraptext("foo"), value)
self.assertIs(value, node.value) self.assertIs(value, node.value)
node.value = "héhehé" node.value = "héhehé"
self.assertWikicodeEqual(wrap([Text("héhehé")]), node.value)
self.assertWikicodeEqual(wraptext("héhehé"), node.value)


def test_showkey(self): def test_showkey(self):
"""test getter/setter for the showkey attribute""" """test getter/setter for the showkey attribute"""
node1 = Parameter(wrap([Text("1")]), wrap([Text("foo")]), showkey=False)
node2 = Parameter(wrap([Text("foo")]), wrap([Text("bar")]))
node1 = Parameter(wraptext("1"), wraptext("foo"), showkey=False)
node2 = Parameter(wraptext("foo"), wraptext("bar"))
self.assertFalse(node1.showkey) self.assertFalse(node1.showkey)
self.assertTrue(node2.showkey) self.assertTrue(node2.showkey)
node1.showkey = True node1.showkey = True


+ 9
- 12
tests/test_parser.py View File

@@ -26,10 +26,8 @@ import unittest
from mwparserfromhell import parser from mwparserfromhell import parser
from mwparserfromhell.nodes import Template, Text, Wikilink from mwparserfromhell.nodes import Template, Text, Wikilink
from mwparserfromhell.nodes.extras import Parameter from mwparserfromhell.nodes.extras import Parameter
from mwparserfromhell.smart_list import SmartList
from mwparserfromhell.wikicode import Wikicode


from ._test_tree_equality import TreeEqualityTestCase
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
from .compat import range from .compat import range


class TestParser(TreeEqualityTestCase): class TestParser(TreeEqualityTestCase):
@@ -45,18 +43,17 @@ class TestParser(TreeEqualityTestCase):
def test_parsing(self): def test_parsing(self):
"""integration test for parsing overall""" """integration test for parsing overall"""
text = "this is text; {{this|is=a|template={{with|[[links]]|in}}it}}" text = "this is text; {{this|is=a|template={{with|[[links]]|in}}it}}"
wrap = lambda L: Wikicode(SmartList(L))
expected = wrap([ expected = wrap([
Text("this is text; "), Text("this is text; "),
Template(wrap([Text("this")]), [
Parameter(wrap([Text("is")]), wrap([Text("a")])),
Parameter(wrap([Text("template")]), wrap([
Template(wrap([Text("with")]), [
Parameter(wrap([Text("1")]),
wrap([Wikilink(wrap([Text("links")]))]),
Template(wraptext("this"), [
Parameter(wraptext("is"), wraptext("a")),
Parameter(wraptext("template"), wrap([
Template(wraptext("with"), [
Parameter(wraptext("1"),
wrap([Wikilink(wraptext("links"))]),
showkey=False), showkey=False),
Parameter(wrap([Text("2")]),
wrap([Text("in")]), showkey=False)
Parameter(wraptext("2"),
wraptext("in"), showkey=False)
]), ]),
Text("it") Text("it")
])) ]))


+ 11
- 16
tests/test_utils.py View File

@@ -24,33 +24,28 @@ from __future__ import unicode_literals
import unittest import unittest


from mwparserfromhell.nodes import Template, Text from mwparserfromhell.nodes import Template, Text
from mwparserfromhell.smart_list import SmartList
from mwparserfromhell.utils import parse_anything from mwparserfromhell.utils import parse_anything
from mwparserfromhell.wikicode import Wikicode


from ._test_tree_equality import TreeEqualityTestCase
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext


class TestUtils(TreeEqualityTestCase): class TestUtils(TreeEqualityTestCase):
"""Tests for the utils module, which provides parse_anything().""" """Tests for the utils module, which provides parse_anything()."""


def test_parse_anything_valid(self): def test_parse_anything_valid(self):
"""tests for valid input to utils.parse_anything()""" """tests for valid input to utils.parse_anything()"""
wrap = lambda L: Wikicode(SmartList(L))
textify = lambda L: wrap([Text(item) for item in L])
tests = [ tests = [
(wrap([Text("foobar")]), textify(["foobar"])),
(Template(wrap([Text("spam")])),
wrap([Template(textify(["spam"]))])),
("fóóbar", textify(["fóóbar"])),
(b"foob\xc3\xa1r", textify(["foobár"])),
(123, textify(["123"])),
(True, textify(["True"])),
(wraptext("foobar"), wraptext("foobar")),
(Template(wraptext("spam")), wrap([Template(wraptext("spam"))])),
("fóóbar", wraptext("fóóbar")),
(b"foob\xc3\xa1r", wraptext("foobár")),
(123, wraptext("123")),
(True, wraptext("True")),
(None, wrap([])), (None, wrap([])),
([Text("foo"), Text("bar"), Text("baz")], ([Text("foo"), Text("bar"), Text("baz")],
textify(["foo", "bar", "baz"])),
([wrap([Text("foo")]), Text("bar"), "baz", 123, 456],
textify(["foo", "bar", "baz", "123", "456"])),
([[[([[((("foo",),),)], "bar"],)]]], textify(["foo", "bar"]))
wraptext("foo", "bar", "baz")),
([wraptext("foo"), Text("bar"), "baz", 123, 456],
wraptext("foo", "bar", "baz", "123", "456")),
([[[([[((("foo",),),)], "bar"],)]]], wraptext("foo", "bar"))
] ]
for test, valid in tests: for test, valid in tests:
self.assertWikicodeEqual(valid, parse_anything(test)) self.assertWikicodeEqual(valid, parse_anything(test))


+ 15
- 15
tests/test_wikilink.py View File

@@ -26,16 +26,16 @@ 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
from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext


class TestWikilink(TreeEqualityTestCase): class TestWikilink(TreeEqualityTestCase):
"""Test cases for the Wikilink node.""" """Test cases for the Wikilink node."""


def test_unicode(self): def test_unicode(self):
"""test Wikilink.__unicode__()""" """test Wikilink.__unicode__()"""
node = Wikilink(wrap([Text("foobar")]))
node = Wikilink(wraptext("foobar"))
self.assertEqual("[[foobar]]", str(node)) self.assertEqual("[[foobar]]", str(node))
node2 = Wikilink(wrap([Text("foo")]), wrap([Text("bar")]))
node2 = Wikilink(wraptext("foo"), wraptext("bar"))
self.assertEqual("[[foo|bar]]", str(node2)) self.assertEqual("[[foo|bar]]", str(node2))


def test_iternodes(self): def test_iternodes(self):
@@ -57,8 +57,8 @@ class TestWikilink(TreeEqualityTestCase):


def test_strip(self): def test_strip(self):
"""test Wikilink.__strip__()""" """test Wikilink.__strip__()"""
node = Wikilink(wrap([Text("foobar")]))
node2 = Wikilink(wrap([Text("foo")]), wrap([Text("bar")]))
node = Wikilink(wraptext("foobar"))
node2 = Wikilink(wraptext("foo"), wraptext("bar"))
for a in (True, False): for a in (True, False):
for b in (True, False): for b in (True, False):
self.assertEqual("foobar", node.__strip__(a, b)) self.assertEqual("foobar", node.__strip__(a, b))
@@ -70,8 +70,8 @@ class TestWikilink(TreeEqualityTestCase):
getter, marker = object(), object() getter, marker = object(), object()
get = lambda code: output.append((getter, code)) get = lambda code: output.append((getter, code))
mark = lambda: output.append(marker) mark = lambda: output.append(marker)
node1 = Wikilink(wrap([Text("foobar")]))
node2 = Wikilink(wrap([Text("foo")]), wrap([Text("bar")]))
node1 = Wikilink(wraptext("foobar"))
node2 = Wikilink(wraptext("foo"), wraptext("bar"))
node1.__showtree__(output.append, get, mark) node1.__showtree__(output.append, get, mark)
node2.__showtree__(output.append, get, mark) node2.__showtree__(output.append, get, mark)
valid = [ valid = [
@@ -81,26 +81,26 @@ class TestWikilink(TreeEqualityTestCase):


def test_title(self): def test_title(self):
"""test getter/setter for the title attribute""" """test getter/setter for the title attribute"""
title = wrap([Text("foobar")])
title = wraptext("foobar")
node1 = Wikilink(title) node1 = Wikilink(title)
node2 = Wikilink(title, wrap([Text("baz")]))
node2 = Wikilink(title, wraptext("baz"))
self.assertIs(title, node1.title) self.assertIs(title, node1.title)
self.assertIs(title, node2.title) self.assertIs(title, node2.title)
node1.title = "héhehé" node1.title = "héhehé"
node2.title = "héhehé" node2.title = "héhehé"
self.assertWikicodeEqual(wrap([Text("héhehé")]), node1.title)
self.assertWikicodeEqual(wrap([Text("héhehé")]), node2.title)
self.assertWikicodeEqual(wraptext("héhehé"), node1.title)
self.assertWikicodeEqual(wraptext("héhehé"), node2.title)


def test_text(self): def test_text(self):
"""test getter/setter for the text attribute""" """test getter/setter for the text attribute"""
text = wrap([Text("baz")])
node1 = Wikilink(wrap([Text("foobar")]))
node2 = Wikilink(wrap([Text("foobar")]), text)
text = wraptext("baz")
node1 = Wikilink(wraptext("foobar"))
node2 = Wikilink(wraptext("foobar"), text)
self.assertIs(None, node1.text) self.assertIs(None, node1.text)
self.assertIs(text, node2.text) self.assertIs(text, node2.text)
node1.text = "buzz" node1.text = "buzz"
node2.text = None node2.text = None
self.assertWikicodeEqual(wrap([Text("buzz")]), node1.text)
self.assertWikicodeEqual(wraptext("buzz"), node1.text)
self.assertIs(None, node2.text) self.assertIs(None, node2.text)


if __name__ == "__main__": if __name__ == "__main__":


Loading…
Cancel
Save