A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_attribute.py 5.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2016 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. try:
  24. import unittest2 as unittest
  25. except ImportError:
  26. import unittest
  27. from mwparserfromhell.compat import str
  28. from mwparserfromhell.nodes import Template
  29. from mwparserfromhell.nodes.extras import Attribute
  30. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  31. class TestAttribute(TreeEqualityTestCase):
  32. """Test cases for the Attribute node extra."""
  33. def test_unicode(self):
  34. """test Attribute.__unicode__()"""
  35. node = Attribute(wraptext("foo"))
  36. self.assertEqual(" foo", str(node))
  37. node2 = Attribute(wraptext("foo"), wraptext("bar"))
  38. self.assertEqual(' foo="bar"', str(node2))
  39. node3 = Attribute(wraptext("a"), wraptext("b"), '"', "", " ", " ")
  40. self.assertEqual('a = "b"', str(node3))
  41. node4 = Attribute(wraptext("a"), wraptext("b"), "'", "", " ", " ")
  42. self.assertEqual("a = 'b'", str(node4))
  43. node5 = Attribute(wraptext("a"), wraptext("b"), None, "", " ", " ")
  44. self.assertEqual("a = b", str(node5))
  45. node6 = Attribute(wraptext("a"), wrap([]), None, " ", "", " ")
  46. self.assertEqual(" a= ", str(node6))
  47. def test_name(self):
  48. """test getter/setter for the name attribute"""
  49. name = wraptext("id")
  50. node = Attribute(name, wraptext("bar"))
  51. self.assertIs(name, node.name)
  52. node.name = "{{id}}"
  53. self.assertWikicodeEqual(wrap([Template(wraptext("id"))]), node.name)
  54. def test_value(self):
  55. """test getter/setter for the value attribute"""
  56. value = wraptext("foo")
  57. node = Attribute(wraptext("id"), value)
  58. self.assertIs(value, node.value)
  59. node.value = "{{bar}}"
  60. self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value)
  61. node.value = None
  62. self.assertIs(None, node.value)
  63. node2 = Attribute(wraptext("id"), wraptext("foo"), None)
  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 = "foo 'bar' baz"
  71. self.assertWikicodeEqual(wraptext("foo 'bar' baz"), node2.value)
  72. self.assertEqual('"', node2.quotes)
  73. node2.value = "fo\"o 'bar' b\"az"
  74. self.assertWikicodeEqual(wraptext("fo\"o 'bar' b\"az"), node2.value)
  75. self.assertEqual('"', node2.quotes)
  76. def test_quotes(self):
  77. """test getter/setter for the quotes attribute"""
  78. node1 = Attribute(wraptext("id"), wraptext("foo"), None)
  79. node2 = Attribute(wraptext("id"), wraptext("bar"))
  80. node3 = Attribute(wraptext("id"), wraptext("foo bar baz"))
  81. self.assertIs(None, node1.quotes)
  82. self.assertEqual('"', node2.quotes)
  83. node1.quotes = "'"
  84. node2.quotes = None
  85. self.assertEqual("'", node1.quotes)
  86. self.assertIs(None, node2.quotes)
  87. self.assertRaises(ValueError, setattr, node1, "quotes", "foobar")
  88. self.assertRaises(ValueError, setattr, node3, "quotes", None)
  89. self.assertRaises(ValueError, Attribute, wraptext("id"),
  90. wraptext("foo bar baz"), None)
  91. def test_padding(self):
  92. """test getter/setter for the padding attributes"""
  93. for pad in ["pad_first", "pad_before_eq", "pad_after_eq"]:
  94. node = Attribute(wraptext("id"), wraptext("foo"), **{pad: "\n"})
  95. self.assertEqual("\n", getattr(node, pad))
  96. setattr(node, pad, " ")
  97. self.assertEqual(" ", getattr(node, pad))
  98. setattr(node, pad, None)
  99. self.assertEqual("", getattr(node, pad))
  100. self.assertRaises(ValueError, setattr, node, pad, True)
  101. if __name__ == "__main__":
  102. unittest.main(verbosity=2)