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.
 
 
 
 

107 lines
4.6 KiB

  1. #
  2. # Copyright (C) 2012-2019 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. import pytest
  22. from mwparserfromhell.nodes import Template
  23. from mwparserfromhell.nodes.extras import Attribute
  24. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  25. class TestAttribute(TreeEqualityTestCase):
  26. """Test cases for the Attribute node extra."""
  27. def test_unicode(self):
  28. """test Attribute.__unicode__()"""
  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(self):
  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. self.assertWikicodeEqual(wrap([Template(wraptext("id"))]), node.name)
  48. def test_value(self):
  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. self.assertWikicodeEqual(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. self.assertWikicodeEqual(wraptext("foo bar baz"), node2.value)
  60. assert '"' == node2.quotes
  61. node2.value = 'foo "bar" baz'
  62. self.assertWikicodeEqual(wraptext('foo "bar" baz'), node2.value)
  63. assert "'" == node2.quotes
  64. node2.value = "foo 'bar' baz"
  65. self.assertWikicodeEqual(wraptext("foo 'bar' baz"), node2.value)
  66. assert '"' == node2.quotes
  67. node2.value = "fo\"o 'bar' b\"az"
  68. self.assertWikicodeEqual(wraptext("fo\"o 'bar' b\"az"), node2.value)
  69. assert '"' == node2.quotes
  70. def test_quotes(self):
  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(self):
  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)