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.
 
 
 
 

606 satır
26 KiB

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