A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Não pode escolher mais do que 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.
 
 
 
 

120 linhas
5.3 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. import unittest
  23. from mwparserfromhell.parameter import Parameter
  24. from mwparserfromhell.template import Template
  25. class TestParameter(unittest.TestCase):
  26. def setUp(self):
  27. self.name = "foo"
  28. self.value1 = "bar"
  29. self.value2 = "{{spam}}"
  30. self.value3 = "bar{{spam}}"
  31. self.value4 = "embedded {{eggs|spam|baz=buz}} {{goes}} here"
  32. self.templates2 = [Template("spam")]
  33. self.templates3 = [Template("spam")]
  34. self.templates4 = [Template("eggs", [Parameter("1", "spam"),
  35. Parameter("baz", "buz")]),
  36. Template("goes")]
  37. def test_construct(self):
  38. Parameter(self.name, self.value1)
  39. Parameter(self.name, self.value2, self.templates2)
  40. Parameter(name=self.name, value=self.value3)
  41. Parameter(name=self.name, value=self.value4, templates=self.templates4)
  42. def test_name(self):
  43. params = [
  44. Parameter(self.name, self.value1),
  45. Parameter(self.name, self.value2, self.templates2),
  46. Parameter(name=self.name, value=self.value3),
  47. Parameter(name=self.name, value=self.value4,
  48. templates=self.templates4)
  49. ]
  50. for param in params:
  51. self.assertEqual(param.name, self.name)
  52. def test_value(self):
  53. tests = [
  54. (Parameter(self.name, self.value1), self.value1),
  55. (Parameter(self.name, self.value2, self.templates2), self.value2),
  56. (Parameter(name=self.name, value=self.value3), self.value3),
  57. (Parameter(name=self.name, value=self.value4,
  58. templates=self.templates4), self.value4)
  59. ]
  60. for param, correct in tests:
  61. self.assertEqual(param.value, correct)
  62. def test_templates(self):
  63. tests = [
  64. (Parameter(self.name, self.value3, self.templates3),
  65. self.templates3),
  66. (Parameter(name=self.name, value=self.value4,
  67. templates=self.templates4), self.templates4)
  68. ]
  69. for param, correct in tests:
  70. self.assertEqual(param.templates, correct)
  71. def test_magic(self):
  72. params = [Parameter(self.name, self.value1),
  73. Parameter(self.name, self.value2, self.templates2),
  74. Parameter(self.name, self.value3, self.templates3),
  75. Parameter(self.name, self.value4, self.templates4)]
  76. for param in params:
  77. self.assertEqual(repr(param), repr(param.value))
  78. self.assertEqual(str(param), str(param.value))
  79. self.assertIs(param < "eggs", param.value < "eggs")
  80. self.assertIs(param <= "bar{{spam}}", param.value <= "bar{{spam}}")
  81. self.assertIs(param == "bar", param.value == "bar")
  82. self.assertIs(param != "bar", param.value != "bar")
  83. self.assertIs(param > "eggs", param.value > "eggs")
  84. self.assertIs(param >= "bar{{spam}}", param.value >= "bar{{spam}}")
  85. self.assertEquals(bool(param), bool(param.value))
  86. self.assertEquals(len(param), len(param.value))
  87. self.assertEquals(list(param), list(param.value))
  88. self.assertEquals(param[2], param.value[2])
  89. self.assertEquals(list(reversed(param)),
  90. list(reversed(param.value)))
  91. self.assertIs("bar" in param, "bar" in param.value)
  92. self.assertEquals(param + "test", param.value + "test")
  93. self.assertEquals("test" + param, "test" + param.value)
  94. # add param
  95. # add template left
  96. # add template right
  97. self.assertEquals(param * 3, Parameter(param.name, param.value * 3,
  98. param.templates * 3))
  99. self.assertEquals(3 * param, Parameter(param.name, 3 * param.value,
  100. 3 * param.templates))
  101. # add param inplace
  102. # add template implace
  103. # add str inplace
  104. # multiply int inplace
  105. self.assertIsInstance(param, Parameter)
  106. self.assertIsInstance(param.value, str)
  107. if __name__ == "__main__":
  108. unittest.main(verbosity=2)