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

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