list_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import pickle
  2. import pytest
  3. from pyrsistent import plist, l
  4. def test_literalish_works():
  5. assert l(1, 2, 3) == plist([1, 2, 3])
  6. def test_first_and_rest():
  7. pl = plist([1, 2])
  8. assert pl.first == 1
  9. assert pl.rest.first == 2
  10. assert pl.rest.rest is plist()
  11. def test_instantiate_large_list():
  12. assert plist(range(1000)).first == 0
  13. def test_iteration():
  14. assert list(plist()) == []
  15. assert list(plist([1, 2, 3])) == [1, 2, 3]
  16. def test_cons():
  17. assert plist([1, 2, 3]).cons(0) == plist([0, 1, 2, 3])
  18. def test_cons_empty_list():
  19. assert plist().cons(0) == plist([0])
  20. def test_truthiness():
  21. assert plist([1])
  22. assert not plist()
  23. def test_len():
  24. assert len(plist([1, 2, 3])) == 3
  25. assert len(plist()) == 0
  26. def test_first_illegal_on_empty_list():
  27. with pytest.raises(AttributeError):
  28. plist().first
  29. def test_rest_return_self_on_empty_list():
  30. assert plist().rest is plist()
  31. def test_reverse():
  32. assert plist([1, 2, 3]).reverse() == plist([3, 2, 1])
  33. assert reversed(plist([1, 2, 3])) == plist([3, 2, 1])
  34. assert plist().reverse() == plist()
  35. assert reversed(plist()) == plist()
  36. def test_inequality():
  37. assert plist([1, 2]) != plist([1, 3])
  38. assert plist([1, 2]) != plist([1, 2, 3])
  39. assert plist() != plist([1, 2, 3])
  40. def test_repr():
  41. assert str(plist()) == "plist([])"
  42. assert str(plist([1, 2, 3])) == "plist([1, 2, 3])"
  43. def test_indexing():
  44. assert plist([1, 2, 3])[2] == 3
  45. assert plist([1, 2, 3])[-1] == 3
  46. def test_indexing_on_empty_list():
  47. with pytest.raises(IndexError):
  48. plist()[0]
  49. def test_index_out_of_range():
  50. with pytest.raises(IndexError):
  51. plist([1, 2])[2]
  52. with pytest.raises(IndexError):
  53. plist([1, 2])[-3]
  54. def test_index_invalid_type():
  55. with pytest.raises(TypeError) as e:
  56. plist([1, 2, 3])['foo'] # type: ignore
  57. assert 'cannot be interpreted' in str(e.value)
  58. def test_slicing_take():
  59. assert plist([1, 2, 3])[:2] == plist([1, 2])
  60. def test_slicing_take_out_of_range():
  61. assert plist([1, 2, 3])[:20] == plist([1, 2, 3])
  62. def test_slicing_drop():
  63. li = plist([1, 2, 3])
  64. assert li[1:] is li.rest
  65. def test_slicing_drop_out_of_range():
  66. assert plist([1, 2, 3])[3:] is plist()
  67. def test_contains():
  68. assert 2 in plist([1, 2, 3])
  69. assert 4 not in plist([1, 2, 3])
  70. assert 1 not in plist()
  71. def test_count():
  72. assert plist([1, 2, 1]).count(1) == 2
  73. assert plist().count(1) == 0
  74. def test_index():
  75. assert plist([1, 2, 3]).index(3) == 2
  76. def test_index_item_not_found():
  77. with pytest.raises(ValueError):
  78. plist().index(3)
  79. with pytest.raises(ValueError):
  80. plist([1, 2]).index(3)
  81. def test_pickling_empty_list():
  82. assert pickle.loads(pickle.dumps(plist(), -1)) == plist()
  83. def test_pickling_non_empty_list():
  84. assert pickle.loads(pickle.dumps(plist([1, 2, 3]), -1)) == plist([1, 2, 3])
  85. def test_comparison():
  86. assert plist([1, 2]) < plist([1, 2, 3])
  87. assert plist([2, 1]) > plist([1, 2, 3])
  88. assert plist() < plist([1])
  89. assert plist([1]) > plist()
  90. def test_comparison_with_other_type():
  91. assert plist() != []
  92. def test_hashing():
  93. assert hash(plist([1, 2])) == hash(plist([1, 2]))
  94. assert hash(plist([1, 2])) != hash(plist([2, 1]))
  95. def test_split():
  96. left_list, right_list = plist([1, 2, 3, 4, 5]).split(3)
  97. assert left_list == plist([1, 2, 3])
  98. assert right_list == plist([4, 5])
  99. def test_split_no_split_occurred():
  100. x = plist([1, 2])
  101. left_list, right_list = x.split(2)
  102. assert left_list is x
  103. assert right_list is plist()
  104. def test_split_empty_list():
  105. left_list, right_list = plist().split(2)
  106. assert left_list == plist()
  107. assert right_list == plist()
  108. def test_remove():
  109. assert plist([1, 2, 3, 2]).remove(2) == plist([1, 3, 2])
  110. assert plist([1, 2, 3]).remove(1) == plist([2, 3])
  111. assert plist([1, 2, 3]).remove(3) == plist([1, 2])
  112. def test_remove_missing_element():
  113. with pytest.raises(ValueError):
  114. plist([1, 2]).remove(3)
  115. with pytest.raises(ValueError):
  116. plist().remove(2)
  117. def test_mcons():
  118. assert plist([1, 2]).mcons([3, 4]) == plist([4, 3, 1, 2])
  119. def test_supports_weakref():
  120. import weakref
  121. weakref.ref(plist())
  122. weakref.ref(plist([1, 2]))
  123. def test_iterable():
  124. """
  125. PLists can be created from iterables even though they can't be len()
  126. hinted.
  127. """
  128. assert plist(iter("a")) == plist(iter("a"))