A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

230 lines
10 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. import unittest
  24. from mwparserfromhell.compat import str
  25. from mwparserfromhell.nodes import Tag, Template, Text
  26. from mwparserfromhell.nodes.extras import Attribute
  27. from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext
  28. agen = lambda name, value: Attribute(wraptext(name), wraptext(value))
  29. agennv = lambda name: Attribute(wraptext(name))
  30. agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), False)
  31. agenp = lambda name, value, a, b, c: Attribute(wraptext(name), wraptext(value),
  32. True, a, b, c)
  33. agenpnv = lambda name, a, b, c: Attribute(wraptext(name), None, True, a, b, c)
  34. class TestTag(TreeEqualityTestCase):
  35. """Test cases for the Tag node."""
  36. def test_unicode(self):
  37. """test Tag.__unicode__()"""
  38. node1 = Tag(wraptext("ref"))
  39. node2 = Tag(wraptext("span"), wraptext("foo"),
  40. [agen("style", "color: red;")])
  41. node3 = Tag(wraptext("ref"),
  42. attrs=[agennq("name", "foo"),
  43. agenpnv("some_attr", " ", "", "")],
  44. self_closing=True)
  45. node4 = Tag(wraptext("br"), self_closing=True, padding=" ")
  46. node5 = Tag(wraptext("br"), self_closing=True, implicit=True)
  47. node6 = Tag(wraptext("br"), self_closing=True, invalid=True,
  48. implicit=True)
  49. node7 = Tag(wraptext("br"), self_closing=True, invalid=True,
  50. padding=" ")
  51. node8 = Tag(wraptext("hr"), showtag=False, self_closing=True)
  52. node9 = Tag(wraptext("i"), wraptext("italics!"), showtag=False)
  53. self.assertEqual("<ref></ref>", str(node1))
  54. self.assertEqual('<span style="color: red;">foo</span>', str(node2))
  55. self.assertEqual("<ref name=foo some_attr/>", str(node3))
  56. self.assertEqual("<br />", str(node4))
  57. self.assertEqual("<br>", str(node5))
  58. self.assertEqual("</br>", str(node6))
  59. self.assertEqual("</br />", str(node7))
  60. self.assertEqual("----", str(node8))
  61. self.assertEqual("''italics!''", str(node9))
  62. def test_iternodes(self):
  63. """test Tag.__iternodes__()"""
  64. node1n1, node1n2 = Text("ref"), Text("foobar")
  65. node2n1, node3n1, node3n2 = Text("bold text"), Text("img"), Text("id")
  66. node3n3, node3n4, node3n5 = Text("foo"), Text("class"), Text("bar")
  67. # <ref>foobar</ref>
  68. node1 = Tag(wrap([node1n1]), wrap([node1n2]))
  69. # '''bold text'''
  70. node2 = Tag(wraptext("i"), wrap([node2n1]), showtag=False)
  71. # <img id="foo" class="bar" />
  72. node3 = Tag(wrap([node3n1]),
  73. attrs=[Attribute(wrap([node3n2]), wrap([node3n3])),
  74. Attribute(wrap([node3n4]), wrap([node3n5]))],
  75. self_closing=True, padding=" ")
  76. gen1 = node1.__iternodes__(getnodes)
  77. gen2 = node2.__iternodes__(getnodes)
  78. gen3 = node3.__iternodes__(getnodes)
  79. self.assertEqual((None, node1), next(gen1))
  80. self.assertEqual((None, node2), next(gen2))
  81. self.assertEqual((None, node3), next(gen3))
  82. self.assertEqual((node1.tag, node1n1), next(gen1))
  83. self.assertEqual((node3.tag, node3n1), next(gen3))
  84. self.assertEqual((node3.attributes[0].name, node3n2), next(gen3))
  85. self.assertEqual((node3.attributes[0].value, node3n3), next(gen3))
  86. self.assertEqual((node3.attributes[1].name, node3n4), next(gen3))
  87. self.assertEqual((node3.attributes[1].value, node3n5), next(gen3))
  88. self.assertEqual((node1.contents, node1n2), next(gen1))
  89. self.assertEqual((node2.contents, node2n1), next(gen2))
  90. self.assertEqual((node1.closing_tag, node1n1), next(gen1))
  91. self.assertRaises(StopIteration, next, gen1)
  92. self.assertRaises(StopIteration, next, gen2)
  93. self.assertRaises(StopIteration, next, gen3)
  94. def test_strip(self):
  95. """test Tag.__strip__()"""
  96. node1 = Tag(wraptext("i"), wraptext("foobar"))
  97. node2 = Tag(wraptext("math"), wraptext("foobar"))
  98. for a in (True, False):
  99. for b in (True, False):
  100. self.assertEqual("foobar", node1.__strip__(a, b))
  101. self.assertEqual(None, node2.__strip__(a, b))
  102. def test_showtree(self):
  103. """test Tag.__showtree__()"""
  104. output = []
  105. getter, marker = object(), object()
  106. get = lambda code: output.append((getter, code))
  107. mark = lambda: output.append(marker)
  108. node1 = Tag(wraptext("ref"), wraptext("text"), [agen("name", "foo")])
  109. node2 = Tag(wraptext("br"), self_closing=True, padding=" ")
  110. node3 = Tag(wraptext("br"), self_closing=True, invalid=True,
  111. implicit=True, padding=" ")
  112. node1.__showtree__(output.append, get, mark)
  113. node2.__showtree__(output.append, get, mark)
  114. node3.__showtree__(output.append, get, mark)
  115. valid = [
  116. "<", (getter, node1.tag), (getter, node1.attributes[0].name),
  117. " = ", marker, (getter, node1.attributes[0].value), ">",
  118. (getter, node1.contents), "</", (getter, node1.closing_tag), ">",
  119. "<", (getter, node2.tag), "/>", "</", (getter, node3.tag), ">"]
  120. self.assertEqual(valid, output)
  121. def test_tag(self):
  122. """test getter/setter for the tag attribute"""
  123. tag = wraptext("ref")
  124. node = Tag(tag, wraptext("text"))
  125. self.assertIs(tag, node.tag)
  126. self.assertIs(tag, node.closing_tag)
  127. node.tag = "span"
  128. self.assertWikicodeEqual(wraptext("span"), node.tag)
  129. self.assertWikicodeEqual(wraptext("span"), node.closing_tag)
  130. self.assertEqual("<span>text</span>", node)
  131. def test_contents(self):
  132. """test getter/setter for the contents attribute"""
  133. contents = wraptext("text")
  134. node = Tag(wraptext("ref"), contents)
  135. self.assertIs(contents, node.contents)
  136. node.contents = "text and a {{template}}"
  137. parsed = wrap([Text("text and a "), Template(wraptext("template"))])
  138. self.assertWikicodeEqual(parsed, node.contents)
  139. self.assertEqual("<ref>text and a {{template}}</ref>", node)
  140. def test_attributes(self):
  141. """test getter for the attributes attribute"""
  142. attrs = [agen("name", "bar")]
  143. node1 = Tag(wraptext("ref"), wraptext("foo"))
  144. node2 = Tag(wraptext("ref"), wraptext("foo"), attrs)
  145. self.assertEqual([], node1.attributes)
  146. self.assertIs(attrs, node2.attributes)
  147. def test_showtag(self):
  148. """test getter/setter for the showtag attribute"""
  149. node = Tag(wraptext("i"), wraptext("italic text"))
  150. self.assertTrue(node.showtag)
  151. node.showtag = False
  152. self.assertFalse(node.showtag)
  153. self.assertEqual("''italic text''", node)
  154. node.showtag = 1
  155. self.assertTrue(node.showtag)
  156. self.assertEqual("<i>italic text</i>", node)
  157. def test_self_closing(self):
  158. """test getter/setter for the self_closing attribute"""
  159. node = Tag(wraptext("ref"), wraptext("foobar"))
  160. self.assertFalse(node.self_closing)
  161. node.self_closing = True
  162. self.assertTrue(node.self_closing)
  163. self.assertEqual("<ref/>", node)
  164. node.self_closing = 0
  165. self.assertFalse(node.self_closing)
  166. self.assertEqual("<ref>foobar</ref>", node)
  167. def test_invalid(self):
  168. """test getter/setter for the invalid attribute"""
  169. node = Tag(wraptext("br"), self_closing=True, implicit=True)
  170. self.assertFalse(node.invalid)
  171. node.invalid = True
  172. self.assertTrue(node.invalid)
  173. self.assertEqual("</br>", node)
  174. node.invalid = 0
  175. self.assertFalse(node.invalid)
  176. self.assertEqual("<br>", node)
  177. def test_implicit(self):
  178. """test getter/setter for the implicit attribute"""
  179. node = Tag(wraptext("br"), self_closing=True)
  180. self.assertFalse(node.implicit)
  181. node.implicit = True
  182. self.assertTrue(node.implicit)
  183. self.assertEqual("<br>", node)
  184. node.implicit = 0
  185. self.assertFalse(node.implicit)
  186. self.assertEqual("<br/>", node)
  187. def test_padding(self):
  188. """test getter/setter for the padding attribute"""
  189. node = Tag(wraptext("ref"), wraptext("foobar"))
  190. self.assertEqual("", node.padding)
  191. node.padding = " "
  192. self.assertEqual(" ", node.padding)
  193. self.assertEqual("<ref >foobar</ref>", node)
  194. node.padding = None
  195. self.assertEqual("", node.padding)
  196. self.assertEqual("<ref>foobar</ref>", node)
  197. self.assertRaises(ValueError, setattr, node, "padding", True)
  198. def test_closing_tag(self):
  199. """test getter/setter for the closing_tag attribute"""
  200. tag = wraptext("ref")
  201. node = Tag(tag, wraptext("foobar"))
  202. self.assertIs(tag, node.closing_tag)
  203. node.closing_tag = "ref {{ignore me}}"
  204. parsed = wrap([Text("ref "), Template(wraptext("ignore me"))])
  205. self.assertWikicodeEqual(parsed, node.closing_tag)
  206. self.assertEqual("<ref>foobar</ref {{ignore me}}>", node)
  207. if __name__ == "__main__":
  208. unittest.main(verbosity=2)