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.

test_smart_list.py 10 KiB

11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, range
  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_get_set_del(self):
  48. """make sure SmartList's getitem/setitem/delitem work"""
  49. def assign(L, s1, s2, s3, val):
  50. L[s1:s2:s3] = val
  51. def delete(L, s1):
  52. del L[s1]
  53. list1 = SmartList([0, 1, 2, 3, "one", "two"])
  54. list2 = SmartList(list(range(10)))
  55. self.assertEquals(1, list1[1])
  56. self.assertEquals("one", list1[-2])
  57. self.assertEquals([2, 3], list1[2:4])
  58. self.assertRaises(IndexError, lambda: list1[6])
  59. self.assertRaises(IndexError, lambda: list1[-7])
  60. self.assertEquals([0, 1, 2], list1[:3])
  61. self.assertEquals([0, 1, 2, 3, "one", "two"], list1[:])
  62. self.assertEquals([3, "one", "two"], list1[3:])
  63. self.assertEquals(["one", "two"], list1[-2:])
  64. self.assertEquals([0, 1], list1[:-4])
  65. self.assertEquals([], list1[6:])
  66. self.assertEquals([], list1[4:2])
  67. self.assertEquals([0, 2, "one"], list1[0:5:2])
  68. self.assertEquals([0, 2], list1[0:-3:2])
  69. self.assertEquals([0, 1, 2, 3, "one", "two"], list1[::])
  70. self.assertEquals([2, 3, "one", "two"], list1[2::])
  71. self.assertEquals([0, 1, 2, 3], list1[:4:])
  72. self.assertEquals([2, 3], list1[2:4:])
  73. self.assertEquals([0, 2, 4, 6, 8], list2[::2])
  74. self.assertEquals([2, 5, 8], list2[2::3])
  75. self.assertEquals([0, 3], list2[:6:3])
  76. self.assertEquals([2, 5, 8], list2[-8:9:3])
  77. self.assertEquals([], list2[100000:1000:-100])
  78. list1[3] = 100
  79. self.assertEquals(100, list1[3])
  80. list1[5:] = [6, 7, 8]
  81. self.assertEquals([6, 7, 8], list1[5:])
  82. self.assertEquals([0, 1, 2, 100, "one", 6, 7, 8], list1)
  83. list1[2:4] = [-1, -2, -3, -4, -5]
  84. self.assertEquals([0, 1, -1, -2, -3, -4, -5, "one", 6, 7, 8], list1)
  85. list1[0:-3] = [99]
  86. self.assertEquals([99, 6, 7, 8], list1)
  87. list2[0:6:2] = [100, 102, 104]
  88. self.assertEquals([100, 1, 102, 3, 104, 5, 6, 7, 8, 9], list2)
  89. list2[::3] = [200, 203, 206, 209]
  90. self.assertEquals([200, 1, 102, 203, 104, 5, 206, 7, 8, 209], list2)
  91. list2[::] = range(7)
  92. self.assertEquals([0, 1, 2, 3, 4, 5, 6], list2)
  93. self.assertRaises(ValueError,
  94. lambda: assign(list2, 0, 5, 2, [100, 102, 104, 106]))
  95. del list2[2]
  96. self.assertEquals([0, 1, 3, 4, 5, 6], list2)
  97. del list2[-3]
  98. self.assertEquals([0, 1, 3, 5, 6], list2)
  99. self.assertRaises(IndexError, lambda: delete(list2, 100))
  100. self.assertRaises(IndexError, lambda: delete(list2, -6))
  101. list2[:] = range(10)
  102. del list2[3:6]
  103. self.assertEquals([0, 1, 2, 6, 7, 8, 9], list2)
  104. del list2[-2:]
  105. self.assertEquals([0, 1, 2, 6, 7], list2)
  106. del list2[:2]
  107. self.assertEquals([2, 6, 7], list2)
  108. list2[:] = range(10)
  109. del list2[2:8:2]
  110. self.assertEquals([0, 1, 3, 5, 7, 8, 9], list2)
  111. def test_parent_add(self):
  112. """make sure SmartList's add/radd/iadd work"""
  113. list1 = SmartList(range(5))
  114. list2 = SmartList(range(5, 10))
  115. self.assertEquals([0, 1, 2, 3, 4, 5, 6], list1 + [5, 6])
  116. self.assertEquals([0, 1, 2, 3, 4], list1)
  117. self.assertEquals(list(range(10)), list1 + list2)
  118. self.assertEquals([-2, -1, 0, 1, 2, 3, 4], [-2, -1] + list1)
  119. self.assertEquals([0, 1, 2, 3, 4], list1)
  120. list1 += ["foo", "bar", "baz"]
  121. self.assertEquals([0, 1, 2, 3, 4, "foo", "bar", "baz"], list1)
  122. def test_parent_unaffected_magics(self):
  123. """sanity checks against SmartList features that were not modified"""
  124. list1 = SmartList([0, 1, 2, 3, "one", "two"])
  125. list2 = SmartList([])
  126. list3 = SmartList([0, 2, 3, 4])
  127. list4 = SmartList([0, 1, 2])
  128. if py3k:
  129. self.assertEquals("[0, 1, 2, 3, 'one', 'two']", str(list1))
  130. self.assertEquals(b"[0, 1, 2, 3, 'one', 'two']", bytes(list1))
  131. self.assertEquals("[0, 1, 2, 3, 'one', 'two']", repr(list1))
  132. else:
  133. self.assertEquals("[0, 1, 2, 3, u'one', u'two']", unicode(list1))
  134. self.assertEquals(b"[0, 1, 2, 3, u'one', u'two']", str(list1))
  135. self.assertEquals(b"[0, 1, 2, 3, u'one', u'two']", repr(list1))
  136. self.assertTrue(list1 < list3)
  137. self.assertTrue(list1 <= list3)
  138. self.assertFalse(list1 == list3)
  139. self.assertTrue(list1 != list3)
  140. self.assertFalse(list1 > list3)
  141. self.assertFalse(list1 >= list3)
  142. other1 = [0, 2, 3, 4]
  143. self.assertTrue(list1 < other1)
  144. self.assertTrue(list1 <= other1)
  145. self.assertFalse(list1 == other1)
  146. self.assertTrue(list1 != other1)
  147. self.assertFalse(list1 > other1)
  148. self.assertFalse(list1 >= other1)
  149. other2 = [0, 0, 1, 2]
  150. self.assertFalse(list1 < other2)
  151. self.assertFalse(list1 <= other2)
  152. self.assertFalse(list1 == other2)
  153. self.assertTrue(list1 != other2)
  154. self.assertTrue(list1 > other2)
  155. self.assertTrue(list1 >= other2)
  156. other3 = [0, 1, 2, 3, "one", "two"]
  157. self.assertFalse(list1 < other3)
  158. self.assertTrue(list1 <= other3)
  159. self.assertTrue(list1 == other3)
  160. self.assertFalse(list1 != other3)
  161. self.assertFalse(list1 > other3)
  162. self.assertTrue(list1 >= other3)
  163. self.assertTrue(bool(list1))
  164. self.assertFalse(bool(list2))
  165. self.assertEquals(6, len(list1))
  166. self.assertEquals(0, len(list2))
  167. out = []
  168. for obj in list1:
  169. out.append(obj)
  170. self.assertEquals([0, 1, 2, 3, "one", "two"], out)
  171. out = []
  172. for ch in list2:
  173. out.append(ch)
  174. self.assertEquals([], out)
  175. gen1 = iter(list1)
  176. out = []
  177. for i in range(len(list1)):
  178. out.append(gen1.next())
  179. self.assertRaises(StopIteration, gen1.next)
  180. self.assertEquals([0, 1, 2, 3, "one", "two"], out)
  181. gen2 = iter(list2)
  182. self.assertRaises(StopIteration, gen2.next)
  183. self.assertEquals(["two", "one", 3, 2, 1, 0], list(reversed(list1)))
  184. self.assertEquals([], list(reversed(list2)))
  185. self.assertTrue("one" in list1)
  186. self.assertTrue(3 in list1)
  187. self.assertFalse(10 in list1)
  188. self.assertFalse(0 in list2)
  189. self.assertEquals([], list2 * 5)
  190. self.assertEquals([], 5 * list2)
  191. self.assertEquals([0, 1, 2, 0, 1, 2, 0, 1, 2], list4 * 3)
  192. self.assertEquals([0, 1, 2, 0, 1, 2, 0, 1, 2], 3 * list4)
  193. list4 *= 2
  194. self.assertEquals([0, 1, 2, 0, 1, 2], list4)
  195. def test_parent_methods(self):
  196. pass
  197. # append
  198. # count
  199. # extend
  200. # index
  201. # insert
  202. # pop
  203. # remove
  204. # reverse
  205. # sort
  206. def test_child_magics(self):
  207. pass
  208. # if py3k:
  209. # __str__
  210. # __bytes__
  211. # else:
  212. # __unicode__
  213. # __str__
  214. # __repr__
  215. # __lt__
  216. # __le__
  217. # __eq__
  218. # __ne__
  219. # __gt__
  220. # __ge__
  221. # if py3k:
  222. # __bool__
  223. # else:
  224. # __nonzero__
  225. # __len__
  226. # __getitem__
  227. # __setitem__
  228. # __delitem__
  229. # __iter__
  230. # __reversed__
  231. # __contains__
  232. # if not py3k:
  233. # __getslice__
  234. # __setslice__
  235. # __delslice__
  236. # __add__
  237. # __radd__
  238. # __iadd__
  239. # __mul__
  240. # __rmul__
  241. # __imul__
  242. def test_child_methods(self):
  243. pass
  244. # append
  245. # count
  246. # extend
  247. # index
  248. # insert
  249. # pop
  250. # remove
  251. # reverse
  252. # sort
  253. def test_influence(self):
  254. pass
  255. # test whether changes are propogated correctly
  256. # also test whether children that exit scope are removed from parent's map
  257. if __name__ == "__main__":
  258. unittest.main(verbosity=2)