A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

187 linhas
8.1 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.compat import str
  25. from mwparserfromhell.nodes import Template, Text
  26. from mwparserfromhell.nodes.extras import Parameter
  27. from mwparserfromhell.smart_list import SmartList
  28. from mwparserfromhell.wikicode import Wikicode
  29. from ._test_tree_equality import TreeEqualityTestCase
  30. wrap = lambda L: Wikicode(SmartList(L))
  31. pgens = lambda k, v: Parameter(wrap([Text(k)]), wrap([Text(v)]), True)
  32. pgenh = lambda k, v: Parameter(wrap([Text(k)]), wrap([Text(v)]), False)
  33. class TestTemplate(TreeEqualityTestCase):
  34. """Test cases for the Template node."""
  35. def test_unicode(self):
  36. """test Template.__unicode__()"""
  37. node = Template(wrap([Text("foobar")]))
  38. self.assertEqual("{{foobar}}", str(node))
  39. node2 = Template(wrap([Text("foo")]),
  40. [pgenh("1", "bar"), pgens("abc", "def")])
  41. self.assertEqual("{{foo|bar|abc=def}}", str(node2))
  42. def test_strip(self):
  43. """test Template.__strip__()"""
  44. node1 = Template(wrap([Text("foobar")]))
  45. node2 = Template(wrap([Text("foo")]),
  46. [pgenh("1", "bar"), pgens("abc", "def")])
  47. for a in (True, False):
  48. for b in (True, False):
  49. self.assertEqual(None, node1.__strip__(a, b))
  50. self.assertEqual(None, node2.__strip__(a, b))
  51. def test_showtree(self):
  52. """test Template.__showtree__()"""
  53. output = []
  54. getter, marker = object(), object()
  55. get = lambda code: output.append((getter, code))
  56. mark = lambda: output.append(marker)
  57. node1 = Template(wrap([Text("foobar")]))
  58. node2 = Template(wrap([Text("foo")]),
  59. [pgenh("1", "bar"), pgens("abc", "def")])
  60. node1.__showtree__(output.append, get, mark)
  61. node2.__showtree__(output.append, get, mark)
  62. valid = [
  63. "{{", (getter, node1.name), "}}", "{{", (getter, node2.name),
  64. " | ", marker, (getter, node2.params[0].name), " = ", marker,
  65. (getter, node2.params[0].value), " | ", marker,
  66. (getter, node2.params[1].name), " = ", marker,
  67. (getter, node2.params[1].value), "}}"]
  68. self.assertEqual(valid, output)
  69. def test_name(self):
  70. """test getter/setter for the name attribute"""
  71. name = wrap([Text("foobar")])
  72. node1 = Template(name)
  73. node2 = Template(name, [pgenh("1", "bar")])
  74. self.assertIs(name, node1.name)
  75. self.assertIs(name, node2.name)
  76. node1.name = "asdf"
  77. node2.name = "téstïng"
  78. self.assertWikicodeEqual(wrap([Text("asdf")]), node1.name)
  79. self.assertWikicodeEqual(wrap([Text("téstïng")]), node2.name)
  80. def test_params(self):
  81. """test getter for the params attribute"""
  82. node1 = Template(wrap([Text("foobar")]))
  83. plist = [pgenh("1", "bar"), pgens("abc", "def")]
  84. node2 = Template(wrap([Text("foo")]), plist)
  85. self.assertEqual([], node1.params)
  86. self.assertIs(plist, node2.params)
  87. def test_has_param(self):
  88. """test Template.has_param()"""
  89. node1 = Template(wrap([Text("foobar")]))
  90. node2 = Template(wrap([Text("foo")]),
  91. [pgenh("1", "bar"), pgens("\nabc ", "def")])
  92. node3 = Template(wrap([Text("foo")]),
  93. [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")])
  94. node4 = Template(wrap([Text("foo")]),
  95. [pgenh("1", "a"), pgens("b", " ")])
  96. self.assertFalse(node1.has_param("foobar"))
  97. self.assertTrue(node2.has_param(1))
  98. self.assertTrue(node2.has_param("abc"))
  99. self.assertFalse(node2.has_param("def"))
  100. self.assertTrue(node3.has_param("1"))
  101. self.assertTrue(node3.has_param(" b "))
  102. self.assertFalse(node4.has_param("b"))
  103. self.assertTrue(node3.has_param("b", False))
  104. self.assertTrue(node4.has_param("b", False))
  105. def test_get(self):
  106. """test Template.get()"""
  107. node1 = Template(wrap([Text("foobar")]))
  108. node2p1 = pgenh("1", "bar")
  109. node2p2 = pgens("abc", "def")
  110. node2 = Template(wrap([Text("foo")]), [node2p1, node2p2])
  111. node3p1 = pgens("b", "c")
  112. node3p2 = pgens("1", "d")
  113. node3 = Template(wrap([Text("foo")]),
  114. [pgenh("1", "a"), node3p1, node3p2])
  115. node4p1 = pgens(" b", " ")
  116. node4 = Template(wrap([Text("foo")]), [pgenh("1", "a"), node4p1])
  117. self.assertRaises(ValueError, node1.get, "foobar")
  118. self.assertIs(node2p1, node2.get(1))
  119. self.assertIs(node2p2, node2.get("abc"))
  120. self.assertRaises(ValueError, node2.get, "def")
  121. self.assertIs(node3p1, node3.get("b"))
  122. self.assertIs(node3p2, node3.get("1"))
  123. self.assertIs(node4p1, node4.get("b "))
  124. # add
  125. def test_remove(self):
  126. """test Template.remove()"""
  127. node1 = Template(wrap([Text("foobar")]))
  128. node2 = Template(wrap([Text("foo")]), [pgenh("1", "bar"),
  129. pgens("abc", "def")])
  130. node3 = Template(wrap([Text("foo")]), [pgenh("1", "bar"),
  131. pgens("abc", "def")])
  132. node4 = Template(wrap([Text("foo")]), [pgenh("1", "bar"),
  133. pgenh("2", "baz")])
  134. node5 = Template(wrap([Text("foo")]), [
  135. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  136. node6 = Template(wrap([Text("foo")]), [
  137. pgens(" a", "b"), pgens("b", "c"), pgens("a ", "d")])
  138. node7 = Template(wrap([Text("foo")]), [
  139. pgens("1 ", "a"), pgens(" 1", "b"), pgens("2", "c")])
  140. node8 = Template(wrap([Text("foo")]), [
  141. pgens("1 ", "a"), pgens(" 1", "b"), pgens("2", "c")])
  142. node9 = Template(wrap([Text("foo")]), [
  143. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  144. node10 = Template(wrap([Text("foo")]), [
  145. pgens("1 ", "a"), pgenh("1", "b"), pgenh("2", "c")])
  146. node2.remove("1")
  147. node2.remove("abc")
  148. node3.remove(1, keep_field=True)
  149. node3.remove("abc", keep_field=True)
  150. node4.remove("1", keep_field=False)
  151. node5.remove("a", keep_field=False)
  152. node6.remove("a", keep_field=True)
  153. node7.remove(1, keep_field=True)
  154. node8.remove(1, keep_field=False)
  155. node9.remove(1, keep_field=True)
  156. node10.remove(1, keep_field=False)
  157. self.assertRaises(ValueError, node1.remove, 1)
  158. self.assertRaises(ValueError, node1.remove, "a")
  159. self.assertRaises(ValueError, node2.remove, "1")
  160. self.assertEqual("{{foo}}", node2)
  161. self.assertEqual("{{foo||abc=}}", node3)
  162. self.assertEqual("{{foo||baz}}", node4)
  163. self.assertEqual("{{foo|b=c}}", node5)
  164. self.assertEqual("{{foo| a=|b=c}}", node6)
  165. self.assertEqual("{{foo|1 =|2=c}}", node7)
  166. self.assertEqual("{{foo|2=c}}", node8)
  167. self.assertEqual("{{foo||c}}", node9)
  168. self.assertEqual("{{foo||c}}", node10)
  169. if __name__ == "__main__":
  170. unittest.main(verbosity=2)