A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

80 satır
3.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 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. """Assert that two Nodes have the same type and have the same data."""
  34. registry = {
  35. Text: self.assertTextNodeEqual,
  36. Template: self.assertTemplateNodeEqual,
  37. Wikilink: self.assertWikilinkNodeEqual
  38. }
  39. for nodetype in registry:
  40. if isinstance(expected, nodetype):
  41. self.assertIsInstance(actual, nodetype)
  42. registry[nodetype](expected, actual)
  43. def assertTextNodeEqual(self, expected, actual):
  44. """Assert that two Text nodes have the same data."""
  45. self.assertEqual(expected.value, actual.value)
  46. def assertTemplateNodeEqual(self, expected, actual):
  47. """Assert that two Template nodes have the same data."""
  48. self.assertWikicodeEqual(expected.name, actual.name)
  49. length = len(expected.params)
  50. self.assertEqual(length, len(actual.params))
  51. for i in range(length):
  52. exp_param = expected.params[i]
  53. act_param = actual.params[i]
  54. self.assertWikicodeEqual(exp_param.name, act_param.name)
  55. self.assertWikicodeEqual(exp_param.value, act_param.value)
  56. self.assertIs(exp_param.showkey, act_param.showkey)
  57. def assertWikilinkNodeEqual(self, expected, actual):
  58. """Assert that two Wikilink nodes have the same data."""
  59. self.assertWikicodeEqual(expected.title, actual.title)
  60. if expected.text is not None:
  61. self.assertWikicodeEqual(expected.text, actual.text)
  62. else:
  63. self.assertIs(None, actual.text)
  64. def assertWikicodeEqual(self, expected, actual):
  65. """Assert that two Wikicode objects have the same data."""
  66. self.assertIsInstance(actual, Wikicode)
  67. length = len(expected.nodes)
  68. self.assertEqual(length, len(actual.nodes))
  69. for i in range(length):
  70. self.assertNodeEqual(expected.get(i), actual.get(i))