A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

134 строки
6.3 KiB

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