A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

_test_tree_equality.py 6.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.assertEqual(expected.type, actual.type)
  82. self.assertWikicodeEqual(expected.tag, actual.tag)
  83. if expected.contents is not None:
  84. self.assertWikicodeEqual(expected.contents, actual.contents)
  85. length = len(expected.attributes)
  86. self.assertEqual(length, len(actual.attributes))
  87. for i in range(length):
  88. exp_attr = expected.attributes[i]
  89. act_attr = actual.attributes[i]
  90. self.assertWikicodeEqual(exp_attr.name, act_attr.name)
  91. if exp_attr.value is not None:
  92. self.assertWikicodeEqual(exp_attr.value, act_attr.value)
  93. self.assertIs(exp_attr.quoted, act_attr.quoted)
  94. self.assertEqual(exp.attr.padding, act_attr.padding)
  95. self.assertIs(expected.showtag, actual.showtag)
  96. self.assertIs(expected.self_closing, actual.self_closing)
  97. self.assertEqual(expected.padding, actual.padding)
  98. self.assertWikicodeEqual(expected.closing_tag, actual.closing_tag)
  99. def assertTemplateNodeEqual(self, expected, actual):
  100. """Assert that two Template nodes have the same data."""
  101. self.assertWikicodeEqual(expected.name, actual.name)
  102. length = len(expected.params)
  103. self.assertEqual(length, len(actual.params))
  104. for i in range(length):
  105. exp_param = expected.params[i]
  106. act_param = actual.params[i]
  107. self.assertWikicodeEqual(exp_param.name, act_param.name)
  108. self.assertWikicodeEqual(exp_param.value, act_param.value)
  109. self.assertIs(exp_param.showkey, act_param.showkey)
  110. def assertTextNodeEqual(self, expected, actual):
  111. """Assert that two Text nodes have the same data."""
  112. self.assertEqual(expected.value, actual.value)
  113. def assertWikilinkNodeEqual(self, expected, actual):
  114. """Assert that two Wikilink nodes have the same data."""
  115. self.assertWikicodeEqual(expected.title, actual.title)
  116. if expected.text is not None:
  117. self.assertWikicodeEqual(expected.text, actual.text)
  118. else:
  119. self.assertIs(None, actual.text)
  120. def assertWikicodeEqual(self, expected, actual):
  121. """Assert that two Wikicode objects have the same data."""
  122. self.assertIsInstance(actual, Wikicode)
  123. length = len(expected.nodes)
  124. self.assertEqual(length, len(actual.nodes))
  125. for i in range(length):
  126. self.assertNodeEqual(expected.get(i), actual.get(i))