A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

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