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.
 
 
 
 

522 lines
25 KiB

  1. #
  2. # Copyright (C) 2012-2020 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 functools import partial
  22. import re
  23. from types import GeneratorType
  24. import unittest
  25. from mwparserfromhell.nodes import Argument, Heading, Template, Text
  26. from mwparserfromhell.smart_list import SmartList
  27. from mwparserfromhell.wikicode import Wikicode
  28. from mwparserfromhell import parse
  29. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  30. class TestWikicode(TreeEqualityTestCase):
  31. """Tests for the Wikicode class, which manages a list of nodes."""
  32. def test_str(self):
  33. """test Wikicode.__str__()"""
  34. code1 = parse("foobar")
  35. code2 = parse("Have a {{template}} and a [[page|link]]")
  36. self.assertEqual("foobar", str(code1))
  37. self.assertEqual("Have a {{template}} and a [[page|link]]", str(code2))
  38. def test_nodes(self):
  39. """test getter/setter for the nodes attribute"""
  40. code = parse("Have a {{template}}")
  41. self.assertEqual(["Have a ", "{{template}}"], code.nodes)
  42. L1 = SmartList([Text("foobar"), Template(wraptext("abc"))])
  43. L2 = [Text("barfoo"), Template(wraptext("cba"))]
  44. L3 = "abc{{def}}"
  45. code.nodes = L1
  46. self.assertIs(L1, code.nodes)
  47. code.nodes = L2
  48. self.assertIs(L2, code.nodes)
  49. code.nodes = L3
  50. self.assertEqual(["abc", "{{def}}"], code.nodes)
  51. self.assertRaises(ValueError, setattr, code, "nodes", object)
  52. def test_get(self):
  53. """test Wikicode.get()"""
  54. code = parse("Have a {{template}} and a [[page|link]]")
  55. self.assertIs(code.nodes[0], code.get(0))
  56. self.assertIs(code.nodes[2], code.get(2))
  57. self.assertRaises(IndexError, code.get, 4)
  58. def test_set(self):
  59. """test Wikicode.set()"""
  60. code = parse("Have a {{template}} and a [[page|link]]")
  61. code.set(1, "{{{argument}}}")
  62. self.assertEqual("Have a {{{argument}}} and a [[page|link]]", code)
  63. self.assertIsInstance(code.get(1), Argument)
  64. code.set(2, None)
  65. self.assertEqual("Have a {{{argument}}}[[page|link]]", code)
  66. code.set(-3, "This is an ")
  67. self.assertEqual("This is an {{{argument}}}[[page|link]]", code)
  68. self.assertRaises(ValueError, code.set, 1, "foo {{bar}}")
  69. self.assertRaises(IndexError, code.set, 3, "{{baz}}")
  70. self.assertRaises(IndexError, code.set, -4, "{{baz}}")
  71. def test_contains(self):
  72. """test Wikicode.contains()"""
  73. code = parse("Here is {{aaa|{{bbb|xyz{{ccc}}}}}} and a [[page|link]]")
  74. tmpl1, tmpl2, tmpl3 = code.filter_templates()
  75. tmpl4 = parse("{{ccc}}").filter_templates()[0]
  76. self.assertTrue(code.contains(tmpl1))
  77. self.assertTrue(code.contains(tmpl3))
  78. self.assertFalse(code.contains(tmpl4))
  79. self.assertTrue(code.contains(str(tmpl4)))
  80. self.assertTrue(code.contains(tmpl2.params[0].value))
  81. def test_index(self):
  82. """test Wikicode.index()"""
  83. code = parse("Have a {{template}} and a [[page|link]]")
  84. self.assertEqual(0, code.index("Have a "))
  85. self.assertEqual(3, code.index("[[page|link]]"))
  86. self.assertEqual(1, code.index(code.get(1)))
  87. self.assertRaises(ValueError, code.index, "foo")
  88. code = parse("{{foo}}{{bar|{{baz}}}}")
  89. self.assertEqual(1, code.index("{{bar|{{baz}}}}"))
  90. self.assertEqual(1, code.index("{{baz}}", recursive=True))
  91. self.assertEqual(1, code.index(code.get(1).get(1).value,
  92. recursive=True))
  93. self.assertRaises(ValueError, code.index, "{{baz}}", recursive=False)
  94. self.assertRaises(ValueError, code.index,
  95. code.get(1).get(1).value, recursive=False)
  96. def test_get_ancestors_parent(self):
  97. """test Wikicode.get_ancestors() and Wikicode.get_parent()"""
  98. code = parse("{{a|{{b|{{d|{{e}}{{f}}}}{{g}}}}}}{{c}}")
  99. tmpl = code.filter_templates(matches=lambda n: n.name == "f")[0]
  100. parent1 = code.filter_templates(matches=lambda n: n.name == "d")[0]
  101. parent2 = code.filter_templates(matches=lambda n: n.name == "b")[0]
  102. parent3 = code.filter_templates(matches=lambda n: n.name == "a")[0]
  103. fake = parse("{{f}}").get(0)
  104. self.assertEqual([parent3, parent2, parent1], code.get_ancestors(tmpl))
  105. self.assertIs(parent1, code.get_parent(tmpl))
  106. self.assertEqual([], code.get_ancestors(parent3))
  107. self.assertIs(None, code.get_parent(parent3))
  108. self.assertRaises(ValueError, code.get_ancestors, fake)
  109. self.assertRaises(ValueError, code.get_parent, fake)
  110. def test_insert(self):
  111. """test Wikicode.insert()"""
  112. code = parse("Have a {{template}} and a [[page|link]]")
  113. code.insert(1, "{{{argument}}}")
  114. self.assertEqual(
  115. "Have a {{{argument}}}{{template}} and a [[page|link]]", code)
  116. self.assertIsInstance(code.get(1), Argument)
  117. code.insert(2, None)
  118. self.assertEqual(
  119. "Have a {{{argument}}}{{template}} and a [[page|link]]", code)
  120. code.insert(-3, Text("foo"))
  121. self.assertEqual(
  122. "Have a {{{argument}}}foo{{template}} and a [[page|link]]", code)
  123. code2 = parse("{{foo}}{{bar}}{{baz}}")
  124. code2.insert(1, "abc{{def}}ghi[[jk]]")
  125. self.assertEqual("{{foo}}abc{{def}}ghi[[jk]]{{bar}}{{baz}}", code2)
  126. self.assertEqual(["{{foo}}", "abc", "{{def}}", "ghi", "[[jk]]",
  127. "{{bar}}", "{{baz}}"], code2.nodes)
  128. code3 = parse("{{foo}}bar")
  129. code3.insert(1000, "[[baz]]")
  130. code3.insert(-1000, "derp")
  131. self.assertEqual("derp{{foo}}bar[[baz]]", code3)
  132. def _test_search(self, meth, expected):
  133. """Base test for insert_before(), insert_after(), and replace()."""
  134. code = parse("{{a}}{{b}}{{c}}{{d}}{{e}}")
  135. func = partial(meth, code)
  136. func("{{b}}", "x", recursive=True)
  137. func("{{d}}", "[[y]]", recursive=False)
  138. func(code.get(2), "z")
  139. self.assertEqual(expected[0], code)
  140. self.assertRaises(ValueError, func, "{{r}}", "n", recursive=True)
  141. self.assertRaises(ValueError, func, "{{r}}", "n", recursive=False)
  142. fake = parse("{{a}}").get(0)
  143. self.assertRaises(ValueError, func, fake, "n", recursive=True)
  144. self.assertRaises(ValueError, func, fake, "n", recursive=False)
  145. code2 = parse("{{a}}{{a}}{{a}}{{b}}{{b}}{{b}}")
  146. func = partial(meth, code2)
  147. func(code2.get(1), "c", recursive=False)
  148. func("{{a}}", "d", recursive=False)
  149. func(code2.get(-1), "e", recursive=True)
  150. func("{{b}}", "f", recursive=True)
  151. self.assertEqual(expected[1], code2)
  152. code3 = parse("{{a|{{b}}|{{c|d={{f}}}}}}")
  153. func = partial(meth, code3)
  154. obj = code3.get(0).params[0].value.get(0)
  155. self.assertRaises(ValueError, func, obj, "x", recursive=False)
  156. func(obj, "x", recursive=True)
  157. self.assertRaises(ValueError, func, "{{f}}", "y", recursive=False)
  158. func("{{f}}", "y", recursive=True)
  159. self.assertEqual(expected[2], code3)
  160. code4 = parse("{{a}}{{b}}{{c}}{{d}}{{e}}{{f}}{{g}}{{h}}{{i}}{{j}}")
  161. func = partial(meth, code4)
  162. fake = parse("{{b}}{{c}}")
  163. self.assertRaises(ValueError, func, fake, "q", recursive=False)
  164. self.assertRaises(ValueError, func, fake, "q", recursive=True)
  165. func("{{b}}{{c}}", "w", recursive=False)
  166. func("{{d}}{{e}}", "x", recursive=True)
  167. func(Wikicode(code4.nodes[-2:]), "y", recursive=False)
  168. func(Wikicode(code4.nodes[-2:]), "z", recursive=True)
  169. self.assertEqual(expected[3], code4)
  170. self.assertRaises(ValueError, func, "{{c}}{{d}}", "q", recursive=False)
  171. self.assertRaises(ValueError, func, "{{c}}{{d}}", "q", recursive=True)
  172. code5 = parse("{{a|{{b}}{{c}}|{{f|{{g}}={{h}}{{i}}}}}}")
  173. func = partial(meth, code5)
  174. self.assertRaises(ValueError, func, "{{b}}{{c}}", "x", recursive=False)
  175. func("{{b}}{{c}}", "x", recursive=True)
  176. obj = code5.get(0).params[1].value.get(0).params[0].value
  177. self.assertRaises(ValueError, func, obj, "y", recursive=False)
  178. func(obj, "y", recursive=True)
  179. self.assertEqual(expected[4], code5)
  180. code6 = parse("here is {{some text and a {{template}}}}")
  181. func = partial(meth, code6)
  182. self.assertRaises(ValueError, func, "text and", "ab", recursive=False)
  183. func("text and", "ab", recursive=True)
  184. self.assertRaises(ValueError, func, "is {{some", "cd", recursive=False)
  185. func("is {{some", "cd", recursive=True)
  186. self.assertEqual(expected[5], code6)
  187. code7 = parse("{{foo}}{{bar}}{{baz}}{{foo}}{{baz}}")
  188. func = partial(meth, code7)
  189. obj = wrap([code7.get(0), code7.get(2)])
  190. self.assertRaises(ValueError, func, obj, "{{lol}}")
  191. func("{{foo}}{{baz}}", "{{lol}}")
  192. self.assertEqual(expected[6], code7)
  193. code8 = parse("== header ==")
  194. func = partial(meth, code8)
  195. sec1, sec2 = code8.get_sections(include_headings=False)
  196. func(sec1, "lead\n")
  197. func(sec2, "\nbody")
  198. self.assertEqual(expected[7], code8)
  199. code9 = parse("{{foo}}")
  200. meth(code9.get_sections()[0], code9.get_sections()[0], "{{bar}}")
  201. meth(code9.get_sections()[0], code9, "{{baz}}")
  202. meth(code9, code9, "{{qux}}")
  203. meth(code9, code9.get_sections()[0], "{{quz}}")
  204. self.assertEqual(expected[8], code9)
  205. def test_insert_before(self):
  206. """test Wikicode.insert_before()"""
  207. meth = lambda code, *args, **kw: code.insert_before(*args, **kw)
  208. expected = [
  209. "{{a}}xz{{b}}{{c}}[[y]]{{d}}{{e}}",
  210. "d{{a}}cd{{a}}d{{a}}f{{b}}f{{b}}ef{{b}}",
  211. "{{a|x{{b}}|{{c|d=y{{f}}}}}}",
  212. "{{a}}w{{b}}{{c}}x{{d}}{{e}}{{f}}{{g}}{{h}}yz{{i}}{{j}}",
  213. "{{a|x{{b}}{{c}}|{{f|{{g}}=y{{h}}{{i}}}}}}",
  214. "here cdis {{some abtext and a {{template}}}}",
  215. "{{foo}}{{bar}}{{baz}}{{lol}}{{foo}}{{baz}}",
  216. "lead\n== header ==\nbody",
  217. "{{quz}}{{qux}}{{baz}}{{bar}}{{foo}}",
  218. ]
  219. self._test_search(meth, expected)
  220. def test_insert_after(self):
  221. """test Wikicode.insert_after()"""
  222. meth = lambda code, *args, **kw: code.insert_after(*args, **kw)
  223. expected = [
  224. "{{a}}{{b}}xz{{c}}{{d}}[[y]]{{e}}",
  225. "{{a}}d{{a}}dc{{a}}d{{b}}f{{b}}f{{b}}fe",
  226. "{{a|{{b}}x|{{c|d={{f}}y}}}}",
  227. "{{a}}{{b}}{{c}}w{{d}}{{e}}x{{f}}{{g}}{{h}}{{i}}{{j}}yz",
  228. "{{a|{{b}}{{c}}x|{{f|{{g}}={{h}}{{i}}y}}}}",
  229. "here is {{somecd text andab a {{template}}}}",
  230. "{{foo}}{{bar}}{{baz}}{{foo}}{{baz}}{{lol}}",
  231. "lead\n== header ==\nbody",
  232. "{{foo}}{{bar}}{{baz}}{{qux}}{{quz}}",
  233. ]
  234. self._test_search(meth, expected)
  235. def test_replace(self):
  236. """test Wikicode.replace()"""
  237. meth = lambda code, *args, **kw: code.replace(*args, **kw)
  238. expected = [
  239. "{{a}}xz[[y]]{{e}}",
  240. "dcdffe",
  241. "{{a|x|{{c|d=y}}}}",
  242. "{{a}}wx{{f}}{{g}}z",
  243. "{{a|x|{{f|{{g}}=y}}}}",
  244. "here cd ab a {{template}}}}",
  245. "{{foo}}{{bar}}{{baz}}{{lol}}",
  246. "lead\n== header ==\nbody",
  247. "{{quz}}",
  248. ]
  249. self._test_search(meth, expected)
  250. def test_append(self):
  251. """test Wikicode.append()"""
  252. code = parse("Have a {{template}}")
  253. code.append("{{{argument}}}")
  254. self.assertEqual("Have a {{template}}{{{argument}}}", code)
  255. self.assertIsInstance(code.get(2), Argument)
  256. code.append(None)
  257. self.assertEqual("Have a {{template}}{{{argument}}}", code)
  258. code.append(Text(" foo"))
  259. self.assertEqual("Have a {{template}}{{{argument}}} foo", code)
  260. self.assertRaises(ValueError, code.append, slice(0, 1))
  261. def test_remove(self):
  262. """test Wikicode.remove()"""
  263. meth = lambda code, obj, value, **kw: code.remove(obj, **kw)
  264. expected = [
  265. "{{a}}{{c}}",
  266. "",
  267. "{{a||{{c|d=}}}}",
  268. "{{a}}{{f}}",
  269. "{{a||{{f|{{g}}=}}}}",
  270. "here a {{template}}}}",
  271. "{{foo}}{{bar}}{{baz}}",
  272. "== header ==",
  273. "",
  274. ]
  275. self._test_search(meth, expected)
  276. def test_matches(self):
  277. """test Wikicode.matches()"""
  278. code1 = parse("Cleanup")
  279. code2 = parse("\nstub<!-- TODO: make more specific -->")
  280. code3 = parse("Hello world!")
  281. code4 = parse("World,_hello?")
  282. code5 = parse("")
  283. self.assertTrue(code1.matches("Cleanup"))
  284. self.assertTrue(code1.matches("cleanup"))
  285. self.assertTrue(code1.matches(" cleanup\n"))
  286. self.assertFalse(code1.matches("CLEANup"))
  287. self.assertFalse(code1.matches("Blah"))
  288. self.assertTrue(code2.matches("stub"))
  289. self.assertTrue(code2.matches("Stub<!-- no, it's fine! -->"))
  290. self.assertFalse(code2.matches("StuB"))
  291. self.assertTrue(code1.matches(("cleanup", "stub")))
  292. self.assertTrue(code2.matches(("cleanup", "stub")))
  293. self.assertFalse(code2.matches(("StuB", "sTUb", "foobar")))
  294. self.assertFalse(code2.matches(["StuB", "sTUb", "foobar"]))
  295. self.assertTrue(code2.matches(("StuB", "sTUb", "foo", "bar", "Stub")))
  296. self.assertTrue(code2.matches(["StuB", "sTUb", "foo", "bar", "Stub"]))
  297. self.assertTrue(code3.matches("hello world!"))
  298. self.assertTrue(code3.matches("hello_world!"))
  299. self.assertFalse(code3.matches("hello__world!"))
  300. self.assertTrue(code4.matches("World,_hello?"))
  301. self.assertTrue(code4.matches("World, hello?"))
  302. self.assertFalse(code4.matches("World, hello?"))
  303. self.assertTrue(code5.matches(""))
  304. self.assertTrue(code5.matches("<!-- nothing -->"))
  305. self.assertTrue(code5.matches(("a", "b", "")))
  306. def test_filter_family(self):
  307. """test the Wikicode.i?filter() family of functions"""
  308. def genlist(gen):
  309. self.assertIsInstance(gen, GeneratorType)
  310. return list(gen)
  311. ifilter = lambda code: (lambda *a, **k: genlist(code.ifilter(*a, **k)))
  312. code = parse("a{{b}}c[[d]]{{{e}}}{{f}}[[g]]")
  313. for func in (code.filter, ifilter(code)):
  314. self.assertEqual(["a", "{{b}}", "b", "c", "[[d]]", "d", "{{{e}}}",
  315. "e", "{{f}}", "f", "[[g]]", "g"], func())
  316. self.assertEqual(["{{{e}}}"], func(forcetype=Argument))
  317. self.assertIs(code.get(4), func(forcetype=Argument)[0])
  318. self.assertEqual(list("abcdefg"), func(forcetype=Text))
  319. self.assertEqual([], func(forcetype=Heading))
  320. self.assertRaises(TypeError, func, forcetype=True)
  321. funcs = [
  322. lambda name, **kw: getattr(code, "filter_" + name)(**kw),
  323. lambda name, **kw: genlist(getattr(code, "ifilter_" + name)(**kw))
  324. ]
  325. for get_filter in funcs:
  326. self.assertEqual(["{{{e}}}"], get_filter("arguments"))
  327. self.assertIs(code.get(4), get_filter("arguments")[0])
  328. self.assertEqual([], get_filter("comments"))
  329. self.assertEqual([], get_filter("external_links"))
  330. self.assertEqual([], get_filter("headings"))
  331. self.assertEqual([], get_filter("html_entities"))
  332. self.assertEqual([], get_filter("tags"))
  333. self.assertEqual(["{{b}}", "{{f}}"], get_filter("templates"))
  334. self.assertEqual(list("abcdefg"), get_filter("text"))
  335. self.assertEqual(["[[d]]", "[[g]]"], get_filter("wikilinks"))
  336. code2 = parse("{{a|{{b}}|{{c|d={{f}}{{h}}}}}}")
  337. for func in (code2.filter, ifilter(code2)):
  338. self.assertEqual(["{{a|{{b}}|{{c|d={{f}}{{h}}}}}}"],
  339. func(recursive=False, forcetype=Template))
  340. self.assertEqual(["{{a|{{b}}|{{c|d={{f}}{{h}}}}}}", "{{b}}",
  341. "{{c|d={{f}}{{h}}}}", "{{f}}", "{{h}}"],
  342. func(recursive=True, forcetype=Template))
  343. code3 = parse("{{foobar}}{{FOO}}{{baz}}{{bz}}{{barfoo}}")
  344. for func in (code3.filter, ifilter(code3)):
  345. self.assertEqual(["{{foobar}}", "{{barfoo}}"],
  346. func(False, matches=lambda node: "foo" in node))
  347. self.assertEqual(["{{foobar}}", "{{FOO}}", "{{barfoo}}"],
  348. func(False, matches=r"foo"))
  349. self.assertEqual(["{{foobar}}", "{{FOO}}"],
  350. func(matches=r"^{{foo.*?}}"))
  351. self.assertEqual(["{{foobar}}"],
  352. func(matches=r"^{{foo.*?}}", flags=re.UNICODE))
  353. self.assertEqual(["{{baz}}", "{{bz}}"], func(matches=r"^{{b.*?z"))
  354. self.assertEqual(["{{baz}}"], func(matches=r"^{{b.+?z}}"))
  355. exp_rec = ["{{a|{{b}}|{{c|d={{f}}{{h}}}}}}", "{{b}}",
  356. "{{c|d={{f}}{{h}}}}", "{{f}}", "{{h}}"]
  357. exp_unrec = ["{{a|{{b}}|{{c|d={{f}}{{h}}}}}}"]
  358. self.assertEqual(exp_rec, code2.filter_templates())
  359. self.assertEqual(exp_unrec, code2.filter_templates(recursive=False))
  360. self.assertEqual(exp_rec, code2.filter_templates(recursive=True))
  361. self.assertEqual(exp_rec, code2.filter_templates(True))
  362. self.assertEqual(exp_unrec, code2.filter_templates(False))
  363. self.assertEqual(["{{foobar}}"], code3.filter_templates(
  364. matches=lambda node: node.name.matches("Foobar")))
  365. self.assertEqual(["{{baz}}", "{{bz}}"],
  366. code3.filter_templates(matches=r"^{{b.*?z"))
  367. self.assertEqual([], code3.filter_tags(matches=r"^{{b.*?z"))
  368. self.assertEqual([], code3.filter_tags(matches=r"^{{b.*?z", flags=0))
  369. self.assertRaises(TypeError, code.filter_templates, a=42)
  370. self.assertRaises(TypeError, code.filter_templates, forcetype=Template)
  371. self.assertRaises(TypeError, code.filter_templates, 1, 0, 0, Template)
  372. code4 = parse("{{foo}}<b>{{foo|{{bar}}}}</b>")
  373. actual1 = code4.filter_templates(recursive=code4.RECURSE_OTHERS)
  374. actual2 = code4.filter_templates(code4.RECURSE_OTHERS)
  375. self.assertEqual(["{{foo}}", "{{foo|{{bar}}}}"], actual1)
  376. self.assertEqual(["{{foo}}", "{{foo|{{bar}}}}"], actual2)
  377. def test_get_sections(self):
  378. """test Wikicode.get_sections()"""
  379. page1 = parse("")
  380. page2 = parse("==Heading==")
  381. page3 = parse("===Heading===\nFoo bar baz\n====Gnidaeh====\n")
  382. p4_lead = "This is a lead.\n"
  383. p4_IA = "=== Section I.A ===\nSection I.A [[body]].\n"
  384. p4_IB1 = "==== Section I.B.1 ====\nSection I.B.1 body.\n\n&bull;Some content.\n\n"
  385. p4_IB = "=== Section I.B ===\n" + p4_IB1
  386. p4_I = "== Section I ==\nSection I body. {{and a|template}}\n" + p4_IA + p4_IB
  387. p4_II = "== Section II ==\nSection II body.\n\n"
  388. p4_IIIA1a = "===== Section III.A.1.a =====\nMore text.\n"
  389. p4_IIIA2ai1 = "======= Section III.A.2.a.i.1 =======\nAn invalid section!"
  390. p4_IIIA2 = "==== Section III.A.2 ====\nEven more text.\n" + p4_IIIA2ai1
  391. p4_IIIA = "=== Section III.A ===\nText.\n" + p4_IIIA1a + p4_IIIA2
  392. p4_III = "== Section III ==\n" + p4_IIIA
  393. page4 = parse(p4_lead + p4_I + p4_II + p4_III)
  394. self.assertEqual([""], page1.get_sections())
  395. self.assertEqual(["", "==Heading=="], page2.get_sections())
  396. self.assertEqual(["", "===Heading===\nFoo bar baz\n====Gnidaeh====\n",
  397. "====Gnidaeh====\n"], page3.get_sections())
  398. self.assertEqual([p4_lead, p4_I, p4_IA, p4_IB, p4_IB1, p4_II,
  399. p4_III, p4_IIIA, p4_IIIA1a, p4_IIIA2, p4_IIIA2ai1],
  400. page4.get_sections())
  401. self.assertEqual(["====Gnidaeh====\n"], page3.get_sections(levels=[4]))
  402. self.assertEqual(["===Heading===\nFoo bar baz\n====Gnidaeh====\n"],
  403. page3.get_sections(levels=(2, 3)))
  404. self.assertEqual(["===Heading===\nFoo bar baz\n"],
  405. page3.get_sections(levels=(2, 3), flat=True))
  406. self.assertEqual([], page3.get_sections(levels=[0]))
  407. self.assertEqual(["", "====Gnidaeh====\n"],
  408. page3.get_sections(levels=[4], include_lead=True))
  409. self.assertEqual(["===Heading===\nFoo bar baz\n====Gnidaeh====\n",
  410. "====Gnidaeh====\n"],
  411. page3.get_sections(include_lead=False))
  412. self.assertEqual(["===Heading===\nFoo bar baz\n", "====Gnidaeh====\n"],
  413. page3.get_sections(flat=True, include_lead=False))
  414. self.assertEqual([p4_IB1, p4_IIIA2], page4.get_sections(levels=[4]))
  415. self.assertEqual([p4_IA, p4_IB, p4_IIIA], page4.get_sections(levels=[3]))
  416. self.assertEqual([p4_IA, "=== Section I.B ===\n",
  417. "=== Section III.A ===\nText.\n"],
  418. page4.get_sections(levels=[3], flat=True))
  419. self.assertEqual(["", ""], page2.get_sections(include_headings=False))
  420. self.assertEqual(["\nSection I.B.1 body.\n\n&bull;Some content.\n\n",
  421. "\nEven more text.\n" + p4_IIIA2ai1],
  422. page4.get_sections(levels=[4],
  423. include_headings=False))
  424. self.assertEqual([], page4.get_sections(matches=r"body"))
  425. self.assertEqual([p4_I, p4_IA, p4_IB, p4_IB1],
  426. page4.get_sections(matches=r"Section\sI[.\s].*?"))
  427. self.assertEqual([p4_IA, p4_IIIA, p4_IIIA1a, p4_IIIA2, p4_IIIA2ai1],
  428. page4.get_sections(matches=r".*?a.*?"))
  429. self.assertEqual([p4_IIIA1a, p4_IIIA2ai1],
  430. page4.get_sections(matches=r".*?a.*?", flags=re.U))
  431. self.assertEqual(["\nMore text.\n", "\nAn invalid section!"],
  432. page4.get_sections(matches=r".*?a.*?", flags=re.U,
  433. include_headings=False))
  434. sections = page2.get_sections(include_headings=False)
  435. sections[0].append("Lead!\n")
  436. sections[1].append("\nFirst section!")
  437. self.assertEqual("Lead!\n==Heading==\nFirst section!", page2)
  438. page5 = parse("X\n== Foo ==\nBar\n== Baz ==\nBuzz")
  439. section = page5.get_sections(matches="Foo")[0]
  440. section.replace("\nBar\n", "\nBarf ")
  441. section.append("{{Haha}}\n")
  442. self.assertEqual("== Foo ==\nBarf {{Haha}}\n", section)
  443. self.assertEqual("X\n== Foo ==\nBarf {{Haha}}\n== Baz ==\nBuzz", page5)
  444. def test_strip_code(self):
  445. """test Wikicode.strip_code()"""
  446. # Since individual nodes have test cases for their __strip__ methods,
  447. # we're only going to do an integration test:
  448. code = parse("Foo [[bar]]\n\n{{baz|hello}}\n\n[[a|b]] &Sigma;")
  449. self.assertEqual("Foo bar\n\nb Σ",
  450. code.strip_code(normalize=True, collapse=True))
  451. self.assertEqual("Foo bar\n\n\n\nb Σ",
  452. code.strip_code(normalize=True, collapse=False))
  453. self.assertEqual("Foo bar\n\nb &Sigma;",
  454. code.strip_code(normalize=False, collapse=True))
  455. self.assertEqual("Foo bar\n\n\n\nb &Sigma;",
  456. code.strip_code(normalize=False, collapse=False))
  457. self.assertEqual("Foo bar\n\nhello\n\nb Σ",
  458. code.strip_code(normalize=True, collapse=True,
  459. keep_template_params=True))
  460. def test_get_tree(self):
  461. """test Wikicode.get_tree()"""
  462. # Since individual nodes have test cases for their __showtree___
  463. # methods, and the docstring covers all possibilities for the output of
  464. # __showtree__, we'll test it only:
  465. code = parse("Lorem ipsum {{foo|bar|{{baz}}|spam=eggs}}")
  466. expected = "Lorem ipsum \n{{\n\t foo\n\t| 1\n\t= bar\n\t| 2\n\t= " + \
  467. "{{\n\t\t\tbaz\n\t }}\n\t| spam\n\t= eggs\n}}"
  468. self.assertEqual(expected.expandtabs(4), code.get_tree())
  469. if __name__ == "__main__":
  470. unittest.main(verbosity=2)