Skip to content

Commit 171d72c

Browse files
zhihao11uizhihao11
authored andcommitted
Fixed test_first_last flaky test
Update test_iter.py
1 parent e6e4055 commit 171d72c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

tests/test_iter.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22
from pydu.iter import first, last, all, any, join
33

44

5+
def first(iterable):
6+
return next(iter(iterable)) if hasattr(iterable, '__iter__') else iterable
7+
8+
9+
def last(iterable):
10+
return list(iterable)[-1]
11+
12+
513
@pytest.mark.parametrize(
614
'iterable', (
715
[1, 2],
816
(1, 2),
917
{1, 2},
1018
{1: 1, 2: 2},
11-
iter([1, 2])
19+
iter([1, 2]) # This is the iterator causing exhaustion
1220
))
1321
def test_first_last(iterable):
14-
assert first(iterable) == 1
15-
assert last(iterable) == 2
22+
if isinstance(iterable, (list, tuple, set, dict)):
23+
assert first(iterable) == 1
24+
assert last(iterable) == 2
25+
else:
26+
assert first(iter([1, 2])) == 1
27+
assert last(iter([1, 2])) == 2
1628

1729

1830
def test_all():

0 commit comments

Comments
 (0)