A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

178 lines
7.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2016 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. self.assertEqual("\xa0", node1.__strip__(normalize=True))
  53. self.assertEqual("&nbsp;", node1.__strip__(normalize=False))
  54. self.assertEqual("k", node2.__strip__(normalize=True))
  55. self.assertEqual("&#107;", node2.__strip__(normalize=False))
  56. self.assertEqual("é", node3.__strip__(normalize=True))
  57. self.assertEqual("&#xe9;", node3.__strip__(normalize=False))
  58. def test_showtree(self):
  59. """test HTMLEntity.__showtree__()"""
  60. output = []
  61. node1 = HTMLEntity("nbsp", named=True, hexadecimal=False)
  62. node2 = HTMLEntity("107", named=False, hexadecimal=False)
  63. node3 = HTMLEntity("e9", named=False, hexadecimal=True)
  64. node1.__showtree__(output.append, None, None)
  65. node2.__showtree__(output.append, None, None)
  66. node3.__showtree__(output.append, None, None)
  67. res = ["&nbsp;", "&#107;", "&#xe9;"]
  68. self.assertEqual(res, output)
  69. def test_value(self):
  70. """test getter/setter for the value attribute"""
  71. node1 = HTMLEntity("nbsp")
  72. node2 = HTMLEntity("107")
  73. node3 = HTMLEntity("e9")
  74. self.assertEqual("nbsp", node1.value)
  75. self.assertEqual("107", node2.value)
  76. self.assertEqual("e9", node3.value)
  77. node1.value = "ffa4"
  78. node2.value = 72
  79. node3.value = "Sigma"
  80. self.assertEqual("ffa4", node1.value)
  81. self.assertFalse(node1.named)
  82. self.assertTrue(node1.hexadecimal)
  83. self.assertEqual("72", node2.value)
  84. self.assertFalse(node2.named)
  85. self.assertFalse(node2.hexadecimal)
  86. self.assertEqual("Sigma", node3.value)
  87. self.assertTrue(node3.named)
  88. self.assertFalse(node3.hexadecimal)
  89. node1.value = "10FFFF"
  90. node2.value = 110000
  91. node2.value = 1114111
  92. self.assertRaises(ValueError, setattr, node3, "value", "")
  93. self.assertRaises(ValueError, setattr, node3, "value", "foobar")
  94. self.assertRaises(ValueError, setattr, node3, "value", True)
  95. self.assertRaises(ValueError, setattr, node3, "value", -1)
  96. self.assertRaises(ValueError, setattr, node1, "value", 110000)
  97. self.assertRaises(ValueError, setattr, node1, "value", "1114112")
  98. self.assertRaises(ValueError, setattr, node1, "value", "12FFFF")
  99. def test_named(self):
  100. """test getter/setter for the named attribute"""
  101. node1 = HTMLEntity("nbsp")
  102. node2 = HTMLEntity("107")
  103. node3 = HTMLEntity("e9")
  104. self.assertTrue(node1.named)
  105. self.assertFalse(node2.named)
  106. self.assertFalse(node3.named)
  107. node1.named = 1
  108. node2.named = 0
  109. node3.named = 0
  110. self.assertTrue(node1.named)
  111. self.assertFalse(node2.named)
  112. self.assertFalse(node3.named)
  113. self.assertRaises(ValueError, setattr, node1, "named", False)
  114. self.assertRaises(ValueError, setattr, node2, "named", True)
  115. self.assertRaises(ValueError, setattr, node3, "named", True)
  116. def test_hexadecimal(self):
  117. """test getter/setter for the hexadecimal attribute"""
  118. node1 = HTMLEntity("nbsp")
  119. node2 = HTMLEntity("107")
  120. node3 = HTMLEntity("e9")
  121. self.assertFalse(node1.hexadecimal)
  122. self.assertFalse(node2.hexadecimal)
  123. self.assertTrue(node3.hexadecimal)
  124. node1.hexadecimal = False
  125. node2.hexadecimal = True
  126. node3.hexadecimal = False
  127. self.assertFalse(node1.hexadecimal)
  128. self.assertTrue(node2.hexadecimal)
  129. self.assertFalse(node3.hexadecimal)
  130. self.assertRaises(ValueError, setattr, node1, "hexadecimal", True)
  131. def test_hex_char(self):
  132. """test getter/setter for the hex_char attribute"""
  133. node1 = HTMLEntity("e9")
  134. node2 = HTMLEntity("e9", hex_char="X")
  135. self.assertEqual("x", node1.hex_char)
  136. self.assertEqual("X", node2.hex_char)
  137. node1.hex_char = "X"
  138. node2.hex_char = "x"
  139. self.assertEqual("X", node1.hex_char)
  140. self.assertEqual("x", node2.hex_char)
  141. self.assertRaises(ValueError, setattr, node1, "hex_char", 123)
  142. self.assertRaises(ValueError, setattr, node1, "hex_char", "foobar")
  143. self.assertRaises(ValueError, setattr, node1, "hex_char", True)
  144. def test_normalize(self):
  145. """test getter/setter for the normalize attribute"""
  146. node1 = HTMLEntity("nbsp")
  147. node2 = HTMLEntity("107")
  148. node3 = HTMLEntity("e9")
  149. node4 = HTMLEntity("1f648")
  150. node5 = HTMLEntity("-2")
  151. node6 = HTMLEntity("110000", named=False, hexadecimal=True)
  152. self.assertEqual("\xa0", node1.normalize())
  153. self.assertEqual("k", node2.normalize())
  154. self.assertEqual("é", node3.normalize())
  155. self.assertEqual("\U0001F648", node4.normalize())
  156. self.assertRaises(ValueError, node5.normalize)
  157. self.assertRaises(ValueError, node6.normalize)
  158. if __name__ == "__main__":
  159. unittest.main(verbosity=2)