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.
 
 
 
 

114 rader
5.1 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.wikicode import Wikicode
  28. class TreeEqualityTestCase(TestCase):
  29. """A base test case with support for comparing the equality of node trees.
  30. This adds a number of type equality functions, for Wikicode, Text,
  31. Templates, and Wikilinks.
  32. """
  33. def assertNodeEqual(self, expected, actual):
  34. """Assert that two Nodes have the same type and have the same data."""
  35. registry = {
  36. Argument: self.assertArgumentNodeEqual,
  37. Comment: self.assertCommentNodeEqual,
  38. Heading: self.assertHeadingNodeEqual,
  39. HTMLEntity: self.assertHTMLEntityNodeEqual,
  40. Tag: self.assertTagNodeEqual,
  41. Template: self.assertTemplateNodeEqual,
  42. Text: self.assertTextNodeEqual,
  43. Wikilink: self.assertWikilinkNodeEqual
  44. }
  45. for nodetype in registry:
  46. if isinstance(expected, nodetype):
  47. self.assertIsInstance(actual, nodetype)
  48. registry[nodetype](expected, actual)
  49. def assertArgumentNodeEqual(self, expected, actual):
  50. """Assert that two Argument nodes have the same data."""
  51. self.assertWikicodeEqual(expected.name, actual.name)
  52. if expected.default is not None:
  53. self.assertWikicodeEqual(expected.default, actual.default)
  54. else:
  55. self.assertIs(None, actual.default)
  56. def assertCommentNodeEqual(self, expected, actual):
  57. """Assert that two Comment nodes have the same data."""
  58. self.assertWikicodeEqual(expected.contents, actual.contents)
  59. def assertHeadingNodeEqual(self, expected, actual):
  60. """Assert that two Heading nodes have the same data."""
  61. self.assertWikicodeEqual(expected.title, actual.title)
  62. self.assertEqual(expected.level, actual.level)
  63. def assertHTMLEntityNodeEqual(self, expected, actual):
  64. """Assert that two HTMLEntity nodes have the same data."""
  65. self.assertEqual(expected.value, actual.value)
  66. self.assertIs(expected.named, actual.named)
  67. self.assertIs(expected.hexadecimal, actual.hexadecimal)
  68. self.assertEqual(expected.hex_char, actual.hex_char)
  69. def assertTagNodeEqual(self, expected, actual):
  70. """Assert that two Tag nodes have the same data."""
  71. self.fail("Holding this until feature/html_tags is ready.")
  72. def assertTemplateNodeEqual(self, expected, actual):
  73. """Assert that two Template nodes have the same data."""
  74. self.assertWikicodeEqual(expected.name, actual.name)
  75. length = len(expected.params)
  76. self.assertEqual(length, len(actual.params))
  77. for i in range(length):
  78. exp_param = expected.params[i]
  79. act_param = actual.params[i]
  80. self.assertWikicodeEqual(exp_param.name, act_param.name)
  81. self.assertWikicodeEqual(exp_param.value, act_param.value)
  82. self.assertIs(exp_param.showkey, act_param.showkey)
  83. def assertTextNodeEqual(self, expected, actual):
  84. """Assert that two Text nodes have the same data."""
  85. self.assertEqual(expected.value, actual.value)
  86. def assertWikilinkNodeEqual(self, expected, actual):
  87. """Assert that two Wikilink nodes have the same data."""
  88. self.assertWikicodeEqual(expected.title, actual.title)
  89. if expected.text is not None:
  90. self.assertWikicodeEqual(expected.text, actual.text)
  91. else:
  92. self.assertIs(None, actual.text)
  93. def assertWikicodeEqual(self, expected, actual):
  94. """Assert that two Wikicode objects have the same data."""
  95. self.assertIsInstance(actual, Wikicode)
  96. length = len(expected.nodes)
  97. self.assertEqual(length, len(actual.nodes))
  98. for i in range(length):
  99. self.assertNodeEqual(expected.get(i), actual.get(i))