A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

127 行
5.5 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.fail("Holding this until feature/html_tags is ready.")
  82. def assertTemplateNodeEqual(self, expected, actual):
  83. """Assert that two Template nodes have the same data."""
  84. self.assertWikicodeEqual(expected.name, actual.name)
  85. length = len(expected.params)
  86. self.assertEqual(length, len(actual.params))
  87. for i in range(length):
  88. exp_param = expected.params[i]
  89. act_param = actual.params[i]
  90. self.assertWikicodeEqual(exp_param.name, act_param.name)
  91. self.assertWikicodeEqual(exp_param.value, act_param.value)
  92. self.assertIs(exp_param.showkey, act_param.showkey)
  93. def assertTextNodeEqual(self, expected, actual):
  94. """Assert that two Text nodes have the same data."""
  95. self.assertEqual(expected.value, actual.value)
  96. def assertWikilinkNodeEqual(self, expected, actual):
  97. """Assert that two Wikilink nodes have the same data."""
  98. self.assertWikicodeEqual(expected.title, actual.title)
  99. if expected.text is not None:
  100. self.assertWikicodeEqual(expected.text, actual.text)
  101. else:
  102. self.assertIs(None, actual.text)
  103. def assertWikicodeEqual(self, expected, actual):
  104. """Assert that two Wikicode objects have the same data."""
  105. self.assertIsInstance(actual, Wikicode)
  106. length = len(expected.nodes)
  107. self.assertEqual(length, len(actual.nodes))
  108. for i in range(length):
  109. self.assertNodeEqual(expected.get(i), actual.get(i))