A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_builder.py 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, Heading, HTMLEntity,
  25. 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 mwparserfromhell.smart_list import SmartList
  30. from mwparserfromhell.wikicode import Wikicode
  31. from ._test_tree_equality import TreeEqualityTestCase
  32. wrap = lambda L: Wikicode(SmartList(L))
  33. class TestBuilder(TreeEqualityTestCase):
  34. """Tests for the builder, which turns tokens into Wikicode objects."""
  35. def setUp(self):
  36. self.builder = Builder()
  37. def test_text(self):
  38. """tests for building Text nodes"""
  39. tests = [
  40. ([tokens.Text(text="foobar")], wrap([Text("foobar")])),
  41. ([tokens.Text(text="fóóbar")], wrap([Text("fóóbar")])),
  42. ([tokens.Text(text="spam"), tokens.Text(text="eggs")],
  43. wrap([Text("spam"), Text("eggs")])),
  44. ]
  45. for test, valid in tests:
  46. self.assertWikicodeEqual(valid, self.builder.build(test))
  47. def test_template(self):
  48. """tests for building Template nodes"""
  49. tests = [
  50. ([tokens.TemplateOpen(), tokens.Text(text="foobar"),
  51. tokens.TemplateClose()],
  52. wrap([Template(wrap([Text("foobar")]))])),
  53. ([tokens.TemplateOpen(), tokens.Text(text="spam"),
  54. tokens.Text(text="eggs"), tokens.TemplateClose()],
  55. wrap([Template(wrap([Text("spam"), Text("eggs")]))])),
  56. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  57. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  58. tokens.TemplateClose()],
  59. wrap([Template(wrap([Text("foo")]), params=[
  60. Parameter(wrap([Text("1")]), wrap([Text("bar")]),
  61. 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(wrap([Text("foo")]), params=[
  67. Parameter(wrap([Text("bar")]), wrap([Text("baz")]))])])),
  68. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  69. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  70. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  71. tokens.TemplateParamSeparator(), tokens.Text(text="biz"),
  72. tokens.TemplateParamSeparator(), tokens.Text(text="buzz"),
  73. tokens.TemplateParamSeparator(), tokens.Text(text="3"),
  74. tokens.TemplateParamEquals(), tokens.Text(text="buff"),
  75. tokens.TemplateParamSeparator(), tokens.Text(text="baff"),
  76. tokens.TemplateClose()],
  77. wrap([Template(wrap([Text("foo")]), params=[
  78. Parameter(wrap([Text("bar")]), wrap([Text("baz")])),
  79. Parameter(wrap([Text("1")]), wrap([Text("biz")]),
  80. showkey=False),
  81. Parameter(wrap([Text("2")]), wrap([Text("buzz")]),
  82. showkey=False),
  83. Parameter(wrap([Text("3")]), wrap([Text("buff")])),
  84. Parameter(wrap([Text("3")]), wrap([Text("baff")]),
  85. showkey=False)])])),
  86. ]
  87. for test, valid in tests:
  88. self.assertWikicodeEqual(valid, self.builder.build(test))
  89. def test_argument(self):
  90. """tests for building Argument nodes"""
  91. tests = [
  92. ([tokens.ArgumentOpen(), tokens.Text(text="foobar"),
  93. tokens.ArgumentClose()],
  94. wrap([Argument(wrap([Text("foobar")]))])),
  95. ([tokens.ArgumentOpen(), tokens.Text(text="spam"),
  96. tokens.Text(text="eggs"), tokens.ArgumentClose()],
  97. wrap([Argument(wrap([Text("spam"), Text("eggs")]))])),
  98. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  99. tokens.ArgumentSeparator(), tokens.Text(text="bar"),
  100. tokens.ArgumentClose()],
  101. wrap([Argument(wrap([Text("foo")]), wrap([Text("bar")]))])),
  102. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  103. tokens.Text(text="bar"), tokens.ArgumentSeparator(),
  104. tokens.Text(text="baz"), tokens.Text(text="biz"),
  105. tokens.ArgumentClose()],
  106. wrap([Argument(wrap([Text("foo"), Text("bar")]),
  107. wrap([Text("baz"), Text("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(wrap([Text("foobar")]))])),
  117. ([tokens.WikilinkOpen(), tokens.Text(text="spam"),
  118. tokens.Text(text="eggs"), tokens.WikilinkClose()],
  119. wrap([Wikilink(wrap([Text("spam"), Text("eggs")]))])),
  120. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  121. tokens.WikilinkSeparator(), tokens.Text(text="bar"),
  122. tokens.WikilinkClose()],
  123. wrap([Wikilink(wrap([Text("foo")]), wrap([Text("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(wrap([Text("foo"), Text("bar")]),
  129. wrap([Text("baz"), Text("biz")]))])),
  130. ]
  131. for test, valid in tests:
  132. self.assertWikicodeEqual(valid, self.builder.build(test))
  133. def test_html_entity(self):
  134. """tests for building HTMLEntity nodes"""
  135. tests = [
  136. ([tokens.HTMLEntityStart(), tokens.Text(text="nbsp"),
  137. tokens.HTMLEntityEnd()],
  138. wrap([HTMLEntity("nbsp", named=True, hexadecimal=False)])),
  139. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  140. tokens.Text(text="107"), tokens.HTMLEntityEnd()],
  141. wrap([HTMLEntity("107", named=False, hexadecimal=False)])),
  142. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  143. tokens.HTMLEntityHex(char="X"), tokens.Text(text="6B"),
  144. tokens.HTMLEntityEnd()],
  145. wrap([HTMLEntity("6B", named=False, hexadecimal=True,
  146. hex_char="X")])),
  147. ]
  148. for test, valid in tests:
  149. self.assertWikicodeEqual(valid, self.builder.build(test))
  150. def test_heading(self):
  151. """tests for building Heading nodes"""
  152. tests = [
  153. ([tokens.HeadingStart(level=2), tokens.Text(text="foobar"),
  154. tokens.HeadingEnd()],
  155. wrap([Heading(wrap([Text("foobar")]), 2)])),
  156. ([tokens.HeadingStart(level=4), tokens.Text(text="spam"),
  157. tokens.Text(text="eggs"), tokens.HeadingEnd()],
  158. wrap([Heading(wrap([Text("spam"), Text("eggs")]), 4)])),
  159. ]
  160. for test, valid in tests:
  161. self.assertWikicodeEqual(valid, self.builder.build(test))
  162. def test_comment(self):
  163. """tests for building Comment nodes"""
  164. tests = [
  165. ([tokens.CommentStart(), tokens.Text(text="foobar"),
  166. tokens.CommentEnd()],
  167. wrap([Comment(wrap([Text("foobar")]))])),
  168. ([tokens.CommentStart(), tokens.Text(text="spam"),
  169. tokens.Text(text="eggs"), tokens.CommentEnd()],
  170. wrap([Comment(wrap([Text("spam"), Text("eggs")]))])),
  171. ]
  172. for test, valid in tests:
  173. self.assertWikicodeEqual(valid, self.builder.build(test))
  174. @unittest.skip("holding this until feature/html_tags is ready")
  175. def test_tag(self):
  176. """tests for building Tag nodes"""
  177. pass
  178. def test_integration(self):
  179. """a test for building a combination of templates together"""
  180. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  181. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  182. tokens.TemplateOpen(), tokens.TemplateOpen(),
  183. tokens.Text(text="foo"), tokens.TemplateClose(),
  184. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  185. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  186. tokens.Text(text="biz"), tokens.TemplateClose(),
  187. tokens.Text(text="buzz"), tokens.TemplateClose(),
  188. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  189. tokens.TemplateOpen(), tokens.Text(text="bin"),
  190. tokens.TemplateClose(), tokens.TemplateClose()]
  191. valid = wrap(
  192. [Template(wrap([Template(wrap([Template(wrap([Template(wrap([Text(
  193. "foo")])), Text("bar")]), params=[Parameter(wrap([Text("baz")]),
  194. wrap([Text("biz")]))]), Text("buzz")])), Text("usr")]), params=[
  195. Parameter(wrap([Text("1")]), wrap([Template(wrap([Text("bin")]))]),
  196. showkey=False)])])
  197. self.assertWikicodeEqual(valid, self.builder.build(test))
  198. def test_integration2(self):
  199. """an even more audacious test for building a horrible wikicode mess"""
  200. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  201. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  202. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  203. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  204. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  205. tokens.WikilinkOpen(), tokens.Text(text="d"),
  206. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  207. tokens.Text(text="e"), tokens.ArgumentClose(),
  208. tokens.TemplateClose(), tokens.TemplateClose(),
  209. tokens.WikilinkOpen(), tokens.Text(text="f"),
  210. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  211. tokens.Text(text="g"), tokens.ArgumentClose(),
  212. tokens.CommentStart(), tokens.Text(text="h"),
  213. tokens.CommentEnd(), tokens.WikilinkClose(),
  214. tokens.TemplateOpen(), tokens.Text(text="i"),
  215. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  216. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  217. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  218. tokens.TemplateClose()]
  219. valid = wrap(
  220. [Template(wrap([Text("a")]), params=[Parameter(wrap([Text("1")]),
  221. wrap([Text("b")]), showkey=False), Parameter(wrap([Text("2")]),
  222. wrap([Template(wrap([Text("c")]), params=[Parameter(wrap([Text("1")
  223. ]), wrap([Wikilink(wrap([Text("d")])), Argument(wrap([Text("e")]))]
  224. ), showkey=False)])]), showkey=False)]), Wikilink(wrap([Text("f")]
  225. ), wrap([Argument(wrap([Text("g")])), Comment(wrap([Text("h")]))])
  226. ), Template(wrap([Text("i")]), params=[Parameter(wrap([Text("j")]),
  227. wrap([HTMLEntity("nbsp", named=True)]))])])
  228. self.assertWikicodeEqual(valid, self.builder.build(test))
  229. if __name__ == "__main__":
  230. unittest.main(verbosity=2)