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ů.
 
 
 
 

118 řádky
5.2 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 t: wrap([Text(t)])
  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.fail("Holding this until feature/html_tags is ready.")
  75. def assertTemplateNodeEqual(self, expected, actual):
  76. """Assert that two Template nodes have the same data."""
  77. self.assertWikicodeEqual(expected.name, actual.name)
  78. length = len(expected.params)
  79. self.assertEqual(length, len(actual.params))
  80. for i in range(length):
  81. exp_param = expected.params[i]
  82. act_param = actual.params[i]
  83. self.assertWikicodeEqual(exp_param.name, act_param.name)
  84. self.assertWikicodeEqual(exp_param.value, act_param.value)
  85. self.assertIs(exp_param.showkey, act_param.showkey)
  86. def assertTextNodeEqual(self, expected, actual):
  87. """Assert that two Text nodes have the same data."""
  88. self.assertEqual(expected.value, actual.value)
  89. def assertWikilinkNodeEqual(self, expected, actual):
  90. """Assert that two Wikilink nodes have the same data."""
  91. self.assertWikicodeEqual(expected.title, actual.title)
  92. if expected.text is not None:
  93. self.assertWikicodeEqual(expected.text, actual.text)
  94. else:
  95. self.assertIs(None, actual.text)
  96. def assertWikicodeEqual(self, expected, actual):
  97. """Assert that two Wikicode objects have the same data."""
  98. self.assertIsInstance(actual, Wikicode)
  99. length = len(expected.nodes)
  100. self.assertEqual(length, len(actual.nodes))
  101. for i in range(length):
  102. self.assertNodeEqual(expected.get(i), actual.get(i))