A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@gmail.com>
  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 HTMLEntity
  26. from ._test_tree_equality import TreeEqualityTestCase, wrap
  27. class TestHTMLEntity(TreeEqualityTestCase):
  28. """Test cases for the HTMLEntity node."""
  29. def test_unicode(self):
  30. """test HTMLEntity.__unicode__()"""
  31. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  32. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  33. node3 = HTMLEntity("6b", named=False, hexadecimal=True)
  34. node4 = HTMLEntity("6C", named=False, hexadecimal=True, hex_char="X")
  35. self.assertEqual("&nbsp;", str(node1))
  36. self.assertEqual("&#107;", str(node2))
  37. self.assertEqual("&#x6b;", str(node3))
  38. self.assertEqual("&#X6C;", str(node4))
  39. def test_children(self):
  40. """test HTMLEntity.__children__()"""
  41. node = HTMLEntity("nbsp", named=True, hexadecimal=False)
  42. gen = node.__children__()
  43. self.assertRaises(StopIteration, next, gen)
  44. def test_strip(self):
  45. """test HTMLEntity.__strip__()"""
  46. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  47. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  48. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  49. for a in (True, False):
  50. self.assertEqual("\xa0", node1.__strip__(True, a))
  51. self.assertEqual("&nbsp;", node1.__strip__(False, a))
  52. self.assertEqual("k", node2.__strip__(True, a))
  53. self.assertEqual("&#107;", node2.__strip__(False, a))
  54. self.assertEqual("é", node3.__strip__(True, a))
  55. self.assertEqual("&#xe9;", node3.__strip__(False, a))
  56. def test_showtree(self):
  57. """test HTMLEntity.__showtree__()"""
  58. output = []
  59. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  60. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  61. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  62. node1.__showtree__(output.append, None, None)
  63. node2.__showtree__(output.append, None, None)
  64. node3.__showtree__(output.append, None, None)
  65. res = ["&nbsp;", "&#107;", "&#xe9;"]
  66. self.assertEqual(res, output)
  67. def test_value(self):
  68. """test getter/setter for the value attribute"""
  69. node1 = HTMLEntity("nbsp")
  70. node2 = HTMLEntity("107")
  71. node3 = HTMLEntity("e9")
  72. self.assertEqual("nbsp", node1.value)
  73. self.assertEqual("107", node2.value)
  74. self.assertEqual("e9", node3.value)
  75. node1.value = "ffa4"
  76. node2.value = 72
  77. node3.value = "Sigma"
  78. self.assertEqual("ffa4", node1.value)
  79. self.assertFalse(node1.named)
  80. self.assertTrue(node1.hexadecimal)
  81. self.assertEqual("72", node2.value)
  82. self.assertFalse(node2.named)
  83. self.assertFalse(node2.hexadecimal)
  84. self.assertEqual("Sigma", node3.value)
  85. self.assertTrue(node3.named)
  86. self.assertFalse(node3.hexadecimal)
  87. node1.value = "10FFFF"
  88. node2.value = 110000
  89. node2.value = 1114111
  90. self.assertRaises(ValueError, setattr, node3, "value", "")
  91. self.assertRaises(ValueError, setattr, node3, "value", "foobar")
  92. self.assertRaises(ValueError, setattr, node3, "value", True)
  93. self.assertRaises(ValueError, setattr, node3, "value", -1)
  94. self.assertRaises(ValueError, setattr, node1, "value", 110000)
  95. self.assertRaises(ValueError, setattr, node1, "value", "1114112")
  96. def test_named(self):
  97. """test getter/setter for the named attribute"""
  98. node1 = HTMLEntity("nbsp")
  99. node2 = HTMLEntity("107")
  100. node3 = HTMLEntity("e9")
  101. self.assertTrue(node1.named)
  102. self.assertFalse(node2.named)
  103. self.assertFalse(node3.named)
  104. node1.named = 1
  105. node2.named = 0
  106. node3.named = 0
  107. self.assertTrue(node1.named)
  108. self.assertFalse(node2.named)
  109. self.assertFalse(node3.named)
  110. self.assertRaises(ValueError, setattr, node1, "named", False)
  111. self.assertRaises(ValueError, setattr, node2, "named", True)
  112. self.assertRaises(ValueError, setattr, node3, "named", True)
  113. def test_hexadecimal(self):
  114. """test getter/setter for the hexadecimal attribute"""
  115. node1 = HTMLEntity("nbsp")
  116. node2 = HTMLEntity("107")
  117. node3 = HTMLEntity("e9")
  118. self.assertFalse(node1.hexadecimal)
  119. self.assertFalse(node2.hexadecimal)
  120. self.assertTrue(node3.hexadecimal)
  121. node1.hexadecimal = False
  122. node2.hexadecimal = True
  123. node3.hexadecimal = False
  124. self.assertFalse(node1.hexadecimal)
  125. self.assertTrue(node2.hexadecimal)
  126. self.assertFalse(node3.hexadecimal)
  127. self.assertRaises(ValueError, setattr, node1, "hexadecimal", True)
  128. def test_hex_char(self):
  129. """test getter/setter for the hex_char attribute"""
  130. node1 = HTMLEntity("e9")
  131. node2 = HTMLEntity("e9", hex_char="X")
  132. self.assertEqual("x", node1.hex_char)
  133. self.assertEqual("X", node2.hex_char)
  134. node1.hex_char = "X"
  135. node2.hex_char = "x"
  136. self.assertEqual("X", node1.hex_char)
  137. self.assertEqual("x", node2.hex_char)
  138. self.assertRaises(ValueError, setattr, node1, "hex_char", 123)
  139. self.assertRaises(ValueError, setattr, node1, "hex_char", "foobar")
  140. self.assertRaises(ValueError, setattr, node1, "hex_char", True)
  141. def test_normalize(self):
  142. """test getter/setter for the normalize attribute"""
  143. node1 = HTMLEntity("nbsp")
  144. node2 = HTMLEntity("107")
  145. node3 = HTMLEntity("e9")
  146. node4 = HTMLEntity("1f648")
  147. self.assertEqual("\xa0", node1.normalize())
  148. self.assertEqual("k", node2.normalize())
  149. self.assertEqual("é", node3.normalize())
  150. self.assertEqual("\U0001F648", node4.normalize())
  151. if __name__ == "__main__":
  152. unittest.main(verbosity=2)