A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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