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

163 строки
6.8 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 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_strip(self):
  40. """test HTMLEntity.__strip__()"""
  41. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  42. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  43. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  44. for a in (True, False):
  45. self.assertEqual("\xa0", node1.__strip__(True, a))
  46. self.assertEqual("&nbsp;", node1.__strip__(False, a))
  47. self.assertEqual("k", node2.__strip__(True, a))
  48. self.assertEqual("&#107;", node2.__strip__(False, a))
  49. self.assertEqual("é", node3.__strip__(True, a))
  50. self.assertEqual("&#xe9;", node3.__strip__(False, a))
  51. def test_showtree(self):
  52. """test HTMLEntity.__showtree__()"""
  53. output = []
  54. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  55. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  56. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  57. node1.__showtree__(output.append, None, None)
  58. node2.__showtree__(output.append, None, None)
  59. node3.__showtree__(output.append, None, None)
  60. res = ["&nbsp;", "&#107;", "&#xe9;"]
  61. self.assertEqual(res, output)
  62. def test_value(self):
  63. """test getter/setter for the value attribute"""
  64. node1 = HTMLEntity("nbsp")
  65. node2 = HTMLEntity("107")
  66. node3 = HTMLEntity("e9")
  67. self.assertEqual("nbsp", node1.value)
  68. self.assertEqual("107", node2.value)
  69. self.assertEqual("e9", node3.value)
  70. node1.value = "ffa4"
  71. node2.value = 72
  72. node3.value = "Sigma"
  73. self.assertEqual("ffa4", node1.value)
  74. self.assertFalse(node1.named)
  75. self.assertTrue(node1.hexadecimal)
  76. self.assertEqual("72", node2.value)
  77. self.assertFalse(node2.named)
  78. self.assertFalse(node2.hexadecimal)
  79. self.assertEqual("Sigma", node3.value)
  80. self.assertTrue(node3.named)
  81. self.assertFalse(node3.hexadecimal)
  82. node1.value = "10FFFF"
  83. node2.value = 110000
  84. node2.value = 1114111
  85. self.assertRaises(ValueError, setattr, node3, "value", "")
  86. self.assertRaises(ValueError, setattr, node3, "value", "foobar")
  87. self.assertRaises(ValueError, setattr, node3, "value", True)
  88. self.assertRaises(ValueError, setattr, node3, "value", -1)
  89. self.assertRaises(ValueError, setattr, node1, "value", 110000)
  90. self.assertRaises(ValueError, setattr, node1, "value", "1114112")
  91. def test_named(self):
  92. """test getter/setter for the named attribute"""
  93. node1 = HTMLEntity("nbsp")
  94. node2 = HTMLEntity("107")
  95. node3 = HTMLEntity("e9")
  96. self.assertTrue(node1.named)
  97. self.assertFalse(node2.named)
  98. self.assertFalse(node3.named)
  99. node1.named = 1
  100. node2.named = 0
  101. node3.named = 0
  102. self.assertTrue(node1.named)
  103. self.assertFalse(node2.named)
  104. self.assertFalse(node3.named)
  105. self.assertRaises(ValueError, setattr, node1, "named", False)
  106. self.assertRaises(ValueError, setattr, node2, "named", True)
  107. self.assertRaises(ValueError, setattr, node3, "named", True)
  108. def test_hexadecimal(self):
  109. """test getter/setter for the hexadecimal attribute"""
  110. node1 = HTMLEntity("nbsp")
  111. node2 = HTMLEntity("107")
  112. node3 = HTMLEntity("e9")
  113. self.assertFalse(node1.hexadecimal)
  114. self.assertFalse(node2.hexadecimal)
  115. self.assertTrue(node3.hexadecimal)
  116. node1.hexadecimal = False
  117. node2.hexadecimal = True
  118. node3.hexadecimal = False
  119. self.assertFalse(node1.hexadecimal)
  120. self.assertTrue(node2.hexadecimal)
  121. self.assertFalse(node3.hexadecimal)
  122. self.assertRaises(ValueError, setattr, node1, "hexadecimal", True)
  123. def test_hex_char(self):
  124. """test getter/setter for the hex_char attribute"""
  125. node1 = HTMLEntity("e9")
  126. node2 = HTMLEntity("e9", hex_char="X")
  127. self.assertEqual("x", node1.hex_char)
  128. self.assertEqual("X", node2.hex_char)
  129. node1.hex_char = "X"
  130. node2.hex_char = "x"
  131. self.assertEqual("X", node1.hex_char)
  132. self.assertEqual("x", node2.hex_char)
  133. self.assertRaises(ValueError, setattr, node1, "hex_char", 123)
  134. self.assertRaises(ValueError, setattr, node1, "hex_char", "foobar")
  135. self.assertRaises(ValueError, setattr, node1, "hex_char", True)
  136. def test_normalize(self):
  137. """test getter/setter for the normalize attribute"""
  138. node1 = HTMLEntity("nbsp")
  139. node2 = HTMLEntity("107")
  140. node3 = HTMLEntity("e9")
  141. node4 = HTMLEntity("1f648")
  142. self.assertEqual("\xa0", node1.normalize())
  143. self.assertEqual("k", node2.normalize())
  144. self.assertEqual("é", node3.normalize())
  145. self.assertEqual("\U0001F648", node4.normalize())
  146. if __name__ == "__main__":
  147. unittest.main(verbosity=2)