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.
 
 
 
 

143 rivejä
6.4 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. from unittest import TestCase
  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.smart_list import SmartList
  28. from mwparserfromhell.wikicode import Wikicode
  29. wrap = lambda L: Wikicode(SmartList(L))
  30. wraptext = lambda *args: wrap([Text(t) for t in args])
  31. def getnodes(code):
  32. """Iterate over all child nodes of a given parent node.
  33. Imitates Wikicode._get_all_nodes().
  34. """
  35. for node in code.nodes:
  36. for context, child in node.__iternodes__(getnodes):
  37. yield child
  38. class TreeEqualityTestCase(TestCase):
  39. """A base test case with support for comparing the equality of node trees.
  40. This adds a number of type equality functions, for Wikicode, Text,
  41. Templates, and Wikilinks.
  42. """
  43. def assertNodeEqual(self, expected, actual):
  44. """Assert that two Nodes have the same type and have the same data."""
  45. registry = {
  46. Argument: self.assertArgumentNodeEqual,
  47. Comment: self.assertCommentNodeEqual,
  48. Heading: self.assertHeadingNodeEqual,
  49. HTMLEntity: self.assertHTMLEntityNodeEqual,
  50. Tag: self.assertTagNodeEqual,
  51. Template: self.assertTemplateNodeEqual,
  52. Text: self.assertTextNodeEqual,
  53. Wikilink: self.assertWikilinkNodeEqual
  54. }
  55. for nodetype in registry:
  56. if isinstance(expected, nodetype):
  57. self.assertIsInstance(actual, nodetype)
  58. registry[nodetype](expected, actual)
  59. def assertArgumentNodeEqual(self, expected, actual):
  60. """Assert that two Argument nodes have the same data."""
  61. self.assertWikicodeEqual(expected.name, actual.name)
  62. if expected.default is not None:
  63. self.assertWikicodeEqual(expected.default, actual.default)
  64. else:
  65. self.assertIs(None, actual.default)
  66. def assertCommentNodeEqual(self, expected, actual):
  67. """Assert that two Comment nodes have the same data."""
  68. self.assertWikicodeEqual(expected.contents, actual.contents)
  69. def assertHeadingNodeEqual(self, expected, actual):
  70. """Assert that two Heading nodes have the same data."""
  71. self.assertWikicodeEqual(expected.title, actual.title)
  72. self.assertEqual(expected.level, actual.level)
  73. def assertHTMLEntityNodeEqual(self, expected, actual):
  74. """Assert that two HTMLEntity nodes have the same data."""
  75. self.assertEqual(expected.value, actual.value)
  76. self.assertIs(expected.named, actual.named)
  77. self.assertIs(expected.hexadecimal, actual.hexadecimal)
  78. self.assertEqual(expected.hex_char, actual.hex_char)
  79. def assertTagNodeEqual(self, expected, actual):
  80. """Assert that two Tag nodes have the same data."""
  81. self.assertWikicodeEqual(expected.tag, actual.tag)
  82. if expected.contents is not None:
  83. self.assertWikicodeEqual(expected.contents, actual.contents)
  84. length = len(expected.attributes)
  85. self.assertEqual(length, len(actual.attributes))
  86. for i in range(length):
  87. exp_attr = expected.attributes[i]
  88. act_attr = actual.attributes[i]
  89. self.assertWikicodeEqual(exp_attr.name, act_attr.name)
  90. if exp_attr.value is not None:
  91. self.assertWikicodeEqual(exp_attr.value, act_attr.value)
  92. self.assertIs(exp_attr.quoted, act_attr.quoted)
  93. self.assertEqual(exp.attr.padding, act_attr.padding)
  94. self.assertIs(expected.showtag, actual.showtag)
  95. self.assertIs(expected.self_closing, actual.self_closing)
  96. self.assertEqual(expected.padding, actual.padding)
  97. self.assertWikicodeEqual(expected.closing_tag, actual.closing_tag)
  98. def assertTemplateNodeEqual(self, expected, actual):
  99. """Assert that two Template nodes have the same data."""
  100. self.assertWikicodeEqual(expected.name, actual.name)
  101. length = len(expected.params)
  102. self.assertEqual(length, len(actual.params))
  103. for i in range(length):
  104. exp_param = expected.params[i]
  105. act_param = actual.params[i]
  106. self.assertWikicodeEqual(exp_param.name, act_param.name)
  107. self.assertWikicodeEqual(exp_param.value, act_param.value)
  108. self.assertIs(exp_param.showkey, act_param.showkey)
  109. def assertTextNodeEqual(self, expected, actual):
  110. """Assert that two Text nodes have the same data."""
  111. self.assertEqual(expected.value, actual.value)
  112. def assertWikilinkNodeEqual(self, expected, actual):
  113. """Assert that two Wikilink nodes have the same data."""
  114. self.assertWikicodeEqual(expected.title, actual.title)
  115. if expected.text is not None:
  116. self.assertWikicodeEqual(expected.text, actual.text)
  117. else:
  118. self.assertIs(None, actual.text)
  119. def assertWikicodeEqual(self, expected, actual):
  120. """Assert that two Wikicode objects have the same data."""
  121. self.assertIsInstance(actual, Wikicode)
  122. length = len(expected.nodes)
  123. self.assertEqual(length, len(actual.nodes))
  124. for i in range(length):
  125. self.assertNodeEqual(expected.get(i), actual.get(i))