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.
 
 
 
 

185 lignes
7.1 KiB

  1. #
  2. # Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. import pytest
  22. from mwparserfromhell.nodes import HTMLEntity
  23. from ._test_tree_equality import TreeEqualityTestCase, wrap
  24. class TestHTMLEntity(TreeEqualityTestCase):
  25. """Test cases for the HTMLEntity node."""
  26. def test_unicode(self):
  27. """test HTMLEntity.__unicode__()"""
  28. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  29. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  30. node3 = HTMLEntity("6b", named=False, hexadecimal=True)
  31. node4 = HTMLEntity("6C", named=False, hexadecimal=True, hex_char="X")
  32. assert "&nbsp;" == str(node1)
  33. assert "&#107;" == str(node2)
  34. assert "&#x6b;" == str(node3)
  35. assert "&#X6C;" == str(node4)
  36. def test_children(self):
  37. """test HTMLEntity.__children__()"""
  38. node = HTMLEntity("nbsp", named=True, hexadecimal=False)
  39. gen = node.__children__()
  40. with pytest.raises(StopIteration):
  41. next(gen)
  42. def test_strip(self):
  43. """test HTMLEntity.__strip__()"""
  44. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  45. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  46. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  47. assert "\xa0" == node1.__strip__(normalize=True)
  48. assert "&nbsp;" == node1.__strip__(normalize=False)
  49. assert "k" == node2.__strip__(normalize=True)
  50. assert "&#107;" == node2.__strip__(normalize=False)
  51. assert "é" == node3.__strip__(normalize=True)
  52. assert "&#xe9;" == node3.__strip__(normalize=False)
  53. def test_showtree(self):
  54. """test HTMLEntity.__showtree__()"""
  55. output = []
  56. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  57. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  58. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  59. node1.__showtree__(output.append, None, None)
  60. node2.__showtree__(output.append, None, None)
  61. node3.__showtree__(output.append, None, None)
  62. res = ["&nbsp;", "&#107;", "&#xe9;"]
  63. assert res == output
  64. def test_value(self):
  65. """test getter/setter for the value attribute"""
  66. node1 = HTMLEntity("nbsp")
  67. node2 = HTMLEntity("107")
  68. node3 = HTMLEntity("e9")
  69. assert "nbsp" == node1.value
  70. assert "107" == node2.value
  71. assert "e9" == node3.value
  72. node1.value = "ffa4"
  73. node2.value = 72
  74. node3.value = "Sigma"
  75. assert "ffa4" == node1.value
  76. assert node1.named is False
  77. assert node1.hexadecimal is True
  78. assert "72" == node2.value
  79. assert node2.named is False
  80. assert node2.hexadecimal is False
  81. assert "Sigma" == node3.value
  82. assert node3.named is True
  83. assert node3.hexadecimal is False
  84. node1.value = "10FFFF"
  85. node2.value = 110000
  86. node2.value = 1114111
  87. with pytest.raises(ValueError):
  88. node3.__setattr__("value", "")
  89. with pytest.raises(ValueError):
  90. node3.__setattr__("value", "foobar")
  91. with pytest.raises(ValueError):
  92. node3.__setattr__("value", True)
  93. with pytest.raises(ValueError):
  94. node3.__setattr__("value", -1)
  95. with pytest.raises(ValueError):
  96. node1.__setattr__("value", 110000)
  97. with pytest.raises(ValueError):
  98. node1.__setattr__("value", "1114112")
  99. with pytest.raises(ValueError):
  100. node1.__setattr__("value", "12FFFF")
  101. def test_named(self):
  102. """test getter/setter for the named attribute"""
  103. node1 = HTMLEntity("nbsp")
  104. node2 = HTMLEntity("107")
  105. node3 = HTMLEntity("e9")
  106. assert node1.named is True
  107. assert node2.named is False
  108. assert node3.named is False
  109. node1.named = 1
  110. node2.named = 0
  111. node3.named = 0
  112. assert node1.named is True
  113. assert node2.named is False
  114. assert node3.named is False
  115. with pytest.raises(ValueError):
  116. node1.__setattr__("named", False)
  117. with pytest.raises(ValueError):
  118. node2.__setattr__("named", True)
  119. with pytest.raises(ValueError):
  120. node3.__setattr__("named", True)
  121. def test_hexadecimal(self):
  122. """test getter/setter for the hexadecimal attribute"""
  123. node1 = HTMLEntity("nbsp")
  124. node2 = HTMLEntity("107")
  125. node3 = HTMLEntity("e9")
  126. assert node1.hexadecimal is False
  127. assert node2.hexadecimal is False
  128. assert node3.hexadecimal is True
  129. node1.hexadecimal = False
  130. node2.hexadecimal = True
  131. node3.hexadecimal = False
  132. assert node1.hexadecimal is False
  133. assert node2.hexadecimal is True
  134. assert node3.hexadecimal is False
  135. with pytest.raises(ValueError):
  136. node1.__setattr__("hexadecimal", True)
  137. def test_hex_char(self):
  138. """test getter/setter for the hex_char attribute"""
  139. node1 = HTMLEntity("e9")
  140. node2 = HTMLEntity("e9", hex_char="X")
  141. assert "x" == node1.hex_char
  142. assert "X" == node2.hex_char
  143. node1.hex_char = "X"
  144. node2.hex_char = "x"
  145. assert "X" == node1.hex_char
  146. assert "x" == node2.hex_char
  147. with pytest.raises(ValueError):
  148. node1.__setattr__("hex_char", 123)
  149. with pytest.raises(ValueError):
  150. node1.__setattr__("hex_char", "foobar")
  151. with pytest.raises(ValueError):
  152. node1.__setattr__("hex_char", True)
  153. def test_normalize(self):
  154. """test getter/setter for the normalize attribute"""
  155. node1 = HTMLEntity("nbsp")
  156. node2 = HTMLEntity("107")
  157. node3 = HTMLEntity("e9")
  158. node4 = HTMLEntity("1f648")
  159. node5 = HTMLEntity("-2")
  160. node6 = HTMLEntity("110000", named=False, hexadecimal=True)
  161. assert "\xa0" == node1.normalize()
  162. assert "k" == node2.normalize()
  163. assert "é" == node3.normalize()
  164. assert "\U0001F648" == node4.normalize()
  165. with pytest.raises(ValueError):
  166. node5.normalize()
  167. with pytest.raises(ValueError):
  168. node6.normalize()