A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

114 строки
4.3 KiB

  1. # Copyright (C) 2012-2025 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. """
  21. Test cases for the Attribute node extra.
  22. """
  23. from __future__ import annotations
  24. import pytest
  25. from mwparserfromhell.nodes import Template
  26. from mwparserfromhell.nodes.extras import Attribute
  27. from .conftest import assert_wikicode_equal, wrap, wraptext
  28. def test_str():
  29. """test Attribute.__str__()"""
  30. node = Attribute(wraptext("foo"))
  31. assert " foo" == str(node)
  32. node2 = Attribute(wraptext("foo"), wraptext("bar"))
  33. assert ' foo="bar"' == str(node2)
  34. node3 = Attribute(wraptext("a"), wraptext("b"), '"', "", " ", " ")
  35. assert 'a = "b"' == str(node3)
  36. node4 = Attribute(wraptext("a"), wraptext("b"), "'", "", " ", " ")
  37. assert "a = 'b'" == str(node4)
  38. node5 = Attribute(wraptext("a"), wraptext("b"), None, "", " ", " ")
  39. assert "a = b" == str(node5)
  40. node6 = Attribute(wraptext("a"), wrap([]), None, " ", "", " ")
  41. assert " a= " == str(node6)
  42. def test_name():
  43. """test getter/setter for the name attribute"""
  44. name = wraptext("id")
  45. node = Attribute(name, wraptext("bar"))
  46. assert name is node.name
  47. node.name = "{{id}}"
  48. assert_wikicode_equal(wrap([Template(wraptext("id"))]), node.name)
  49. def test_value():
  50. """test getter/setter for the value attribute"""
  51. value = wraptext("foo")
  52. node = Attribute(wraptext("id"), value)
  53. assert value is node.value
  54. node.value = "{{bar}}"
  55. assert_wikicode_equal(wrap([Template(wraptext("bar"))]), node.value)
  56. node.value = None
  57. assert None is node.value
  58. node2 = Attribute(wraptext("id"), wraptext("foo"), None)
  59. node2.value = "foo bar baz"
  60. assert_wikicode_equal(wraptext("foo bar baz"), node2.value)
  61. assert '"' == node2.quotes
  62. node2.value = 'foo "bar" baz'
  63. assert_wikicode_equal(wraptext('foo "bar" baz'), node2.value)
  64. assert "'" == node2.quotes
  65. node2.value = "foo 'bar' baz"
  66. assert_wikicode_equal(wraptext("foo 'bar' baz"), node2.value)
  67. assert '"' == node2.quotes
  68. node2.value = "fo\"o 'bar' b\"az"
  69. assert_wikicode_equal(wraptext("fo\"o 'bar' b\"az"), node2.value)
  70. assert '"' == node2.quotes
  71. def test_quotes():
  72. """test getter/setter for the quotes attribute"""
  73. node1 = Attribute(wraptext("id"), wraptext("foo"), None)
  74. node2 = Attribute(wraptext("id"), wraptext("bar"))
  75. node3 = Attribute(wraptext("id"), wraptext("foo bar baz"))
  76. assert None is node1.quotes
  77. assert '"' == node2.quotes
  78. node1.quotes = "'"
  79. node2.quotes = None
  80. assert "'" == node1.quotes
  81. assert None is node2.quotes
  82. with pytest.raises(ValueError):
  83. node1.__setattr__("quotes", "foobar")
  84. with pytest.raises(ValueError):
  85. node3.__setattr__("quotes", None)
  86. with pytest.raises(ValueError):
  87. Attribute(wraptext("id"), wraptext("foo bar baz"), None)
  88. def test_padding():
  89. """test getter/setter for the padding attributes"""
  90. for pad in ["pad_first", "pad_before_eq", "pad_after_eq"]:
  91. node = Attribute(wraptext("id"), wraptext("foo"), **{pad: "\n"})
  92. assert "\n" == getattr(node, pad)
  93. setattr(node, pad, " ")
  94. assert " " == getattr(node, pad)
  95. setattr(node, pad, None)
  96. assert "" == getattr(node, pad)
  97. with pytest.raises(ValueError):
  98. node.__setattr__(pad, True)