A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

170 lignes
7.2 KiB

  1. # Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. import unittest
  21. from mwparserfromhell.nodes import HTMLEntity
  22. from ._test_tree_equality import TreeEqualityTestCase
  23. class TestHTMLEntity(TreeEqualityTestCase):
  24. """Test cases for the HTMLEntity node."""
  25. def test_str(self):
  26. """test HTMLEntity.__str__()"""
  27. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  28. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  29. node3 = HTMLEntity("6b", named=False, hexadecimal=True)
  30. node4 = HTMLEntity("6C", named=False, hexadecimal=True, hex_char="X")
  31. self.assertEqual("&nbsp;", str(node1))
  32. self.assertEqual("&#107;", str(node2))
  33. self.assertEqual("&#x6b;", str(node3))
  34. self.assertEqual("&#X6C;", str(node4))
  35. def test_children(self):
  36. """test HTMLEntity.__children__()"""
  37. node = HTMLEntity("nbsp", named=True, hexadecimal=False)
  38. gen = node.__children__()
  39. self.assertRaises(StopIteration, next, gen)
  40. def test_strip(self):
  41. """test HTMLEntity.__strip__()"""
  42. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  43. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  44. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  45. self.assertEqual("\xa0", node1.__strip__(normalize=True))
  46. self.assertEqual("&nbsp;", node1.__strip__(normalize=False))
  47. self.assertEqual("k", node2.__strip__(normalize=True))
  48. self.assertEqual("&#107;", node2.__strip__(normalize=False))
  49. self.assertEqual("é", node3.__strip__(normalize=True))
  50. self.assertEqual("&#xe9;", node3.__strip__(normalize=False))
  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. self.assertRaises(ValueError, setattr, node1, "value", "12FFFF")
  92. def test_named(self):
  93. """test getter/setter for the named attribute"""
  94. node1 = HTMLEntity("nbsp")
  95. node2 = HTMLEntity("107")
  96. node3 = HTMLEntity("e9")
  97. self.assertTrue(node1.named)
  98. self.assertFalse(node2.named)
  99. self.assertFalse(node3.named)
  100. node1.named = 1
  101. node2.named = 0
  102. node3.named = 0
  103. self.assertTrue(node1.named)
  104. self.assertFalse(node2.named)
  105. self.assertFalse(node3.named)
  106. self.assertRaises(ValueError, setattr, node1, "named", False)
  107. self.assertRaises(ValueError, setattr, node2, "named", True)
  108. self.assertRaises(ValueError, setattr, node3, "named", True)
  109. def test_hexadecimal(self):
  110. """test getter/setter for the hexadecimal attribute"""
  111. node1 = HTMLEntity("nbsp")
  112. node2 = HTMLEntity("107")
  113. node3 = HTMLEntity("e9")
  114. self.assertFalse(node1.hexadecimal)
  115. self.assertFalse(node2.hexadecimal)
  116. self.assertTrue(node3.hexadecimal)
  117. node1.hexadecimal = False
  118. node2.hexadecimal = True
  119. node3.hexadecimal = False
  120. self.assertFalse(node1.hexadecimal)
  121. self.assertTrue(node2.hexadecimal)
  122. self.assertFalse(node3.hexadecimal)
  123. self.assertRaises(ValueError, setattr, node1, "hexadecimal", True)
  124. def test_hex_char(self):
  125. """test getter/setter for the hex_char attribute"""
  126. node1 = HTMLEntity("e9")
  127. node2 = HTMLEntity("e9", hex_char="X")
  128. self.assertEqual("x", node1.hex_char)
  129. self.assertEqual("X", node2.hex_char)
  130. node1.hex_char = "X"
  131. node2.hex_char = "x"
  132. self.assertEqual("X", node1.hex_char)
  133. self.assertEqual("x", node2.hex_char)
  134. self.assertRaises(ValueError, setattr, node1, "hex_char", 123)
  135. self.assertRaises(ValueError, setattr, node1, "hex_char", "foobar")
  136. self.assertRaises(ValueError, setattr, node1, "hex_char", True)
  137. def test_normalize(self):
  138. """test getter/setter for the normalize attribute"""
  139. node1 = HTMLEntity("nbsp")
  140. node2 = HTMLEntity("107")
  141. node3 = HTMLEntity("e9")
  142. node4 = HTMLEntity("1f648")
  143. node5 = HTMLEntity("-2")
  144. node6 = HTMLEntity("110000", named=False, hexadecimal=True)
  145. self.assertEqual("\xa0", node1.normalize())
  146. self.assertEqual("k", node2.normalize())
  147. self.assertEqual("é", node3.normalize())
  148. self.assertEqual("\U0001F648", node4.normalize())
  149. self.assertRaises(ValueError, node5.normalize)
  150. self.assertRaises(ValueError, node6.normalize)
  151. if __name__ == "__main__":
  152. unittest.main(verbosity=2)