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.
 
 
 
 

118 lines
3.9 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. pass
  62. def test_index(self):
  63. """test Wikicode.index()"""
  64. pass
  65. def test_insert(self):
  66. """test Wikicode.insert()"""
  67. pass
  68. def test_insert_before(self):
  69. """test Wikicode.insert_before()"""
  70. pass
  71. def test_insert_after(self):
  72. """test Wikicode.insert_after()"""
  73. pass
  74. def test_replace(self):
  75. """test Wikicode.replace()"""
  76. pass
  77. def test_append(self):
  78. """test Wikicode.append()"""
  79. pass
  80. def test_remove(self):
  81. """test Wikicode.remove()"""
  82. pass
  83. def test_filter_family(self):
  84. """test the Wikicode.i?filter() family of functions"""
  85. pass
  86. def test_get_sections(self):
  87. """test Wikicode.get_sections()"""
  88. pass
  89. def test_strip_code(self):
  90. """test Wikicode.strip_code()"""
  91. pass
  92. def test_get_tree(self):
  93. """test Wikicode.get_tree()"""
  94. pass
  95. if __name__ == "__main__":
  96. unittest.main(verbosity=2)