A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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