A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

test_builder.py 9.1 KiB

pirms 11 gadiem
pirms 11 gadiem
pirms 11 gadiem
pirms 11 gadiem
pirms 11 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if __name__ == "__main__":
  179. unittest.main(verbosity=2)