A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_builder.py 21 KiB

11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  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. try:
  24. import unittest2 as unittest
  25. except ImportError:
  26. import unittest
  27. from mwparserfromhell.compat import py3k
  28. from mwparserfromhell.nodes import (Argument, Comment, ExternalLink, Heading,
  29. HTMLEntity, Tag, Template, Text, Wikilink)
  30. from mwparserfromhell.nodes.extras import Attribute, Parameter
  31. from mwparserfromhell.parser import tokens, ParserError
  32. from mwparserfromhell.parser.builder import Builder
  33. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  34. class TestBuilder(TreeEqualityTestCase):
  35. """Tests for the builder, which turns tokens into Wikicode objects."""
  36. def setUp(self):
  37. self.builder = Builder()
  38. def test_text(self):
  39. """tests for building Text nodes"""
  40. tests = [
  41. ([tokens.Text(text="foobar")], wraptext("foobar")),
  42. ([tokens.Text(text="fóóbar")], wraptext("fóóbar")),
  43. ([tokens.Text(text="spam"), tokens.Text(text="eggs")],
  44. wraptext("spam", "eggs")),
  45. ]
  46. for test, valid in tests:
  47. self.assertWikicodeEqual(valid, self.builder.build(test))
  48. def test_template(self):
  49. """tests for building Template nodes"""
  50. tests = [
  51. ([tokens.TemplateOpen(), tokens.Text(text="foobar"),
  52. tokens.TemplateClose()],
  53. wrap([Template(wraptext("foobar"))])),
  54. ([tokens.TemplateOpen(), tokens.Text(text="spam"),
  55. tokens.Text(text="eggs"), tokens.TemplateClose()],
  56. wrap([Template(wraptext("spam", "eggs"))])),
  57. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  58. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  59. tokens.TemplateClose()],
  60. wrap([Template(wraptext("foo"), params=[
  61. Parameter(wraptext("1"), wraptext("bar"), showkey=False)])])),
  62. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  63. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  64. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  65. tokens.TemplateClose()],
  66. wrap([Template(wraptext("foo"), params=[
  67. Parameter(wraptext("bar"), wraptext("baz"))])])),
  68. ([tokens.TemplateOpen(), tokens.TemplateParamSeparator(),
  69. tokens.TemplateParamSeparator(), tokens.TemplateParamEquals(),
  70. tokens.TemplateParamSeparator(), tokens.TemplateClose()],
  71. wrap([Template(wrap([]), params=[
  72. Parameter(wraptext("1"), wrap([]), showkey=False),
  73. Parameter(wrap([]), wrap([]), showkey=True),
  74. Parameter(wraptext("2"), wrap([]), showkey=False)])])),
  75. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  76. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  77. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  78. tokens.TemplateParamSeparator(), tokens.Text(text="biz"),
  79. tokens.TemplateParamSeparator(), tokens.Text(text="buzz"),
  80. tokens.TemplateParamSeparator(), tokens.Text(text="3"),
  81. tokens.TemplateParamEquals(), tokens.Text(text="buff"),
  82. tokens.TemplateParamSeparator(), tokens.Text(text="baff"),
  83. tokens.TemplateClose()],
  84. wrap([Template(wraptext("foo"), params=[
  85. Parameter(wraptext("bar"), wraptext("baz")),
  86. Parameter(wraptext("1"), wraptext("biz"), showkey=False),
  87. Parameter(wraptext("2"), wraptext("buzz"), showkey=False),
  88. Parameter(wraptext("3"), wraptext("buff")),
  89. Parameter(wraptext("3"), wraptext("baff"),
  90. showkey=False)])])),
  91. ]
  92. for test, valid in tests:
  93. self.assertWikicodeEqual(valid, self.builder.build(test))
  94. def test_argument(self):
  95. """tests for building Argument nodes"""
  96. tests = [
  97. ([tokens.ArgumentOpen(), tokens.Text(text="foobar"),
  98. tokens.ArgumentClose()],
  99. wrap([Argument(wraptext("foobar"))])),
  100. ([tokens.ArgumentOpen(), tokens.Text(text="spam"),
  101. tokens.Text(text="eggs"), tokens.ArgumentClose()],
  102. wrap([Argument(wraptext("spam", "eggs"))])),
  103. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  104. tokens.ArgumentSeparator(), tokens.Text(text="bar"),
  105. tokens.ArgumentClose()],
  106. wrap([Argument(wraptext("foo"), wraptext("bar"))])),
  107. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  108. tokens.Text(text="bar"), tokens.ArgumentSeparator(),
  109. tokens.Text(text="baz"), tokens.Text(text="biz"),
  110. tokens.ArgumentClose()],
  111. wrap([Argument(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  112. ]
  113. for test, valid in tests:
  114. self.assertWikicodeEqual(valid, self.builder.build(test))
  115. def test_wikilink(self):
  116. """tests for building Wikilink nodes"""
  117. tests = [
  118. ([tokens.WikilinkOpen(), tokens.Text(text="foobar"),
  119. tokens.WikilinkClose()],
  120. wrap([Wikilink(wraptext("foobar"))])),
  121. ([tokens.WikilinkOpen(), tokens.Text(text="spam"),
  122. tokens.Text(text="eggs"), tokens.WikilinkClose()],
  123. wrap([Wikilink(wraptext("spam", "eggs"))])),
  124. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  125. tokens.WikilinkSeparator(), tokens.Text(text="bar"),
  126. tokens.WikilinkClose()],
  127. wrap([Wikilink(wraptext("foo"), wraptext("bar"))])),
  128. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  129. tokens.Text(text="bar"), tokens.WikilinkSeparator(),
  130. tokens.Text(text="baz"), tokens.Text(text="biz"),
  131. tokens.WikilinkClose()],
  132. wrap([Wikilink(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  133. ]
  134. for test, valid in tests:
  135. self.assertWikicodeEqual(valid, self.builder.build(test))
  136. def test_external_link(self):
  137. """tests for building ExternalLink nodes"""
  138. tests = [
  139. ([tokens.ExternalLinkOpen(brackets=False),
  140. tokens.Text(text="http://example.com/"),
  141. tokens.ExternalLinkClose()],
  142. wrap([ExternalLink(wraptext("http://example.com/"),
  143. brackets=False)])),
  144. ([tokens.ExternalLinkOpen(brackets=True),
  145. tokens.Text(text="http://example.com/"),
  146. tokens.ExternalLinkClose()],
  147. wrap([ExternalLink(wraptext("http://example.com/"))])),
  148. ([tokens.ExternalLinkOpen(brackets=True),
  149. tokens.Text(text="http://example.com/"),
  150. tokens.ExternalLinkSeparator(), tokens.ExternalLinkClose()],
  151. wrap([ExternalLink(wraptext("http://example.com/"), wrap([]))])),
  152. ([tokens.ExternalLinkOpen(brackets=True),
  153. tokens.Text(text="http://example.com/"),
  154. tokens.ExternalLinkSeparator(), tokens.Text(text="Example"),
  155. tokens.ExternalLinkClose()],
  156. wrap([ExternalLink(wraptext("http://example.com/"),
  157. wraptext("Example"))])),
  158. ([tokens.ExternalLinkOpen(brackets=False),
  159. tokens.Text(text="http://example"), tokens.Text(text=".com/foo"),
  160. tokens.ExternalLinkClose()],
  161. wrap([ExternalLink(wraptext("http://example", ".com/foo"),
  162. brackets=False)])),
  163. ([tokens.ExternalLinkOpen(brackets=True),
  164. tokens.Text(text="http://example"), tokens.Text(text=".com/foo"),
  165. tokens.ExternalLinkSeparator(), tokens.Text(text="Example"),
  166. tokens.Text(text=" Web Page"), tokens.ExternalLinkClose()],
  167. wrap([ExternalLink(wraptext("http://example", ".com/foo"),
  168. wraptext("Example", " Web Page"))])),
  169. ]
  170. for test, valid in tests:
  171. self.assertWikicodeEqual(valid, self.builder.build(test))
  172. def test_html_entity(self):
  173. """tests for building HTMLEntity nodes"""
  174. tests = [
  175. ([tokens.HTMLEntityStart(), tokens.Text(text="nbsp"),
  176. tokens.HTMLEntityEnd()],
  177. wrap([HTMLEntity("nbsp", named=True, hexadecimal=False)])),
  178. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  179. tokens.Text(text="107"), tokens.HTMLEntityEnd()],
  180. wrap([HTMLEntity("107", named=False, hexadecimal=False)])),
  181. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  182. tokens.HTMLEntityHex(char="X"), tokens.Text(text="6B"),
  183. tokens.HTMLEntityEnd()],
  184. wrap([HTMLEntity("6B", named=False, hexadecimal=True,
  185. hex_char="X")])),
  186. ]
  187. for test, valid in tests:
  188. self.assertWikicodeEqual(valid, self.builder.build(test))
  189. def test_heading(self):
  190. """tests for building Heading nodes"""
  191. tests = [
  192. ([tokens.HeadingStart(level=2), tokens.Text(text="foobar"),
  193. tokens.HeadingEnd()],
  194. wrap([Heading(wraptext("foobar"), 2)])),
  195. ([tokens.HeadingStart(level=4), tokens.Text(text="spam"),
  196. tokens.Text(text="eggs"), tokens.HeadingEnd()],
  197. wrap([Heading(wraptext("spam", "eggs"), 4)])),
  198. ]
  199. for test, valid in tests:
  200. self.assertWikicodeEqual(valid, self.builder.build(test))
  201. def test_comment(self):
  202. """tests for building Comment nodes"""
  203. tests = [
  204. ([tokens.CommentStart(), tokens.Text(text="foobar"),
  205. tokens.CommentEnd()],
  206. wrap([Comment(wraptext("foobar"))])),
  207. ([tokens.CommentStart(), tokens.Text(text="spam"),
  208. tokens.Text(text="eggs"), tokens.CommentEnd()],
  209. wrap([Comment(wraptext("spam", "eggs"))])),
  210. ]
  211. for test, valid in tests:
  212. self.assertWikicodeEqual(valid, self.builder.build(test))
  213. def test_tag(self):
  214. """tests for building Tag nodes"""
  215. tests = [
  216. # <ref></ref>
  217. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  218. tokens.TagCloseOpen(padding=""), tokens.TagOpenClose(),
  219. tokens.Text(text="ref"), tokens.TagCloseClose()],
  220. wrap([Tag(wraptext("ref"), wrap([]),
  221. closing_tag=wraptext("ref"))])),
  222. # <ref name></ref>
  223. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  224. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  225. pad_after_eq=""),
  226. tokens.Text(text="name"), tokens.TagCloseOpen(padding=""),
  227. tokens.TagOpenClose(), tokens.Text(text="ref"),
  228. tokens.TagCloseClose()],
  229. wrap([Tag(wraptext("ref"), wrap([]),
  230. attrs=[Attribute(wraptext("name"))])])),
  231. # <ref name="abc" />
  232. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  233. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  234. pad_after_eq=""),
  235. tokens.Text(text="name"), tokens.TagAttrEquals(),
  236. tokens.TagAttrQuote(char='"'), tokens.Text(text="abc"),
  237. tokens.TagCloseSelfclose(padding=" ")],
  238. wrap([Tag(wraptext("ref"),
  239. attrs=[Attribute(wraptext("name"), wraptext("abc"))],
  240. self_closing=True, padding=" ")])),
  241. # <br/>
  242. ([tokens.TagOpenOpen(), tokens.Text(text="br"),
  243. tokens.TagCloseSelfclose(padding="")],
  244. wrap([Tag(wraptext("br"), self_closing=True)])),
  245. # <li>
  246. ([tokens.TagOpenOpen(), tokens.Text(text="li"),
  247. tokens.TagCloseSelfclose(padding="", implicit=True)],
  248. wrap([Tag(wraptext("li"), self_closing=True, implicit=True)])),
  249. # </br>
  250. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  251. tokens.TagCloseSelfclose(padding="", implicit=True)],
  252. wrap([Tag(wraptext("br"), self_closing=True, invalid=True,
  253. implicit=True)])),
  254. # </br/>
  255. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  256. tokens.TagCloseSelfclose(padding="")],
  257. wrap([Tag(wraptext("br"), self_closing=True, invalid=True)])),
  258. # <ref name={{abc}} foo="bar {{baz}}" abc={{de}}f ghi=j{{k}}{{l}}
  259. # mno = '{{p}} [[q]] {{r}}'>[[Source]]</ref>
  260. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  261. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  262. pad_after_eq=""),
  263. tokens.Text(text="name"), tokens.TagAttrEquals(),
  264. tokens.TemplateOpen(), tokens.Text(text="abc"),
  265. tokens.TemplateClose(),
  266. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  267. pad_after_eq=""),
  268. tokens.Text(text="foo"), tokens.TagAttrEquals(),
  269. tokens.TagAttrQuote(char='"'), tokens.Text(text="bar "),
  270. tokens.TemplateOpen(), tokens.Text(text="baz"),
  271. tokens.TemplateClose(),
  272. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  273. pad_after_eq=""),
  274. tokens.Text(text="abc"), tokens.TagAttrEquals(),
  275. tokens.TemplateOpen(), tokens.Text(text="de"),
  276. tokens.TemplateClose(), tokens.Text(text="f"),
  277. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  278. pad_after_eq=""),
  279. tokens.Text(text="ghi"), tokens.TagAttrEquals(),
  280. tokens.Text(text="j"), tokens.TemplateOpen(),
  281. tokens.Text(text="k"), tokens.TemplateClose(),
  282. tokens.TemplateOpen(), tokens.Text(text="l"),
  283. tokens.TemplateClose(),
  284. tokens.TagAttrStart(pad_first=" \n ", pad_before_eq=" ",
  285. pad_after_eq=" "),
  286. tokens.Text(text="mno"), tokens.TagAttrEquals(),
  287. tokens.TagAttrQuote(char="'"), tokens.TemplateOpen(),
  288. tokens.Text(text="p"), tokens.TemplateClose(),
  289. tokens.Text(text=" "), tokens.WikilinkOpen(),
  290. tokens.Text(text="q"), tokens.WikilinkClose(),
  291. tokens.Text(text=" "), tokens.TemplateOpen(),
  292. tokens.Text(text="r"), tokens.TemplateClose(),
  293. tokens.TagCloseOpen(padding=""), tokens.WikilinkOpen(),
  294. tokens.Text(text="Source"), tokens.WikilinkClose(),
  295. tokens.TagOpenClose(), tokens.Text(text="ref"),
  296. tokens.TagCloseClose()],
  297. wrap([Tag(wraptext("ref"), wrap([Wikilink(wraptext("Source"))]), [
  298. Attribute(wraptext("name"),
  299. wrap([Template(wraptext("abc"))]), None),
  300. Attribute(wraptext("foo"), wrap([Text("bar "),
  301. Template(wraptext("baz"))]), pad_first=" "),
  302. Attribute(wraptext("abc"), wrap([Template(wraptext("de")),
  303. Text("f")]), None),
  304. Attribute(wraptext("ghi"), wrap([Text("j"),
  305. Template(wraptext("k")),
  306. Template(wraptext("l"))]), None),
  307. Attribute(wraptext("mno"), wrap([Template(wraptext("p")),
  308. Text(" "), Wikilink(wraptext("q")), Text(" "),
  309. Template(wraptext("r"))]), "'", " \n ", " ",
  310. " ")])])),
  311. # "''italic text''"
  312. ([tokens.TagOpenOpen(wiki_markup="''"), tokens.Text(text="i"),
  313. tokens.TagCloseOpen(), tokens.Text(text="italic text"),
  314. tokens.TagOpenClose(), tokens.Text(text="i"),
  315. tokens.TagCloseClose()],
  316. wrap([Tag(wraptext("i"), wraptext("italic text"),
  317. wiki_markup="''")])),
  318. # * bullet
  319. ([tokens.TagOpenOpen(wiki_markup="*"), tokens.Text(text="li"),
  320. tokens.TagCloseSelfclose(), tokens.Text(text=" bullet")],
  321. wrap([Tag(wraptext("li"), wiki_markup="*", self_closing=True),
  322. Text(" bullet")])),
  323. ]
  324. for test, valid in tests:
  325. self.assertWikicodeEqual(valid, self.builder.build(test))
  326. def test_integration(self):
  327. """a test for building a combination of templates together"""
  328. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  329. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  330. tokens.TemplateOpen(), tokens.TemplateOpen(),
  331. tokens.Text(text="foo"), tokens.TemplateClose(),
  332. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  333. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  334. tokens.Text(text="biz"), tokens.TemplateClose(),
  335. tokens.Text(text="buzz"), tokens.TemplateClose(),
  336. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  337. tokens.TemplateOpen(), tokens.Text(text="bin"),
  338. tokens.TemplateClose(), tokens.TemplateClose()]
  339. valid = wrap(
  340. [Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
  341. "foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
  342. wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
  343. Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
  344. showkey=False)])])
  345. self.assertWikicodeEqual(valid, self.builder.build(test))
  346. def test_integration2(self):
  347. """an even more audacious test for building a horrible wikicode mess"""
  348. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  349. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  350. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  351. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  352. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  353. tokens.WikilinkOpen(), tokens.Text(text="d"),
  354. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  355. tokens.Text(text="e"), tokens.ArgumentClose(),
  356. tokens.TemplateClose(), tokens.TemplateClose(),
  357. tokens.WikilinkOpen(), tokens.Text(text="f"),
  358. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  359. tokens.Text(text="g"), tokens.ArgumentClose(),
  360. tokens.CommentStart(), tokens.Text(text="h"),
  361. tokens.CommentEnd(), tokens.WikilinkClose(),
  362. tokens.TemplateOpen(), tokens.Text(text="i"),
  363. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  364. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  365. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  366. tokens.TemplateClose()]
  367. valid = wrap(
  368. [Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
  369. "b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
  370. wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
  371. wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
  372. showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
  373. "g")), Comment(wraptext("h"))])), Template(wraptext("i"), params=[
  374. Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
  375. named=True)]))])])
  376. self.assertWikicodeEqual(valid, self.builder.build(test))
  377. def test_parser_errors(self):
  378. """test whether ParserError gets thrown for bad input"""
  379. missing_closes = [
  380. [tokens.TemplateOpen(), tokens.TemplateParamSeparator()],
  381. [tokens.TemplateOpen()], [tokens.ArgumentOpen()],
  382. [tokens.WikilinkOpen()], [tokens.ExternalLinkOpen()],
  383. [tokens.HeadingStart()], [tokens.CommentStart()],
  384. [tokens.TagOpenOpen(), tokens.TagAttrStart()],
  385. [tokens.TagOpenOpen()]
  386. ]
  387. func = self.assertRaisesRegex if py3k else self.assertRaisesRegexp
  388. msg = r"_handle_token\(\) got unexpected TemplateClose"
  389. func(ParserError, msg, self.builder.build, [tokens.TemplateClose()])
  390. for test in missing_closes:
  391. self.assertRaises(ParserError, self.builder.build, test)
  392. if __name__ == "__main__":
  393. unittest.main(verbosity=2)