A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

178 Zeilen
7.3 KiB

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