A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

189 lines
8.2 KiB

  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. pass
  153. def test_comment(self):
  154. """tests for building Comment nodes"""
  155. pass
  156. @unittest.skip("holding this until feature/html_tags is ready")
  157. def test_tag(self):
  158. """tests for building Tag nodes"""
  159. pass
  160. if __name__ == "__main__":
  161. unittest.main(verbosity=2)