| @@ -183,11 +183,10 @@ class Template(Node): | |||
| def get(self, name): | |||
| """Get the parameter whose name is *name*. | |||
| The returned object is a | |||
| :py:class:`~.Parameter` instance. Raises :py:exc:`ValueError` if no | |||
| parameter has this name. Since multiple parameters can have the same | |||
| name, we'll return the last match, since the last parameter is the only | |||
| one read by the MediaWiki parser. | |||
| The returned object is a :py:class:`~.Parameter` instance. Raises | |||
| :py:exc:`ValueError` if no parameter has this name. Since multiple | |||
| parameters can have the same name, we'll return the last match, since | |||
| the last parameter is the only one read by the MediaWiki parser. | |||
| """ | |||
| name = name.strip() if isinstance(name, basestring) else str(name) | |||
| for param in reversed(self.params): | |||
| @@ -78,7 +78,7 @@ class TestHTMLEntity(TreeEqualityTestCase): | |||
| self.assertEqual(res, output) | |||
| def test_value(self): | |||
| """test HTMLEntity.value()""" | |||
| """test getter/setter for the value attribute""" | |||
| node1 = HTMLEntity("nbsp") | |||
| node2 = HTMLEntity("107") | |||
| node3 = HTMLEntity("e9") | |||
| @@ -110,7 +110,7 @@ class TestHTMLEntity(TreeEqualityTestCase): | |||
| self.assertRaises(ValueError, setattr, node1, "value", "1114112") | |||
| def test_named(self): | |||
| """test HTMLEntity.named()""" | |||
| """test getter/setter for the named attribute""" | |||
| node1 = HTMLEntity("nbsp") | |||
| node2 = HTMLEntity("107") | |||
| node3 = HTMLEntity("e9") | |||
| @@ -128,7 +128,7 @@ class TestHTMLEntity(TreeEqualityTestCase): | |||
| self.assertRaises(ValueError, setattr, node3, "named", True) | |||
| def test_hexadecimal(self): | |||
| """test HTMLEntity.hexadecimal()""" | |||
| """test getter/setter for the hexadecimal attribute""" | |||
| node1 = HTMLEntity("nbsp") | |||
| node2 = HTMLEntity("107") | |||
| node3 = HTMLEntity("e9") | |||
| @@ -144,7 +144,7 @@ class TestHTMLEntity(TreeEqualityTestCase): | |||
| self.assertRaises(ValueError, setattr, node1, "hexadecimal", True) | |||
| def test_hex_char(self): | |||
| """test HTMLEntity.hex_char()""" | |||
| """test getter/setter for the hex_char attribute""" | |||
| node1 = HTMLEntity("e9") | |||
| node2 = HTMLEntity("e9", hex_char="X") | |||
| self.assertEqual("x", node1.hex_char) | |||
| @@ -158,7 +158,7 @@ class TestHTMLEntity(TreeEqualityTestCase): | |||
| self.assertRaises(ValueError, setattr, node1, "hex_char", True) | |||
| def test_normalize(self): | |||
| """test HTMLEntity.normalize()""" | |||
| """test getter/setter for the normalize attribute""" | |||
| node1 = HTMLEntity("nbsp") | |||
| node2 = HTMLEntity("107") | |||
| node3 = HTMLEntity("e9") | |||
| @@ -0,0 +1,79 @@ | |||
| # -*- coding: utf-8 -*- | |||
| # | |||
| # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net> | |||
| # | |||
| # 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 | |||
| from mwparserfromhell.nodes.extras import Parameter | |||
| from mwparserfromhell.smart_list import SmartList | |||
| from mwparserfromhell.wikicode import Wikicode | |||
| from ._test_tree_equality import TreeEqualityTestCase | |||
| wrap = lambda L: Wikicode(SmartList(L)) | |||
| class TestParameter(TreeEqualityTestCase): | |||
| """Test cases for the Parameter node extra.""" | |||
| def test_unicode(self): | |||
| """test Parameter.__unicode__()""" | |||
| node = Parameter(wrap([Text("1")]), wrap([Text("foo")]), showkey=False) | |||
| self.assertEqual("foo", str(node)) | |||
| node2 = Parameter(wrap([Text("foo")]), wrap([Text("bar")])) | |||
| self.assertEqual("foo=bar", str(node2)) | |||
| def test_name(self): | |||
| """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")])) | |||
| self.assertIs(name1, node1.name) | |||
| self.assertIs(name2, 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_value(self): | |||
| """test getter/setter for the value attribute""" | |||
| value = wrap([Text("bar")]) | |||
| node = Parameter(wrap([Text("foo")]), value) | |||
| self.assertIs(value, node.value) | |||
| node.value = "héhehé" | |||
| self.assertWikicodeEqual(wrap([Text("héhehé")]), node.value) | |||
| def test_showkey(self): | |||
| """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")])) | |||
| self.assertFalse(node1.showkey) | |||
| self.assertTrue(node2.showkey) | |||
| node1.showkey = True | |||
| node2.showkey = "" | |||
| self.assertTrue(node1.showkey) | |||
| self.assertFalse(node2.showkey) | |||
| if __name__ == "__main__": | |||
| unittest.main(verbosity=2) | |||
| @@ -0,0 +1,140 @@ | |||
| # -*- coding: utf-8 -*- | |||
| # | |||
| # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net> | |||
| # | |||
| # 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 Template, Text | |||
| from mwparserfromhell.nodes.extras import Parameter | |||
| from mwparserfromhell.smart_list import SmartList | |||
| from mwparserfromhell.wikicode import Wikicode | |||
| from ._test_tree_equality import TreeEqualityTestCase | |||
| wrap = lambda L: Wikicode(SmartList(L)) | |||
| pgens = lambda k, v: Parameter(wrap([Text(k)]), wrap([Text(v)]), True) | |||
| pgenh = lambda k, v: Parameter(wrap([Text(k)]), wrap([Text(v)]), False) | |||
| class TestTemplate(TreeEqualityTestCase): | |||
| """Test cases for the Template node.""" | |||
| def test_unicode(self): | |||
| """test Template.__unicode__()""" | |||
| node = Template(wrap([Text("foobar")])) | |||
| self.assertEqual("{{foobar}}", str(node)) | |||
| node2 = Template(wrap([Text("foo")]), | |||
| [pgenh("1", "bar"), pgens("abc", "def")]) | |||
| self.assertEqual("{{foo|bar|abc=def}}", str(node2)) | |||
| def test_strip(self): | |||
| """test Template.__strip__()""" | |||
| node1 = Template(wrap([Text("foobar")])) | |||
| node2 = Template(wrap([Text("foo")]), | |||
| [pgenh("1", "bar"), pgens("abc", "def")]) | |||
| for a in (True, False): | |||
| for b in (True, False): | |||
| self.assertEqual(None, node1.__strip__(a, b)) | |||
| self.assertEqual(None, node2.__strip__(a, b)) | |||
| def test_showtree(self): | |||
| """test Template.__showtree__()""" | |||
| output = [] | |||
| getter, marker = object(), object() | |||
| get = lambda code: output.append((getter, code)) | |||
| mark = lambda: output.append(marker) | |||
| node1 = Template(wrap([Text("foobar")])) | |||
| node2 = Template(wrap([Text("foo")]), | |||
| [pgenh("1", "bar"), pgens("abc", "def")]) | |||
| node1.__showtree__(output.append, get, mark) | |||
| node2.__showtree__(output.append, get, mark) | |||
| valid = [ | |||
| "{{", (getter, node1.name), "}}", "{{", (getter, node2.name), | |||
| " | ", marker, (getter, node2.params[0].name), " = ", marker, | |||
| (getter, node2.params[0].value), " | ", marker, | |||
| (getter, node2.params[1].name), " = ", marker, | |||
| (getter, node2.params[1].value), "}}"] | |||
| self.assertEqual(valid, output) | |||
| def test_name(self): | |||
| """test getter/setter for the name attribute""" | |||
| name = wrap([Text("foobar")]) | |||
| node1 = Template(name) | |||
| node2 = Template(name, [pgenh("1", "bar")]) | |||
| self.assertIs(name, node1.name) | |||
| self.assertIs(name, node2.name) | |||
| node1.name = "asdf" | |||
| node2.name = "téstïng" | |||
| self.assertWikicodeEqual(wrap([Text("asdf")]), node1.name) | |||
| self.assertWikicodeEqual(wrap([Text("téstïng")]), node2.name) | |||
| def test_params(self): | |||
| """test getter for the params attribute""" | |||
| node1 = Template(wrap([Text("foobar")])) | |||
| plist = [pgenh("1", "bar"), pgens("abc", "def")] | |||
| node2 = Template(wrap([Text("foo")]), plist) | |||
| self.assertEqual([], node1.params) | |||
| self.assertIs(plist, node2.params) | |||
| def test_has_param(self): | |||
| """test Template.has_param()""" | |||
| node1 = Template(wrap([Text("foobar")])) | |||
| node2 = Template(wrap([Text("foo")]), | |||
| [pgenh("1", "bar"), pgens("abc", "def")]) | |||
| node3 = Template(wrap([Text("foo")]), | |||
| [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")]) | |||
| node4 = Template(wrap([Text("foo")]), | |||
| [pgenh("1", "a"), pgens("b", " ")]) | |||
| self.assertFalse(node1.has_param("foobar")) | |||
| self.assertTrue(node2.has_param(1)) | |||
| self.assertTrue(node2.has_param("abc")) | |||
| self.assertFalse(node2.has_param("def")) | |||
| self.assertTrue(node3.has_param("1")) | |||
| self.assertTrue(node3.has_param("b")) | |||
| self.assertFalse(node4.has_param("b")) | |||
| self.assertTrue(node3.has_param("b", False)) | |||
| self.assertTrue(node4.has_param("b", False)) | |||
| def test_get(self): | |||
| """test Template.get()""" | |||
| node1 = Template(wrap([Text("foobar")])) | |||
| node2p1 = pgenh("1", "bar") | |||
| node2p2 = pgens("abc", "def") | |||
| node2 = Template(wrap([Text("foo")]), [node2p1, node2p2]) | |||
| node3p1 = pgens("b", "c") | |||
| node3p2 = pgens("1", "d") | |||
| node3 = Template(wrap([Text("foo")]), | |||
| [pgenh("1", "a"), node3p1, node3p2]) | |||
| node4p1 = pgens("b", " ") | |||
| node4 = Template(wrap([Text("foo")]), [pgenh("1", "a"), node4p1]) | |||
| self.assertRaises(ValueError, node1.get, "foobar") | |||
| self.assertIs(node2p1, node2.get(1)) | |||
| self.assertIs(node2p2, node2.get("abc")) | |||
| self.assertRaises(ValueError, node2.get, "def") | |||
| self.assertIs(node3p1, node3.get("b")) | |||
| self.assertIs(node3p2, node3.get("1")) | |||
| self.assertIs(node4p1, node4.get("b")) | |||
| # add | |||
| # remove | |||
| if __name__ == "__main__": | |||
| unittest.main(verbosity=2) | |||