A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

conftest.py 6.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright (C) 2012-2021 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from mwparserfromhell.nodes import (Argument, Comment, ExternalLink, Heading,
  21. HTMLEntity, Tag, Template, Text, Wikilink)
  22. from mwparserfromhell.smart_list import SmartList
  23. from mwparserfromhell.wikicode import Wikicode
  24. wrap = lambda L: Wikicode(SmartList(L))
  25. wraptext = lambda *args: wrap([Text(t) for t in args])
  26. def _assert_node_equal(expected, actual):
  27. """Assert that two Nodes have the same type and have the same data."""
  28. registry = {
  29. Argument: _assert_argument_node_equal,
  30. Comment: _assert_comment_node_equal,
  31. ExternalLink: _assert_external_link_node_equal,
  32. Heading: _assert_heading_node_equal,
  33. HTMLEntity: _assert_html_entity_node_equal,
  34. Tag: _assert_tag_node_equal,
  35. Template: _assert_template_node_equal,
  36. Text: _assert_text_node_equal,
  37. Wikilink: _assert_wikilink_node_equal,
  38. }
  39. # pylint: disable=unidiomatic-typecheck
  40. assert type(expected) == type(actual)
  41. registry[type(expected)](expected, actual)
  42. def _assert_argument_node_equal(expected, actual):
  43. """Assert that two Argument nodes have the same data."""
  44. assert_wikicode_equal(expected.name, actual.name)
  45. if expected.default is not None:
  46. assert_wikicode_equal(expected.default, actual.default)
  47. else:
  48. assert actual.default is None
  49. def _assert_comment_node_equal(expected, actual):
  50. """Assert that two Comment nodes have the same data."""
  51. assert expected.contents == actual.contents
  52. def _assert_external_link_node_equal(expected, actual):
  53. """Assert that two ExternalLink nodes have the same data."""
  54. assert_wikicode_equal(expected.url, actual.url)
  55. if expected.title is not None:
  56. assert_wikicode_equal(expected.title, actual.title)
  57. else:
  58. assert actual.title is None
  59. assert expected.brackets is actual.brackets
  60. assert expected.suppress_space is actual.suppress_space
  61. def _assert_heading_node_equal(expected, actual):
  62. """Assert that two Heading nodes have the same data."""
  63. assert_wikicode_equal(expected.title, actual.title)
  64. assert expected.level == actual.level
  65. def _assert_html_entity_node_equal(expected, actual):
  66. """Assert that two HTMLEntity nodes have the same data."""
  67. assert expected.value == actual.value
  68. assert expected.named is actual.named
  69. assert expected.hexadecimal is actual.hexadecimal
  70. assert expected.hex_char == actual.hex_char
  71. def _assert_tag_node_equal(expected, actual):
  72. """Assert that two Tag nodes have the same data."""
  73. assert_wikicode_equal(expected.tag, actual.tag)
  74. if expected.contents is not None:
  75. assert_wikicode_equal(expected.contents, actual.contents)
  76. else:
  77. assert actual.contents is None
  78. length = len(expected.attributes)
  79. assert length == len(actual.attributes)
  80. for i in range(length):
  81. exp_attr = expected.attributes[i]
  82. act_attr = actual.attributes[i]
  83. assert_wikicode_equal(exp_attr.name, act_attr.name)
  84. if exp_attr.value is not None:
  85. assert_wikicode_equal(exp_attr.value, act_attr.value)
  86. assert exp_attr.quotes == act_attr.quotes
  87. else:
  88. assert act_attr.value is None
  89. assert exp_attr.pad_first == act_attr.pad_first
  90. assert exp_attr.pad_before_eq == act_attr.pad_before_eq
  91. assert exp_attr.pad_after_eq == act_attr.pad_after_eq
  92. assert expected.wiki_markup == actual.wiki_markup
  93. assert expected.self_closing is actual.self_closing
  94. assert expected.invalid is actual.invalid
  95. assert expected.implicit is actual.implicit
  96. assert expected.padding == actual.padding
  97. assert_wikicode_equal(expected.closing_tag, actual.closing_tag)
  98. def _assert_template_node_equal(expected, actual):
  99. """Assert that two Template nodes have the same data."""
  100. assert_wikicode_equal(expected.name, actual.name)
  101. length = len(expected.params)
  102. assert length == len(actual.params)
  103. for i in range(length):
  104. exp_param = expected.params[i]
  105. act_param = actual.params[i]
  106. assert_wikicode_equal(exp_param.name, act_param.name)
  107. assert_wikicode_equal(exp_param.value, act_param.value)
  108. assert exp_param.showkey is act_param.showkey
  109. def _assert_text_node_equal(expected, actual):
  110. """Assert that two Text nodes have the same data."""
  111. assert expected.value == actual.value
  112. def _assert_wikilink_node_equal(expected, actual):
  113. """Assert that two Wikilink nodes have the same data."""
  114. assert_wikicode_equal(expected.title, actual.title)
  115. if expected.text is not None:
  116. assert_wikicode_equal(expected.text, actual.text)
  117. else:
  118. assert actual.text is None
  119. def assert_wikicode_equal(expected, actual):
  120. """Assert that two Wikicode objects have the same data."""
  121. assert isinstance(actual, Wikicode)
  122. length = len(expected.nodes)
  123. assert length == len(actual.nodes)
  124. for i in range(length):
  125. _assert_node_equal(expected.get(i), actual.get(i))