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_template.py 21 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2016 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. try:
  24. import unittest2 as unittest
  25. except ImportError:
  26. import unittest
  27. from mwparserfromhell.compat import str
  28. from mwparserfromhell.nodes import HTMLEntity, Template, Text
  29. from mwparserfromhell.nodes.extras import Parameter
  30. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  31. pgens = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=True)
  32. pgenh = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=False)
  33. class TestTemplate(TreeEqualityTestCase):
  34. """Test cases for the Template node."""
  35. def test_unicode(self):
  36. """test Template.__unicode__()"""
  37. node = Template(wraptext("foobar"))
  38. self.assertEqual("{{foobar}}", str(node))
  39. node2 = Template(wraptext("foo"),
  40. [pgenh("1", "bar"), pgens("abc", "def")])
  41. self.assertEqual("{{foo|bar|abc=def}}", str(node2))
  42. def test_children(self):
  43. """test Template.__children__()"""
  44. node2p1 = Parameter(wraptext("1"), wraptext("bar"), showkey=False)
  45. node2p2 = Parameter(wraptext("abc"), wrap([Text("def"), Text("ghi")]),
  46. showkey=True)
  47. node1 = Template(wraptext("foobar"))
  48. node2 = Template(wraptext("foo"), [node2p1, node2p2])
  49. gen1 = node1.__children__()
  50. gen2 = node2.__children__()
  51. self.assertEqual(node1.name, next(gen1))
  52. self.assertEqual(node2.name, next(gen2))
  53. self.assertEqual(node2.params[0].value, next(gen2))
  54. self.assertEqual(node2.params[1].name, next(gen2))
  55. self.assertEqual(node2.params[1].value, next(gen2))
  56. self.assertRaises(StopIteration, next, gen1)
  57. self.assertRaises(StopIteration, next, gen2)
  58. def test_strip(self):
  59. """test Template.__strip__()"""
  60. node1 = Template(wraptext("foobar"))
  61. node2 = Template(wraptext("foo"),
  62. [pgenh("1", "bar"), pgens("abc", "def")])
  63. for a in (True, False):
  64. for b in (True, False):
  65. self.assertEqual(None, node1.__strip__(a, b))
  66. self.assertEqual(None, node2.__strip__(a, b))
  67. def test_showtree(self):
  68. """test Template.__showtree__()"""
  69. output = []
  70. getter, marker = object(), object()
  71. get = lambda code: output.append((getter, code))
  72. mark = lambda: output.append(marker)
  73. node1 = Template(wraptext("foobar"))
  74. node2 = Template(wraptext("foo"),
  75. [pgenh("1", "bar"), pgens("abc", "def")])
  76. node1.__showtree__(output.append, get, mark)
  77. node2.__showtree__(output.append, get, mark)
  78. valid = [
  79. "{{", (getter, node1.name), "}}", "{{", (getter, node2.name),
  80. " | ", marker, (getter, node2.params[0].name), " = ", marker,
  81. (getter, node2.params[0].value), " | ", marker,
  82. (getter, node2.params[1].name), " = ", marker,
  83. (getter, node2.params[1].value), "}}"]
  84. self.assertEqual(valid, output)
  85. def test_name(self):
  86. """test getter/setter for the name attribute"""
  87. name = wraptext("foobar")
  88. node1 = Template(name)
  89. node2 = Template(name, [pgenh("1", "bar")])
  90. self.assertIs(name, node1.name)
  91. self.assertIs(name, node2.name)
  92. node1.name = "asdf"
  93. node2.name = "téstïng"
  94. self.assertWikicodeEqual(wraptext("asdf"), node1.name)
  95. self.assertWikicodeEqual(wraptext("téstïng"), node2.name)
  96. def test_params(self):
  97. """test getter for the params attribute"""
  98. node1 = Template(wraptext("foobar"))
  99. plist = [pgenh("1", "bar"), pgens("abc", "def")]
  100. node2 = Template(wraptext("foo"), plist)
  101. self.assertEqual([], node1.params)
  102. self.assertIs(plist, node2.params)
  103. def test_has(self):
  104. """test Template.has()"""
  105. node1 = Template(wraptext("foobar"))
  106. node2 = Template(wraptext("foo"),
  107. [pgenh("1", "bar"), pgens("\nabc ", "def")])
  108. node3 = Template(wraptext("foo"),
  109. [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")])
  110. node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")])
  111. self.assertFalse(node1.has("foobar", False))
  112. self.assertTrue(node2.has(1, False))
  113. self.assertTrue(node2.has("abc", False))
  114. self.assertFalse(node2.has("def", False))
  115. self.assertTrue(node3.has("1", False))
  116. self.assertTrue(node3.has(" b ", False))
  117. self.assertTrue(node4.has("b", False))
  118. self.assertTrue(node3.has("b", True))
  119. self.assertFalse(node4.has("b", True))
  120. self.assertFalse(node1.has_param("foobar", False))
  121. self.assertTrue(node2.has_param(1, False))
  122. def test_get(self):
  123. """test Template.get()"""
  124. node1 = Template(wraptext("foobar"))
  125. node2p1 = pgenh("1", "bar")
  126. node2p2 = pgens("abc", "def")
  127. node2 = Template(wraptext("foo"), [node2p1, node2p2])
  128. node3p1 = pgens("b", "c")
  129. node3p2 = pgens("1", "d")
  130. node3 = Template(wraptext("foo"), [pgenh("1", "a"), node3p1, node3p2])
  131. node4p1 = pgens(" b", " ")
  132. node4 = Template(wraptext("foo"), [pgenh("1", "a"), node4p1])
  133. self.assertRaises(ValueError, node1.get, "foobar")
  134. self.assertIs(node2p1, node2.get(1))
  135. self.assertIs(node2p2, node2.get("abc"))
  136. self.assertRaises(ValueError, node2.get, "def")
  137. self.assertIs(node3p1, node3.get("b"))
  138. self.assertIs(node3p2, node3.get("1"))
  139. self.assertIs(node4p1, node4.get("b "))
  140. def test_add(self):
  141. """test Template.add()"""
  142. node1 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  143. node2 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  144. node3 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  145. node4 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  146. node5 = Template(wraptext("a"), [pgens("b", "c"),
  147. pgens(" d ", "e")])
  148. node6 = Template(wraptext("a"), [pgens("b", "c"), pgens("b", "d"),
  149. pgens("b", "e")])
  150. node7 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  151. node8p = pgenh("1", "d")
  152. node8 = Template(wraptext("a"), [pgens("b", "c"), node8p])
  153. node9 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  154. node10 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "e")])
  155. node11 = Template(wraptext("a"), [pgens("b", "c")])
  156. node12 = Template(wraptext("a"), [pgens("b", "c")])
  157. node13 = Template(wraptext("a"), [
  158. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  159. node14 = Template(wraptext("a\n"), [
  160. pgens("b ", "c\n"), pgens("d ", " e"), pgens("f ", "g\n"),
  161. pgens("h ", " i\n")])
  162. node15 = Template(wraptext("a"), [
  163. pgens("b ", " c\n"), pgens("\nd ", " e"), pgens("\nf ", "g ")])
  164. node16 = Template(wraptext("a"), [
  165. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  166. node17 = Template(wraptext("a"), [pgenh("1", "b")])
  167. node18 = Template(wraptext("a"), [pgenh("1", "b")])
  168. node19 = Template(wraptext("a"), [pgenh("1", "b")])
  169. node20 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  170. pgenh("3", "d"), pgenh("4", "e")])
  171. node21 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  172. pgens("4", "d"), pgens("5", "e")])
  173. node22 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  174. pgens("4", "d"), pgens("5", "e")])
  175. node23 = Template(wraptext("a"), [pgenh("1", "b")])
  176. node24 = Template(wraptext("a"), [pgenh("1", "b")])
  177. node25 = Template(wraptext("a"), [pgens("b", "c")])
  178. node26 = Template(wraptext("a"), [pgenh("1", "b")])
  179. node27 = Template(wraptext("a"), [pgenh("1", "b")])
  180. node28 = Template(wraptext("a"), [pgens("1", "b")])
  181. node29 = Template(wraptext("a"), [
  182. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  183. node30 = Template(wraptext("a\n"), [
  184. pgens("b ", "c\n"), pgens("d ", " e"), pgens("f ", "g\n"),
  185. pgens("h ", " i\n")])
  186. node31 = Template(wraptext("a"), [
  187. pgens("b ", " c\n"), pgens("\nd ", " e"), pgens("\nf ", "g ")])
  188. node32 = Template(wraptext("a"), [
  189. pgens("\nb ", " c "), pgens("\nd ", " e "), pgens("\nf ", " g ")])
  190. node33 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"),
  191. pgens("b", "f"), pgens("b", "h"),
  192. pgens("i", "j")])
  193. node34 = Template(wraptext("a"), [pgens("1", "b"), pgens("x", "y"),
  194. pgens("1", "c"), pgens("2", "d")])
  195. node35 = Template(wraptext("a"), [pgens("1", "b"), pgens("x", "y"),
  196. pgenh("1", "c"), pgenh("2", "d")])
  197. node36 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"),
  198. pgens("f", "g")])
  199. node37 = Template(wraptext("a"), [pgenh("1", "")])
  200. node38 = Template(wraptext("abc"))
  201. node39 = Template(wraptext("a"), [pgenh("1", " b ")])
  202. node40 = Template(wraptext("a"), [pgenh("1", " b"), pgenh("2", " c")])
  203. node41 = Template(wraptext("a"), [pgens("1", " b"), pgens("2", " c")])
  204. node42 = Template(wraptext("a"), [pgens("b", " \n")])
  205. node1.add("e", "f", showkey=True)
  206. node2.add(2, "g", showkey=False)
  207. node3.add("e", "foo|bar", showkey=True)
  208. node4.add("e", "f", showkey=True, before="b")
  209. node5.add("f", "g", showkey=True, before=" d ")
  210. node6.add("f", "g", showkey=True, before="b")
  211. self.assertRaises(ValueError, node7.add, "e", "f", showkey=True,
  212. before="q")
  213. node8.add("e", "f", showkey=True, before=node8p)
  214. node9.add("e", "f", showkey=True, before=pgenh("1", "d"))
  215. self.assertRaises(ValueError, node10.add, "e", "f", showkey=True,
  216. before=pgenh("1", "d"))
  217. node11.add("d", "foo=bar", showkey=True)
  218. node12.add("1", "foo=bar", showkey=False)
  219. node13.add("h", "i", showkey=True)
  220. node14.add("j", "k", showkey=True)
  221. node15.add("h", "i", showkey=True)
  222. node16.add("h", "i", showkey=True, preserve_spacing=False)
  223. node17.add("2", "c")
  224. node18.add("3", "c")
  225. node19.add("c", "d")
  226. node20.add("5", "f")
  227. node21.add("3", "f")
  228. node22.add("6", "f")
  229. node23.add("c", "foo=bar")
  230. node24.add("2", "foo=bar")
  231. node25.add("b", "d")
  232. node26.add("1", "foo=bar")
  233. node27.add("1", "foo=bar", showkey=True)
  234. node28.add("1", "foo=bar", showkey=False)
  235. node29.add("d", "foo")
  236. node30.add("f", "foo")
  237. node31.add("f", "foo")
  238. node32.add("d", "foo", preserve_spacing=False)
  239. node33.add("b", "k")
  240. node34.add("1", "e")
  241. node35.add("1", "e")
  242. node36.add("d", "h", before="b")
  243. node37.add(1, "b")
  244. node38.add("1", "foo")
  245. self.assertRaises(ValueError, node38.add, "z", "bar", showkey=False)
  246. node39.add("1", "c")
  247. node40.add("3", "d")
  248. node41.add("3", "d")
  249. node42.add("b", "hello")
  250. self.assertEqual("{{a|b=c|d|e=f}}", node1)
  251. self.assertEqual("{{a|b=c|d|g}}", node2)
  252. self.assertEqual("{{a|b=c|d|e=foo&#124;bar}}", node3)
  253. self.assertIsInstance(node3.params[2].value.get(1), HTMLEntity)
  254. self.assertEqual("{{a|e=f|b=c|d}}", node4)
  255. self.assertEqual("{{a|b=c|f=g| d =e}}", node5)
  256. self.assertEqual("{{a|b=c|b=d|f=g|b=e}}", node6)
  257. self.assertEqual("{{a|b=c|d}}", node7)
  258. self.assertEqual("{{a|b=c|e=f|d}}", node8)
  259. self.assertEqual("{{a|b=c|e=f|d}}", node9)
  260. self.assertEqual("{{a|b=c|e}}", node10)
  261. self.assertEqual("{{a|b=c|d=foo=bar}}", node11)
  262. self.assertEqual("{{a|b=c|foo&#61;bar}}", node12)
  263. self.assertIsInstance(node12.params[1].value.get(1), HTMLEntity)
  264. self.assertEqual("{{a|\nb = c|\nd = e|\nf = g|\nh = i}}", node13)
  265. self.assertEqual("{{a\n|b =c\n|d = e|f =g\n|h = i\n|j =k\n}}", node14)
  266. self.assertEqual("{{a|b = c\n|\nd = e|\nf =g |h =i}}", node15)
  267. self.assertEqual("{{a|\nb = c|\nd = e|\nf = g|h=i}}", node16)
  268. self.assertEqual("{{a|b|c}}", node17)
  269. self.assertEqual("{{a|b|3=c}}", node18)
  270. self.assertEqual("{{a|b|c=d}}", node19)
  271. self.assertEqual("{{a|b|c|d|e|f}}", node20)
  272. self.assertEqual("{{a|b|c|4=d|5=e|f}}", node21)
  273. self.assertEqual("{{a|b|c|4=d|5=e|6=f}}", node22)
  274. self.assertEqual("{{a|b|c=foo=bar}}", node23)
  275. self.assertEqual("{{a|b|foo&#61;bar}}", node24)
  276. self.assertIsInstance(node24.params[1].value.get(1), HTMLEntity)
  277. self.assertEqual("{{a|b=d}}", node25)
  278. self.assertEqual("{{a|foo&#61;bar}}", node26)
  279. self.assertIsInstance(node26.params[0].value.get(1), HTMLEntity)
  280. self.assertEqual("{{a|1=foo=bar}}", node27)
  281. self.assertEqual("{{a|foo&#61;bar}}", node28)
  282. self.assertIsInstance(node28.params[0].value.get(1), HTMLEntity)
  283. self.assertEqual("{{a|\nb = c|\nd = foo|\nf = g}}", node29)
  284. self.assertEqual("{{a\n|b =c\n|d = e|f =foo\n|h = i\n}}", node30)
  285. self.assertEqual("{{a|b = c\n|\nd = e|\nf =foo }}", node31)
  286. self.assertEqual("{{a|\nb = c |\nd =foo|\nf = g }}", node32)
  287. self.assertEqual("{{a|b=k|d=e|i=j}}", node33)
  288. self.assertEqual("{{a|1=e|x=y|2=d}}", node34)
  289. self.assertEqual("{{a|x=y|e|d}}", node35)
  290. self.assertEqual("{{a|b=c|d=h|f=g}}", node36)
  291. self.assertEqual("{{a|b}}", node37)
  292. self.assertEqual("{{abc|foo}}", node38)
  293. self.assertEqual("{{a|c}}", node39)
  294. self.assertEqual("{{a| b| c|d}}", node40)
  295. self.assertEqual("{{a|1= b|2= c|3= d}}", node41)
  296. self.assertEqual("{{a|b=hello \n}}", node42)
  297. def test_remove(self):
  298. """test Template.remove()"""
  299. node1 = Template(wraptext("foobar"))
  300. node2 = Template(wraptext("foo"),
  301. [pgenh("1", "bar"), pgens("abc", "def")])
  302. node3 = Template(wraptext("foo"),
  303. [pgenh("1", "bar"), pgens("abc", "def")])
  304. node4 = Template(wraptext("foo"),
  305. [pgenh("1", "bar"), pgenh("2", "baz")])
  306. node5 = Template(wraptext("foo"), [
  307. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  308. node6 = Template(wraptext("foo"), [
  309. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  310. node7 = Template(wraptext("foo"), [
  311. pgens("1 ", "a"), pgens(" 1", "b"), pgens("2", "c")])
  312. node8 = Template(wraptext("foo"), [
  313. pgens("1 ", "a"), pgens(" 1", "b"), pgens("2", "c")])
  314. node9 = Template(wraptext("foo"), [
  315. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  316. node10 = Template(wraptext("foo"), [
  317. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  318. node11 = Template(wraptext("foo"), [
  319. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  320. node12 = Template(wraptext("foo"), [
  321. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  322. node13 = Template(wraptext("foo"), [
  323. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  324. node14 = Template(wraptext("foo"), [
  325. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  326. node15 = Template(wraptext("foo"), [
  327. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  328. node16 = Template(wraptext("foo"), [
  329. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  330. node17 = Template(wraptext("foo"), [
  331. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  332. node18 = Template(wraptext("foo"), [
  333. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  334. node19 = Template(wraptext("foo"), [
  335. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  336. node20 = Template(wraptext("foo"), [
  337. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  338. node21 = Template(wraptext("foo"), [
  339. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  340. pgens("a", "b")])
  341. node22 = Template(wraptext("foo"), [
  342. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  343. pgens("a", "b")])
  344. node23 = Template(wraptext("foo"), [
  345. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  346. pgens("a", "b")])
  347. node24 = Template(wraptext("foo"), [
  348. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  349. pgens("a", "b")])
  350. node25 = Template(wraptext("foo"), [
  351. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  352. pgens("a", "b")])
  353. node26 = Template(wraptext("foo"), [
  354. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  355. pgens("a", "b")])
  356. node27 = Template(wraptext("foo"), [pgenh("1", "bar")])
  357. node28 = Template(wraptext("foo"), [pgenh("1", "bar")])
  358. node2.remove("1")
  359. node2.remove("abc")
  360. node3.remove(1, keep_field=True)
  361. node3.remove("abc", keep_field=True)
  362. node4.remove("1", keep_field=False)
  363. node5.remove("a", keep_field=False)
  364. node6.remove("a", keep_field=True)
  365. node7.remove(1, keep_field=True)
  366. node8.remove(1, keep_field=False)
  367. node9.remove(1, keep_field=True)
  368. node10.remove(1, keep_field=False)
  369. node11.remove(node11.params[0], keep_field=False)
  370. node12.remove(node12.params[0], keep_field=True)
  371. node13.remove(node13.params[1], keep_field=False)
  372. node14.remove(node14.params[1], keep_field=True)
  373. node15.remove(node15.params[2], keep_field=False)
  374. node16.remove(node16.params[2], keep_field=True)
  375. node17.remove(node17.params[0], keep_field=False)
  376. node18.remove(node18.params[0], keep_field=True)
  377. node19.remove(node19.params[1], keep_field=False)
  378. node20.remove(node20.params[1], keep_field=True)
  379. node21.remove("a", keep_field=False)
  380. node22.remove("a", keep_field=True)
  381. node23.remove(node23.params[0], keep_field=False)
  382. node24.remove(node24.params[0], keep_field=True)
  383. node25.remove(node25.params[3], keep_field=False)
  384. node26.remove(node26.params[3], keep_field=True)
  385. self.assertRaises(ValueError, node1.remove, 1)
  386. self.assertRaises(ValueError, node1.remove, "a")
  387. self.assertRaises(ValueError, node2.remove, "1")
  388. self.assertEqual("{{foo}}", node2)
  389. self.assertEqual("{{foo||abc=}}", node3)
  390. self.assertEqual("{{foo|2=baz}}", node4)
  391. self.assertEqual("{{foo|b=c}}", node5)
  392. self.assertEqual("{{foo| a=|b=c}}", node6)
  393. self.assertEqual("{{foo|1 =|2=c}}", node7)
  394. self.assertEqual("{{foo|2=c}}", node8)
  395. self.assertEqual("{{foo||c}}", node9)
  396. self.assertEqual("{{foo|2=c}}", node10)
  397. self.assertEqual("{{foo|b=c|a =d}}", node11)
  398. self.assertEqual("{{foo| a=|b=c|a =d}}", node12)
  399. self.assertEqual("{{foo| a=b|a =d}}", node13)
  400. self.assertEqual("{{foo| a=b|b=|a =d}}", node14)
  401. self.assertEqual("{{foo| a=b|b=c}}", node15)
  402. self.assertEqual("{{foo| a=b|b=c|a =}}", node16)
  403. self.assertEqual("{{foo|b|c}}", node17)
  404. self.assertEqual("{{foo|1 =|b|c}}", node18)
  405. self.assertEqual("{{foo|1 =a|2=c}}", node19)
  406. self.assertEqual("{{foo|1 =a||c}}", node20)
  407. self.assertEqual("{{foo|c=d|e=f}}", node21)
  408. self.assertEqual("{{foo|a=|c=d|e=f}}", node22)
  409. self.assertEqual("{{foo|c=d|e=f|a=b|a=b}}", node23)
  410. self.assertEqual("{{foo|a=|c=d|e=f|a=b|a=b}}", node24)
  411. self.assertEqual("{{foo|a=b|c=d|e=f|a=b}}", node25)
  412. self.assertEqual("{{foo|a=b|c=d|e=f|a=|a=b}}", node26)
  413. self.assertRaises(ValueError, node27.remove, node28.get(1))
  414. if __name__ == "__main__":
  415. unittest.main(verbosity=2)