A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

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