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.
 
 
 
 

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