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

393 рядки
16 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 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. import unittest
  24. from mwparserfromhell.compat import py3k
  25. from mwparserfromhell.smart_list import SmartList, _ListProxy
  26. from .compat import range
  27. class TestSmartList(unittest.TestCase):
  28. """Test cases for the SmartList class and its child, _ListProxy."""
  29. def _test_get_set_del_item(self, builder):
  30. """Run tests on __get/set/delitem__ of a list built with *builder*."""
  31. def assign(L, s1, s2, s3, val):
  32. L[s1:s2:s3] = val
  33. def delete(L, s1):
  34. del L[s1]
  35. list1 = builder([0, 1, 2, 3, "one", "two"])
  36. list2 = builder(list(range(10)))
  37. self.assertEqual(1, list1[1])
  38. self.assertEqual("one", list1[-2])
  39. self.assertEqual([2, 3], list1[2:4])
  40. self.assertRaises(IndexError, lambda: list1[6])
  41. self.assertRaises(IndexError, lambda: list1[-7])
  42. self.assertEqual([0, 1, 2], list1[:3])
  43. self.assertEqual([0, 1, 2, 3, "one", "two"], list1[:])
  44. self.assertEqual([3, "one", "two"], list1[3:])
  45. self.assertEqual(["one", "two"], list1[-2:])
  46. self.assertEqual([0, 1], list1[:-4])
  47. self.assertEqual([], list1[6:])
  48. self.assertEqual([], list1[4:2])
  49. self.assertEqual([0, 2, "one"], list1[0:5:2])
  50. self.assertEqual([0, 2], list1[0:-3:2])
  51. self.assertEqual([0, 1, 2, 3, "one", "two"], list1[::])
  52. self.assertEqual([2, 3, "one", "two"], list1[2::])
  53. self.assertEqual([0, 1, 2, 3], list1[:4:])
  54. self.assertEqual([2, 3], list1[2:4:])
  55. self.assertEqual([0, 2, 4, 6, 8], list2[::2])
  56. self.assertEqual([2, 5, 8], list2[2::3])
  57. self.assertEqual([0, 3], list2[:6:3])
  58. self.assertEqual([2, 5, 8], list2[-8:9:3])
  59. self.assertEqual([], list2[100000:1000:-100])
  60. list1[3] = 100
  61. self.assertEqual(100, list1[3])
  62. list1[-3] = 101
  63. self.assertEqual([0, 1, 2, 101, "one", "two"], list1)
  64. list1[5:] = [6, 7, 8]
  65. self.assertEqual([6, 7, 8], list1[5:])
  66. self.assertEqual([0, 1, 2, 101, "one", 6, 7, 8], list1)
  67. list1[2:4] = [-1, -2, -3, -4, -5]
  68. self.assertEqual([0, 1, -1, -2, -3, -4, -5, "one", 6, 7, 8], list1)
  69. list1[0:-3] = [99]
  70. self.assertEqual([99, 6, 7, 8], list1)
  71. list2[0:6:2] = [100, 102, 104]
  72. self.assertEqual([100, 1, 102, 3, 104, 5, 6, 7, 8, 9], list2)
  73. list2[::3] = [200, 203, 206, 209]
  74. self.assertEqual([200, 1, 102, 203, 104, 5, 206, 7, 8, 209], list2)
  75. list2[::] = range(7)
  76. self.assertEqual([0, 1, 2, 3, 4, 5, 6], list2)
  77. self.assertRaises(ValueError, assign, list2, 0, 5, 2,
  78. [100, 102, 104, 106])
  79. del list2[2]
  80. self.assertEqual([0, 1, 3, 4, 5, 6], list2)
  81. del list2[-3]
  82. self.assertEqual([0, 1, 3, 5, 6], list2)
  83. self.assertRaises(IndexError, delete, list2, 100)
  84. self.assertRaises(IndexError, delete, list2, -6)
  85. list2[:] = range(10)
  86. del list2[3:6]
  87. self.assertEqual([0, 1, 2, 6, 7, 8, 9], list2)
  88. del list2[-2:]
  89. self.assertEqual([0, 1, 2, 6, 7], list2)
  90. del list2[:2]
  91. self.assertEqual([2, 6, 7], list2)
  92. list2[:] = range(10)
  93. del list2[2:8:2]
  94. self.assertEqual([0, 1, 3, 5, 7, 8, 9], list2)
  95. def _test_add_radd_iadd(self, builder):
  96. """Run tests on __r/i/add__ of a list built with *builder*."""
  97. list1 = builder(range(5))
  98. list2 = builder(range(5, 10))
  99. self.assertEqual([0, 1, 2, 3, 4, 5, 6], list1 + [5, 6])
  100. self.assertEqual([0, 1, 2, 3, 4], list1)
  101. self.assertEqual(list(range(10)), list1 + list2)
  102. self.assertEqual([-2, -1, 0, 1, 2, 3, 4], [-2, -1] + list1)
  103. self.assertEqual([0, 1, 2, 3, 4], list1)
  104. list1 += ["foo", "bar", "baz"]
  105. self.assertEqual([0, 1, 2, 3, 4, "foo", "bar", "baz"], list1)
  106. def _test_other_magic_methods(self, builder):
  107. """Run tests on other magic methods of a list built with *builder*."""
  108. list1 = builder([0, 1, 2, 3, "one", "two"])
  109. list2 = builder([])
  110. list3 = builder([0, 2, 3, 4])
  111. list4 = builder([0, 1, 2])
  112. if py3k:
  113. self.assertEqual("[0, 1, 2, 3, 'one', 'two']", str(list1))
  114. self.assertEqual(b"\x00\x01\x02", bytes(list4))
  115. self.assertEqual("[0, 1, 2, 3, 'one', 'two']", repr(list1))
  116. else:
  117. self.assertEqual("[0, 1, 2, 3, u'one', u'two']", unicode(list1))
  118. self.assertEqual(b"[0, 1, 2, 3, u'one', u'two']", str(list1))
  119. self.assertEqual(b"[0, 1, 2, 3, u'one', u'two']", repr(list1))
  120. self.assertTrue(list1 < list3)
  121. self.assertTrue(list1 <= list3)
  122. self.assertFalse(list1 == list3)
  123. self.assertTrue(list1 != list3)
  124. self.assertFalse(list1 > list3)
  125. self.assertFalse(list1 >= list3)
  126. other1 = [0, 2, 3, 4]
  127. self.assertTrue(list1 < other1)
  128. self.assertTrue(list1 <= other1)
  129. self.assertFalse(list1 == other1)
  130. self.assertTrue(list1 != other1)
  131. self.assertFalse(list1 > other1)
  132. self.assertFalse(list1 >= other1)
  133. other2 = [0, 0, 1, 2]
  134. self.assertFalse(list1 < other2)
  135. self.assertFalse(list1 <= other2)
  136. self.assertFalse(list1 == other2)
  137. self.assertTrue(list1 != other2)
  138. self.assertTrue(list1 > other2)
  139. self.assertTrue(list1 >= other2)
  140. other3 = [0, 1, 2, 3, "one", "two"]
  141. self.assertFalse(list1 < other3)
  142. self.assertTrue(list1 <= other3)
  143. self.assertTrue(list1 == other3)
  144. self.assertFalse(list1 != other3)
  145. self.assertFalse(list1 > other3)
  146. self.assertTrue(list1 >= other3)
  147. self.assertTrue(bool(list1))
  148. self.assertFalse(bool(list2))
  149. self.assertEqual(6, len(list1))
  150. self.assertEqual(0, len(list2))
  151. out = []
  152. for obj in list1:
  153. out.append(obj)
  154. self.assertEqual([0, 1, 2, 3, "one", "two"], out)
  155. out = []
  156. for ch in list2:
  157. out.append(ch)
  158. self.assertEqual([], out)
  159. gen1 = iter(list1)
  160. out = []
  161. for i in range(len(list1)):
  162. out.append(next(gen1))
  163. self.assertRaises(StopIteration, next, gen1)
  164. self.assertEqual([0, 1, 2, 3, "one", "two"], out)
  165. gen2 = iter(list2)
  166. self.assertRaises(StopIteration, next, gen2)
  167. self.assertEqual(["two", "one", 3, 2, 1, 0], list(reversed(list1)))
  168. self.assertEqual([], list(reversed(list2)))
  169. self.assertTrue("one" in list1)
  170. self.assertTrue(3 in list1)
  171. self.assertFalse(10 in list1)
  172. self.assertFalse(0 in list2)
  173. self.assertEqual([], list2 * 5)
  174. self.assertEqual([], 5 * list2)
  175. self.assertEqual([0, 1, 2, 0, 1, 2, 0, 1, 2], list4 * 3)
  176. self.assertEqual([0, 1, 2, 0, 1, 2, 0, 1, 2], 3 * list4)
  177. list4 *= 2
  178. self.assertEqual([0, 1, 2, 0, 1, 2], list4)
  179. def _test_list_methods(self, builder):
  180. """Run tests on the public methods of a list built with *builder*."""
  181. list1 = builder(range(5))
  182. list2 = builder(["foo"])
  183. list3 = builder([("a", 5), ("d", 2), ("b", 8), ("c", 3)])
  184. list1.append(5)
  185. list1.append(1)
  186. list1.append(2)
  187. self.assertEqual([0, 1, 2, 3, 4, 5, 1, 2], list1)
  188. self.assertEqual(0, list1.count(6))
  189. self.assertEqual(2, list1.count(1))
  190. list1.extend(range(5, 8))
  191. self.assertEqual([0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
  192. self.assertEqual(1, list1.index(1))
  193. self.assertEqual(6, list1.index(1, 3))
  194. self.assertEqual(6, list1.index(1, 3, 7))
  195. self.assertRaises(ValueError, list1.index, 1, 3, 5)
  196. list1.insert(0, -1)
  197. self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
  198. list1.insert(-1, 6.5)
  199. self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7], list1)
  200. list1.insert(13, 8)
  201. self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7, 8], list1)
  202. self.assertEqual(8, list1.pop())
  203. self.assertEqual(7, list1.pop())
  204. self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5], list1)
  205. self.assertEqual(-1, list1.pop(0))
  206. self.assertEqual(5, list1.pop(5))
  207. self.assertEqual(6.5, list1.pop(-1))
  208. self.assertEqual([0, 1, 2, 3, 4, 1, 2, 5, 6], list1)
  209. self.assertEqual("foo", list2.pop())
  210. self.assertRaises(IndexError, list2.pop)
  211. self.assertEqual([], list2)
  212. list1.remove(6)
  213. self.assertEqual([0, 1, 2, 3, 4, 1, 2, 5], list1)
  214. list1.remove(1)
  215. self.assertEqual([0, 2, 3, 4, 1, 2, 5], list1)
  216. list1.remove(1)
  217. self.assertEqual([0, 2, 3, 4, 2, 5], list1)
  218. self.assertRaises(ValueError, list1.remove, 1)
  219. list1.reverse()
  220. self.assertEqual([5, 2, 4, 3, 2, 0], list1)
  221. list1.sort()
  222. self.assertEqual([0, 2, 2, 3, 4, 5], list1)
  223. list1.sort(reverse=True)
  224. self.assertEqual([5, 4, 3, 2, 2, 0], list1)
  225. if not py3k:
  226. func = lambda x, y: abs(3 - x) - abs(3 - y) # Distance from 3
  227. list1.sort(cmp=func)
  228. self.assertEqual([3, 4, 2, 2, 5, 0], list1)
  229. list1.sort(cmp=func, reverse=True)
  230. self.assertEqual([0, 5, 4, 2, 2, 3], list1)
  231. list3.sort(key=lambda i: i[1])
  232. self.assertEqual([("d", 2), ("c", 3), ("a", 5), ("b", 8)], list3)
  233. list3.sort(key=lambda i: i[1], reverse=True)
  234. self.assertEqual([("b", 8), ("a", 5), ("c", 3), ("d", 2)], list3)
  235. def test_docs(self):
  236. """make sure the methods of SmartList/_ListProxy have docstrings"""
  237. methods = ["append", "count", "extend", "index", "insert", "pop",
  238. "remove", "reverse", "sort"]
  239. for meth in methods:
  240. expected = getattr(list, meth).__doc__
  241. smartlist_doc = getattr(SmartList, meth).__doc__
  242. listproxy_doc = getattr(_ListProxy, meth).__doc__
  243. self.assertEqual(expected, smartlist_doc)
  244. self.assertEqual(expected, listproxy_doc)
  245. def test_doctest(self):
  246. """make sure the test embedded in SmartList's docstring passes"""
  247. parent = SmartList([0, 1, 2, 3])
  248. self.assertEqual([0, 1, 2, 3], parent)
  249. child = parent[2:]
  250. self.assertEqual([2, 3], child)
  251. child.append(4)
  252. self.assertEqual([2, 3, 4], child)
  253. self.assertEqual([0, 1, 2, 3, 4], parent)
  254. def test_parent_get_set_del(self):
  255. """make sure SmartList's getitem/setitem/delitem work"""
  256. self._test_get_set_del_item(SmartList)
  257. def test_parent_add(self):
  258. """make sure SmartList's add/radd/iadd work"""
  259. self._test_add_radd_iadd(SmartList)
  260. def test_parent_unaffected_magics(self):
  261. """sanity checks against SmartList features that were not modified"""
  262. self._test_other_magic_methods(SmartList)
  263. def test_parent_methods(self):
  264. """make sure SmartList's non-magic methods work, like append()"""
  265. self._test_list_methods(SmartList)
  266. def test_child_get_set_del(self):
  267. """make sure _ListProxy's getitem/setitem/delitem work"""
  268. self._test_get_set_del_item(lambda L: SmartList(list(L))[:])
  269. self._test_get_set_del_item(lambda L: SmartList([999] + list(L))[1:])
  270. self._test_get_set_del_item(lambda L: SmartList(list(L) + [999])[:-1])
  271. builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
  272. self._test_get_set_del_item(builder)
  273. def test_child_add(self):
  274. """make sure _ListProxy's add/radd/iadd work"""
  275. self._test_add_radd_iadd(lambda L: SmartList(list(L))[:])
  276. self._test_add_radd_iadd(lambda L: SmartList([999] + list(L))[1:])
  277. self._test_add_radd_iadd(lambda L: SmartList(list(L) + [999])[:-1])
  278. builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
  279. self._test_add_radd_iadd(builder)
  280. def test_child_other_magics(self):
  281. """make sure _ListProxy's other magically implemented features work"""
  282. self._test_other_magic_methods(lambda L: SmartList(list(L))[:])
  283. self._test_other_magic_methods(lambda L: SmartList([999] + list(L))[1:])
  284. self._test_other_magic_methods(lambda L: SmartList(list(L) + [999])[:-1])
  285. builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
  286. self._test_other_magic_methods(builder)
  287. def test_child_methods(self):
  288. """make sure _ListProxy's non-magic methods work, like append()"""
  289. self._test_list_methods(lambda L: SmartList(list(L))[:])
  290. self._test_list_methods(lambda L: SmartList([999] + list(L))[1:])
  291. self._test_list_methods(lambda L: SmartList(list(L) + [999])[:-1])
  292. builder = lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2]
  293. self._test_list_methods(builder)
  294. def test_influence(self):
  295. """make sure changes are propagated from parents to children"""
  296. parent = SmartList([0, 1, 2, 3, 4, 5])
  297. child1 = parent[2:]
  298. child2 = parent[2:5]
  299. parent.append(6)
  300. child1.append(7)
  301. child2.append(4.5)
  302. self.assertEqual([0, 1, 2, 3, 4, 4.5, 5, 6, 7], parent)
  303. self.assertEqual([2, 3, 4, 4.5, 5, 6, 7], child1)
  304. self.assertEqual([2, 3, 4, 4.5], child2)
  305. parent.insert(0, -1)
  306. parent.insert(4, 2.5)
  307. parent.insert(10, 6.5)
  308. self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], parent)
  309. self.assertEqual([2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], child1)
  310. self.assertEqual([2, 2.5, 3, 4, 4.5], child2)
  311. self.assertEqual(7, parent.pop())
  312. self.assertEqual(6.5, child1.pop())
  313. self.assertEqual(4.5, child2.pop())
  314. self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 5, 6], parent)
  315. self.assertEqual([2, 2.5, 3, 4, 5, 6], child1)
  316. self.assertEqual([2, 2.5, 3, 4], child2)
  317. parent.remove(-1)
  318. child1.remove(2.5)
  319. self.assertEqual([0, 1, 2, 3, 4, 5, 6], parent)
  320. self.assertEqual([2, 3, 4, 5, 6], child1)
  321. self.assertEqual([2, 3, 4], child2)
  322. self.assertEqual(0, parent.pop(0))
  323. self.assertEqual([1, 2, 3, 4, 5, 6], parent)
  324. self.assertEqual([2, 3, 4, 5, 6], child1)
  325. self.assertEqual([2, 3, 4], child2)
  326. child2.reverse()
  327. self.assertEqual([1, 4, 3, 2, 5, 6], parent)
  328. self.assertEqual([4, 3, 2, 5, 6], child1)
  329. self.assertEqual([4, 3, 2], child2)
  330. parent.extend([7, 8])
  331. child1.extend([8.1, 8.2])
  332. child2.extend([1.9, 1.8])
  333. self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], parent)
  334. self.assertEqual([4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], child1)
  335. self.assertEqual([4, 3, 2, 1.9, 1.8], child2)
  336. if __name__ == "__main__":
  337. unittest.main(verbosity=2)