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.
 
 
 
 

99 lines
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 by 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 mwtemplateparserfromhell.parameter import Parameter
  25. from mwtemplateparserfromhell.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_get(self):
  54. template = Template(name=self.name, params=self.params)
  55. self.assertIs(template.get(0), self.bar)
  56. self.assertIs(template.get(1), self.baz)
  57. self.assertIs(template.get(2), self.eggs)
  58. self.assertIs(template.get("1"), self.bar)
  59. self.assertIs(template.get("2"), self.baz)
  60. self.assertIs(template.get("eggs"), self.eggs)
  61. def test_repr(self):
  62. correct1= 'Template(name=foo, params={})'
  63. correct2 = 'Template(name=foo, params={"1": "bar", "2": "baz", "eggs": "spam"})'
  64. tests = [(Template(self.name), correct1),
  65. (Template(self.name, self.params), correct2)]
  66. for template, correct in tests:
  67. self.assertEqual(repr(template), correct)
  68. self.assertEqual(str(template), correct)
  69. def test_cmp(self):
  70. tmp1 = Template(self.name)
  71. tmp2 = Template(name=self.name)
  72. tmp3 = Template(self.name, [])
  73. tmp4 = Template(name=self.name, params=[])
  74. tmp5 = Template(self.name, self.params)
  75. tmp6 = Template(name=self.name, params=self.params)
  76. for tmpA, tmpB in permutations((tmp1, tmp2, tmp3, tmp4), 2):
  77. self.assertEqual(tmpA, tmpB)
  78. for tmpA, tmpB in permutations((tmp5, tmp6), 2):
  79. self.assertEqual(tmpA, tmpB)
  80. for tmpA in (tmp5, tmp6):
  81. for tmpB in (tmp1, tmp2, tmp3, tmp4):
  82. self.assertNotEqual(tmpA, tmpB)
  83. self.assertNotEqual(tmpB, tmpA)
  84. if __name__ == "__main__":
  85. unittest.main(verbosity=2)