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

84 рядки
3.3 KiB

  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 Heading, Text
  23. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  24. class TestHeading(TreeEqualityTestCase):
  25. """Test cases for the Heading node."""
  26. def test_unicode(self):
  27. """test Heading.__unicode__()"""
  28. node = Heading(wraptext("foobar"), 2)
  29. self.assertEqual("==foobar==", str(node))
  30. node2 = Heading(wraptext(" zzz "), 5)
  31. self.assertEqual("===== zzz =====", str(node2))
  32. def test_children(self):
  33. """test Heading.__children__()"""
  34. node = Heading(wrap([Text("foo"), Text("bar")]), 3)
  35. gen = node.__children__()
  36. self.assertEqual(node.title, next(gen))
  37. self.assertRaises(StopIteration, next, gen)
  38. def test_strip(self):
  39. """test Heading.__strip__()"""
  40. node = Heading(wraptext("foobar"), 3)
  41. self.assertEqual("foobar", node.__strip__())
  42. def test_showtree(self):
  43. """test Heading.__showtree__()"""
  44. output = []
  45. getter = object()
  46. get = lambda code: output.append((getter, code))
  47. node1 = Heading(wraptext("foobar"), 3)
  48. node2 = Heading(wraptext(" baz "), 4)
  49. node1.__showtree__(output.append, get, None)
  50. node2.__showtree__(output.append, get, None)
  51. valid = ["===", (getter, node1.title), "===",
  52. "====", (getter, node2.title), "===="]
  53. self.assertEqual(valid, output)
  54. def test_title(self):
  55. """test getter/setter for the title attribute"""
  56. title = wraptext("foobar")
  57. node = Heading(title, 3)
  58. self.assertIs(title, node.title)
  59. node.title = "héhehé"
  60. self.assertWikicodeEqual(wraptext("héhehé"), node.title)
  61. def test_level(self):
  62. """test getter/setter for the level attribute"""
  63. node = Heading(wraptext("foobar"), 3)
  64. self.assertEqual(3, node.level)
  65. node.level = 5
  66. self.assertEqual(5, node.level)
  67. self.assertRaises(ValueError, setattr, node, "level", 0)
  68. self.assertRaises(ValueError, setattr, node, "level", 7)
  69. self.assertRaises(ValueError, setattr, node, "level", "abc")
  70. self.assertRaises(ValueError, setattr, node, "level", False)
  71. if __name__ == "__main__":
  72. unittest.main(verbosity=2)