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.
 
 
 
 

391 line
16 KiB

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