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 12 KiB

пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, assign, list2, 0, 5, 2,
  94. [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, delete, list2, 100)
  100. self.assertRaises(IndexError, 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. """make sure SmartList's non-magic methods work, like append()"""
  197. list1 = SmartList(range(5))
  198. list2 = SmartList(["foo"])
  199. list3 = SmartList([("a", 5), ("d", 2), ("b", 8), ("c", 3)])
  200. list1.append(5)
  201. list1.append(1)
  202. list1.append(2)
  203. self.assertEquals([0, 1, 2, 3, 4, 5, 1, 2], list1)
  204. self.assertEquals(0, list1.count(6))
  205. self.assertEquals(2, list1.count(1))
  206. list1.extend(range(5, 8))
  207. self.assertEquals([0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
  208. self.assertEquals(1, list1.index(1))
  209. self.assertEquals(6, list1.index(1, 3))
  210. self.assertEquals(6, list1.index(1, 3, 7))
  211. self.assertRaises(ValueError, list1.index, 1, 3, 5)
  212. list1.insert(0, -1)
  213. self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
  214. list1.insert(-1, 6.5)
  215. self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7], list1)
  216. list1.insert(100, 8)
  217. self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7, 8], list1)
  218. self.assertEquals(8, list1.pop())
  219. self.assertEquals(7, list1.pop())
  220. self.assertEquals([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5], list1)
  221. self.assertEquals(-1, list1.pop(0))
  222. self.assertEquals(5, list1.pop(5))
  223. self.assertEquals([0, 1, 2, 3, 4, 1, 2, 5, 6, 6.5], list1)
  224. self.assertEquals("foo", list2.pop())
  225. self.assertRaises(IndexError, list2.pop)
  226. self.assertEquals([], list2)
  227. list1.remove(6.5)
  228. self.assertEquals([0, 1, 2, 3, 4, 1, 2, 5, 6], list1)
  229. list1.remove(1)
  230. self.assertEquals([0, 2, 3, 4, 1, 2, 5, 6], list1)
  231. list1.remove(1)
  232. self.assertEquals([0, 2, 3, 4, 2, 5, 6], list1)
  233. self.assertRaises(ValueError, list1.remove, 1)
  234. list1.reverse()
  235. self.assertEquals([6, 5, 2, 4, 3, 2, 0], list1)
  236. list1.sort()
  237. self.assertEquals([0, 2, 2, 3, 4, 5, 6], list1)
  238. list1.sort(reverse=True)
  239. self.assertEquals([6, 5, 4, 3, 2, 2, 0], list1)
  240. list1.sort(cmp=lambda x, y: abs(3 - x) - abs(3 - y)) # Distance from 3
  241. self.assertEquals([3, 4, 2, 2, 5, 6, 0], list1)
  242. list1.sort(cmp=lambda x, y: abs(3 - x) - abs(3 - y), reverse=True)
  243. self.assertEquals([6, 0, 5, 4, 2, 2, 3], list1)
  244. list3.sort(key=lambda i: i[1])
  245. self.assertEquals([("d", 2), ("c", 3), ("a", 5), ("b", 8)], list3)
  246. list3.sort(key=lambda i: i[1], reverse=True)
  247. self.assertEquals([("b", 8), ("a", 5), ("c", 3), ("d", 2)], list3)
  248. def test_child_magics(self):
  249. pass
  250. # if py3k:
  251. # __str__
  252. # __bytes__
  253. # else:
  254. # __unicode__
  255. # __str__
  256. # __repr__
  257. # __lt__
  258. # __le__
  259. # __eq__
  260. # __ne__
  261. # __gt__
  262. # __ge__
  263. # if py3k:
  264. # __bool__
  265. # else:
  266. # __nonzero__
  267. # __len__
  268. # __getitem__
  269. # __setitem__
  270. # __delitem__
  271. # __iter__
  272. # __reversed__
  273. # __contains__
  274. # if not py3k:
  275. # __getslice__
  276. # __setslice__
  277. # __delslice__
  278. # __add__
  279. # __radd__
  280. # __iadd__
  281. # __mul__
  282. # __rmul__
  283. # __imul__
  284. def test_child_methods(self):
  285. pass
  286. # append
  287. # count
  288. # extend
  289. # index
  290. # insert
  291. # pop
  292. # remove
  293. # reverse
  294. # sort
  295. def test_influence(self):
  296. pass
  297. # test whether changes are propogated correctly
  298. # also test whether children that exit scope are removed from parent's map
  299. if __name__ == "__main__":
  300. unittest.main(verbosity=2)