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.
 
 
 
 

341 satır
17 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 HTMLEntity, Template, Text
  26. from mwparserfromhell.nodes.extras import Parameter
  27. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  28. pgens = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=True)
  29. pgenh = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=False)
  30. class TestTemplate(TreeEqualityTestCase):
  31. """Test cases for the Template node."""
  32. def test_unicode(self):
  33. """test Template.__unicode__()"""
  34. node = Template(wraptext("foobar"))
  35. self.assertEqual("{{foobar}}", str(node))
  36. node2 = Template(wraptext("foo"),
  37. [pgenh("1", "bar"), pgens("abc", "def")])
  38. self.assertEqual("{{foo|bar|abc=def}}", str(node2))
  39. def test_strip(self):
  40. """test Template.__strip__()"""
  41. node1 = Template(wraptext("foobar"))
  42. node2 = Template(wraptext("foo"),
  43. [pgenh("1", "bar"), pgens("abc", "def")])
  44. for a in (True, False):
  45. for b in (True, False):
  46. self.assertEqual(None, node1.__strip__(a, b))
  47. self.assertEqual(None, node2.__strip__(a, b))
  48. def test_showtree(self):
  49. """test Template.__showtree__()"""
  50. output = []
  51. getter, marker = object(), object()
  52. get = lambda code: output.append((getter, code))
  53. mark = lambda: output.append(marker)
  54. node1 = Template(wraptext("foobar"))
  55. node2 = Template(wraptext("foo"),
  56. [pgenh("1", "bar"), pgens("abc", "def")])
  57. node1.__showtree__(output.append, get, mark)
  58. node2.__showtree__(output.append, get, mark)
  59. valid = [
  60. "{{", (getter, node1.name), "}}", "{{", (getter, node2.name),
  61. " | ", marker, (getter, node2.params[0].name), " = ", marker,
  62. (getter, node2.params[0].value), " | ", marker,
  63. (getter, node2.params[1].name), " = ", marker,
  64. (getter, node2.params[1].value), "}}"]
  65. self.assertEqual(valid, output)
  66. def test_name(self):
  67. """test getter/setter for the name attribute"""
  68. name = wraptext("foobar")
  69. node1 = Template(name)
  70. node2 = Template(name, [pgenh("1", "bar")])
  71. self.assertIs(name, node1.name)
  72. self.assertIs(name, node2.name)
  73. node1.name = "asdf"
  74. node2.name = "téstïng"
  75. self.assertWikicodeEqual(wraptext("asdf"), node1.name)
  76. self.assertWikicodeEqual(wraptext("téstïng"), node2.name)
  77. def test_params(self):
  78. """test getter for the params attribute"""
  79. node1 = Template(wraptext("foobar"))
  80. plist = [pgenh("1", "bar"), pgens("abc", "def")]
  81. node2 = Template(wraptext("foo"), plist)
  82. self.assertEqual([], node1.params)
  83. self.assertIs(plist, node2.params)
  84. def test_has_param(self):
  85. """test Template.has_param()"""
  86. node1 = Template(wraptext("foobar"))
  87. node2 = Template(wraptext("foo"),
  88. [pgenh("1", "bar"), pgens("\nabc ", "def")])
  89. node3 = Template(wraptext("foo"),
  90. [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")])
  91. node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")])
  92. self.assertFalse(node1.has_param("foobar"))
  93. self.assertTrue(node2.has_param(1))
  94. self.assertTrue(node2.has_param("abc"))
  95. self.assertFalse(node2.has_param("def"))
  96. self.assertTrue(node3.has_param("1"))
  97. self.assertTrue(node3.has_param(" b "))
  98. self.assertFalse(node4.has_param("b"))
  99. self.assertTrue(node3.has_param("b", False))
  100. self.assertTrue(node4.has_param("b", False))
  101. def test_get(self):
  102. """test Template.get()"""
  103. node1 = Template(wraptext("foobar"))
  104. node2p1 = pgenh("1", "bar")
  105. node2p2 = pgens("abc", "def")
  106. node2 = Template(wraptext("foo"), [node2p1, node2p2])
  107. node3p1 = pgens("b", "c")
  108. node3p2 = pgens("1", "d")
  109. node3 = Template(wraptext("foo"), [pgenh("1", "a"), node3p1, node3p2])
  110. node4p1 = pgens(" b", " ")
  111. node4 = Template(wraptext("foo"), [pgenh("1", "a"), node4p1])
  112. self.assertRaises(ValueError, node1.get, "foobar")
  113. self.assertIs(node2p1, node2.get(1))
  114. self.assertIs(node2p2, node2.get("abc"))
  115. self.assertRaises(ValueError, node2.get, "def")
  116. self.assertIs(node3p1, node3.get("b"))
  117. self.assertIs(node3p2, node3.get("1"))
  118. self.assertIs(node4p1, node4.get("b "))
  119. def test_add(self):
  120. """test Template.add()"""
  121. node1 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  122. node2 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  123. node3 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  124. node4 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  125. node5 = Template(wraptext("a"), [pgens("b", "c"),
  126. pgens(" d ", "e")])
  127. node6 = Template(wraptext("a"), [pgens("b", "c"), pgens("b", "d"),
  128. pgens("b", "e")])
  129. node7 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  130. node8p = pgenh("1", "d")
  131. node8 = Template(wraptext("a"), [pgens("b", "c"), node8p])
  132. node9 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  133. node10 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "e")])
  134. node11 = Template(wraptext("a"), [pgens("b", "c")])
  135. node12 = Template(wraptext("a"), [pgens("b", "c")])
  136. node13 = Template(wraptext("a"), [
  137. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  138. node14 = Template(wraptext("a\n"), [
  139. pgens("b ", "c\n"), pgens("d ", " e"), pgens("f ", "g\n"),
  140. pgens("h ", " i\n")])
  141. node15 = Template(wraptext("a"), [
  142. pgens("b ", " c\n"), pgens("\nd ", " e"), pgens("\nf ", "g ")])
  143. node16 = Template(wraptext("a"), [
  144. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  145. node17 = Template(wraptext("a"), [
  146. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  147. node18 = Template(wraptext("a\n"), [
  148. pgens("b ", "c\n"), pgens("d ", " e"), pgens("f ", "g\n"),
  149. pgens("h ", " i\n")])
  150. node19 = Template(wraptext("a"), [
  151. pgens("b ", " c\n"), pgens("\nd ", " e"), pgens("\nf ", "g ")])
  152. node20 = Template(wraptext("a"), [
  153. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  154. node21 = Template(wraptext("a"), [pgenh("1", "b")])
  155. node22 = Template(wraptext("a"), [pgenh("1", "b")])
  156. node23 = Template(wraptext("a"), [pgenh("1", "b")])
  157. node24 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  158. pgenh("3", "d"), pgenh("4", "e")])
  159. node25 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  160. pgens("4", "d"), pgens("5", "e")])
  161. node26 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  162. pgens("4", "d"), pgens("5", "e")])
  163. node27 = Template(wraptext("a"), [pgenh("1", "b")])
  164. node28 = Template(wraptext("a"), [pgenh("1", "b")])
  165. node29 = Template(wraptext("a"), [pgens("b", "c")])
  166. node30 = Template(wraptext("a"), [pgenh("1", "b")])
  167. node31 = Template(wraptext("a"), [pgenh("1", "b")])
  168. node32 = Template(wraptext("a"), [pgens("1", "b")])
  169. node33 = Template(wraptext("a"), [
  170. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  171. node34 = Template(wraptext("a\n"), [
  172. pgens("b ", "c\n"), pgens("d ", " e"), pgens("f ", "g\n"),
  173. pgens("h ", " i\n")])
  174. node35 = Template(wraptext("a"), [
  175. pgens("b ", " c\n"), pgens("\nd ", " e"), pgens("\nf ", "g ")])
  176. node36 = Template(wraptext("a"), [
  177. pgens("\nb ", " c "), pgens("\nd ", " e "), pgens("\nf ", " g ")])
  178. node37 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"),
  179. pgens("b", "f"), pgens("b", "h"),
  180. pgens("i", "j")])
  181. node37 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"),
  182. pgens("b", "f"), pgens("b", "h"),
  183. pgens("i", "j")])
  184. node38 = Template(wraptext("a"), [pgens("1", "b"), pgens("x", "y"),
  185. pgens("1", "c"), pgens("2", "d")])
  186. node39 = Template(wraptext("a"), [pgens("1", "b"), pgens("x", "y"),
  187. pgenh("1", "c"), pgenh("2", "d")])
  188. node40 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"),
  189. pgens("f", "g")])
  190. node1.add("e", "f", showkey=True)
  191. node2.add(2, "g", showkey=False)
  192. node3.add("e", "foo|bar", showkey=True)
  193. node4.add("e", "f", showkey=True, before="b")
  194. node5.add("f", "g", showkey=True, before=" d ")
  195. node6.add("f", "g", showkey=True, before="b")
  196. self.assertRaises(ValueError, node7.add, "e", "f", showkey=True,
  197. before="q")
  198. node8.add("e", "f", showkey=True, before=node8p)
  199. node9.add("e", "f", showkey=True, before=pgenh("1", "d"))
  200. self.assertRaises(ValueError, node10.add, "e", "f", showkey=True,
  201. before=pgenh("1", "d"))
  202. node11.add("d", "foo=bar", showkey=True)
  203. node12.add("1", "foo=bar", showkey=False)
  204. node13.add("h", "i", showkey=True)
  205. node14.add("j", "k", showkey=True)
  206. node15.add("h", "i", showkey=True)
  207. node16.add("h", "i", showkey=True, preserve_spacing=False)
  208. node17.add("h", "i", showkey=False)
  209. node18.add("j", "k", showkey=False)
  210. node19.add("h", "i", showkey=False)
  211. node20.add("h", "i", showkey=False, preserve_spacing=False)
  212. node21.add("2", "c")
  213. node22.add("3", "c")
  214. node23.add("c", "d")
  215. node24.add("5", "f")
  216. node25.add("3", "f")
  217. node26.add("6", "f")
  218. node27.add("c", "foo=bar")
  219. node28.add("2", "foo=bar")
  220. node29.add("b", "d")
  221. node30.add("1", "foo=bar")
  222. node31.add("1", "foo=bar", showkey=True)
  223. node32.add("1", "foo=bar", showkey=False)
  224. node33.add("d", "foo")
  225. node34.add("f", "foo")
  226. node35.add("f", "foo")
  227. node36.add("d", "foo", preserve_spacing=False)
  228. node37.add("b", "k")
  229. node38.add("1", "e")
  230. node39.add("1", "e")
  231. node40.add("d", "h", before="b")
  232. self.assertEqual("{{a|b=c|d|e=f}}", node1)
  233. self.assertEqual("{{a|b=c|d|g}}", node2)
  234. self.assertEqual("{{a|b=c|d|e=foo&#124;bar}}", node3)
  235. self.assertIsInstance(node3.params[2].value.get(1), HTMLEntity)
  236. self.assertEqual("{{a|e=f|b=c|d}}", node4)
  237. self.assertEqual("{{a|b=c|f=g| d =e}}", node5)
  238. self.assertEqual("{{a|b=c|b=d|f=g|b=e}}", node6)
  239. self.assertEqual("{{a|b=c|d}}", node7)
  240. self.assertEqual("{{a|b=c|e=f|d}}", node8)
  241. self.assertEqual("{{a|b=c|e=f|d}}", node9)
  242. self.assertEqual("{{a|b=c|e}}", node10)
  243. self.assertEqual("{{a|b=c|d=foo=bar}}", node11)
  244. self.assertEqual("{{a|b=c|foo&#61;bar}}", node12)
  245. self.assertIsInstance(node12.params[1].value.get(1), HTMLEntity)
  246. self.assertEqual("{{a|\nb = c|\nd = e|\nf = g|\nh = i}}", node13)
  247. self.assertEqual("{{a\n|b =c\n|d = e|f =g\n|h = i\n|j =k\n}}", node14)
  248. self.assertEqual("{{a|b = c\n|\nd = e|\nf =g |h =i}}", node15)
  249. self.assertEqual("{{a|\nb = c|\nd = e|\nf = g|h=i}}", node16)
  250. self.assertEqual("{{a|\nb = c|\nd = e|\nf = g| i}}", node17)
  251. self.assertEqual("{{a\n|b =c\n|d = e|f =g\n|h = i\n|k\n}}", node18)
  252. self.assertEqual("{{a|b = c\n|\nd = e|\nf =g |i}}", node19)
  253. self.assertEqual("{{a|\nb = c|\nd = e|\nf = g|i}}", node20)
  254. self.assertEqual("{{a|b|c}}", node21)
  255. self.assertEqual("{{a|b|3=c}}", node22)
  256. self.assertEqual("{{a|b|c=d}}", node23)
  257. self.assertEqual("{{a|b|c|d|e|f}}", node24)
  258. self.assertEqual("{{a|b|c|4=d|5=e|f}}", node25)
  259. self.assertEqual("{{a|b|c|4=d|5=e|6=f}}", node26)
  260. self.assertEqual("{{a|b|c=foo=bar}}", node27)
  261. self.assertEqual("{{a|b|foo&#61;bar}}", node28)
  262. self.assertIsInstance(node28.params[1].value.get(1), HTMLEntity)
  263. self.assertEqual("{{a|b=d}}", node29)
  264. self.assertEqual("{{a|foo&#61;bar}}", node30)
  265. self.assertIsInstance(node30.params[0].value.get(1), HTMLEntity)
  266. self.assertEqual("{{a|1=foo=bar}}", node31)
  267. self.assertEqual("{{a|foo&#61;bar}}", node32)
  268. self.assertIsInstance(node32.params[0].value.get(1), HTMLEntity)
  269. self.assertEqual("{{a|\nb = c|\nd = foo|\nf = g}}", node33)
  270. self.assertEqual("{{a\n|b =c\n|d = e|f =foo\n|h = i\n}}", node34)
  271. self.assertEqual("{{a|b = c\n|\nd = e|\nf =foo }}", node35)
  272. self.assertEqual("{{a|\nb = c |\nd =foo|\nf = g }}", node36)
  273. self.assertEqual("{{a|b=k|d=e|i=j}}", node37)
  274. self.assertEqual("{{a|1=e|x=y|2=d}}", node38)
  275. self.assertEqual("{{a|x=y|e|d}}", node39)
  276. self.assertEqual("{{a|b=c|d=h|f=g}}", node40)
  277. def test_remove(self):
  278. """test Template.remove()"""
  279. node1 = Template(wraptext("foobar"))
  280. node2 = Template(wraptext("foo"), [pgenh("1", "bar"),
  281. pgens("abc", "def")])
  282. node3 = Template(wraptext("foo"), [pgenh("1", "bar"),
  283. pgens("abc", "def")])
  284. node4 = Template(wraptext("foo"), [pgenh("1", "bar"),
  285. pgenh("2", "baz")])
  286. node5 = Template(wraptext("foo"), [
  287. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  288. node6 = Template(wraptext("foo"), [
  289. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  290. node7 = Template(wraptext("foo"), [
  291. pgens("1 ", "a"), pgens(" 1", "b"), pgens("2", "c")])
  292. node8 = Template(wraptext("foo"), [
  293. pgens("1 ", "a"), pgens(" 1", "b"), pgens("2", "c")])
  294. node9 = Template(wraptext("foo"), [
  295. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  296. node10 = Template(wraptext("foo"), [
  297. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  298. node2.remove("1")
  299. node2.remove("abc")
  300. node3.remove(1, keep_field=True)
  301. node3.remove("abc", keep_field=True)
  302. node4.remove("1", keep_field=False)
  303. node5.remove("a", keep_field=False)
  304. node6.remove("a", keep_field=True)
  305. node7.remove(1, keep_field=True)
  306. node8.remove(1, keep_field=False)
  307. node9.remove(1, keep_field=True)
  308. node10.remove(1, keep_field=False)
  309. self.assertRaises(ValueError, node1.remove, 1)
  310. self.assertRaises(ValueError, node1.remove, "a")
  311. self.assertRaises(ValueError, node2.remove, "1")
  312. self.assertEqual("{{foo}}", node2)
  313. self.assertEqual("{{foo||abc=}}", node3)
  314. self.assertEqual("{{foo||baz}}", node4)
  315. self.assertEqual("{{foo|b=c}}", node5)
  316. self.assertEqual("{{foo| a=|b=c}}", node6)
  317. self.assertEqual("{{foo|1 =|2=c}}", node7)
  318. self.assertEqual("{{foo|2=c}}", node8)
  319. self.assertEqual("{{foo||c}}", node9)
  320. self.assertEqual("{{foo||c}}", node10)
  321. if __name__ == "__main__":
  322. unittest.main(verbosity=2)