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.
 
 
 
 

607 line
26 KiB

  1. #
  2. # Copyright (C) 2012-2017 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. from difflib import unified_diff
  22. import unittest
  23. from mwparserfromhell.nodes import HTMLEntity, Template, Text
  24. from mwparserfromhell.nodes.extras import Parameter
  25. from mwparserfromhell import parse
  26. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  27. pgens = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=True)
  28. pgenh = lambda k, v: Parameter(wraptext(k), wraptext(v), showkey=False)
  29. class TestTemplate(TreeEqualityTestCase):
  30. """Test cases for the Template node."""
  31. def test_unicode(self):
  32. """test Template.__unicode__()"""
  33. node = Template(wraptext("foobar"))
  34. self.assertEqual("{{foobar}}", str(node))
  35. node2 = Template(wraptext("foo"),
  36. [pgenh("1", "bar"), pgens("abc", "def")])
  37. self.assertEqual("{{foo|bar|abc=def}}", str(node2))
  38. def test_children(self):
  39. """test Template.__children__()"""
  40. node2p1 = Parameter(wraptext("1"), wraptext("bar"), showkey=False)
  41. node2p2 = Parameter(wraptext("abc"), wrap([Text("def"), Text("ghi")]),
  42. showkey=True)
  43. node1 = Template(wraptext("foobar"))
  44. node2 = Template(wraptext("foo"), [node2p1, node2p2])
  45. gen1 = node1.__children__()
  46. gen2 = node2.__children__()
  47. self.assertEqual(node1.name, next(gen1))
  48. self.assertEqual(node2.name, next(gen2))
  49. self.assertEqual(node2.params[0].value, next(gen2))
  50. self.assertEqual(node2.params[1].name, next(gen2))
  51. self.assertEqual(node2.params[1].value, next(gen2))
  52. self.assertRaises(StopIteration, next, gen1)
  53. self.assertRaises(StopIteration, next, gen2)
  54. def test_strip(self):
  55. """test Template.__strip__()"""
  56. node1 = Template(wraptext("foobar"))
  57. node2 = Template(wraptext("foo"), [
  58. pgenh("1", "bar"), pgens("foo", ""), pgens("abc", "def")])
  59. node3 = Template(wraptext("foo"), [
  60. pgenh("1", "foo"),
  61. Parameter(wraptext("2"), wrap([Template(wraptext("hello"))]),
  62. showkey=False),
  63. pgenh("3", "bar")])
  64. self.assertEqual(None, node1.__strip__(keep_template_params=False))
  65. self.assertEqual(None, node2.__strip__(keep_template_params=False))
  66. self.assertEqual("", node1.__strip__(keep_template_params=True))
  67. self.assertEqual("bar def", node2.__strip__(keep_template_params=True))
  68. self.assertEqual("foo bar", node3.__strip__(keep_template_params=True))
  69. def test_showtree(self):
  70. """test Template.__showtree__()"""
  71. output = []
  72. getter, marker = object(), object()
  73. get = lambda code: output.append((getter, code))
  74. mark = lambda: output.append(marker)
  75. node1 = Template(wraptext("foobar"))
  76. node2 = Template(wraptext("foo"),
  77. [pgenh("1", "bar"), pgens("abc", "def")])
  78. node1.__showtree__(output.append, get, mark)
  79. node2.__showtree__(output.append, get, mark)
  80. valid = [
  81. "{{", (getter, node1.name), "}}", "{{", (getter, node2.name),
  82. " | ", marker, (getter, node2.params[0].name), " = ", marker,
  83. (getter, node2.params[0].value), " | ", marker,
  84. (getter, node2.params[1].name), " = ", marker,
  85. (getter, node2.params[1].value), "}}"]
  86. self.assertEqual(valid, output)
  87. def test_name(self):
  88. """test getter/setter for the name attribute"""
  89. name = wraptext("foobar")
  90. node1 = Template(name)
  91. node2 = Template(name, [pgenh("1", "bar")])
  92. self.assertIs(name, node1.name)
  93. self.assertIs(name, node2.name)
  94. node1.name = "asdf"
  95. node2.name = "téstïng"
  96. self.assertWikicodeEqual(wraptext("asdf"), node1.name)
  97. self.assertWikicodeEqual(wraptext("téstïng"), node2.name)
  98. def test_params(self):
  99. """test getter for the params attribute"""
  100. node1 = Template(wraptext("foobar"))
  101. plist = [pgenh("1", "bar"), pgens("abc", "def")]
  102. node2 = Template(wraptext("foo"), plist)
  103. self.assertEqual([], node1.params)
  104. self.assertIs(plist, node2.params)
  105. def test_has(self):
  106. """test Template.has()"""
  107. node1 = Template(wraptext("foobar"))
  108. node2 = Template(wraptext("foo"),
  109. [pgenh("1", "bar"), pgens("\nabc ", "def")])
  110. node3 = Template(wraptext("foo"),
  111. [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")])
  112. node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")])
  113. self.assertFalse(node1.has("foobar", False))
  114. self.assertTrue(node2.has(1, False))
  115. self.assertTrue(node2.has("abc", False))
  116. self.assertFalse(node2.has("def", False))
  117. self.assertTrue(node3.has("1", False))
  118. self.assertTrue(node3.has(" b ", False))
  119. self.assertTrue(node4.has("b", False))
  120. self.assertTrue(node3.has("b", True))
  121. self.assertFalse(node4.has("b", True))
  122. self.assertFalse(node1.has_param("foobar", False))
  123. self.assertTrue(node2.has_param(1, False))
  124. def test_get(self):
  125. """test Template.get()"""
  126. node1 = Template(wraptext("foobar"))
  127. node2p1 = pgenh("1", "bar")
  128. node2p2 = pgens("abc", "def")
  129. node2 = Template(wraptext("foo"), [node2p1, node2p2])
  130. node3p1 = pgens("b", "c")
  131. node3p2 = pgens("1", "d")
  132. node3 = Template(wraptext("foo"), [pgenh("1", "a"), node3p1, node3p2])
  133. node4p1 = pgens(" b", " ")
  134. node4 = Template(wraptext("foo"), [pgenh("1", "a"), node4p1])
  135. self.assertRaises(ValueError, node1.get, "foobar")
  136. self.assertIs(node2p1, node2.get(1))
  137. self.assertIs(node2p2, node2.get("abc"))
  138. self.assertRaises(ValueError, node2.get, "def")
  139. self.assertIs(node3p1, node3.get("b"))
  140. self.assertIs(node3p2, node3.get("1"))
  141. self.assertIs(node4p1, node4.get("b "))
  142. def test_add(self):
  143. """test Template.add()"""
  144. node1 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  145. node2 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  146. node3 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  147. node4 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  148. node5 = Template(wraptext("a"), [pgens("b", "c"),
  149. pgens(" d ", "e")])
  150. node6 = Template(wraptext("a"), [pgens("b", "c"), pgens("b", "d"),
  151. pgens("b", "e")])
  152. node7 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  153. node8p = pgenh("1", "d")
  154. node8 = Template(wraptext("a"), [pgens("b", "c"), node8p])
  155. node9 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "d")])
  156. node10 = Template(wraptext("a"), [pgens("b", "c"), pgenh("1", "e")])
  157. node11 = Template(wraptext("a"), [pgens("b", "c")])
  158. node12 = Template(wraptext("a"), [pgens("b", "c")])
  159. node13 = Template(wraptext("a"), [
  160. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  161. node14 = Template(wraptext("a\n"), [
  162. pgens("b ", "c\n"), pgens("d ", " e"), pgens("f ", "g\n"),
  163. pgens("h ", " i\n")])
  164. node15 = Template(wraptext("a"), [
  165. pgens("b ", " c\n"), pgens("\nd ", " e"), pgens("\nf ", "g ")])
  166. node16 = Template(wraptext("a"), [
  167. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  168. node17 = Template(wraptext("a"), [pgenh("1", "b")])
  169. node18 = Template(wraptext("a"), [pgenh("1", "b")])
  170. node19 = Template(wraptext("a"), [pgenh("1", "b")])
  171. node20 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  172. pgenh("3", "d"), pgenh("4", "e")])
  173. node21 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  174. pgens("4", "d"), pgens("5", "e")])
  175. node22 = Template(wraptext("a"), [pgenh("1", "b"), pgenh("2", "c"),
  176. pgens("4", "d"), pgens("5", "e")])
  177. node23 = Template(wraptext("a"), [pgenh("1", "b")])
  178. node24 = Template(wraptext("a"), [pgenh("1", "b")])
  179. node25 = Template(wraptext("a"), [pgens("b", "c")])
  180. node26 = Template(wraptext("a"), [pgenh("1", "b")])
  181. node27 = Template(wraptext("a"), [pgenh("1", "b")])
  182. node28 = Template(wraptext("a"), [pgens("1", "b")])
  183. node29 = Template(wraptext("a"), [
  184. pgens("\nb ", " c"), pgens("\nd ", " e"), pgens("\nf ", " g")])
  185. node30 = Template(wraptext("a\n"), [
  186. pgens("b ", "c\n"), pgens("d ", " e"), pgens("f ", "g\n"),
  187. pgens("h ", " i\n")])
  188. node31 = Template(wraptext("a"), [
  189. pgens("b ", " c\n"), pgens("\nd ", " e"), pgens("\nf ", "g ")])
  190. node32 = Template(wraptext("a"), [
  191. pgens("\nb ", " c "), pgens("\nd ", " e "), pgens("\nf ", " g ")])
  192. node33 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"),
  193. pgens("b", "f"), pgens("b", "h"),
  194. pgens("i", "j")])
  195. node34 = Template(wraptext("a"), [pgens("1", "b"), pgens("x", "y"),
  196. pgens("1", "c"), pgens("2", "d")])
  197. node35 = Template(wraptext("a"), [pgens("1", "b"), pgens("x", "y"),
  198. pgenh("1", "c"), pgenh("2", "d")])
  199. node36 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"),
  200. pgens("f", "g")])
  201. node37 = Template(wraptext("a"), [pgenh("1", "")])
  202. node38 = Template(wraptext("abc"))
  203. node39 = Template(wraptext("a"), [pgenh("1", " b ")])
  204. node40 = Template(wraptext("a"), [pgenh("1", " b"), pgenh("2", " c")])
  205. node41 = Template(wraptext("a"), [pgens("1", " b"), pgens("2", " c")])
  206. node42 = Template(wraptext("a"), [pgens("b", " \n")])
  207. node1.add("e", "f", showkey=True)
  208. node2.add(2, "g", showkey=False)
  209. node3.add("e", "foo|bar", showkey=True)
  210. node4.add("e", "f", showkey=True, before="b")
  211. node5.add("f", "g", showkey=True, before=" d ")
  212. node6.add("f", "g", showkey=True, before="b")
  213. self.assertRaises(ValueError, node7.add, "e", "f", showkey=True,
  214. before="q")
  215. node8.add("e", "f", showkey=True, before=node8p)
  216. node9.add("e", "f", showkey=True, before=pgenh("1", "d"))
  217. self.assertRaises(ValueError, node10.add, "e", "f", showkey=True,
  218. before=pgenh("1", "d"))
  219. node11.add("d", "foo=bar", showkey=True)
  220. node12.add("1", "foo=bar", showkey=False)
  221. node13.add("h", "i", showkey=True)
  222. node14.add("j", "k", showkey=True)
  223. node15.add("h", "i", showkey=True)
  224. node16.add("h", "i", showkey=True, preserve_spacing=False)
  225. node17.add("2", "c")
  226. node18.add("3", "c")
  227. node19.add("c", "d")
  228. node20.add("5", "f")
  229. node21.add("3", "f")
  230. node22.add("6", "f")
  231. node23.add("c", "foo=bar")
  232. node24.add("2", "foo=bar")
  233. node25.add("b", "d")
  234. node26.add("1", "foo=bar")
  235. node27.add("1", "foo=bar", showkey=True)
  236. node28.add("1", "foo=bar", showkey=False)
  237. node29.add("d", "foo")
  238. node30.add("f", "foo")
  239. node31.add("f", "foo")
  240. node32.add("d", "foo", preserve_spacing=False)
  241. node33.add("b", "k")
  242. node34.add("1", "e")
  243. node35.add("1", "e")
  244. node36.add("d", "h", before="b")
  245. node37.add(1, "b")
  246. node38.add("1", "foo")
  247. self.assertRaises(ValueError, node38.add, "z", "bar", showkey=False)
  248. node39.add("1", "c")
  249. node40.add("3", "d")
  250. node41.add("3", "d")
  251. node42.add("b", "hello")
  252. self.assertEqual("{{a|b=c|d|e=f}}", node1)
  253. self.assertEqual("{{a|b=c|d|g}}", node2)
  254. self.assertEqual("{{a|b=c|d|e=foo&#124;bar}}", node3)
  255. self.assertIsInstance(node3.params[2].value.get(1), HTMLEntity)
  256. self.assertEqual("{{a|e=f|b=c|d}}", node4)
  257. self.assertEqual("{{a|b=c|f=g| d =e}}", node5)
  258. self.assertEqual("{{a|b=c|b=d|f=g|b=e}}", node6)
  259. self.assertEqual("{{a|b=c|d}}", node7)
  260. self.assertEqual("{{a|b=c|e=f|d}}", node8)
  261. self.assertEqual("{{a|b=c|e=f|d}}", node9)
  262. self.assertEqual("{{a|b=c|e}}", node10)
  263. self.assertEqual("{{a|b=c|d=foo=bar}}", node11)
  264. self.assertEqual("{{a|b=c|foo&#61;bar}}", node12)
  265. self.assertIsInstance(node12.params[1].value.get(1), HTMLEntity)
  266. self.assertEqual("{{a|\nb = c|\nd = e|\nf = g|\nh = i}}", node13)
  267. self.assertEqual("{{a\n|b =c\n|d = e|f =g\n|h = i\n|j =k\n}}", node14)
  268. self.assertEqual("{{a|b = c\n|\nd = e|\nf =g |\nh = i}}", node15)
  269. self.assertEqual("{{a|\nb = c|\nd = e|\nf = g|h=i}}", node16)
  270. self.assertEqual("{{a|b|c}}", node17)
  271. self.assertEqual("{{a|b|3=c}}", node18)
  272. self.assertEqual("{{a|b|c=d}}", node19)
  273. self.assertEqual("{{a|b|c|d|e|f}}", node20)
  274. self.assertEqual("{{a|b|c|4=d|5=e|f}}", node21)
  275. self.assertEqual("{{a|b|c|4=d|5=e|6=f}}", node22)
  276. self.assertEqual("{{a|b|c=foo=bar}}", node23)
  277. self.assertEqual("{{a|b|foo&#61;bar}}", node24)
  278. self.assertIsInstance(node24.params[1].value.get(1), HTMLEntity)
  279. self.assertEqual("{{a|b=d}}", node25)
  280. self.assertEqual("{{a|foo&#61;bar}}", node26)
  281. self.assertIsInstance(node26.params[0].value.get(1), HTMLEntity)
  282. self.assertEqual("{{a|1=foo=bar}}", node27)
  283. self.assertEqual("{{a|foo&#61;bar}}", node28)
  284. self.assertIsInstance(node28.params[0].value.get(1), HTMLEntity)
  285. self.assertEqual("{{a|\nb = c|\nd = foo|\nf = g}}", node29)
  286. self.assertEqual("{{a\n|b =c\n|d = e|f =foo\n|h = i\n}}", node30)
  287. self.assertEqual("{{a|b = c\n|\nd = e|\nf =foo }}", node31)
  288. self.assertEqual("{{a|\nb = c |\nd =foo|\nf = g }}", node32)
  289. self.assertEqual("{{a|b=k|d=e|i=j}}", node33)
  290. self.assertEqual("{{a|1=e|x=y|2=d}}", node34)
  291. self.assertEqual("{{a|x=y|e|d}}", node35)
  292. self.assertEqual("{{a|b=c|d=h|f=g}}", node36)
  293. self.assertEqual("{{a|b}}", node37)
  294. self.assertEqual("{{abc|foo}}", node38)
  295. self.assertEqual("{{a|c}}", node39)
  296. self.assertEqual("{{a| b| c|d}}", node40)
  297. self.assertEqual("{{a|1= b|2= c|3= d}}", node41)
  298. self.assertEqual("{{a|b=hello \n}}", node42)
  299. def test_remove(self):
  300. """test Template.remove()"""
  301. node1 = Template(wraptext("foobar"))
  302. node2 = Template(wraptext("foo"),
  303. [pgenh("1", "bar"), pgens("abc", "def")])
  304. node3 = Template(wraptext("foo"),
  305. [pgenh("1", "bar"), pgens("abc", "def")])
  306. node4 = Template(wraptext("foo"),
  307. [pgenh("1", "bar"), 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. node11 = Template(wraptext("foo"), [
  321. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  322. node12 = Template(wraptext("foo"), [
  323. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  324. node13 = Template(wraptext("foo"), [
  325. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  326. node14 = Template(wraptext("foo"), [
  327. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  328. node15 = Template(wraptext("foo"), [
  329. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  330. node16 = Template(wraptext("foo"), [
  331. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  332. node17 = Template(wraptext("foo"), [
  333. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  334. node18 = Template(wraptext("foo"), [
  335. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  336. node19 = Template(wraptext("foo"), [
  337. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  338. node20 = Template(wraptext("foo"), [
  339. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  340. node21 = Template(wraptext("foo"), [
  341. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  342. pgens("a", "b")])
  343. node22 = Template(wraptext("foo"), [
  344. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  345. pgens("a", "b")])
  346. node23 = Template(wraptext("foo"), [
  347. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  348. pgens("a", "b")])
  349. node24 = Template(wraptext("foo"), [
  350. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  351. pgens("a", "b")])
  352. node25 = Template(wraptext("foo"), [
  353. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  354. pgens("a", "b")])
  355. node26 = Template(wraptext("foo"), [
  356. pgens("a", "b"), pgens("c", "d"), pgens("e", "f"), pgens("a", "b"),
  357. pgens("a", "b")])
  358. node27 = Template(wraptext("foo"), [pgenh("1", "bar")])
  359. node28 = Template(wraptext("foo"), [pgenh("1", "bar")])
  360. node2.remove("1")
  361. node2.remove("abc")
  362. node3.remove(1, keep_field=True)
  363. node3.remove("abc", keep_field=True)
  364. node4.remove("1", keep_field=False)
  365. node5.remove("a", keep_field=False)
  366. node6.remove("a", keep_field=True)
  367. node7.remove(1, keep_field=True)
  368. node8.remove(1, keep_field=False)
  369. node9.remove(1, keep_field=True)
  370. node10.remove(1, keep_field=False)
  371. node11.remove(node11.params[0], keep_field=False)
  372. node12.remove(node12.params[0], keep_field=True)
  373. node13.remove(node13.params[1], keep_field=False)
  374. node14.remove(node14.params[1], keep_field=True)
  375. node15.remove(node15.params[2], keep_field=False)
  376. node16.remove(node16.params[2], keep_field=True)
  377. node17.remove(node17.params[0], keep_field=False)
  378. node18.remove(node18.params[0], keep_field=True)
  379. node19.remove(node19.params[1], keep_field=False)
  380. node20.remove(node20.params[1], keep_field=True)
  381. node21.remove("a", keep_field=False)
  382. node22.remove("a", keep_field=True)
  383. node23.remove(node23.params[0], keep_field=False)
  384. node24.remove(node24.params[0], keep_field=True)
  385. node25.remove(node25.params[3], keep_field=False)
  386. node26.remove(node26.params[3], keep_field=True)
  387. self.assertRaises(ValueError, node1.remove, 1)
  388. self.assertRaises(ValueError, node1.remove, "a")
  389. self.assertRaises(ValueError, node2.remove, "1")
  390. self.assertEqual("{{foo}}", node2)
  391. self.assertEqual("{{foo||abc=}}", node3)
  392. self.assertEqual("{{foo|2=baz}}", node4)
  393. self.assertEqual("{{foo|b=c}}", node5)
  394. self.assertEqual("{{foo| a=|b=c}}", node6)
  395. self.assertEqual("{{foo|1 =|2=c}}", node7)
  396. self.assertEqual("{{foo|2=c}}", node8)
  397. self.assertEqual("{{foo||c}}", node9)
  398. self.assertEqual("{{foo|2=c}}", node10)
  399. self.assertEqual("{{foo|b=c|a =d}}", node11)
  400. self.assertEqual("{{foo| a=|b=c|a =d}}", node12)
  401. self.assertEqual("{{foo| a=b|a =d}}", node13)
  402. self.assertEqual("{{foo| a=b|b=|a =d}}", node14)
  403. self.assertEqual("{{foo| a=b|b=c}}", node15)
  404. self.assertEqual("{{foo| a=b|b=c|a =}}", node16)
  405. self.assertEqual("{{foo|b|c}}", node17)
  406. self.assertEqual("{{foo|1 =|b|c}}", node18)
  407. self.assertEqual("{{foo|1 =a|2=c}}", node19)
  408. self.assertEqual("{{foo|1 =a||c}}", node20)
  409. self.assertEqual("{{foo|c=d|e=f}}", node21)
  410. self.assertEqual("{{foo|a=|c=d|e=f}}", node22)
  411. self.assertEqual("{{foo|c=d|e=f|a=b|a=b}}", node23)
  412. self.assertEqual("{{foo|a=|c=d|e=f|a=b|a=b}}", node24)
  413. self.assertEqual("{{foo|a=b|c=d|e=f|a=b}}", node25)
  414. self.assertEqual("{{foo|a=b|c=d|e=f|a=|a=b}}", node26)
  415. self.assertRaises(ValueError, node27.remove, node28.get(1))
  416. def test_formatting(self):
  417. """test realistic param manipulation with complex whitespace formatting
  418. (assumes that parsing works correctly)"""
  419. tests = [
  420. # https://en.wikipedia.org/w/index.php?title=Lamar_County,_Georgia&oldid=792356004
  421. ("""{{Infobox U.S. county
  422. | county = Lamar County
  423. | state = Georgia
  424. | seal =
  425. | founded = 1920
  426. | seat wl = Barnesville
  427. | largest city wl = Barnesville
  428. | area_total_sq_mi = 186
  429. | area_land_sq_mi = 184
  430. | area_water_sq_mi = 2.3
  431. | area percentage = 1.3%
  432. | census yr = 2010
  433. | pop = 18317
  434. | density_sq_mi = 100
  435. | time zone = Eastern
  436. | footnotes =
  437. | web = www.lamarcountyga.com
  438. | ex image = Lamar County Georgia Courthouse.jpg
  439. | ex image cap = Lamar County courthouse in Barnesville
  440. | district = 3rd
  441. | named for = [[Lucius Quintus Cincinnatus Lamar II]]
  442. }}""",
  443. """@@ -11,4 +11,4 @@
  444. | area percentage = 1.3%
  445. -| census yr = 2010
  446. -| pop = 18317
  447. +| census estimate yr = 2016
  448. +| pop = 12345<ref>example ref</ref>
  449. | density_sq_mi = 100"""),
  450. # https://en.wikipedia.org/w/index.php?title=Rockdale_County,_Georgia&oldid=792359760
  451. ("""{{Infobox U.S. County|
  452. county = Rockdale County |
  453. state = Georgia |
  454. seal = |
  455. founded = October 18, 1870 |
  456. seat wl = Conyers |
  457. largest city wl = Conyers |
  458. area_total_sq_mi = 132 |
  459. area_land_sq_mi = 130 |
  460. area_water_sq_mi = 2.3 |
  461. area percentage = 1.7% |
  462. census yr = 2010|
  463. pop = 85215 |
  464. density_sq_mi = 657 |
  465. web = www.rockdalecounty.org
  466. | ex image = Rockdale-county-courthouse.jpg
  467. | ex image cap = Rockdale County Courthouse in Conyers
  468. | district = 4th
  469. | time zone= Eastern
  470. }}""",
  471. """@@ -11,4 +11,4 @@
  472. area percentage = 1.7% |
  473. - census yr = 2010|
  474. - pop = 85215 |
  475. + census estimate yr = 2016 |
  476. + pop = 12345<ref>example ref</ref> |
  477. density_sq_mi = 657 |"""),
  478. # https://en.wikipedia.org/w/index.php?title=Spalding_County,_Georgia&oldid=792360413
  479. ("""{{Infobox U.S. County|
  480. | county = Spalding County |
  481. | state = Georgia |
  482. | seal = |
  483. | founded = 1851 |
  484. | seat wl = Griffin |
  485. | largest city wl = Griffin |
  486. | area_total_sq_mi = 200 |
  487. | area_land_sq_mi = 196 |
  488. | area_water_sq_mi = 3.1 |
  489. | area percentage = 1.6% |
  490. | census yr = 2010|
  491. | pop = 64073 |
  492. | density_sq_mi = 326 |
  493. | web = www.spaldingcounty.com |
  494. | named for = [[Thomas Spalding]]
  495. | ex image = Spalding County Courthouse (NE corner).JPG
  496. | ex image cap = Spalding County Courthouse in Griffin
  497. | district = 3rd
  498. | time zone = Eastern
  499. }}""",
  500. """@@ -11,4 +11,4 @@
  501. | area percentage = 1.6% |
  502. -| census yr = 2010|
  503. -| pop = 64073 |
  504. +|
  505. +| census estimate yr = 2016 | pop = 12345<ref>example ref</ref> |
  506. | density_sq_mi = 326 |"""),
  507. # https://en.wikipedia.org/w/index.php?title=Clinton_County,_Illinois&oldid=794694648
  508. ("""{{Infobox U.S. county
  509. |county = Clinton County
  510. |state = Illinois
  511. | ex image = File:Clinton County Courthouse, Carlyle.jpg
  512. | ex image cap = [[Clinton County Courthouse (Illinois)|Clinton County Courthouse]]
  513. |seal =
  514. |founded = 1824
  515. |named for = [[DeWitt Clinton]]
  516. |seat wl= Carlyle
  517. | largest city wl = Breese
  518. |time zone=Central
  519. |area_total_sq_mi = 503
  520. |area_land_sq_mi = 474
  521. |area_water_sq_mi = 29
  522. |area percentage = 5.8%
  523. |census yr = 2010
  524. |pop = 37762
  525. |density_sq_mi = 80
  526. |web = www.clintonco.illinois.gov
  527. | district = 15th
  528. }}""",
  529. """@@ -15,4 +15,4 @@
  530. |area percentage = 5.8%
  531. - |census yr = 2010
  532. - |pop = 37762
  533. + |census estimate yr = 2016
  534. + |pop = 12345<ref>example ref</ref>
  535. |density_sq_mi = 80"""),
  536. # https://en.wikipedia.org/w/index.php?title=Winnebago_County,_Illinois&oldid=789193800
  537. ("""{{Infobox U.S. county |
  538. county = Winnebago County |
  539. state = Illinois |
  540. seal = Winnebago County il seal.png |
  541. named for = [[Winnebago (tribe)|Winnebago Tribe]] |
  542. seat wl= Rockford |
  543. largest city wl = Rockford|
  544. area_total_sq_mi = 519 |
  545. area_land_sq_mi = 513|
  546. area_water_sq_mi = 5.9 |
  547. area percentage = 1.1% |
  548. census yr = 2010|
  549. pop = 295266 |
  550. density_sq_mi = 575
  551. | web = www.wincoil.us
  552. | founded year = 1836
  553. | founded date = January 16
  554. | time zone = Central
  555. | district = 16th
  556. | district2 = 17th
  557. }}""",
  558. """@@ -11,4 +11,4 @@
  559. area percentage = 1.1% |
  560. - census yr = 2010|
  561. - pop = 295266 |
  562. + census estimate yr = 2016|
  563. + pop = 12345<ref>example ref</ref> |
  564. density_sq_mi = 575""")]
  565. for (original, expected) in tests:
  566. code = parse(original)
  567. template = code.filter_templates()[0]
  568. template.add("pop", "12345<ref>example ref</ref>")
  569. template.add('census estimate yr', "2016", before="pop")
  570. template.remove("census yr")
  571. oldlines = original.splitlines(True)
  572. newlines = str(code).splitlines(True)
  573. difflines = unified_diff(oldlines, newlines, n=1)
  574. diff = "".join(list(difflines)[2:]).strip()
  575. self.assertEqual(expected, diff)
  576. if __name__ == "__main__":
  577. unittest.main(verbosity=2)