From 5cf451eb22aa47b119183eb25de141627d0e1ef7 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 20 Apr 2013 18:01:39 -0400 Subject: [PATCH] Adding a bunch of tests for different nodes. --- tests/test_argument.py | 99 ++++++++++++++++++++++++++ tests/test_comment.py | 62 +++++++++++++++++ tests/test_heading.py | 88 ++++++++++++++++++++++++ tests/test_html_entity.py | 172 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_text.py | 69 +++++++++++++++++++ tests/test_wikilink.py | 99 ++++++++++++++++++++++++++ 6 files changed, 589 insertions(+) create mode 100644 tests/test_argument.py create mode 100644 tests/test_comment.py create mode 100644 tests/test_heading.py create mode 100644 tests/test_html_entity.py create mode 100644 tests/test_text.py create mode 100644 tests/test_wikilink.py diff --git a/tests/test_argument.py b/tests/test_argument.py new file mode 100644 index 0000000..e0524c4 --- /dev/null +++ b/tests/test_argument.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012-2013 Ben Kurtovic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import unicode_literals +import unittest + +from mwparserfromhell.compat import str +from mwparserfromhell.nodes import Argument, Text +from mwparserfromhell.smart_list import SmartList +from mwparserfromhell.wikicode import Wikicode + +from ._test_tree_equality import TreeEqualityTestCase + +wrap = lambda L: Wikicode(SmartList(L)) + +class TestArgument(TreeEqualityTestCase): + """Test cases for the Argument node.""" + + def test_unicode(self): + """test Argument.__unicode__()""" + node = Argument(wrap([Text("foobar")])) + self.assertEqual("{{{foobar}}}", str(node)) + node2 = Argument(wrap([Text("foo")]), wrap([Text("bar")])) + self.assertEqual("{{{foo|bar}}}", str(node2)) + + def test_strip(self): + """test Argument.__strip__()""" + node = Argument(wrap([Text("foobar")])) + self.assertIs(None, node.__strip__(True, True)) + self.assertIs(None, node.__strip__(True, False)) + self.assertIs(None, node.__strip__(False, True)) + self.assertIs(None, node.__strip__(False, False)) + + node2 = Argument(wrap([Text("foo")]), wrap([Text("bar")])) + self.assertEqual("bar", node2.__strip__(True, True)) + self.assertEqual("bar", node2.__strip__(True, False)) + self.assertEqual("bar", node2.__strip__(False, True)) + self.assertEqual("bar", node2.__strip__(False, False)) + + def test_showtree(self): + """test Argument.__showtree__()""" + output = [] + getter, marker = object(), object() + get = lambda code: output.append((getter, code)) + mark = lambda: output.append(marker) + node1 = Argument(wrap([Text("foobar")])) + node2 = Argument(wrap([Text("foo")]), wrap([Text("bar")])) + node1.__showtree__(output.append, get, mark) + node2.__showtree__(output.append, get, mark) + valid = [ + "{{{", (getter, node1.name), "}}}", "{{{", (getter, node2.name), + " | ", marker, (getter, node2.default), "}}}"] + self.assertEqual(valid, output) + + def test_name(self): + """test getter/setter for the name attribute""" + name = wrap([Text("foobar")]) + node1 = Argument(name) + node2 = Argument(name, wrap([Text("baz")])) + self.assertIs(name, node1.name) + self.assertIs(name, node2.name) + node1.name = "héhehé" + node2.name = "héhehé" + self.assertWikicodeEqual(wrap([Text("héhehé")]), node1.name) + self.assertWikicodeEqual(wrap([Text("héhehé")]), node2.name) + + def test_default(self): + """test getter/setter for the default attribute""" + default = wrap([Text("baz")]) + node1 = Argument(wrap([Text("foobar")])) + node2 = Argument(wrap([Text("foobar")]), default) + self.assertIs(None, node1.default) + self.assertIs(default, node2.default) + node1.default = "buzz" + node2.default = None + self.assertWikicodeEqual(wrap([Text("buzz")]), node1.default) + self.assertIs(None, node2.default) + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/tests/test_comment.py b/tests/test_comment.py new file mode 100644 index 0000000..980f594 --- /dev/null +++ b/tests/test_comment.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012-2013 Ben Kurtovic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import unicode_literals +import unittest + +from mwparserfromhell.compat import str +from mwparserfromhell.nodes import Comment + +from ._test_tree_equality import TreeEqualityTestCase + +class TestComment(TreeEqualityTestCase): + """Test cases for the Comment node.""" + + def test_unicode(self): + """test Comment.__unicode__()""" + node = Comment("foobar") + self.assertEqual("", str(node)) + + def test_strip(self): + """test Comment.__strip__()""" + node = Comment("foobar") + self.assertIs(None, node.__strip__(True, True)) + self.assertIs(None, node.__strip__(True, False)) + self.assertIs(None, node.__strip__(False, True)) + self.assertIs(None, node.__strip__(False, False)) + + def test_showtree(self): + """test Comment.__showtree__()""" + output = [] + node = Comment("foobar") + node.__showtree__(output.append, None, None) + self.assertEqual([""], output) + + def test_contents(self): + """test getter/setter for the contents attribute""" + node = Comment("foobar") + self.assertEqual("foobar", node.contents) + node.contents = "barfoo" + self.assertEqual("barfoo", node.contents) + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/tests/test_heading.py b/tests/test_heading.py new file mode 100644 index 0000000..a0e78e5 --- /dev/null +++ b/tests/test_heading.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012-2013 Ben Kurtovic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import unicode_literals +import unittest + +from mwparserfromhell.compat import str +from mwparserfromhell.nodes import Heading, Text +from mwparserfromhell.smart_list import SmartList +from mwparserfromhell.wikicode import Wikicode + +from ._test_tree_equality import TreeEqualityTestCase + +wrap = lambda L: Wikicode(SmartList(L)) + +class TestHeading(TreeEqualityTestCase): + """Test cases for the Heading node.""" + + def test_unicode(self): + """test Heading.__unicode__()""" + node = Heading(wrap([Text("foobar")]), 2) + self.assertEqual("==foobar==", str(node)) + node2 = Heading(wrap([Text(" zzz ")]), 5) + self.assertEqual("===== zzz =====", str(node2)) + + def test_strip(self): + """test Heading.__strip__()""" + node = Heading(wrap([Text("foobar")]), 3) + self.assertEqual("foobar", node.__strip__(True, True)) + self.assertEqual("foobar", node.__strip__(True, False)) + self.assertEqual("foobar", node.__strip__(False, True)) + self.assertEqual("foobar", node.__strip__(False, False)) + + def test_showtree(self): + """test Heading.__showtree__()""" + output = [] + getter = object() + get = lambda code: output.append((getter, code)) + node1 = Heading(wrap([Text("foobar")]), 3) + node2 = Heading(wrap([Text(" baz ")]), 4) + node1.__showtree__(output.append, get, None) + node2.__showtree__(output.append, get, None) + valid = ["===", (getter, node1.title), "===", + "====", (getter, node2.title), "===="] + self.assertEqual(valid, output) + + def test_title(self): + """test getter/setter for the title attribute""" + title = wrap([Text("foobar")]) + node = Heading(title, 3) + self.assertIs(title, node.title) + node.title = "héhehé" + self.assertWikicodeEqual(wrap([Text("héhehé")]), node.title) + + def test_level(self): + """test getter/setter for the level attribute""" + node = Heading(wrap([Text("foobar")]), 3) + self.assertEqual(3, node.level) + node.level = 5 + 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", 7) + self.assertRaises(ValueError, setattr, node, "level", "abc") + self.assertRaises(ValueError, setattr, node, "level", False) + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/tests/test_html_entity.py b/tests/test_html_entity.py new file mode 100644 index 0000000..20c8fc0 --- /dev/null +++ b/tests/test_html_entity.py @@ -0,0 +1,172 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012-2013 Ben Kurtovic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import unicode_literals +import unittest + +from mwparserfromhell.compat import str +from mwparserfromhell.nodes import HTMLEntity +from mwparserfromhell.smart_list import SmartList +from mwparserfromhell.wikicode import Wikicode + +from ._test_tree_equality import TreeEqualityTestCase + +wrap = lambda L: Wikicode(SmartList(L)) + +class TestHTMLEntity(TreeEqualityTestCase): + """Test cases for the HTMLEntity node.""" + + def test_unicode(self): + """test HTMLEntity.__unicode__()""" + node1 = HTMLEntity("nbsp", named=True, hexadecimal=False) + node2 = HTMLEntity("107", named=False, hexadecimal=False) + node3 = HTMLEntity("6b", named=False, hexadecimal=True) + node4 = HTMLEntity("6C", named=False, hexadecimal=True, hex_char="X") + self.assertEqual(" ", str(node1)) + self.assertEqual("k", str(node2)) + self.assertEqual("k", str(node3)) + self.assertEqual("l", str(node4)) + + def test_strip(self): + """test HTMLEntity.__strip__()""" + node1 = HTMLEntity("nbsp", named=True, hexadecimal=False) + node2 = HTMLEntity("107", named=False, hexadecimal=False) + node3 = HTMLEntity("e9", named=False, hexadecimal=True) + + self.assertEqual("\xa0", node1.__strip__(True, True)) + self.assertEqual("\xa0", node1.__strip__(True, False)) + self.assertEqual(" ", node1.__strip__(False, True)) + self.assertEqual(" ", node1.__strip__(False, False)) + self.assertEqual("k", node2.__strip__(True, True)) + self.assertEqual("k", node2.__strip__(True, False)) + self.assertEqual("k", node2.__strip__(False, True)) + self.assertEqual("k", node2.__strip__(False, False)) + self.assertEqual("é", node3.__strip__(True, True)) + self.assertEqual("é", node3.__strip__(True, False)) + self.assertEqual("é", node3.__strip__(False, True)) + self.assertEqual("é", node3.__strip__(False, False)) + + def test_showtree(self): + """test HTMLEntity.__showtree__()""" + output = [] + node1 = HTMLEntity("nbsp", named=True, hexadecimal=False) + node2 = HTMLEntity("107", named=False, hexadecimal=False) + node3 = HTMLEntity("e9", named=False, hexadecimal=True) + node1.__showtree__(output.append, None, None) + node2.__showtree__(output.append, None, None) + node3.__showtree__(output.append, None, None) + res = [" ", "k", "é"] + self.assertEqual(res, output) + + def test_value(self): + """test HTMLEntity.value()""" + node1 = HTMLEntity("nbsp") + node2 = HTMLEntity("107") + node3 = HTMLEntity("e9") + self.assertEquals("nbsp", node1.value) + self.assertEquals("107", node2.value) + self.assertEquals("e9", node3.value) + + node1.value = "ffa4" + node2.value = 72 + node3.value = "Sigma" + self.assertEquals("ffa4", node1.value) + self.assertFalse(node1.named) + self.assertTrue(node1.hexadecimal) + self.assertEquals("72", node2.value) + self.assertFalse(node2.named) + self.assertFalse(node2.hexadecimal) + self.assertEquals("Sigma", node3.value) + self.assertTrue(node3.named) + self.assertFalse(node3.hexadecimal) + + node1.value = "10FFFF" + node2.value = 110000 + node2.value = 1114111 + self.assertRaises(ValueError, setattr, node3, "value", "") + self.assertRaises(ValueError, setattr, node3, "value", "foobar") + self.assertRaises(ValueError, setattr, node3, "value", True) + self.assertRaises(ValueError, setattr, node3, "value", -1) + self.assertRaises(ValueError, setattr, node1, "value", 110000) + self.assertRaises(ValueError, setattr, node1, "value", "1114112") + + def test_named(self): + """test HTMLEntity.named()""" + node1 = HTMLEntity("nbsp") + node2 = HTMLEntity("107") + node3 = HTMLEntity("e9") + self.assertTrue(node1.named) + self.assertFalse(node2.named) + self.assertFalse(node3.named) + node1.named = 1 + node2.named = 0 + node3.named = 0 + self.assertTrue(node1.named) + self.assertFalse(node2.named) + self.assertFalse(node3.named) + self.assertRaises(ValueError, setattr, node1, "named", False) + self.assertRaises(ValueError, setattr, node2, "named", True) + self.assertRaises(ValueError, setattr, node3, "named", True) + + def test_hexadecimal(self): + """test HTMLEntity.hexadecimal()""" + node1 = HTMLEntity("nbsp") + node2 = HTMLEntity("107") + node3 = HTMLEntity("e9") + self.assertFalse(node1.hexadecimal) + self.assertFalse(node2.hexadecimal) + self.assertTrue(node3.hexadecimal) + node1.hexadecimal = False + node2.hexadecimal = True + node3.hexadecimal = False + self.assertFalse(node1.hexadecimal) + self.assertTrue(node2.hexadecimal) + self.assertFalse(node3.hexadecimal) + self.assertRaises(ValueError, setattr, node1, "hexadecimal", True) + + def test_hex_char(self): + """test HTMLEntity.hex_char()""" + node1 = HTMLEntity("e9") + node2 = HTMLEntity("e9", hex_char="X") + self.assertEquals("x", node1.hex_char) + self.assertEquals("X", node2.hex_char) + node1.hex_char = "X" + node2.hex_char = "x" + self.assertEquals("X", node1.hex_char) + self.assertEquals("x", node2.hex_char) + self.assertRaises(ValueError, setattr, node1, "hex_char", 123) + self.assertRaises(ValueError, setattr, node1, "hex_char", "foobar") + self.assertRaises(ValueError, setattr, node1, "hex_char", True) + + def test_normalize(self): + """test HTMLEntity.normalize()""" + node1 = HTMLEntity("nbsp") + node2 = HTMLEntity("107") + node3 = HTMLEntity("e9") + node4 = HTMLEntity("1f648") + self.assertEquals("\xa0", node1.normalize()) + self.assertEquals("k", node2.normalize()) + self.assertEquals("é", node3.normalize()) + self.assertEquals("\U0001F648", node4.normalize()) + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/tests/test_text.py b/tests/test_text.py new file mode 100644 index 0000000..13636bf --- /dev/null +++ b/tests/test_text.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012-2013 Ben Kurtovic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import unicode_literals +import unittest + +from mwparserfromhell.compat import str +from mwparserfromhell.nodes import Text + +class TestText(unittest.TestCase): + """Test cases for the Text node.""" + + def test_unicode(self): + """test Text.__unicode__()""" + node = Text("foobar") + self.assertEqual("foobar", str(node)) + node2 = Text("fóóbar") + self.assertEqual("fóóbar", str(node2)) + + def test_strip(self): + """test Text.__strip__()""" + node = Text("foobar") + self.assertIs(node, node.__strip__(True, True)) + self.assertIs(node, node.__strip__(True, False)) + self.assertIs(node, node.__strip__(False, True)) + self.assertIs(node, node.__strip__(False, False)) + + def test_showtree(self): + """test Text.__showtree__()""" + output = [] + node1 = Text("foobar") + node2 = Text("fóóbar") + node3 = Text("𐌲𐌿𐍄") + node1.__showtree__(output.append, None, None) + node2.__showtree__(output.append, None, None) + node3.__showtree__(output.append, None, None) + res = ["foobar", r"f\xf3\xf3bar", "\\U00010332\\U0001033f\\U00010344"] + self.assertEqual(res, output) + + def test_value(self): + """test getter/setter for the value attribute""" + node = Text("foobar") + self.assertEqual("foobar", node.value) + self.assertIsInstance(node.value, str) + node.value = "héhéhé" + self.assertEqual("héhéhé", node.value) + self.assertIsInstance(node.value, str) + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/tests/test_wikilink.py b/tests/test_wikilink.py new file mode 100644 index 0000000..422489f --- /dev/null +++ b/tests/test_wikilink.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012-2013 Ben Kurtovic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import unicode_literals +import unittest + +from mwparserfromhell.compat import str +from mwparserfromhell.nodes import Text, Wikilink +from mwparserfromhell.smart_list import SmartList +from mwparserfromhell.wikicode import Wikicode + +from ._test_tree_equality import TreeEqualityTestCase + +wrap = lambda L: Wikicode(SmartList(L)) + +class TestWikilink(TreeEqualityTestCase): + """Test cases for the Wikilink node.""" + + def test_unicode(self): + """test Wikilink.__unicode__()""" + node = Wikilink(wrap([Text("foobar")])) + self.assertEqual("[[foobar]]", str(node)) + node2 = Wikilink(wrap([Text("foo")]), wrap([Text("bar")])) + self.assertEqual("[[foo|bar]]", str(node2)) + + def test_strip(self): + """test Wikilink.__strip__()""" + node = Wikilink(wrap([Text("foobar")])) + self.assertEqual("foobar", node.__strip__(True, True)) + self.assertEqual("foobar", node.__strip__(True, False)) + self.assertEqual("foobar", node.__strip__(False, True)) + self.assertEqual("foobar", node.__strip__(False, False)) + + node2 = Wikilink(wrap([Text("foo")]), wrap([Text("bar")])) + self.assertEqual("bar", node2.__strip__(True, True)) + self.assertEqual("bar", node2.__strip__(True, False)) + self.assertEqual("bar", node2.__strip__(False, True)) + self.assertEqual("bar", node2.__strip__(False, False)) + + def test_showtree(self): + """test Wikilink.__showtree__()""" + output = [] + getter, marker = object(), object() + get = lambda code: output.append((getter, code)) + mark = lambda: output.append(marker) + node1 = Wikilink(wrap([Text("foobar")])) + node2 = Wikilink(wrap([Text("foo")]), wrap([Text("bar")])) + node1.__showtree__(output.append, get, mark) + node2.__showtree__(output.append, get, mark) + valid = [ + "[[", (getter, node1.title), "]]", "[[", (getter, node2.title), + " | ", marker, (getter, node2.text), "]]"] + self.assertEqual(valid, output) + + def test_title(self): + """test getter/setter for the title attribute""" + title = wrap([Text("foobar")]) + node1 = Wikilink(title) + node2 = Wikilink(title, wrap([Text("baz")])) + self.assertIs(title, node1.title) + self.assertIs(title, node2.title) + node1.title = "héhehé" + node2.title = "héhehé" + self.assertWikicodeEqual(wrap([Text("héhehé")]), node1.title) + self.assertWikicodeEqual(wrap([Text("héhehé")]), node2.title) + + def test_text(self): + """test getter/setter for the text attribute""" + text = wrap([Text("baz")]) + node1 = Wikilink(wrap([Text("foobar")])) + node2 = Wikilink(wrap([Text("foobar")]), text) + self.assertIs(None, node1.text) + self.assertIs(text, node2.text) + node1.text = "buzz" + node2.text = None + self.assertWikicodeEqual(wrap([Text("buzz")]), node1.text) + self.assertIs(None, node2.text) + +if __name__ == "__main__": + unittest.main(verbosity=2)