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.

test_tag.py 14 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  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 unittest2
  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, wrap, wraptext
  28. agen = lambda name, value: Attribute(wraptext(name), wraptext(value))
  29. agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), False)
  30. agenp = lambda name, v, a, b, c: Attribute(wraptext(name), v, True, a, b, c)
  31. agenpnv = lambda name, a, b, c: Attribute(wraptext(name), None, True, a, b, c)
  32. class TestTag(TreeEqualityTestCase):
  33. """Test cases for the Tag node."""
  34. def test_unicode(self):
  35. """test Tag.__unicode__()"""
  36. node1 = Tag(wraptext("ref"))
  37. node2 = Tag(wraptext("span"), wraptext("foo"),
  38. [agen("style", "color: red;")])
  39. node3 = Tag(wraptext("ref"),
  40. attrs=[agennq("name", "foo"),
  41. agenpnv("some_attr", " ", "", "")],
  42. self_closing=True)
  43. node4 = Tag(wraptext("br"), self_closing=True, padding=" ")
  44. node5 = Tag(wraptext("br"), self_closing=True, implicit=True)
  45. node6 = Tag(wraptext("br"), self_closing=True, invalid=True,
  46. implicit=True)
  47. node7 = Tag(wraptext("br"), self_closing=True, invalid=True,
  48. padding=" ")
  49. node8 = Tag(wraptext("hr"), wiki_markup="----", self_closing=True)
  50. node9 = Tag(wraptext("i"), wraptext("italics!"), wiki_markup="''")
  51. self.assertEqual("<ref></ref>", str(node1))
  52. self.assertEqual('<span style="color: red;">foo</span>', str(node2))
  53. self.assertEqual("<ref name=foo some_attr/>", str(node3))
  54. self.assertEqual("<br />", str(node4))
  55. self.assertEqual("<br>", str(node5))
  56. self.assertEqual("</br>", str(node6))
  57. self.assertEqual("</br />", str(node7))
  58. self.assertEqual("----", str(node8))
  59. self.assertEqual("''italics!''", str(node9))
  60. def test_children(self):
  61. """test Tag.__children__()"""
  62. # <ref>foobar</ref>
  63. node1 = Tag(wraptext("ref"), wraptext("foobar"))
  64. # '''bold text'''
  65. node2 = Tag(wraptext("b"), wraptext("bold text"), wiki_markup="'''")
  66. # <img id="foo" class="bar" />
  67. node3 = Tag(wraptext("img"),
  68. attrs=[Attribute(wraptext("id"), wraptext("foo")),
  69. Attribute(wraptext("class"), wraptext("bar"))],
  70. self_closing=True, padding=" ")
  71. gen1 = node1.__children__()
  72. gen2 = node2.__children__()
  73. gen3 = node3.__children__()
  74. self.assertEqual(node1.tag, next(gen1))
  75. self.assertEqual(node3.tag, next(gen3))
  76. self.assertEqual(node3.attributes[0].name, next(gen3))
  77. self.assertEqual(node3.attributes[0].value, next(gen3))
  78. self.assertEqual(node3.attributes[1].name, next(gen3))
  79. self.assertEqual(node3.attributes[1].value, next(gen3))
  80. self.assertEqual(node1.contents, next(gen1))
  81. self.assertEqual(node2.contents, next(gen2))
  82. self.assertEqual(node1.closing_tag, next(gen1))
  83. self.assertRaises(StopIteration, next, gen1)
  84. self.assertRaises(StopIteration, next, gen2)
  85. self.assertRaises(StopIteration, next, gen3)
  86. def test_strip(self):
  87. """test Tag.__strip__()"""
  88. node1 = Tag(wraptext("i"), wraptext("foobar"))
  89. node2 = Tag(wraptext("math"), wraptext("foobar"))
  90. node3 = Tag(wraptext("br"), self_closing=True)
  91. for a in (True, False):
  92. for b in (True, False):
  93. self.assertEqual("foobar", node1.__strip__(a, b))
  94. self.assertEqual(None, node2.__strip__(a, b))
  95. self.assertEqual(None, node3.__strip__(a, b))
  96. def test_showtree(self):
  97. """test Tag.__showtree__()"""
  98. output = []
  99. getter, marker = object(), object()
  100. get = lambda code: output.append((getter, code))
  101. mark = lambda: output.append(marker)
  102. node1 = Tag(wraptext("ref"), wraptext("text"), [agen("name", "foo")])
  103. node2 = Tag(wraptext("br"), self_closing=True, padding=" ")
  104. node3 = Tag(wraptext("br"), self_closing=True, invalid=True,
  105. implicit=True, padding=" ")
  106. node1.__showtree__(output.append, get, mark)
  107. node2.__showtree__(output.append, get, mark)
  108. node3.__showtree__(output.append, get, mark)
  109. valid = [
  110. "<", (getter, node1.tag), (getter, node1.attributes[0].name),
  111. " = ", marker, (getter, node1.attributes[0].value), ">",
  112. (getter, node1.contents), "</", (getter, node1.closing_tag), ">",
  113. "<", (getter, node2.tag), "/>", "</", (getter, node3.tag), ">"]
  114. self.assertEqual(valid, output)
  115. def test_tag(self):
  116. """test getter/setter for the tag attribute"""
  117. tag = wraptext("ref")
  118. node = Tag(tag, wraptext("text"))
  119. self.assertIs(tag, node.tag)
  120. self.assertIs(tag, node.closing_tag)
  121. node.tag = "span"
  122. self.assertWikicodeEqual(wraptext("span"), node.tag)
  123. self.assertWikicodeEqual(wraptext("span"), node.closing_tag)
  124. self.assertEqual("<span>text</span>", node)
  125. def test_contents(self):
  126. """test getter/setter for the contents attribute"""
  127. contents = wraptext("text")
  128. node = Tag(wraptext("ref"), contents)
  129. self.assertIs(contents, node.contents)
  130. node.contents = "text and a {{template}}"
  131. parsed = wrap([Text("text and a "), Template(wraptext("template"))])
  132. self.assertWikicodeEqual(parsed, node.contents)
  133. self.assertEqual("<ref>text and a {{template}}</ref>", node)
  134. def test_attributes(self):
  135. """test getter for the attributes attribute"""
  136. attrs = [agen("name", "bar")]
  137. node1 = Tag(wraptext("ref"), wraptext("foo"))
  138. node2 = Tag(wraptext("ref"), wraptext("foo"), attrs)
  139. self.assertEqual([], node1.attributes)
  140. self.assertIs(attrs, node2.attributes)
  141. def test_wiki_markup(self):
  142. """test getter/setter for the wiki_markup attribute"""
  143. node = Tag(wraptext("i"), wraptext("italic text"))
  144. self.assertIs(None, node.wiki_markup)
  145. node.wiki_markup = "''"
  146. self.assertEqual("''", node.wiki_markup)
  147. self.assertEqual("''italic text''", node)
  148. node.wiki_markup = False
  149. self.assertFalse(node.wiki_markup)
  150. self.assertEqual("<i>italic text</i>", node)
  151. def test_self_closing(self):
  152. """test getter/setter for the self_closing attribute"""
  153. node = Tag(wraptext("ref"), wraptext("foobar"))
  154. self.assertFalse(node.self_closing)
  155. node.self_closing = True
  156. self.assertTrue(node.self_closing)
  157. self.assertEqual("<ref/>", node)
  158. node.self_closing = 0
  159. self.assertFalse(node.self_closing)
  160. self.assertEqual("<ref>foobar</ref>", node)
  161. def test_invalid(self):
  162. """test getter/setter for the invalid attribute"""
  163. node = Tag(wraptext("br"), self_closing=True, implicit=True)
  164. self.assertFalse(node.invalid)
  165. node.invalid = True
  166. self.assertTrue(node.invalid)
  167. self.assertEqual("</br>", node)
  168. node.invalid = 0
  169. self.assertFalse(node.invalid)
  170. self.assertEqual("<br>", node)
  171. def test_implicit(self):
  172. """test getter/setter for the implicit attribute"""
  173. node = Tag(wraptext("br"), self_closing=True)
  174. self.assertFalse(node.implicit)
  175. node.implicit = True
  176. self.assertTrue(node.implicit)
  177. self.assertEqual("<br>", node)
  178. node.implicit = 0
  179. self.assertFalse(node.implicit)
  180. self.assertEqual("<br/>", node)
  181. def test_padding(self):
  182. """test getter/setter for the padding attribute"""
  183. node = Tag(wraptext("ref"), wraptext("foobar"))
  184. self.assertEqual("", node.padding)
  185. node.padding = " "
  186. self.assertEqual(" ", node.padding)
  187. self.assertEqual("<ref >foobar</ref>", node)
  188. node.padding = None
  189. self.assertEqual("", node.padding)
  190. self.assertEqual("<ref>foobar</ref>", node)
  191. self.assertRaises(ValueError, setattr, node, "padding", True)
  192. def test_closing_tag(self):
  193. """test getter/setter for the closing_tag attribute"""
  194. tag = wraptext("ref")
  195. node = Tag(tag, wraptext("foobar"))
  196. self.assertIs(tag, node.closing_tag)
  197. node.closing_tag = "ref {{ignore me}}"
  198. parsed = wrap([Text("ref "), Template(wraptext("ignore me"))])
  199. self.assertWikicodeEqual(parsed, node.closing_tag)
  200. self.assertEqual("<ref>foobar</ref {{ignore me}}>", node)
  201. def test_has(self):
  202. """test Tag.has()"""
  203. node = Tag(wraptext("ref"), wraptext("cite"), [agen("name", "foo")])
  204. self.assertTrue(node.has("name"))
  205. self.assertTrue(node.has(" name "))
  206. self.assertTrue(node.has(wraptext("name")))
  207. self.assertFalse(node.has("Name"))
  208. self.assertFalse(node.has("foo"))
  209. attrs = [agen("id", "foo"), agenp("class", "bar", " ", "\n", "\n"),
  210. agen("foo", "bar"), agenpnv("foo", " ", " \n ", " \t")]
  211. node2 = Tag(wraptext("div"), attrs=attrs, self_closing=True)
  212. self.assertTrue(node2.has("id"))
  213. self.assertTrue(node2.has("class"))
  214. self.assertTrue(node2.has(attrs[1].pad_first + str(attrs[1].name) +
  215. attrs[1].pad_before_eq))
  216. self.assertTrue(node2.has(attrs[3]))
  217. self.assertTrue(node2.has(str(attrs[3])))
  218. self.assertFalse(node2.has("idclass"))
  219. self.assertFalse(node2.has("id class"))
  220. self.assertFalse(node2.has("id=foo"))
  221. def test_get(self):
  222. """test Tag.get()"""
  223. attrs = [agen("name", "foo")]
  224. node = Tag(wraptext("ref"), wraptext("cite"), attrs)
  225. self.assertIs(attrs[0], node.get("name"))
  226. self.assertIs(attrs[0], node.get(" name "))
  227. self.assertIs(attrs[0], node.get(wraptext("name")))
  228. self.assertRaises(ValueError, node.get, "Name")
  229. self.assertRaises(ValueError, node.get, "foo")
  230. attrs = [agen("id", "foo"), agenp("class", "bar", " ", "\n", "\n"),
  231. agen("foo", "bar"), agenpnv("foo", " ", " \n ", " \t")]
  232. node2 = Tag(wraptext("div"), attrs=attrs, self_closing=True)
  233. self.assertIs(attrs[0], node2.get("id"))
  234. self.assertIs(attrs[1], node2.get("class"))
  235. self.assertIs(attrs[1], node2.get(
  236. attrs[1].pad_first + str(attrs[1].name) + attrs[1].pad_before_eq))
  237. self.assertIs(attrs[3], node2.get(attrs[3]))
  238. self.assertIs(attrs[3], node2.get(str(attrs[3])))
  239. self.assertIs(attrs[3], node2.get(" foo"))
  240. self.assertRaises(ValueError, node2.get, "idclass")
  241. self.assertRaises(ValueError, node2.get, "id class")
  242. self.assertRaises(ValueError, node2.get, "id=foo")
  243. def test_add(self):
  244. """test Tag.add()"""
  245. node = Tag(wraptext("ref"), wraptext("cite"))
  246. node.add("name", "value")
  247. node.add("name", "value", quoted=False)
  248. node.add("name")
  249. node.add(1, False)
  250. node.add("style", "{{foobar}}")
  251. node.add("name", "value", True, "\n", " ", " ")
  252. attr1 = ' name="value"'
  253. attr2 = " name=value"
  254. attr3 = " name"
  255. attr4 = ' 1="False"'
  256. attr5 = ' style="{{foobar}}"'
  257. attr6 = '\nname = "value"'
  258. self.assertEqual(attr1, node.attributes[0])
  259. self.assertEqual(attr2, node.attributes[1])
  260. self.assertEqual(attr3, node.attributes[2])
  261. self.assertEqual(attr4, node.attributes[3])
  262. self.assertEqual(attr5, node.attributes[4])
  263. self.assertEqual(attr6, node.attributes[5])
  264. self.assertEqual(attr6, node.get("name"))
  265. self.assertWikicodeEqual(wrap([Template(wraptext("foobar"))]),
  266. node.attributes[4].value)
  267. self.assertEqual("".join(("<ref", attr1, attr2, attr3, attr4, attr5,
  268. attr6, ">cite</ref>")), node)
  269. def test_remove(self):
  270. """test Tag.remove()"""
  271. attrs = [agen("id", "foo"), agenp("class", "bar", " ", "\n", "\n"),
  272. agen("foo", "bar"), agenpnv("foo", " ", " \n ", " \t")]
  273. node = Tag(wraptext("div"), attrs=attrs, self_closing=True)
  274. node.remove("class")
  275. self.assertEqual('<div id="foo" foo="bar" foo \n />', node)
  276. node.remove("foo")
  277. self.assertEqual('<div id="foo"/>', node)
  278. self.assertRaises(ValueError, node.remove, "foo")
  279. node.remove("id")
  280. self.assertEqual('<div/>', node)
  281. if __name__ == "__main__":
  282. unittest2.main(verbosity=2)