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

6 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #
  2. # Copyright (C) 2012-2019 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.nodes.extras import Attribute, Parameter
  25. from mwparserfromhell.smart_list import SmartList
  26. from mwparserfromhell.wikicode import Wikicode
  27. wrap = lambda L: Wikicode(SmartList(L))
  28. wraptext = lambda *args: wrap([Text(t) for t in args])
  29. class TreeEqualityTestCase(TestCase):
  30. """A base test case with support for comparing the equality of node trees.
  31. This adds a number of type equality functions, for Wikicode, Text,
  32. Templates, and Wikilinks.
  33. """
  34. def assertNodeEqual(self, expected, actual):
  35. """Assert that two Nodes have the same type and have the same data."""
  36. registry = {
  37. Argument: self.assertArgumentNodeEqual,
  38. Comment: self.assertCommentNodeEqual,
  39. Heading: self.assertHeadingNodeEqual,
  40. HTMLEntity: self.assertHTMLEntityNodeEqual,
  41. Tag: self.assertTagNodeEqual,
  42. Template: self.assertTemplateNodeEqual,
  43. Text: self.assertTextNodeEqual,
  44. Wikilink: self.assertWikilinkNodeEqual
  45. }
  46. for nodetype in registry:
  47. if isinstance(expected, nodetype):
  48. self.assertIsInstance(actual, nodetype)
  49. registry[nodetype](expected, actual)
  50. def assertArgumentNodeEqual(self, expected, actual):
  51. """Assert that two Argument nodes have the same data."""
  52. self.assertWikicodeEqual(expected.name, actual.name)
  53. if expected.default is not None:
  54. self.assertWikicodeEqual(expected.default, actual.default)
  55. else:
  56. self.assertIs(None, actual.default)
  57. def assertCommentNodeEqual(self, expected, actual):
  58. """Assert that two Comment nodes have the same data."""
  59. self.assertEqual(expected.contents, actual.contents)
  60. def assertHeadingNodeEqual(self, expected, actual):
  61. """Assert that two Heading nodes have the same data."""
  62. self.assertWikicodeEqual(expected.title, actual.title)
  63. self.assertEqual(expected.level, actual.level)
  64. def assertHTMLEntityNodeEqual(self, expected, actual):
  65. """Assert that two HTMLEntity nodes have the same data."""
  66. self.assertEqual(expected.value, actual.value)
  67. self.assertIs(expected.named, actual.named)
  68. self.assertIs(expected.hexadecimal, actual.hexadecimal)
  69. self.assertEqual(expected.hex_char, actual.hex_char)
  70. def assertTagNodeEqual(self, expected, actual):
  71. """Assert that two Tag nodes have the same data."""
  72. self.assertWikicodeEqual(expected.tag, actual.tag)
  73. if expected.contents is not None:
  74. self.assertWikicodeEqual(expected.contents, actual.contents)
  75. length = len(expected.attributes)
  76. self.assertEqual(length, len(actual.attributes))
  77. for i in range(length):
  78. exp_attr = expected.attributes[i]
  79. act_attr = actual.attributes[i]
  80. self.assertWikicodeEqual(exp_attr.name, act_attr.name)
  81. if exp_attr.value is not None:
  82. self.assertWikicodeEqual(exp_attr.value, act_attr.value)
  83. self.assertEqual(exp_attr.quotes, act_attr.quotes)
  84. self.assertEqual(exp_attr.pad_first, act_attr.pad_first)
  85. self.assertEqual(exp_attr.pad_before_eq, act_attr.pad_before_eq)
  86. self.assertEqual(exp_attr.pad_after_eq, act_attr.pad_after_eq)
  87. self.assertEqual(expected.wiki_markup, actual.wiki_markup)
  88. self.assertIs(expected.self_closing, actual.self_closing)
  89. self.assertIs(expected.invalid, actual.invalid)
  90. self.assertIs(expected.implicit, actual.implicit)
  91. self.assertEqual(expected.padding, actual.padding)
  92. self.assertWikicodeEqual(expected.closing_tag, actual.closing_tag)
  93. def assertTemplateNodeEqual(self, expected, actual):
  94. """Assert that two Template nodes have the same data."""
  95. self.assertWikicodeEqual(expected.name, actual.name)
  96. length = len(expected.params)
  97. self.assertEqual(length, len(actual.params))
  98. for i in range(length):
  99. exp_param = expected.params[i]
  100. act_param = actual.params[i]
  101. self.assertWikicodeEqual(exp_param.name, act_param.name)
  102. self.assertWikicodeEqual(exp_param.value, act_param.value)
  103. self.assertIs(exp_param.showkey, act_param.showkey)
  104. def assertTextNodeEqual(self, expected, actual):
  105. """Assert that two Text nodes have the same data."""
  106. self.assertEqual(expected.value, actual.value)
  107. def assertWikilinkNodeEqual(self, expected, actual):
  108. """Assert that two Wikilink nodes have the same data."""
  109. self.assertWikicodeEqual(expected.title, actual.title)
  110. if expected.text is not None:
  111. self.assertWikicodeEqual(expected.text, actual.text)
  112. else:
  113. self.assertIs(None, actual.text)
  114. def assertWikicodeEqual(self, expected, actual):
  115. """Assert that two Wikicode objects have the same data."""
  116. self.assertIsInstance(actual, Wikicode)
  117. length = len(expected.nodes)
  118. self.assertEqual(length, len(actual.nodes))
  119. for i in range(length):
  120. self.assertNodeEqual(expected.get(i), actual.get(i))