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.
 
 
 
 

135 lines
6.3 KiB

  1. #
  2. # Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. from unittest import TestCase
  22. from mwparserfromhell.nodes import (Argument, Comment, Heading, HTMLEntity,
  23. Tag, Template, Text, Wikilink)
  24. from mwparserfromhell.smart_list import SmartList
  25. from mwparserfromhell.wikicode import Wikicode
  26. wrap = lambda L: Wikicode(SmartList(L))
  27. wraptext = lambda *args: wrap([Text(t) for t in args])
  28. class TreeEqualityTestCase(TestCase):
  29. """A base test case with support for comparing the equality of node trees.
  30. This adds a number of type equality functions, for Wikicode, Text,
  31. Templates, and Wikilinks.
  32. """
  33. def assertNodeEqual(self, expected, actual):
  34. """Assert that two Nodes have the same type and have the same data."""
  35. registry = {
  36. Argument: self.assertArgumentNodeEqual,
  37. Comment: self.assertCommentNodeEqual,
  38. Heading: self.assertHeadingNodeEqual,
  39. HTMLEntity: self.assertHTMLEntityNodeEqual,
  40. Tag: self.assertTagNodeEqual,
  41. Template: self.assertTemplateNodeEqual,
  42. Text: self.assertTextNodeEqual,
  43. Wikilink: self.assertWikilinkNodeEqual
  44. }
  45. for nodetype in registry:
  46. if isinstance(expected, nodetype):
  47. self.assertIsInstance(actual, nodetype)
  48. registry[nodetype](expected, actual)
  49. def assertArgumentNodeEqual(self, expected, actual):
  50. """Assert that two Argument nodes have the same data."""
  51. self.assertWikicodeEqual(expected.name, actual.name)
  52. if expected.default is not None:
  53. self.assertWikicodeEqual(expected.default, actual.default)
  54. else:
  55. self.assertIs(None, actual.default)
  56. def assertCommentNodeEqual(self, expected, actual):
  57. """Assert that two Comment nodes have the same data."""
  58. self.assertEqual(expected.contents, actual.contents)
  59. def assertHeadingNodeEqual(self, expected, actual):
  60. """Assert that two Heading nodes have the same data."""
  61. self.assertWikicodeEqual(expected.title, actual.title)
  62. self.assertEqual(expected.level, actual.level)
  63. def assertHTMLEntityNodeEqual(self, expected, actual):
  64. """Assert that two HTMLEntity nodes have the same data."""
  65. self.assertEqual(expected.value, actual.value)
  66. self.assertIs(expected.named, actual.named)
  67. self.assertIs(expected.hexadecimal, actual.hexadecimal)
  68. self.assertEqual(expected.hex_char, actual.hex_char)
  69. def assertTagNodeEqual(self, expected, actual):
  70. """Assert that two Tag nodes have the same data."""
  71. self.assertWikicodeEqual(expected.tag, actual.tag)
  72. if expected.contents is not None:
  73. self.assertWikicodeEqual(expected.contents, actual.contents)
  74. length = len(expected.attributes)
  75. self.assertEqual(length, len(actual.attributes))
  76. for i in range(length):
  77. exp_attr = expected.attributes[i]
  78. act_attr = actual.attributes[i]
  79. self.assertWikicodeEqual(exp_attr.name, act_attr.name)
  80. if exp_attr.value is not None:
  81. self.assertWikicodeEqual(exp_attr.value, act_attr.value)
  82. self.assertEqual(exp_attr.quotes, act_attr.quotes)
  83. self.assertEqual(exp_attr.pad_first, act_attr.pad_first)
  84. self.assertEqual(exp_attr.pad_before_eq, act_attr.pad_before_eq)
  85. self.assertEqual(exp_attr.pad_after_eq, act_attr.pad_after_eq)
  86. self.assertEqual(expected.wiki_markup, actual.wiki_markup)
  87. self.assertIs(expected.self_closing, actual.self_closing)
  88. self.assertIs(expected.invalid, actual.invalid)
  89. self.assertIs(expected.implicit, actual.implicit)
  90. self.assertEqual(expected.padding, actual.padding)
  91. self.assertWikicodeEqual(expected.closing_tag, actual.closing_tag)
  92. def assertTemplateNodeEqual(self, expected, actual):
  93. """Assert that two Template nodes have the same data."""
  94. self.assertWikicodeEqual(expected.name, actual.name)
  95. length = len(expected.params)
  96. self.assertEqual(length, len(actual.params))
  97. for i in range(length):
  98. exp_param = expected.params[i]
  99. act_param = actual.params[i]
  100. self.assertWikicodeEqual(exp_param.name, act_param.name)
  101. self.assertWikicodeEqual(exp_param.value, act_param.value)
  102. self.assertIs(exp_param.showkey, act_param.showkey)
  103. def assertTextNodeEqual(self, expected, actual):
  104. """Assert that two Text nodes have the same data."""
  105. self.assertEqual(expected.value, actual.value)
  106. def assertWikilinkNodeEqual(self, expected, actual):
  107. """Assert that two Wikilink nodes have the same data."""
  108. self.assertWikicodeEqual(expected.title, actual.title)
  109. if expected.text is not None:
  110. self.assertWikicodeEqual(expected.text, actual.text)
  111. else:
  112. self.assertIs(None, actual.text)
  113. def assertWikicodeEqual(self, expected, actual):
  114. """Assert that two Wikicode objects have the same data."""
  115. self.assertIsInstance(actual, Wikicode)
  116. length = len(expected.nodes)
  117. self.assertEqual(length, len(actual.nodes))
  118. for i in range(length):
  119. self.assertNodeEqual(expected.get(i), actual.get(i))