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.

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