A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

79 lines
3.4 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 Template, Text, Wikilink
  25. from mwparserfromhell.nodes.extras import Parameter
  26. from mwparserfromhell.wikicode import Wikicode
  27. class TreeEqualityTestCase(TestCase):
  28. """A base test case with support for comparing the equality of node trees.
  29. This adds a number of type equality functions, for Wikicode, Text,
  30. Templates, and Wikilinks.
  31. """
  32. def assertNodeEqual(self, expected, actual):
  33. registry = {
  34. Text: self.assertTextNodeEqual,
  35. Template: self.assertTemplateNodeEqual,
  36. Wikilink: self.assertWikilinkNodeEqual
  37. }
  38. for nodetype in registry:
  39. if isinstance(expected, nodetype):
  40. self.assertIsInstance(actual, nodetype)
  41. registry[nodetype](expected, actual)
  42. def assertTextNodeEqual(self, expected, actual):
  43. """Assert that two Text nodes have the same data."""
  44. self.assertEqual(expected.value, actual.value)
  45. def assertTemplateNodeEqual(self, expected, actual):
  46. """Assert that two Template nodes have the same data."""
  47. self.assertWikicodeEqual(expected.name, actual.name)
  48. length = len(expected.params)
  49. self.assertEqual(length, len(actual.params))
  50. for i in range(length):
  51. exp_param = expected.params[i]
  52. act_param = actual.params[i]
  53. self.assertWikicodeEqual(exp_param.name, act_param.name)
  54. self.assertWikicodeEqual(exp_param.value, act_param.value)
  55. self.assertIs(exp_param.showkey, act_param.showkey)
  56. def assertWikilinkNodeEqual(self, expected, actual):
  57. """Assert that two Wikilink nodes have the same data."""
  58. self.assertWikicodeEqual(expected.title, actual.title)
  59. if expected.text is not None:
  60. self.assertWikicodeEqual(expected.text, actual.text)
  61. else:
  62. self.assertIs(None, actual.text)
  63. def assertWikicodeEqual(self, expected, actual):
  64. """Assert that two Wikicode objects have the same data."""
  65. self.assertIsInstance(actual, Wikicode)
  66. length = len(expected.nodes)
  67. self.assertEqual(length, len(actual.nodes))
  68. for i in range(length):
  69. self.assertNodeEqual(expected.get(i), actual.get(i))