A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test_html_entity.py 7.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2016 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. self.assertEqual("\xa0", node1.__strip__(normalize=True))
  50. self.assertEqual("&nbsp;", node1.__strip__(normalize=False))
  51. self.assertEqual("k", node2.__strip__(normalize=True))
  52. self.assertEqual("&#107;", node2.__strip__(normalize=False))
  53. self.assertEqual("é", node3.__strip__(normalize=True))
  54. self.assertEqual("&#xe9;", node3.__strip__(normalize=False))
  55. def test_showtree(self):
  56. """test HTMLEntity.__showtree__()"""
  57. output = []
  58. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  59. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  60. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  61. node1.__showtree__(output.append, None, None)
  62. node2.__showtree__(output.append, None, None)
  63. node3.__showtree__(output.append, None, None)
  64. res = ["&nbsp;", "&#107;", "&#xe9;"]
  65. self.assertEqual(res, output)
  66. def test_value(self):
  67. """test getter/setter for the value attribute"""
  68. node1 = HTMLEntity("nbsp")
  69. node2 = HTMLEntity("107")
  70. node3 = HTMLEntity("e9")
  71. self.assertEqual("nbsp", node1.value)
  72. self.assertEqual("107", node2.value)
  73. self.assertEqual("e9", node3.value)
  74. node1.value = "ffa4"
  75. node2.value = 72
  76. node3.value = "Sigma"
  77. self.assertEqual("ffa4", node1.value)
  78. self.assertFalse(node1.named)
  79. self.assertTrue(node1.hexadecimal)
  80. self.assertEqual("72", node2.value)
  81. self.assertFalse(node2.named)
  82. self.assertFalse(node2.hexadecimal)
  83. self.assertEqual("Sigma", node3.value)
  84. self.assertTrue(node3.named)
  85. self.assertFalse(node3.hexadecimal)
  86. node1.value = "10FFFF"
  87. node2.value = 110000
  88. node2.value = 1114111
  89. self.assertRaises(ValueError, setattr, node3, "value", "")
  90. self.assertRaises(ValueError, setattr, node3, "value", "foobar")
  91. self.assertRaises(ValueError, setattr, node3, "value", True)
  92. self.assertRaises(ValueError, setattr, node3, "value", -1)
  93. self.assertRaises(ValueError, setattr, node1, "value", 110000)
  94. self.assertRaises(ValueError, setattr, node1, "value", "1114112")
  95. self.assertRaises(ValueError, setattr, node1, "value", "12FFFF")
  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. node5 = HTMLEntity("-2")
  148. node6 = HTMLEntity("110000", named=False, hexadecimal=True)
  149. self.assertEqual("\xa0", node1.normalize())
  150. self.assertEqual("k", node2.normalize())
  151. self.assertEqual("é", node3.normalize())
  152. self.assertEqual("\U0001F648", node4.normalize())
  153. self.assertRaises(ValueError, node5.normalize)
  154. self.assertRaises(ValueError, node6.normalize)
  155. if __name__ == "__main__":
  156. unittest.main(verbosity=2)