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.

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