A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

138 rindas
6.4 KiB

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