A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test_builder.py 20 KiB

11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. import unittest
  24. from mwparserfromhell.nodes import (Argument, Comment, ExternalLink, Heading,
  25. HTMLEntity, Tag, Template, Text, Wikilink)
  26. from mwparserfromhell.nodes.extras import Attribute, Parameter
  27. from mwparserfromhell.parser import tokens
  28. from mwparserfromhell.parser.builder import Builder
  29. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  30. class TestBuilder(TreeEqualityTestCase):
  31. """Tests for the builder, which turns tokens into Wikicode objects."""
  32. def setUp(self):
  33. self.builder = Builder()
  34. def test_text(self):
  35. """tests for building Text nodes"""
  36. tests = [
  37. ([tokens.Text(text="foobar")], wraptext("foobar")),
  38. ([tokens.Text(text="fóóbar")], wraptext("fóóbar")),
  39. ([tokens.Text(text="spam"), tokens.Text(text="eggs")],
  40. wraptext("spam", "eggs")),
  41. ]
  42. for test, valid in tests:
  43. self.assertWikicodeEqual(valid, self.builder.build(test))
  44. def test_template(self):
  45. """tests for building Template nodes"""
  46. tests = [
  47. ([tokens.TemplateOpen(), tokens.Text(text="foobar"),
  48. tokens.TemplateClose()],
  49. wrap([Template(wraptext("foobar"))])),
  50. ([tokens.TemplateOpen(), tokens.Text(text="spam"),
  51. tokens.Text(text="eggs"), tokens.TemplateClose()],
  52. wrap([Template(wraptext("spam", "eggs"))])),
  53. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  54. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  55. tokens.TemplateClose()],
  56. wrap([Template(wraptext("foo"), params=[
  57. Parameter(wraptext("1"), wraptext("bar"), showkey=False)])])),
  58. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  59. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  60. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  61. tokens.TemplateClose()],
  62. wrap([Template(wraptext("foo"), params=[
  63. Parameter(wraptext("bar"), wraptext("baz"))])])),
  64. ([tokens.TemplateOpen(), tokens.TemplateParamSeparator(),
  65. tokens.TemplateParamSeparator(), tokens.TemplateParamEquals(),
  66. tokens.TemplateParamSeparator(), tokens.TemplateClose()],
  67. wrap([Template(wrap([]), params=[
  68. Parameter(wraptext("1"), wrap([]), showkey=False),
  69. Parameter(wrap([]), wrap([]), showkey=True),
  70. Parameter(wraptext("2"), wrap([]), showkey=False)])])),
  71. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  72. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  73. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  74. tokens.TemplateParamSeparator(), tokens.Text(text="biz"),
  75. tokens.TemplateParamSeparator(), tokens.Text(text="buzz"),
  76. tokens.TemplateParamSeparator(), tokens.Text(text="3"),
  77. tokens.TemplateParamEquals(), tokens.Text(text="buff"),
  78. tokens.TemplateParamSeparator(), tokens.Text(text="baff"),
  79. tokens.TemplateClose()],
  80. wrap([Template(wraptext("foo"), params=[
  81. Parameter(wraptext("bar"), wraptext("baz")),
  82. Parameter(wraptext("1"), wraptext("biz"), showkey=False),
  83. Parameter(wraptext("2"), wraptext("buzz"), showkey=False),
  84. Parameter(wraptext("3"), wraptext("buff")),
  85. Parameter(wraptext("3"), wraptext("baff"),
  86. showkey=False)])])),
  87. ]
  88. for test, valid in tests:
  89. self.assertWikicodeEqual(valid, self.builder.build(test))
  90. def test_argument(self):
  91. """tests for building Argument nodes"""
  92. tests = [
  93. ([tokens.ArgumentOpen(), tokens.Text(text="foobar"),
  94. tokens.ArgumentClose()],
  95. wrap([Argument(wraptext("foobar"))])),
  96. ([tokens.ArgumentOpen(), tokens.Text(text="spam"),
  97. tokens.Text(text="eggs"), tokens.ArgumentClose()],
  98. wrap([Argument(wraptext("spam", "eggs"))])),
  99. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  100. tokens.ArgumentSeparator(), tokens.Text(text="bar"),
  101. tokens.ArgumentClose()],
  102. wrap([Argument(wraptext("foo"), wraptext("bar"))])),
  103. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  104. tokens.Text(text="bar"), tokens.ArgumentSeparator(),
  105. tokens.Text(text="baz"), tokens.Text(text="biz"),
  106. tokens.ArgumentClose()],
  107. wrap([Argument(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  108. ]
  109. for test, valid in tests:
  110. self.assertWikicodeEqual(valid, self.builder.build(test))
  111. def test_wikilink(self):
  112. """tests for building Wikilink nodes"""
  113. tests = [
  114. ([tokens.WikilinkOpen(), tokens.Text(text="foobar"),
  115. tokens.WikilinkClose()],
  116. wrap([Wikilink(wraptext("foobar"))])),
  117. ([tokens.WikilinkOpen(), tokens.Text(text="spam"),
  118. tokens.Text(text="eggs"), tokens.WikilinkClose()],
  119. wrap([Wikilink(wraptext("spam", "eggs"))])),
  120. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  121. tokens.WikilinkSeparator(), tokens.Text(text="bar"),
  122. tokens.WikilinkClose()],
  123. wrap([Wikilink(wraptext("foo"), wraptext("bar"))])),
  124. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  125. tokens.Text(text="bar"), tokens.WikilinkSeparator(),
  126. tokens.Text(text="baz"), tokens.Text(text="biz"),
  127. tokens.WikilinkClose()],
  128. wrap([Wikilink(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  129. ]
  130. for test, valid in tests:
  131. self.assertWikicodeEqual(valid, self.builder.build(test))
  132. def test_external_link(self):
  133. """tests for building ExternalLink nodes"""
  134. tests = [
  135. ([tokens.ExternalLinkOpen(brackets=False),
  136. tokens.Text(text="http://example.com/"),
  137. tokens.ExternalLinkClose()],
  138. wrap([ExternalLink(wraptext("http://example.com/"),
  139. brackets=False)])),
  140. ([tokens.ExternalLinkOpen(brackets=True),
  141. tokens.Text(text="http://example.com/"),
  142. tokens.ExternalLinkClose()],
  143. wrap([ExternalLink(wraptext("http://example.com/"))])),
  144. ([tokens.ExternalLinkOpen(brackets=True),
  145. tokens.Text(text="http://example.com/"),
  146. tokens.ExternalLinkSeparator(), tokens.ExternalLinkClose()],
  147. wrap([ExternalLink(wraptext("http://example.com/"), wrap([]))])),
  148. ([tokens.ExternalLinkOpen(brackets=True),
  149. tokens.Text(text="http://example.com/"),
  150. tokens.ExternalLinkSeparator(), tokens.Text(text="Example"),
  151. tokens.ExternalLinkClose()],
  152. wrap([ExternalLink(wraptext("http://example.com/"),
  153. wraptext("Example"))])),
  154. ([tokens.ExternalLinkOpen(brackets=False),
  155. tokens.Text(text="http://example"), tokens.Text(text=".com/foo"),
  156. tokens.ExternalLinkClose()],
  157. wrap([ExternalLink(wraptext("http://example", ".com/foo"),
  158. brackets=False)])),
  159. ([tokens.ExternalLinkOpen(brackets=True),
  160. tokens.Text(text="http://example"), tokens.Text(text=".com/foo"),
  161. tokens.ExternalLinkSeparator(), tokens.Text(text="Example"),
  162. tokens.Text(text=" Web Page"), tokens.ExternalLinkClose()],
  163. wrap([ExternalLink(wraptext("http://example", ".com/foo"),
  164. wraptext("Example", " Web Page"))])),
  165. ]
  166. for test, valid in tests:
  167. self.assertWikicodeEqual(valid, self.builder.build(test))
  168. def test_html_entity(self):
  169. """tests for building HTMLEntity nodes"""
  170. tests = [
  171. ([tokens.HTMLEntityStart(), tokens.Text(text="nbsp"),
  172. tokens.HTMLEntityEnd()],
  173. wrap([HTMLEntity("nbsp", named=True, hexadecimal=False)])),
  174. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  175. tokens.Text(text="107"), tokens.HTMLEntityEnd()],
  176. wrap([HTMLEntity("107", named=False, hexadecimal=False)])),
  177. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  178. tokens.HTMLEntityHex(char="X"), tokens.Text(text="6B"),
  179. tokens.HTMLEntityEnd()],
  180. wrap([HTMLEntity("6B", named=False, hexadecimal=True,
  181. hex_char="X")])),
  182. ]
  183. for test, valid in tests:
  184. self.assertWikicodeEqual(valid, self.builder.build(test))
  185. def test_heading(self):
  186. """tests for building Heading nodes"""
  187. tests = [
  188. ([tokens.HeadingStart(level=2), tokens.Text(text="foobar"),
  189. tokens.HeadingEnd()],
  190. wrap([Heading(wraptext("foobar"), 2)])),
  191. ([tokens.HeadingStart(level=4), tokens.Text(text="spam"),
  192. tokens.Text(text="eggs"), tokens.HeadingEnd()],
  193. wrap([Heading(wraptext("spam", "eggs"), 4)])),
  194. ]
  195. for test, valid in tests:
  196. self.assertWikicodeEqual(valid, self.builder.build(test))
  197. def test_comment(self):
  198. """tests for building Comment nodes"""
  199. tests = [
  200. ([tokens.CommentStart(), tokens.Text(text="foobar"),
  201. tokens.CommentEnd()],
  202. wrap([Comment(wraptext("foobar"))])),
  203. ([tokens.CommentStart(), tokens.Text(text="spam"),
  204. tokens.Text(text="eggs"), tokens.CommentEnd()],
  205. wrap([Comment(wraptext("spam", "eggs"))])),
  206. ]
  207. for test, valid in tests:
  208. self.assertWikicodeEqual(valid, self.builder.build(test))
  209. def test_tag(self):
  210. """tests for building Tag nodes"""
  211. tests = [
  212. # <ref></ref>
  213. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  214. tokens.TagCloseOpen(padding=""), tokens.TagOpenClose(),
  215. tokens.Text(text="ref"), tokens.TagCloseClose()],
  216. wrap([Tag(wraptext("ref"), wrap([]),
  217. closing_tag=wraptext("ref"))])),
  218. # <ref name></ref>
  219. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  220. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  221. pad_after_eq=""),
  222. tokens.Text(text="name"), tokens.TagCloseOpen(padding=""),
  223. tokens.TagOpenClose(), tokens.Text(text="ref"),
  224. tokens.TagCloseClose()],
  225. wrap([Tag(wraptext("ref"), wrap([]),
  226. attrs=[Attribute(wraptext("name"))])])),
  227. # <ref name="abc" />
  228. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  229. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  230. pad_after_eq=""),
  231. tokens.Text(text="name"), tokens.TagAttrEquals(),
  232. tokens.TagAttrQuote(), tokens.Text(text="abc"),
  233. tokens.TagCloseSelfclose(padding=" ")],
  234. wrap([Tag(wraptext("ref"),
  235. attrs=[Attribute(wraptext("name"), wraptext("abc"))],
  236. self_closing=True, padding=" ")])),
  237. # <br/>
  238. ([tokens.TagOpenOpen(), tokens.Text(text="br"),
  239. tokens.TagCloseSelfclose(padding="")],
  240. wrap([Tag(wraptext("br"), self_closing=True)])),
  241. # <li>
  242. ([tokens.TagOpenOpen(), tokens.Text(text="li"),
  243. tokens.TagCloseSelfclose(padding="", implicit=True)],
  244. wrap([Tag(wraptext("li"), self_closing=True, implicit=True)])),
  245. # </br>
  246. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  247. tokens.TagCloseSelfclose(padding="", implicit=True)],
  248. wrap([Tag(wraptext("br"), self_closing=True, invalid=True,
  249. implicit=True)])),
  250. # </br/>
  251. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  252. tokens.TagCloseSelfclose(padding="")],
  253. wrap([Tag(wraptext("br"), self_closing=True, invalid=True)])),
  254. # <ref name={{abc}} foo="bar {{baz}}" abc={{de}}f ghi=j{{k}}{{l}}
  255. # mno = "{{p}} [[q]] {{r}}">[[Source]]</ref>
  256. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  257. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  258. pad_after_eq=""),
  259. tokens.Text(text="name"), tokens.TagAttrEquals(),
  260. tokens.TemplateOpen(), tokens.Text(text="abc"),
  261. tokens.TemplateClose(),
  262. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  263. pad_after_eq=""),
  264. tokens.Text(text="foo"), tokens.TagAttrEquals(),
  265. tokens.TagAttrQuote(), tokens.Text(text="bar "),
  266. tokens.TemplateOpen(), tokens.Text(text="baz"),
  267. tokens.TemplateClose(),
  268. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  269. pad_after_eq=""),
  270. tokens.Text(text="abc"), tokens.TagAttrEquals(),
  271. tokens.TemplateOpen(), tokens.Text(text="de"),
  272. tokens.TemplateClose(), tokens.Text(text="f"),
  273. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  274. pad_after_eq=""),
  275. tokens.Text(text="ghi"), tokens.TagAttrEquals(),
  276. tokens.Text(text="j"), tokens.TemplateOpen(),
  277. tokens.Text(text="k"), tokens.TemplateClose(),
  278. tokens.TemplateOpen(), tokens.Text(text="l"),
  279. tokens.TemplateClose(),
  280. tokens.TagAttrStart(pad_first=" \n ", pad_before_eq=" ",
  281. pad_after_eq=" "),
  282. tokens.Text(text="mno"), tokens.TagAttrEquals(),
  283. tokens.TagAttrQuote(), tokens.TemplateOpen(),
  284. tokens.Text(text="p"), tokens.TemplateClose(),
  285. tokens.Text(text=" "), tokens.WikilinkOpen(),
  286. tokens.Text(text="q"), tokens.WikilinkClose(),
  287. tokens.Text(text=" "), tokens.TemplateOpen(),
  288. tokens.Text(text="r"), tokens.TemplateClose(),
  289. tokens.TagCloseOpen(padding=""), tokens.WikilinkOpen(),
  290. tokens.Text(text="Source"), tokens.WikilinkClose(),
  291. tokens.TagOpenClose(), tokens.Text(text="ref"),
  292. tokens.TagCloseClose()],
  293. wrap([Tag(wraptext("ref"), wrap([Wikilink(wraptext("Source"))]), [
  294. Attribute(wraptext("name"),
  295. wrap([Template(wraptext("abc"))]), False),
  296. Attribute(wraptext("foo"), wrap([Text("bar "),
  297. Template(wraptext("baz"))]), pad_first=" "),
  298. Attribute(wraptext("abc"), wrap([Template(wraptext("de")),
  299. Text("f")]), False),
  300. Attribute(wraptext("ghi"), wrap([Text("j"),
  301. Template(wraptext("k")),
  302. Template(wraptext("l"))]), False),
  303. Attribute(wraptext("mno"), wrap([Template(wraptext("p")),
  304. Text(" "), Wikilink(wraptext("q")), Text(" "),
  305. Template(wraptext("r"))]), True, " \n ", " ",
  306. " ")])])),
  307. # "''italic text''"
  308. ([tokens.TagOpenOpen(wiki_markup="''"), tokens.Text(text="i"),
  309. tokens.TagCloseOpen(), tokens.Text(text="italic text"),
  310. tokens.TagOpenClose(), tokens.Text(text="i"),
  311. tokens.TagCloseClose()],
  312. wrap([Tag(wraptext("i"), wraptext("italic text"),
  313. wiki_markup="''")])),
  314. # * bullet
  315. ([tokens.TagOpenOpen(wiki_markup="*"), tokens.Text(text="li"),
  316. tokens.TagCloseSelfclose(), tokens.Text(text=" bullet")],
  317. wrap([Tag(wraptext("li"), wiki_markup="*", self_closing=True),
  318. Text(" bullet")])),
  319. ]
  320. for test, valid in tests:
  321. self.assertWikicodeEqual(valid, self.builder.build(test))
  322. def test_integration(self):
  323. """a test for building a combination of templates together"""
  324. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  325. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  326. tokens.TemplateOpen(), tokens.TemplateOpen(),
  327. tokens.Text(text="foo"), tokens.TemplateClose(),
  328. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  329. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  330. tokens.Text(text="biz"), tokens.TemplateClose(),
  331. tokens.Text(text="buzz"), tokens.TemplateClose(),
  332. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  333. tokens.TemplateOpen(), tokens.Text(text="bin"),
  334. tokens.TemplateClose(), tokens.TemplateClose()]
  335. valid = wrap(
  336. [Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
  337. "foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
  338. wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
  339. Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
  340. showkey=False)])])
  341. self.assertWikicodeEqual(valid, self.builder.build(test))
  342. def test_integration2(self):
  343. """an even more audacious test for building a horrible wikicode mess"""
  344. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  345. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  346. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  347. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  348. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  349. tokens.WikilinkOpen(), tokens.Text(text="d"),
  350. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  351. tokens.Text(text="e"), tokens.ArgumentClose(),
  352. tokens.TemplateClose(), tokens.TemplateClose(),
  353. tokens.WikilinkOpen(), tokens.Text(text="f"),
  354. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  355. tokens.Text(text="g"), tokens.ArgumentClose(),
  356. tokens.CommentStart(), tokens.Text(text="h"),
  357. tokens.CommentEnd(), tokens.WikilinkClose(),
  358. tokens.TemplateOpen(), tokens.Text(text="i"),
  359. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  360. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  361. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  362. tokens.TemplateClose()]
  363. valid = wrap(
  364. [Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
  365. "b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
  366. wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
  367. wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
  368. showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
  369. "g")), Comment(wraptext("h"))])), Template(wraptext("i"), params=[
  370. Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
  371. named=True)]))])])
  372. self.assertWikicodeEqual(valid, self.builder.build(test))
  373. if __name__ == "__main__":
  374. unittest.main(verbosity=2)