A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

215 рядки
6.8 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  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. import unittest
  24. from mwparserfromhell.compat import py3k
  25. from mwparserfromhell.smart_list import SmartList, _ListProxy
  26. class TestSmartList(unittest.TestCase):
  27. """Test cases for the SmartList class and its child, _ListProxy."""
  28. def test_docs(self):
  29. """make sure the methods of SmartList/_ListProxy have docstrings"""
  30. methods = ["append", "count", "extend", "index", "insert", "pop",
  31. "remove", "reverse", "sort"]
  32. for meth in methods:
  33. expected = getattr(list, meth).__doc__
  34. smartlist_doc = getattr(SmartList, meth).__doc__
  35. listproxy_doc = getattr(_ListProxy, meth).__doc__
  36. self.assertEquals(expected, smartlist_doc)
  37. self.assertEquals(expected, listproxy_doc)
  38. def test_doctest(self):
  39. """make sure the test embedded in SmartList's docstring passes"""
  40. parent = SmartList([0, 1, 2, 3])
  41. self.assertEquals([0, 1, 2, 3], parent)
  42. child = parent[2:]
  43. self.assertEquals([2, 3], child)
  44. child.append(4)
  45. self.assertEquals([2, 3, 4], child)
  46. self.assertEquals([0, 1, 2, 3, 4], parent)
  47. def test_parent_magics(self):
  48. """make sure magically implemented SmartList features work"""
  49. # __getitem__
  50. # __setitem__
  51. # __delitem__
  52. # if not py3k:
  53. # __getslice__
  54. # __setslice__
  55. # __delslice__
  56. # __add__
  57. # __radd__
  58. # __iadd__
  59. def test_parent_unaffected_magics(self):
  60. """sanity checks against SmartList features that were not modified"""
  61. list1 = SmartList([0, 1, 2, 3, "one", "two"])
  62. list2 = SmartList([])
  63. list3 = SmartList([0, 2, 3, 4])
  64. list4 = SmartList([0, 1, 2])
  65. if py3k:
  66. self.assertEquals("[0, 1, 2, 3, 'one', 'two']", str(list1))
  67. self.assertEquals(b"[0, 1, 2, 3, 'one', 'two']", bytes(list1))
  68. self.assertEquals("[0, 1, 2, 3, 'one', 'two']", repr(list1))
  69. else:
  70. self.assertEquals("[0, 1, 2, 3, u'one', u'two']", unicode(list1))
  71. self.assertEquals(b"[0, 1, 2, 3, u'one', u'two']", str(list1))
  72. self.assertEquals(b"[0, 1, 2, 3, u'one', u'two']", repr(list1))
  73. self.assertTrue(list1 < list3)
  74. self.assertTrue(list1 <= list3)
  75. self.assertFalse(list1 == list3)
  76. self.assertTrue(list1 != list3)
  77. self.assertFalse(list1 > list3)
  78. self.assertFalse(list1 >= list3)
  79. other1 = [0, 2, 3, 4]
  80. self.assertTrue(list1 < other1)
  81. self.assertTrue(list1 <= other1)
  82. self.assertFalse(list1 == other1)
  83. self.assertTrue(list1 != other1)
  84. self.assertFalse(list1 > other1)
  85. self.assertFalse(list1 >= other1)
  86. other2 = [0, 0, 1, 2]
  87. self.assertFalse(list1 < other2)
  88. self.assertFalse(list1 <= other2)
  89. self.assertFalse(list1 == other2)
  90. self.assertTrue(list1 != other2)
  91. self.assertTrue(list1 > other2)
  92. self.assertTrue(list1 >= other2)
  93. other3 = [0, 1, 2, 3, "one", "two"]
  94. self.assertFalse(list1 < other3)
  95. self.assertTrue(list1 <= other3)
  96. self.assertTrue(list1 == other3)
  97. self.assertFalse(list1 != other3)
  98. self.assertFalse(list1 > other3)
  99. self.assertTrue(list1 >= other3)
  100. self.assertTrue(bool(list1))
  101. self.assertFalse(bool(list2))
  102. self.assertEquals(6, len(list1))
  103. self.assertEquals(0, len(list2))
  104. out = []
  105. for obj in list1:
  106. out.append(obj)
  107. self.assertEquals([0, 1, 2, 3, "one", "two"], out)
  108. out = []
  109. for ch in list2:
  110. out.append(ch)
  111. self.assertEquals([], out)
  112. gen1 = iter(list1)
  113. out = []
  114. for i in range(len(list1)):
  115. out.append(gen1.next())
  116. self.assertRaises(StopIteration, gen1.next)
  117. self.assertEquals([0, 1, 2, 3, "one", "two"], out)
  118. gen2 = iter(list2)
  119. self.assertRaises(StopIteration, gen2.next)
  120. self.assertEquals(["two", "one", 3, 2, 1, 0], list(reversed(list1)))
  121. self.assertEquals([], list(reversed(list2)))
  122. self.assertTrue("one" in list1)
  123. self.assertTrue(3 in list1)
  124. self.assertFalse(10 in list1)
  125. self.assertFalse(0 in list2)
  126. self.assertEquals([], list2 * 5)
  127. self.assertEquals([], 5 * list2)
  128. self.assertEquals([0, 1, 2, 0, 1, 2, 0, 1, 2], list4 * 3)
  129. self.assertEquals([0, 1, 2, 0, 1, 2, 0, 1, 2], 3 * list4)
  130. list4 *= 2
  131. self.assertEquals([0, 1, 2, 0, 1, 2], list4)
  132. def test_parent_methods(self):
  133. # append
  134. # count
  135. # extend
  136. # index
  137. # insert
  138. # pop
  139. # remove
  140. # reverse
  141. # sort
  142. def test_child_magics(self):
  143. # if py3k:
  144. # __str__
  145. # __bytes__
  146. # else:
  147. # __unicode__
  148. # __str__
  149. # __repr__
  150. # __lt__
  151. # __le__
  152. # __eq__
  153. # __ne__
  154. # __gt__
  155. # __ge__
  156. # if py3k:
  157. # __bool__
  158. # else:
  159. # __nonzero__
  160. # __len__
  161. # __getitem__
  162. # __setitem__
  163. # __delitem__
  164. # __iter__
  165. # __reversed__
  166. # __contains__
  167. # if not py3k:
  168. # __getslice__
  169. # __setslice__
  170. # __delslice__
  171. # __add__
  172. # __radd__
  173. # __iadd__
  174. # __mul__
  175. # __rmul__
  176. # __imul__
  177. def test_child_methods(self):
  178. # append
  179. # count
  180. # extend
  181. # index
  182. # insert
  183. # pop
  184. # remove
  185. # reverse
  186. # sort
  187. def test_influence(self):
  188. pass
  189. if __name__ == "__main__":
  190. unittest.main(verbosity=2)