A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

147 rader
6.6 KiB

  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.showtag, actual.showtag)
  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))