A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

410 řádky
16 KiB

  1. #
  2. # Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. import unittest
  22. from mwparserfromhell.smart_list import SmartList
  23. from mwparserfromhell.smart_list.ListProxy import _ListProxy
  24. class TestSmartList(unittest.TestCase):
  25. """Test cases for the SmartList class and its child, _ListProxy."""
  26. def _test_get_set_del_item(self, builder):
  27. """Run tests on __get/set/delitem__ of a list built with *builder*."""
  28. def assign(L, s1, s2, s3, val):
  29. L[s1:s2:s3] = val
  30. def delete(L, s1):
  31. del L[s1]
  32. list1 = builder([0, 1, 2, 3, "one", "two"])
  33. list2 = builder(list(range(10)))
  34. self.assertEqual(1, list1[1])
  35. self.assertEqual("one", list1[-2])
  36. self.assertEqual([2, 3], list1[2:4])
  37. self.assertRaises(IndexError, lambda: list1[6])
  38. self.assertRaises(IndexError, lambda: list1[-7])
  39. self.assertEqual([0, 1, 2], list1[:3])
  40. self.assertEqual([0, 1, 2, 3, "one", "two"], list1[:])
  41. self.assertEqual([3, "one", "two"], list1[3:])
  42. self.assertEqual([3, "one", "two"], list1[3:100])
  43. self.assertEqual(["one", "two"], list1[-2:])
  44. self.assertEqual([0, 1], list1[:-4])
  45. self.assertEqual([], list1[6:])
  46. self.assertEqual([], list1[4:2])
  47. self.assertEqual([0, 2, "one"], list1[0:5:2])
  48. self.assertEqual([0, 2], list1[0:-3:2])
  49. self.assertEqual([0, 1, 2, 3, "one", "two"], list1[::])
  50. self.assertEqual([2, 3, "one", "two"], list1[2::])
  51. self.assertEqual([0, 1, 2, 3], list1[:4:])
  52. self.assertEqual([2, 3], list1[2:4:])
  53. self.assertEqual([0, 2, 4, 6, 8], list2[::2])
  54. self.assertEqual([2, 5, 8], list2[2::3])
  55. self.assertEqual([0, 3], list2[:6:3])
  56. self.assertEqual([2, 5, 8], list2[-8:9:3])
  57. self.assertEqual([], list2[100000:1000:-100])
  58. list1[3] = 100
  59. self.assertEqual(100, list1[3])
  60. list1[-3] = 101
  61. self.assertEqual([0, 1, 2, 101, "one", "two"], list1)
  62. list1[5:] = [6, 7, 8]
  63. self.assertEqual([6, 7, 8], list1[5:])
  64. self.assertEqual([0, 1, 2, 101, "one", 6, 7, 8], list1)
  65. list1[2:4] = [-1, -2, -3, -4, -5]
  66. self.assertEqual([0, 1, -1, -2, -3, -4, -5, "one", 6, 7, 8], list1)
  67. list1[0:-3] = [99]
  68. self.assertEqual([99, 6, 7, 8], list1)
  69. list2[0:6:2] = [100, 102, 104]
  70. self.assertEqual([100, 1, 102, 3, 104, 5, 6, 7, 8, 9], list2)
  71. list2[::3] = [200, 203, 206, 209]
  72. self.assertEqual([200, 1, 102, 203, 104, 5, 206, 7, 8, 209], list2)
  73. list2[::] = range(7)
  74. self.assertEqual([0, 1, 2, 3, 4, 5, 6], list2)
  75. self.assertRaises(ValueError, assign, list2, 0, 5, 2,
  76. [100, 102, 104, 106])
  77. with self.assertRaises(IndexError):
  78. list2[7] = "foo"
  79. with self.assertRaises(IndexError):
  80. list2[-8] = "foo"
  81. del list2[2]
  82. self.assertEqual([0, 1, 3, 4, 5, 6], list2)
  83. del list2[-3]
  84. self.assertEqual([0, 1, 3, 5, 6], list2)
  85. self.assertRaises(IndexError, delete, list2, 100)
  86. self.assertRaises(IndexError, delete, list2, -6)
  87. list2[:] = range(10)
  88. del list2[3:6]
  89. self.assertEqual([0, 1, 2, 6, 7, 8, 9], list2)
  90. del list2[-2:]
  91. self.assertEqual([0, 1, 2, 6, 7], list2)
  92. del list2[:2]
  93. self.assertEqual([2, 6, 7], list2)
  94. list2[:] = range(10)
  95. del list2[2:8:2]
  96. self.assertEqual([0, 1, 3, 5, 7, 8, 9], list2)
  97. def _test_add_radd_iadd(self, builder):
  98. """Run tests on __r/i/add__ of a list built with *builder*."""
  99. list1 = builder(range(5))
  100. list2 = builder(range(5, 10))
  101. self.assertEqual([0, 1, 2, 3, 4, 5, 6], list1 + [5, 6])
  102. self.assertEqual([0, 1, 2, 3, 4], list1)
  103. self.assertEqual(list(range(10)), list1 + list2)
  104. self.assertEqual([-2, -1, 0, 1, 2, 3, 4], [-2, -1] + list1)
  105. self.assertEqual([0, 1, 2, 3, 4], list1)
  106. list1 += ["foo", "bar", "baz"]
  107. self.assertEqual([0, 1, 2, 3, 4, "foo", "bar", "baz"], list1)
  108. def _test_other_magic_methods(self, builder):
  109. """Run tests on other magic methods of a list built with *builder*."""
  110. list1 = builder([0, 1, 2, 3, "one", "two"])
  111. list2 = builder([])
  112. list3 = builder([0, 2, 3, 4])
  113. list4 = builder([0, 1, 2])
  114. self.assertEqual("[0, 1, 2, 3, 'one', 'two']", str(list1))
  115. self.assertEqual(b"\x00\x01\x02", bytes(list4))
  116. self.assertEqual("[0, 1, 2, 3, 'one', 'two']", repr(list1))
  117. self.assertLess(list1, list3)
  118. self.assertLessEqual(list1, list3)
  119. self.assertNotEqual(list1, list3)
  120. self.assertNotEqual(list1, list3)
  121. self.assertLessEqual(list1, list3)
  122. self.assertLess(list1, list3)
  123. other1 = [0, 2, 3, 4]
  124. self.assertLess(list1, other1)
  125. self.assertLessEqual(list1, other1)
  126. self.assertNotEqual(list1, other1)
  127. self.assertNotEqual(list1, other1)
  128. self.assertLessEqual(list1, other1)
  129. self.assertLess(list1, other1)
  130. other2 = [0, 0, 1, 2]
  131. self.assertGreaterEqual(list1, other2)
  132. self.assertGreater(list1, other2)
  133. self.assertNotEqual(list1, other2)
  134. self.assertNotEqual(list1, other2)
  135. self.assertGreater(list1, other2)
  136. self.assertGreaterEqual(list1, other2)
  137. other3 = [0, 1, 2, 3, "one", "two"]
  138. self.assertGreaterEqual(list1, other3)
  139. self.assertLessEqual(list1, other3)
  140. self.assertEqual(list1, other3)
  141. self.assertEqual(list1, other3)
  142. self.assertLessEqual(list1, other3)
  143. self.assertGreaterEqual(list1, other3)
  144. self.assertTrue(bool(list1))
  145. self.assertFalse(bool(list2))
  146. self.assertEqual(6, len(list1))
  147. self.assertEqual(0, len(list2))
  148. out = []
  149. for obj in list1:
  150. out.append(obj)
  151. self.assertEqual([0, 1, 2, 3, "one", "two"], out)
  152. out = []
  153. for ch in list2:
  154. out.append(ch)
  155. self.assertEqual([], out)
  156. gen1 = iter(list1)
  157. out = []
  158. for i in range(len(list1)):
  159. out.append(next(gen1))
  160. self.assertRaises(StopIteration, next, gen1)
  161. self.assertEqual([0, 1, 2, 3, "one", "two"], out)
  162. gen2 = iter(list2)
  163. self.assertRaises(StopIteration, next, gen2)
  164. self.assertEqual(["two", "one", 3, 2, 1, 0], list(reversed(list1)))
  165. self.assertEqual([], list(reversed(list2)))
  166. self.assertIn("one", list1)
  167. self.assertIn(3, list1)
  168. self.assertNotIn(10, list1)
  169. self.assertNotIn(0, list2)
  170. self.assertEqual([], list2 * 5)
  171. self.assertEqual([], 5 * list2)
  172. self.assertEqual([0, 1, 2, 0, 1, 2, 0, 1, 2], list4 * 3)
  173. self.assertEqual([0, 1, 2, 0, 1, 2, 0, 1, 2], 3 * list4)
  174. list4 *= 2
  175. self.assertEqual([0, 1, 2, 0, 1, 2], list4)
  176. def _test_list_methods(self, builder):
  177. """Run tests on the public methods of a list built with *builder*."""
  178. list1 = builder(range(5))
  179. list2 = builder(["foo"])
  180. list3 = builder([("a", 5), ("d", 2), ("b", 8), ("c", 3)])
  181. list1.append(5)
  182. list1.append(1)
  183. list1.append(2)
  184. self.assertEqual([0, 1, 2, 3, 4, 5, 1, 2], list1)
  185. self.assertEqual(0, list1.count(6))
  186. self.assertEqual(2, list1.count(1))
  187. list1.extend(range(5, 8))
  188. self.assertEqual([0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
  189. self.assertEqual(1, list1.index(1))
  190. self.assertEqual(6, list1.index(1, 3))
  191. self.assertEqual(6, list1.index(1, 3, 7))
  192. self.assertRaises(ValueError, list1.index, 1, 3, 5)
  193. list1.insert(0, -1)
  194. self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 7], list1)
  195. list1.insert(-1, 6.5)
  196. self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7], list1)
  197. list1.insert(13, 8)
  198. self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5, 7, 8], list1)
  199. self.assertEqual(8, list1.pop())
  200. self.assertEqual(7, list1.pop())
  201. self.assertEqual([-1, 0, 1, 2, 3, 4, 5, 1, 2, 5, 6, 6.5], list1)
  202. self.assertEqual(-1, list1.pop(0))
  203. self.assertEqual(5, list1.pop(5))
  204. self.assertEqual(6.5, list1.pop(-1))
  205. self.assertEqual([0, 1, 2, 3, 4, 1, 2, 5, 6], list1)
  206. self.assertEqual("foo", list2.pop())
  207. self.assertRaises(IndexError, list2.pop)
  208. self.assertEqual([], list2)
  209. list1.remove(6)
  210. self.assertEqual([0, 1, 2, 3, 4, 1, 2, 5], list1)
  211. list1.remove(1)
  212. self.assertEqual([0, 2, 3, 4, 1, 2, 5], list1)
  213. list1.remove(1)
  214. self.assertEqual([0, 2, 3, 4, 2, 5], list1)
  215. self.assertRaises(ValueError, list1.remove, 1)
  216. list1.reverse()
  217. self.assertEqual([5, 2, 4, 3, 2, 0], list1)
  218. list1.sort()
  219. self.assertEqual([0, 2, 2, 3, 4, 5], list1)
  220. list1.sort(reverse=True)
  221. self.assertEqual([5, 4, 3, 2, 2, 0], list1)
  222. list3.sort(key=lambda i: i[1])
  223. self.assertEqual([("d", 2), ("c", 3), ("a", 5), ("b", 8)], list3)
  224. list3.sort(key=lambda i: i[1], reverse=True)
  225. self.assertEqual([("b", 8), ("a", 5), ("c", 3), ("d", 2)], list3)
  226. def _dispatch_test_for_children(self, meth):
  227. """Run a test method on various different types of children."""
  228. meth(lambda L: SmartList(list(L))[:])
  229. meth(lambda L: SmartList([999] + list(L))[1:])
  230. meth(lambda L: SmartList(list(L) + [999])[:-1])
  231. meth(lambda L: SmartList([101, 102] + list(L) + [201, 202])[2:-2])
  232. def test_docs(self):
  233. """make sure the methods of SmartList/_ListProxy have docstrings"""
  234. methods = ["append", "count", "extend", "index", "insert", "pop",
  235. "remove", "reverse", "sort"]
  236. for meth in methods:
  237. expected = getattr(list, meth).__doc__
  238. smartlist_doc = getattr(SmartList, meth).__doc__
  239. listproxy_doc = getattr(_ListProxy, meth).__doc__
  240. self.assertEqual(expected, smartlist_doc)
  241. self.assertEqual(expected, listproxy_doc)
  242. def test_doctest(self):
  243. """make sure the test embedded in SmartList's docstring passes"""
  244. parent = SmartList([0, 1, 2, 3])
  245. self.assertEqual([0, 1, 2, 3], parent)
  246. child = parent[2:]
  247. self.assertEqual([2, 3], child)
  248. child.append(4)
  249. self.assertEqual([2, 3, 4], child)
  250. self.assertEqual([0, 1, 2, 3, 4], parent)
  251. def test_parent_get_set_del(self):
  252. """make sure SmartList's getitem/setitem/delitem work"""
  253. self._test_get_set_del_item(SmartList)
  254. def test_parent_add(self):
  255. """make sure SmartList's add/radd/iadd work"""
  256. self._test_add_radd_iadd(SmartList)
  257. def test_parent_other_magics(self):
  258. """make sure SmartList's other magically implemented features work"""
  259. self._test_other_magic_methods(SmartList)
  260. def test_parent_methods(self):
  261. """make sure SmartList's non-magic methods work, like append()"""
  262. self._test_list_methods(SmartList)
  263. def test_child_get_set_del(self):
  264. """make sure _ListProxy's getitem/setitem/delitem work"""
  265. self._dispatch_test_for_children(self._test_get_set_del_item)
  266. def test_child_add(self):
  267. """make sure _ListProxy's add/radd/iadd work"""
  268. self._dispatch_test_for_children(self._test_add_radd_iadd)
  269. def test_child_other_magics(self):
  270. """make sure _ListProxy's other magically implemented features work"""
  271. self._dispatch_test_for_children(self._test_other_magic_methods)
  272. def test_child_methods(self):
  273. """make sure _ListProxy's non-magic methods work, like append()"""
  274. self._dispatch_test_for_children(self._test_list_methods)
  275. def test_influence(self):
  276. """make sure changes are propagated from parents to children"""
  277. parent = SmartList([0, 1, 2, 3, 4, 5])
  278. child1 = parent[2:]
  279. child2 = parent[2:5]
  280. self.assertEqual([0, 1, 2, 3, 4, 5], parent)
  281. self.assertEqual([2, 3, 4, 5], child1)
  282. self.assertEqual([2, 3, 4], child2)
  283. self.assertEqual(2, len(parent._children))
  284. parent.append(6)
  285. child1.append(7)
  286. child2.append(4.5)
  287. self.assertEqual([0, 1, 2, 3, 4, 4.5, 5, 6, 7], parent)
  288. self.assertEqual([2, 3, 4, 4.5, 5, 6, 7], child1)
  289. self.assertEqual([2, 3, 4, 4.5], child2)
  290. parent.insert(0, -1)
  291. parent.insert(4, 2.5)
  292. parent.insert(10, 6.5)
  293. self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], parent)
  294. self.assertEqual([2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7], child1)
  295. self.assertEqual([2, 2.5, 3, 4, 4.5], child2)
  296. self.assertEqual(7, parent.pop())
  297. self.assertEqual(6.5, child1.pop())
  298. self.assertEqual(4.5, child2.pop())
  299. self.assertEqual([-1, 0, 1, 2, 2.5, 3, 4, 5, 6], parent)
  300. self.assertEqual([2, 2.5, 3, 4, 5, 6], child1)
  301. self.assertEqual([2, 2.5, 3, 4], child2)
  302. parent.remove(-1)
  303. child1.remove(2.5)
  304. self.assertEqual([0, 1, 2, 3, 4, 5, 6], parent)
  305. self.assertEqual([2, 3, 4, 5, 6], child1)
  306. self.assertEqual([2, 3, 4], child2)
  307. self.assertEqual(0, parent.pop(0))
  308. self.assertEqual([1, 2, 3, 4, 5, 6], parent)
  309. self.assertEqual([2, 3, 4, 5, 6], child1)
  310. self.assertEqual([2, 3, 4], child2)
  311. child2.reverse()
  312. self.assertEqual([1, 4, 3, 2, 5, 6], parent)
  313. self.assertEqual([4, 3, 2, 5, 6], child1)
  314. self.assertEqual([4, 3, 2], child2)
  315. parent.extend([7, 8])
  316. child1.extend([8.1, 8.2])
  317. child2.extend([1.9, 1.8])
  318. self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], parent)
  319. self.assertEqual([4, 3, 2, 1.9, 1.8, 5, 6, 7, 8, 8.1, 8.2], child1)
  320. self.assertEqual([4, 3, 2, 1.9, 1.8], child2)
  321. child3 = parent[9:]
  322. self.assertEqual([8, 8.1, 8.2], child3)
  323. del parent[8:]
  324. self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6], parent)
  325. self.assertEqual([4, 3, 2, 1.9, 1.8, 5, 6], child1)
  326. self.assertEqual([4, 3, 2, 1.9, 1.8], child2)
  327. self.assertEqual([], child3)
  328. self.assertEqual(0, len(child3))
  329. del child1
  330. self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6], parent)
  331. self.assertEqual([4, 3, 2, 1.9, 1.8], child2)
  332. self.assertEqual([], child3)
  333. self.assertEqual(2, len(parent._children))
  334. del child3
  335. self.assertEqual([1, 4, 3, 2, 1.9, 1.8, 5, 6], parent)
  336. self.assertEqual([4, 3, 2, 1.9, 1.8], child2)
  337. self.assertEqual(1, len(parent._children))
  338. parent.remove(1.9)
  339. parent.remove(1.8)
  340. self.assertEqual([1, 4, 3, 2, 5, 6], parent)
  341. self.assertEqual([4, 3, 2], child2)
  342. parent.reverse()
  343. self.assertEqual([6, 5, 2, 3, 4, 1], parent)
  344. self.assertEqual([4, 3, 2], child2)
  345. self.assertEqual(0, len(parent._children))
  346. if __name__ == "__main__":
  347. unittest.main(verbosity=2)