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.9 KiB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2019 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. import unittest
  24. from mwparserfromhell.compat import str
  25. from mwparserfromhell.nodes import Template
  26. from mwparserfromhell.nodes.extras import Attribute
  27. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  28. class TestAttribute(TreeEqualityTestCase):
  29. """Test cases for the Attribute node extra."""
  30. def test_unicode(self):
  31. """test Attribute.__unicode__()"""
  32. node = Attribute(wraptext("foo"))
  33. self.assertEqual(" foo", str(node))
  34. node2 = Attribute(wraptext("foo"), wraptext("bar"))
  35. self.assertEqual(' foo="bar"', str(node2))
  36. node3 = Attribute(wraptext("a"), wraptext("b"), '"', "", " ", " ")
  37. self.assertEqual('a = "b"', str(node3))
  38. node4 = Attribute(wraptext("a"), wraptext("b"), "'", "", " ", " ")
  39. self.assertEqual("a = 'b'", str(node4))
  40. node5 = Attribute(wraptext("a"), wraptext("b"), None, "", " ", " ")
  41. self.assertEqual("a = b", str(node5))
  42. node6 = Attribute(wraptext("a"), wrap([]), None, " ", "", " ")
  43. self.assertEqual(" a= ", str(node6))
  44. def test_name(self):
  45. """test getter/setter for the name attribute"""
  46. name = wraptext("id")
  47. node = Attribute(name, wraptext("bar"))
  48. self.assertIs(name, node.name)
  49. node.name = "{{id}}"
  50. self.assertWikicodeEqual(wrap([Template(wraptext("id"))]), node.name)
  51. def test_value(self):
  52. """test getter/setter for the value attribute"""
  53. value = wraptext("foo")
  54. node = Attribute(wraptext("id"), value)
  55. self.assertIs(value, node.value)
  56. node.value = "{{bar}}"
  57. self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value)
  58. node.value = None
  59. self.assertIs(None, node.value)
  60. node2 = Attribute(wraptext("id"), wraptext("foo"), None)
  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 = "foo 'bar' baz"
  68. self.assertWikicodeEqual(wraptext("foo 'bar' baz"), node2.value)
  69. self.assertEqual('"', node2.quotes)
  70. node2.value = "fo\"o 'bar' b\"az"
  71. self.assertWikicodeEqual(wraptext("fo\"o 'bar' b\"az"), node2.value)
  72. self.assertEqual('"', node2.quotes)
  73. def test_quotes(self):
  74. """test getter/setter for the quotes attribute"""
  75. node1 = Attribute(wraptext("id"), wraptext("foo"), None)
  76. node2 = Attribute(wraptext("id"), wraptext("bar"))
  77. node3 = Attribute(wraptext("id"), wraptext("foo bar baz"))
  78. self.assertIs(None, node1.quotes)
  79. self.assertEqual('"', node2.quotes)
  80. node1.quotes = "'"
  81. node2.quotes = None
  82. self.assertEqual("'", node1.quotes)
  83. self.assertIs(None, node2.quotes)
  84. self.assertRaises(ValueError, setattr, node1, "quotes", "foobar")
  85. self.assertRaises(ValueError, setattr, node3, "quotes", None)
  86. self.assertRaises(ValueError, Attribute, wraptext("id"),
  87. wraptext("foo bar baz"), None)
  88. def test_padding(self):
  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. self.assertEqual("\n", getattr(node, pad))
  93. setattr(node, pad, " ")
  94. self.assertEqual(" ", getattr(node, pad))
  95. setattr(node, pad, None)
  96. self.assertEqual("", getattr(node, pad))
  97. self.assertRaises(ValueError, setattr, node, pad, True)
  98. if __name__ == "__main__":
  99. unittest.main(verbosity=2)