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.
 
 
 
 

106 lines
4.2 KiB

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