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.
 
 
 
 

139 linhas
6.5 KiB

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