A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

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