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.
 
 
 
 

107 lines
4.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 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 itertools import permutations
  23. import unittest
  24. from mwparserfromhell.parameter import Parameter
  25. from mwparserfromhell.template import Template
  26. class TestTemplate(unittest.TestCase):
  27. def setUp(self):
  28. self.name = "foo"
  29. self.bar = Parameter("1", "bar")
  30. self.baz = Parameter("2", "baz")
  31. self.eggs = Parameter("eggs", "spam")
  32. self.params = [self.bar, self.baz, self.eggs]
  33. def test_construct(self):
  34. Template(self.name)
  35. Template(self.name, self.params)
  36. Template(name=self.name)
  37. Template(name=self.name, params=self.params)
  38. def test_name(self):
  39. templates = [
  40. Template(self.name),
  41. Template(self.name, self.params),
  42. Template(name=self.name),
  43. Template(name=self.name, params=self.params)
  44. ]
  45. for template in templates:
  46. self.assertEqual(template.name, self.name)
  47. def test_params(self):
  48. for template in (Template(self.name), Template(name=self.name)):
  49. self.assertEqual(template.params, [])
  50. for template in (Template(self.name, self.params),
  51. Template(name=self.name, params=self.params)):
  52. self.assertEqual(template.params, self.params)
  53. def test_getitem(self):
  54. template = Template(name=self.name, params=self.params)
  55. self.assertIs(template[0], self.bar)
  56. self.assertIs(template[1], self.baz)
  57. self.assertIs(template[2], self.eggs)
  58. self.assertIs(template["1"], self.bar)
  59. self.assertIs(template["2"], self.baz)
  60. self.assertIs(template["eggs"], self.eggs)
  61. def test_render(self):
  62. tests = [
  63. (Template(self.name), "{{foo}}"),
  64. (Template(self.name, self.params), "{{foo|bar|baz|eggs=spam}}")
  65. ]
  66. for template, rendered in tests:
  67. self.assertEqual(template.render(), rendered)
  68. def test_repr(self):
  69. correct1= 'Template(name=foo, params={})'
  70. correct2 = 'Template(name=foo, params={"1": "bar", "2": "baz", "eggs": "spam"})'
  71. tests = [(Template(self.name), correct1),
  72. (Template(self.name, self.params), correct2)]
  73. for template, correct in tests:
  74. self.assertEqual(repr(template), correct)
  75. self.assertEqual(str(template), correct)
  76. def test_cmp(self):
  77. tmp1 = Template(self.name)
  78. tmp2 = Template(name=self.name)
  79. tmp3 = Template(self.name, [])
  80. tmp4 = Template(name=self.name, params=[])
  81. tmp5 = Template(self.name, self.params)
  82. tmp6 = Template(name=self.name, params=self.params)
  83. for tmpA, tmpB in permutations((tmp1, tmp2, tmp3, tmp4), 2):
  84. self.assertEqual(tmpA, tmpB)
  85. for tmpA, tmpB in permutations((tmp5, tmp6), 2):
  86. self.assertEqual(tmpA, tmpB)
  87. for tmpA in (tmp5, tmp6):
  88. for tmpB in (tmp1, tmp2, tmp3, tmp4):
  89. self.assertNotEqual(tmpA, tmpB)
  90. self.assertNotEqual(tmpB, tmpA)
  91. if __name__ == "__main__":
  92. unittest.main(verbosity=2)