A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

_test_tree_equality.py 6.6 KiB

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