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.

test_attribute.py 4.8 KiB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 unittest
  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. self.assertEqual(" foo", str(node))
  31. node2 = Attribute(wraptext("foo"), wraptext("bar"))
  32. self.assertEqual(' foo="bar"', str(node2))
  33. node3 = Attribute(wraptext("a"), wraptext("b"), '"', "", " ", " ")
  34. self.assertEqual('a = "b"', str(node3))
  35. node4 = Attribute(wraptext("a"), wraptext("b"), "'", "", " ", " ")
  36. self.assertEqual("a = 'b'", str(node4))
  37. node5 = Attribute(wraptext("a"), wraptext("b"), None, "", " ", " ")
  38. self.assertEqual("a = b", str(node5))
  39. node6 = Attribute(wraptext("a"), wrap([]), None, " ", "", " ")
  40. self.assertEqual(" 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. self.assertIs(name, 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. self.assertIs(value, node.value)
  53. node.value = "{{bar}}"
  54. self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value)
  55. node.value = None
  56. self.assertIs(None, 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. self.assertEqual('"', node2.quotes)
  61. node2.value = 'foo "bar" baz'
  62. self.assertWikicodeEqual(wraptext('foo "bar" baz'), node2.value)
  63. self.assertEqual("'", node2.quotes)
  64. node2.value = "foo 'bar' baz"
  65. self.assertWikicodeEqual(wraptext("foo 'bar' baz"), node2.value)
  66. self.assertEqual('"', node2.quotes)
  67. node2.value = "fo\"o 'bar' b\"az"
  68. self.assertWikicodeEqual(wraptext("fo\"o 'bar' b\"az"), node2.value)
  69. self.assertEqual('"', 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. self.assertIs(None, node1.quotes)
  76. self.assertEqual('"', node2.quotes)
  77. node1.quotes = "'"
  78. node2.quotes = None
  79. self.assertEqual("'", node1.quotes)
  80. self.assertIs(None, node2.quotes)
  81. self.assertRaises(ValueError, setattr, node1, "quotes", "foobar")
  82. self.assertRaises(ValueError, setattr, node3, "quotes", None)
  83. self.assertRaises(ValueError, Attribute, wraptext("id"),
  84. wraptext("foo bar baz"), None)
  85. def test_padding(self):
  86. """test getter/setter for the padding attributes"""
  87. for pad in ["pad_first", "pad_before_eq", "pad_after_eq"]:
  88. node = Attribute(wraptext("id"), wraptext("foo"), **{pad: "\n"})
  89. self.assertEqual("\n", getattr(node, pad))
  90. setattr(node, pad, " ")
  91. self.assertEqual(" ", getattr(node, pad))
  92. setattr(node, pad, None)
  93. self.assertEqual("", getattr(node, pad))
  94. self.assertRaises(ValueError, setattr, node, pad, True)
  95. if __name__ == "__main__":
  96. unittest.main(verbosity=2)