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.
 
 
 
 

141 lines
5.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  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.nodes import (Argument, Comment, Heading, HTMLEntity,
  25. Tag, Template, Text, Wikilink)
  26. from mwparserfromhell.smart_list import SmartList
  27. from mwparserfromhell.wikicode import Wikicode
  28. from mwparserfromhell import parse
  29. from mwparserfromhell.compat import str
  30. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  31. class TestWikicode(TreeEqualityTestCase):
  32. """Tests for the Wikicode class, which manages a list of nodes."""
  33. def test_unicode(self):
  34. """test Wikicode.__unicode__()"""
  35. code1 = parse("foobar")
  36. code2 = parse("Have a {{template}} and a [[page|link]]")
  37. self.assertEqual("foobar", str(code1))
  38. self.assertEqual("Have a {{template}} and a [[page|link]]", str(code2))
  39. def test_nodes(self):
  40. """test getter/setter for the nodes attribute"""
  41. code = parse("Have a {{template}}")
  42. self.assertEqual(["Have a ", "{{template}}"], code.nodes)
  43. L1 = SmartList([Text("foobar"), Template(wraptext("abc"))])
  44. L2 = [Text("barfoo"), Template(wraptext("cba"))]
  45. L3 = "abc{{def}}"
  46. code.nodes = L1
  47. self.assertIs(L1, code.nodes)
  48. code.nodes = L2
  49. self.assertIs(L2, code.nodes)
  50. code.nodes = L3
  51. self.assertEqual(["abc", "{{def}}"], code.nodes)
  52. self.assertRaises(ValueError, setattr, code, "nodes", object)
  53. def test_get(self):
  54. """test Wikicode.get()"""
  55. code = parse("Have a {{template}} and a [[page|link]]")
  56. self.assertIs(code.nodes[0], code.get(0))
  57. self.assertIs(code.nodes[2], code.get(2))
  58. self.assertRaises(IndexError, code.get, 4)
  59. def test_set(self):
  60. """test Wikicode.set()"""
  61. code = parse("Have a {{template}} and a [[page|link]]")
  62. code.set(1, "{{{argument}}}")
  63. self.assertEqual("Have a {{{argument}}} and a [[page|link]]", code)
  64. self.assertIsInstance(code.get(1), Argument)
  65. code.set(2, None)
  66. self.assertEqual("Have a {{{argument}}}[[page|link]]", code)
  67. code.set(-3, "This is an ")
  68. self.assertEqual("This is an {{{argument}}}[[page|link]]", code)
  69. self.assertRaises(ValueError, code.set, 1, "foo {{bar}}")
  70. self.assertRaises(IndexError, code.set, 3, "{{baz}}")
  71. self.assertRaises(IndexError, code.set, -4, "{{baz}}")
  72. def test_index(self):
  73. """test Wikicode.index()"""
  74. code = parse("Have a {{template}} and a [[page|link]]")
  75. self.assertEqual(0, code.index("Have a "))
  76. self.assertEqual(3, code.index("[[page|link]]"))
  77. self.assertEqual(1, code.index(code.get(1)))
  78. self.assertRaises(ValueError, code.index, "foo")
  79. code = parse("{{foo}}{{bar|{{baz}}}}")
  80. self.assertEqual(1, code.index("{{bar|{{baz}}}}"))
  81. self.assertEqual(1, code.index("{{baz}}", recursive=True))
  82. self.assertEqual(1, code.index(code.get(1).get(1).value,
  83. recursive=True))
  84. self.assertRaises(ValueError, code.index, "{{baz}}", recursive=False)
  85. self.assertRaises(ValueError, code.index,
  86. code.get(1).get(1).value, recursive=False)
  87. def test_insert(self):
  88. """test Wikicode.insert()"""
  89. pass
  90. def test_insert_before(self):
  91. """test Wikicode.insert_before()"""
  92. pass
  93. def test_insert_after(self):
  94. """test Wikicode.insert_after()"""
  95. pass
  96. def test_replace(self):
  97. """test Wikicode.replace()"""
  98. pass
  99. def test_append(self):
  100. """test Wikicode.append()"""
  101. pass
  102. def test_remove(self):
  103. """test Wikicode.remove()"""
  104. pass
  105. def test_filter_family(self):
  106. """test the Wikicode.i?filter() family of functions"""
  107. pass
  108. def test_get_sections(self):
  109. """test Wikicode.get_sections()"""
  110. pass
  111. def test_strip_code(self):
  112. """test Wikicode.strip_code()"""
  113. pass
  114. def test_get_tree(self):
  115. """test Wikicode.get_tree()"""
  116. pass
  117. if __name__ == "__main__":
  118. unittest.main(verbosity=2)