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.
 
 
 
 

183 Zeilen
6.4 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. """
  21. Test cases for the HTMLEntity node.
  22. """
  23. import pytest
  24. from mwparserfromhell.nodes import HTMLEntity
  25. def test_str():
  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. assert "&nbsp;" == str(node1)
  32. assert "&#107;" == str(node2)
  33. assert "&#x6b;" == str(node3)
  34. assert "&#X6C;" == str(node4)
  35. def test_children():
  36. """test HTMLEntity.__children__()"""
  37. node = HTMLEntity("nbsp", named=True, hexadecimal=False)
  38. gen = node.__children__()
  39. with pytest.raises(StopIteration):
  40. next(gen)
  41. def test_strip():
  42. """test HTMLEntity.__strip__()"""
  43. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  44. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  45. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  46. assert "\xa0" == node1.__strip__(normalize=True)
  47. assert "&nbsp;" == node1.__strip__(normalize=False)
  48. assert "k" == node2.__strip__(normalize=True)
  49. assert "&#107;" == node2.__strip__(normalize=False)
  50. assert "é" == node3.__strip__(normalize=True)
  51. assert "&#xe9;" == node3.__strip__(normalize=False)
  52. def test_showtree():
  53. """test HTMLEntity.__showtree__()"""
  54. output = []
  55. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  56. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  57. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  58. node1.__showtree__(output.append, None, None)
  59. node2.__showtree__(output.append, None, None)
  60. node3.__showtree__(output.append, None, None)
  61. res = ["&nbsp;", "&#107;", "&#xe9;"]
  62. assert res == output
  63. def test_value():
  64. """test getter/setter for the value attribute"""
  65. node1 = HTMLEntity("nbsp")
  66. node2 = HTMLEntity("107")
  67. node3 = HTMLEntity("e9")
  68. assert "nbsp" == node1.value
  69. assert "107" == node2.value
  70. assert "e9" == node3.value
  71. node1.value = "ffa4"
  72. node2.value = 72
  73. node3.value = "Sigma"
  74. assert "ffa4" == node1.value
  75. assert node1.named is False
  76. assert node1.hexadecimal is True
  77. assert "72" == node2.value
  78. assert node2.named is False
  79. assert node2.hexadecimal is False
  80. assert "Sigma" == node3.value
  81. assert node3.named is True
  82. assert node3.hexadecimal is False
  83. node1.value = "10FFFF"
  84. node2.value = 110000
  85. node2.value = 1114111
  86. with pytest.raises(ValueError):
  87. node3.__setattr__("value", "")
  88. with pytest.raises(ValueError):
  89. node3.__setattr__("value", "foobar")
  90. with pytest.raises(ValueError):
  91. node3.__setattr__("value", True)
  92. with pytest.raises(ValueError):
  93. node3.__setattr__("value", -1)
  94. with pytest.raises(ValueError):
  95. node1.__setattr__("value", 110000)
  96. with pytest.raises(ValueError):
  97. node1.__setattr__("value", "1114112")
  98. with pytest.raises(ValueError):
  99. node1.__setattr__("value", "12FFFF")
  100. def test_named():
  101. """test getter/setter for the named attribute"""
  102. node1 = HTMLEntity("nbsp")
  103. node2 = HTMLEntity("107")
  104. node3 = HTMLEntity("e9")
  105. assert node1.named is True
  106. assert node2.named is False
  107. assert node3.named is False
  108. node1.named = 1
  109. node2.named = 0
  110. node3.named = 0
  111. assert node1.named is True
  112. assert node2.named is False
  113. assert node3.named is False
  114. with pytest.raises(ValueError):
  115. node1.__setattr__("named", False)
  116. with pytest.raises(ValueError):
  117. node2.__setattr__("named", True)
  118. with pytest.raises(ValueError):
  119. node3.__setattr__("named", True)
  120. def test_hexadecimal():
  121. """test getter/setter for the hexadecimal attribute"""
  122. node1 = HTMLEntity("nbsp")
  123. node2 = HTMLEntity("107")
  124. node3 = HTMLEntity("e9")
  125. assert node1.hexadecimal is False
  126. assert node2.hexadecimal is False
  127. assert node3.hexadecimal is True
  128. node1.hexadecimal = False
  129. node2.hexadecimal = True
  130. node3.hexadecimal = False
  131. assert node1.hexadecimal is False
  132. assert node2.hexadecimal is True
  133. assert node3.hexadecimal is False
  134. with pytest.raises(ValueError):
  135. node1.__setattr__("hexadecimal", True)
  136. def test_hex_char():
  137. """test getter/setter for the hex_char attribute"""
  138. node1 = HTMLEntity("e9")
  139. node2 = HTMLEntity("e9", hex_char="X")
  140. assert "x" == node1.hex_char
  141. assert "X" == node2.hex_char
  142. node1.hex_char = "X"
  143. node2.hex_char = "x"
  144. assert "X" == node1.hex_char
  145. assert "x" == node2.hex_char
  146. with pytest.raises(ValueError):
  147. node1.__setattr__("hex_char", 123)
  148. with pytest.raises(ValueError):
  149. node1.__setattr__("hex_char", "foobar")
  150. with pytest.raises(ValueError):
  151. node1.__setattr__("hex_char", True)
  152. def test_normalize():
  153. """test getter/setter for the normalize attribute"""
  154. node1 = HTMLEntity("nbsp")
  155. node2 = HTMLEntity("107")
  156. node3 = HTMLEntity("e9")
  157. node4 = HTMLEntity("1f648")
  158. node5 = HTMLEntity("-2")
  159. node6 = HTMLEntity("110000", named=False, hexadecimal=True)
  160. assert "\xa0" == node1.normalize()
  161. assert "k" == node2.normalize()
  162. assert "é" == node3.normalize()
  163. assert "\U0001F648" == node4.normalize()
  164. with pytest.raises(ValueError):
  165. node5.normalize()
  166. with pytest.raises(ValueError):
  167. node6.normalize()