A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

test_argument.py 4.0 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #
  2. # Copyright (C) 2012-2016 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 Argument, Text
  23. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  24. class TestArgument(TreeEqualityTestCase):
  25. """Test cases for the Argument node."""
  26. def test_unicode(self):
  27. """test Argument.__unicode__()"""
  28. node = Argument(wraptext("foobar"))
  29. self.assertEqual("{{{foobar}}}", str(node))
  30. node2 = Argument(wraptext("foo"), wraptext("bar"))
  31. self.assertEqual("{{{foo|bar}}}", str(node2))
  32. def test_children(self):
  33. """test Argument.__children__()"""
  34. node1 = Argument(wraptext("foobar"))
  35. node2 = Argument(wraptext("foo"), wrap([Text("bar"), Text("baz")]))
  36. gen1 = node1.__children__()
  37. gen2 = node2.__children__()
  38. self.assertIs(node1.name, next(gen1))
  39. self.assertIs(node2.name, next(gen2))
  40. self.assertIs(node2.default, next(gen2))
  41. self.assertRaises(StopIteration, next, gen1)
  42. self.assertRaises(StopIteration, next, gen2)
  43. def test_strip(self):
  44. """test Argument.__strip__()"""
  45. node1 = Argument(wraptext("foobar"))
  46. node2 = Argument(wraptext("foo"), wraptext("bar"))
  47. self.assertIs(None, node1.__strip__())
  48. self.assertEqual("bar", node2.__strip__())
  49. def test_showtree(self):
  50. """test Argument.__showtree__()"""
  51. output = []
  52. getter, marker = object(), object()
  53. get = lambda code: output.append((getter, code))
  54. mark = lambda: output.append(marker)
  55. node1 = Argument(wraptext("foobar"))
  56. node2 = Argument(wraptext("foo"), wraptext("bar"))
  57. node1.__showtree__(output.append, get, mark)
  58. node2.__showtree__(output.append, get, mark)
  59. valid = [
  60. "{{{", (getter, node1.name), "}}}", "{{{", (getter, node2.name),
  61. " | ", marker, (getter, node2.default), "}}}"]
  62. self.assertEqual(valid, output)
  63. def test_name(self):
  64. """test getter/setter for the name attribute"""
  65. name = wraptext("foobar")
  66. node1 = Argument(name)
  67. node2 = Argument(name, wraptext("baz"))
  68. self.assertIs(name, node1.name)
  69. self.assertIs(name, node2.name)
  70. node1.name = "héhehé"
  71. node2.name = "héhehé"
  72. self.assertWikicodeEqual(wraptext("héhehé"), node1.name)
  73. self.assertWikicodeEqual(wraptext("héhehé"), node2.name)
  74. def test_default(self):
  75. """test getter/setter for the default attribute"""
  76. default = wraptext("baz")
  77. node1 = Argument(wraptext("foobar"))
  78. node2 = Argument(wraptext("foobar"), default)
  79. self.assertIs(None, node1.default)
  80. self.assertIs(default, node2.default)
  81. node1.default = "buzz"
  82. node2.default = None
  83. self.assertWikicodeEqual(wraptext("buzz"), node1.default)
  84. self.assertIs(None, node2.default)
  85. if __name__ == "__main__":
  86. unittest.main(verbosity=2)