A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

test_tag.py 10 KiB

11 yıl önce
11 yıl önce
11 yıl önce
11 yıl önce
11 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), False)
  30. agenpnv = lambda name, a, b, c: Attribute(wraptext(name), None, True, a, b, c)
  31. class TestTag(TreeEqualityTestCase):
  32. """Test cases for the Tag node."""
  33. def test_unicode(self):
  34. """test Tag.__unicode__()"""
  35. node1 = Tag(wraptext("ref"))
  36. node2 = Tag(wraptext("span"), wraptext("foo"),
  37. [agen("style", "color: red;")])
  38. node3 = Tag(wraptext("ref"),
  39. attrs=[agennq("name", "foo"),
  40. agenpnv("some_attr", " ", "", "")],
  41. self_closing=True)
  42. node4 = Tag(wraptext("br"), self_closing=True, padding=" ")
  43. node5 = Tag(wraptext("br"), self_closing=True, implicit=True)
  44. node6 = Tag(wraptext("br"), self_closing=True, invalid=True,
  45. implicit=True)
  46. node7 = Tag(wraptext("br"), self_closing=True, invalid=True,
  47. padding=" ")
  48. node8 = Tag(wraptext("hr"), wiki_markup="----", self_closing=True)
  49. node9 = Tag(wraptext("i"), wraptext("italics!"), wiki_markup="''")
  50. self.assertEqual("<ref></ref>", str(node1))
  51. self.assertEqual('<span style="color: red;">foo</span>', str(node2))
  52. self.assertEqual("<ref name=foo some_attr/>", str(node3))
  53. self.assertEqual("<br />", str(node4))
  54. self.assertEqual("<br>", str(node5))
  55. self.assertEqual("</br>", str(node6))
  56. self.assertEqual("</br />", str(node7))
  57. self.assertEqual("----", str(node8))
  58. self.assertEqual("''italics!''", str(node9))
  59. def test_iternodes(self):
  60. """test Tag.__iternodes__()"""
  61. node1n1, node1n2 = Text("ref"), Text("foobar")
  62. node2n1, node3n1, node3n2 = Text("bold text"), Text("img"), Text("id")
  63. node3n3, node3n4, node3n5 = Text("foo"), Text("class"), Text("bar")
  64. # <ref>foobar</ref>
  65. node1 = Tag(wrap([node1n1]), wrap([node1n2]))
  66. # '''bold text'''
  67. node2 = Tag(wraptext("b"), wrap([node2n1]), wiki_markup="'''")
  68. # <img id="foo" class="bar" />
  69. node3 = Tag(wrap([node3n1]),
  70. attrs=[Attribute(wrap([node3n2]), wrap([node3n3])),
  71. Attribute(wrap([node3n4]), wrap([node3n5]))],
  72. self_closing=True, padding=" ")
  73. gen1 = node1.__iternodes__(getnodes)
  74. gen2 = node2.__iternodes__(getnodes)
  75. gen3 = node3.__iternodes__(getnodes)
  76. self.assertEqual((None, node1), next(gen1))
  77. self.assertEqual((None, node2), next(gen2))
  78. self.assertEqual((None, node3), next(gen3))
  79. self.assertEqual((node1.tag, node1n1), next(gen1))
  80. self.assertEqual((node3.tag, node3n1), next(gen3))
  81. self.assertEqual((node3.attributes[0].name, node3n2), next(gen3))
  82. self.assertEqual((node3.attributes[0].value, node3n3), next(gen3))
  83. self.assertEqual((node3.attributes[1].name, node3n4), next(gen3))
  84. self.assertEqual((node3.attributes[1].value, node3n5), next(gen3))
  85. self.assertEqual((node1.contents, node1n2), next(gen1))
  86. self.assertEqual((node2.contents, node2n1), next(gen2))
  87. self.assertEqual((node1.closing_tag, node1n1), next(gen1))
  88. self.assertRaises(StopIteration, next, gen1)
  89. self.assertRaises(StopIteration, next, gen2)
  90. self.assertRaises(StopIteration, next, gen3)
  91. def test_strip(self):
  92. """test Tag.__strip__()"""
  93. node1 = Tag(wraptext("i"), wraptext("foobar"))
  94. node2 = Tag(wraptext("math"), wraptext("foobar"))
  95. for a in (True, False):
  96. for b in (True, False):
  97. self.assertEqual("foobar", node1.__strip__(a, b))
  98. self.assertEqual(None, node2.__strip__(a, b))
  99. def test_showtree(self):
  100. """test Tag.__showtree__()"""
  101. output = []
  102. getter, marker = object(), object()
  103. get = lambda code: output.append((getter, code))
  104. mark = lambda: output.append(marker)
  105. node1 = Tag(wraptext("ref"), wraptext("text"), [agen("name", "foo")])
  106. node2 = Tag(wraptext("br"), self_closing=True, padding=" ")
  107. node3 = Tag(wraptext("br"), self_closing=True, invalid=True,
  108. implicit=True, padding=" ")
  109. node1.__showtree__(output.append, get, mark)
  110. node2.__showtree__(output.append, get, mark)
  111. node3.__showtree__(output.append, get, mark)
  112. valid = [
  113. "<", (getter, node1.tag), (getter, node1.attributes[0].name),
  114. " = ", marker, (getter, node1.attributes[0].value), ">",
  115. (getter, node1.contents), "</", (getter, node1.closing_tag), ">",
  116. "<", (getter, node2.tag), "/>", "</", (getter, node3.tag), ">"]
  117. self.assertEqual(valid, output)
  118. def test_tag(self):
  119. """test getter/setter for the tag attribute"""
  120. tag = wraptext("ref")
  121. node = Tag(tag, wraptext("text"))
  122. self.assertIs(tag, node.tag)
  123. self.assertIs(tag, node.closing_tag)
  124. node.tag = "span"
  125. self.assertWikicodeEqual(wraptext("span"), node.tag)
  126. self.assertWikicodeEqual(wraptext("span"), node.closing_tag)
  127. self.assertEqual("<span>text</span>", node)
  128. def test_contents(self):
  129. """test getter/setter for the contents attribute"""
  130. contents = wraptext("text")
  131. node = Tag(wraptext("ref"), contents)
  132. self.assertIs(contents, node.contents)
  133. node.contents = "text and a {{template}}"
  134. parsed = wrap([Text("text and a "), Template(wraptext("template"))])
  135. self.assertWikicodeEqual(parsed, node.contents)
  136. self.assertEqual("<ref>text and a {{template}}</ref>", node)
  137. def test_attributes(self):
  138. """test getter for the attributes attribute"""
  139. attrs = [agen("name", "bar")]
  140. node1 = Tag(wraptext("ref"), wraptext("foo"))
  141. node2 = Tag(wraptext("ref"), wraptext("foo"), attrs)
  142. self.assertEqual([], node1.attributes)
  143. self.assertIs(attrs, node2.attributes)
  144. def test_wiki_markup(self):
  145. """test getter/setter for the wiki_markup attribute"""
  146. node = Tag(wraptext("i"), wraptext("italic text"))
  147. self.assertIs(None, node.wiki_markup)
  148. node.wiki_markup = "''"
  149. self.assertEqual("''", node.wiki_markup)
  150. self.assertEqual("''italic text''", node)
  151. node.wiki_markup = False
  152. self.assertFalse(node.wiki_markup)
  153. self.assertEqual("<i>italic text</i>", node)
  154. def test_self_closing(self):
  155. """test getter/setter for the self_closing attribute"""
  156. node = Tag(wraptext("ref"), wraptext("foobar"))
  157. self.assertFalse(node.self_closing)
  158. node.self_closing = True
  159. self.assertTrue(node.self_closing)
  160. self.assertEqual("<ref/>", node)
  161. node.self_closing = 0
  162. self.assertFalse(node.self_closing)
  163. self.assertEqual("<ref>foobar</ref>", node)
  164. def test_invalid(self):
  165. """test getter/setter for the invalid attribute"""
  166. node = Tag(wraptext("br"), self_closing=True, implicit=True)
  167. self.assertFalse(node.invalid)
  168. node.invalid = True
  169. self.assertTrue(node.invalid)
  170. self.assertEqual("</br>", node)
  171. node.invalid = 0
  172. self.assertFalse(node.invalid)
  173. self.assertEqual("<br>", node)
  174. def test_implicit(self):
  175. """test getter/setter for the implicit attribute"""
  176. node = Tag(wraptext("br"), self_closing=True)
  177. self.assertFalse(node.implicit)
  178. node.implicit = True
  179. self.assertTrue(node.implicit)
  180. self.assertEqual("<br>", node)
  181. node.implicit = 0
  182. self.assertFalse(node.implicit)
  183. self.assertEqual("<br/>", node)
  184. def test_padding(self):
  185. """test getter/setter for the padding attribute"""
  186. node = Tag(wraptext("ref"), wraptext("foobar"))
  187. self.assertEqual("", node.padding)
  188. node.padding = " "
  189. self.assertEqual(" ", node.padding)
  190. self.assertEqual("<ref >foobar</ref>", node)
  191. node.padding = None
  192. self.assertEqual("", node.padding)
  193. self.assertEqual("<ref>foobar</ref>", node)
  194. self.assertRaises(ValueError, setattr, node, "padding", True)
  195. def test_closing_tag(self):
  196. """test getter/setter for the closing_tag attribute"""
  197. tag = wraptext("ref")
  198. node = Tag(tag, wraptext("foobar"))
  199. self.assertIs(tag, node.closing_tag)
  200. node.closing_tag = "ref {{ignore me}}"
  201. parsed = wrap([Text("ref "), Template(wraptext("ignore me"))])
  202. self.assertWikicodeEqual(parsed, node.closing_tag)
  203. self.assertEqual("<ref>foobar</ref {{ignore me}}>", node)
  204. if __name__ == "__main__":
  205. unittest.main(verbosity=2)