def test_length(): fiter = FIt(range(5)) assert len(fiter) == 5 next(fiter) assert len(fiter) == 4 fiter.peek() assert len(fiter) == 4
def test_starmap(): lst = [(x, x**2) for x in range(10)] fiter = FIt(lst) def fn(x, y): return x + y assert_equal_it(fiter.starmap(fn), starmap(fn, lst))
def fiter(request, lst): if request.param == "list": fiter = FIt(lst) assert len_or_none(fiter) is not None return fiter else: fiter = FIt(iter(lst)) assert len_or_none(fiter) is None return fiter
def test_sliding_window(): fiter = FIt(range(5)) expected = [(0, 1, 2), (1, 2, 3), (2, 3, 4)] assert_equal_it(fiter.sliding_window(3), expected) fiter2 = FIt(range(5)) assert_equal_it(fiter2.sliding_window(10), [])
def test_next(): fiter = FIt([1]) fiter2 = FIt([1]) assert fiter.next() == next(fiter2) with pytest.raises(StopIteration): fiter.next()
def test_chunk(): fiter = FIt(range(5)) expected = [[0, 1], [2, 3], [4]] assert_equal_it(fiter.chunk(2), expected)
def test_flatten_0(): lst = [1, 2, [3, 4], [5, [6, 7]]] fiter = FIt(lst) assert_equal_it(fiter.flatten(0), lst)
def test_flatten_nostr(): fiter = FIt([1, [2, "potato"]]) assert_equal_it(fiter.flatten(split_strings=True), [1, 2] + list("potato"))
def test_getitem_neg_nolen(lst): fiter = FIt(iter(lst)) with pytest.raises(ValueError): fiter[-1]
def test_any(lst, expected): fiter = FIt(lst) assert fiter.any() == expected
def test_ensure_FIt(lst): assert isinstance(ensure_FIt(lst), FIt) fiter = FIt(lst) assert ensure_FIt(fiter) is fiter
def test_repeat(): it = FIt.repeat(1, 5) assert len(it) == 5 assert list(it) == [1] * 5
def test_count(fiter, lst): fiter = FIt.count(2, 20) other = count(2, 20) assert_equal_it(islice(fiter, 20), islice(other, 20))
def test_range(args): assert_equal_it(FIt.range(*args), range(*args))
def test_getitem_neg(lst): fiter = FIt(lst) assert fiter[-1] == lst[-1]
def test_getitem(lst): fiter = FIt(lst) assert fiter[2] == lst[2]
def test_all(lst, expected): fiter = FIt(lst) assert fiter.all() == expected
def test_interleave(wrapped): fiter = FIt(wrapped) mix_with = range(5, 10) expected = [0, 5, 1, 6, 2, 7, 3, 8, 4, 9] assert_equal_it(fiter.interleave(mix_with), expected)
def test_instantiate_nolen(lst): it = FIt(iter(lst)) assert it.length is None with pytest.raises(TypeError): len(it)
def test_islice_neg(lst): fiter = FIt(lst) assert_equal_it(fiter.islice(5, -2), lst[5:-2])
def test_instantiate(lst): it = FIt(lst) assert it.length == len(it) == len(lst)
def test_flatten(): fiter = FIt([1, 2, [3, 4], [5, [6, 7]]]) assert_equal_it(fiter.flatten(), [1, 2, 3, 4, 5, 6, 7])
def test_instantiate_explicit_len(lst): it = FIt(iter(lst), len(lst)) assert it.length == len(it) == len(lst)
def test_flatten_levels(): fiter = FIt([1, 2, [3, 4], [5, [6, 7]]]) assert_equal_it(fiter.flatten(levels=1), [1, 2, 3, 4, 5, [6, 7]])
def test_to(lst): assert FIt(lst).to(set) == set(lst)
def test_flatten_str(): fiter = FIt([1, [2, "potato"]]) assert_equal_it(fiter.flatten(), [1, 2, "potato"])