A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

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