This repository was archived by the owner on Mar 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenumerable_list_test.py
More file actions
288 lines (164 loc) · 7.44 KB
/
enumerable_list_test.py
File metadata and controls
288 lines (164 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import pytest
import re
from operator import add
from pyby import EnumerableList
from .test_helpers import assert_enumerable_list, assert_enumerator, pass_through
@pytest.fixture
def empty_list():
return EnumerableList()
@pytest.fixture
def letters():
return EnumerableList(["a", "b", "c"])
@pytest.fixture
def list_with_a_tuple():
return EnumerableList(["a", ("b", None), "c"])
@pytest.fixture
def numbers():
return EnumerableList([1, 2, 3])
@pytest.fixture
def numbers_with_duplicates():
return EnumerableList([1, 2, 3, 3, 2])
def test_repr(letters):
assert repr(letters) == "EnumerableList(['a', 'b', 'c'])"
def test_any(empty_list, numbers):
assert numbers.any()
assert not empty_list.any()
assert not EnumerableList([False, None]).any()
def test_any_with_an_object(numbers):
assert numbers.any(3)
assert not numbers.any(4)
def test_any_with_a_predicate(empty_list, numbers):
assert EnumerableList([0]).any(is_zero)
assert not empty_list.any(is_zero)
assert not numbers.any(is_zero)
def test_any_with_a_regex_pattern(numbers):
string_pattern = re.compile(r"\d")
assert not numbers.any(string_pattern)
numbers.append("the number 69")
assert numbers.any(string_pattern)
bytes_pattern = re.compile(r"\d".encode())
assert not numbers.any(bytes_pattern)
numbers.append(b"binary 420")
assert numbers.any(bytes_pattern)
def test_any_with_a_class(numbers):
assert numbers.any(int)
assert not numbers.any(str)
def test_all(empty_list, numbers):
assert numbers.all()
assert empty_list.all()
assert not EnumerableList([False, None]).all()
def test_all_with_an_object(numbers):
assert not numbers.all(3)
assert EnumerableList([4, 4, 4]).all(4)
def test_all_with_a_predicate(empty_list, numbers):
assert empty_list.all(is_zero)
assert EnumerableList([0]).all(is_zero)
assert not numbers.all(larger_than_one)
# def test_all_with_a_regex_pattern(empty_list, numbers):
# string_pattern = re.compile(r"\d")
# assert not numbers.all(string_pattern)
# assert numbers.all(string_pattern)
# bytes_pattern = re.compile(r"\d".encode())
# assert not numbers.any(bytes_pattern)
# numbers.append(b"420")
# assert numbers.any(bytes_pattern)
def test_all_with_a_class(numbers, list_with_a_tuple):
assert numbers.all(int)
assert not numbers.all(str)
assert not list_with_a_tuple.all(int)
def test_collect_with_a_function_maps_over_the_items_and_returns_an_enumerable_list(numbers):
assert_enumerable_list(numbers.collect(increment), [2, 3, 4])
def test_collect_with_a_sequence_containing_a_tuple(list_with_a_tuple):
assert_enumerable_list(list_with_a_tuple.collect(not_none), [True, True, True])
def test_collect_without_a_function_returns_an_enumerator(letters):
assert_enumerator(letters.collect(), ["a", "b", "c"])
def test_compact():
enumerable_list = EnumerableList([None, "a", None, "b", "c", None])
assert_enumerable_list(enumerable_list.compact(), ["a", "b", "c"])
def test_compact_when_empty(empty_list):
assert_enumerable_list(empty_list.compact(), [])
def test_compact_when_not_containing_any_None_values(letters):
assert_enumerable_list(letters.compact(), ["a", "b", "c"])
def test_compact_with_a_sequence_containing_a_tuple(list_with_a_tuple):
assert_enumerable_list(list_with_a_tuple.compact(), ["a", ("b", None), "c"])
def test_count(numbers):
assert numbers.count() == 3
def test_count_with_a_non_callable_argument(numbers):
assert numbers.count(2) == 1
def test_count_with_a_callable_argument(numbers):
assert numbers.count(larger_than_one) == 2
def test_each_with_a_function_calls_it_once_for_each_item(letters, seen):
letters.each(seen)
assert seen == ["a", "b", "c"]
def test_each_without_a_function_returns_an_enumerator(letters):
assert_enumerator(letters.each(), ["a", "b", "c"])
def test_find(numbers):
assert numbers.find(larger_than_one) == 2
def test_find_when_not_found(numbers):
assert numbers.find(is_zero) is None
def test_find_when_not_found_with_default(numbers):
assert numbers.find(lambda: 69, is_zero) == 69
def test_find_whitout_predicate_returns_an_enumerator(letters):
assert_enumerator(letters.find(), ["a", "b", "c"])
def test_first(numbers):
assert numbers.first() == 1
def test_first_when_empty(empty_list):
assert empty_list.first() is None
def test_first_with_number_of_elements_specified(letters):
assert_enumerable_list(letters.first(2), ["a", "b"])
def test_first_with_fewer_elements_than_asked_for(letters):
assert_enumerable_list(letters.first(5), ["a", "b", "c"])
def test_first_when_empty_when_asked_for_a_number_of_elements(empty_list):
assert_enumerable_list(empty_list.first(5), [])
def test_flat_map_without_a_function_returns_an_enumerator(letters):
assert_enumerator(letters.flat_map(), ["a", "b", "c"])
def test_flat_map_with_nested_iterables(letters, numbers):
enumerable_list = EnumerableList([letters, numbers, 4])
assert_enumerable_list(enumerable_list.flat_map(pass_through), ["a", "b", "c", 1, 2, 3, 4])
def test_flat_map_does_not_treat_strings_as_nested_iterables():
enumerable_list = EnumerableList(["abc", "def"])
assert_enumerable_list(enumerable_list.flat_map(pass_through), ["abc", "def"])
def test_include(numbers):
assert numbers.include(3)
assert not numbers.include(4)
def test_inject(numbers):
assert numbers.inject(add) == 6
def test_inject_with_initial_value(numbers):
assert numbers.inject(4, add) == 10
def test_inject_when_empty(empty_list):
assert empty_list.inject(add) is None
def test_inject_when_empty_with_initial_value(empty_list):
assert empty_list.inject(0, add) == 0
def test_reject_returns_the_elements_for_which_the_function_is_falsy(numbers):
assert_enumerable_list(numbers.reject(larger_than_one), [1])
def test_reject_without_a_function_returns_an_enumerator(letters):
assert_enumerator(letters.reject(), ["a", "b", "c"])
def test_reject_with_a_sequence_containing_a_tuple(list_with_a_tuple):
assert_enumerable_list(list_with_a_tuple.reject(not_none), [])
def test_select_returns_the_elements_for_which_the_function_is_truthy(numbers):
assert_enumerable_list(numbers.select(larger_than_one), [2, 3])
def test_select_without_a_function_returns_an_enumerator(letters):
assert_enumerator(letters.select(), ["a", "b", "c"])
def test_select_with_a_sequence_containing_a_tuple(list_with_a_tuple):
assert_enumerable_list(list_with_a_tuple.select(not_none), ["a", ("b", None), "c"])
def test_take(letters):
assert_enumerable_list(letters.take(2), ["a", "b"])
def test_take_with_fewer_elements_than_asked_for(letters):
assert_enumerable_list(letters.take(5), ["a", "b", "c"])
def test_take_when_empty(empty_list):
assert_enumerable_list(empty_list.take(5), [])
def test_uniq(numbers_with_duplicates):
assert_enumerable_list(numbers_with_duplicates.uniq(), [1, 2, 3])
def test_uniq_with_predicate(numbers_with_duplicates):
assert_enumerable_list(numbers_with_duplicates.uniq(larger_than_one), [1, 2])
def test_uniq_with_no_hashable_elements():
enumerable_list = EnumerableList([1, 2, [1], [2], [1, 2]])
assert_enumerable_list(enumerable_list.uniq(), [1, 2, [1], [2], [1, 2]])
def increment(element):
return element + 1
def is_zero(element):
return element == 0
def larger_than_one(element):
return element > 1
def not_none(element):
return element is not None