File size: 1,119 Bytes
0a795e1 |
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 |
import pytest
from combine import combine
def test_empty_list():
assert combine([], lambda x, y: x + y) == []
def test_single_item():
assert combine(["hello"], lambda x, y: x + y) == ["hello"]
def test_two_items():
assert combine(["hello", "world"], lambda x, y: x + y) == ["helloworld"]
def test_sum():
assert combine([1, 2, 3, 4], lambda x, y: x + y) == [10]
def test_add_if_even():
def add_if_even(x: int, y: int) -> int | None:
if (x + y) % 2 == 0:
return x + y
return None
assert combine([1, 3, 1, 4], add_if_even) == [4, 1, 4]
assert combine([1, 3, 2, 4], add_if_even) == [10]
def test_join_if_same_letter():
def join_if_same_letter(x: str, y: str) -> str | None:
if x[0] == y[0]:
return x + y
return None
assert combine(["hello", "hi", "home", "world", "welcome"], join_if_same_letter) == ["hellohihome", "worldwelcome"]
def test_no_combinations():
def never_combine(x: int, y: int) -> None:
return None
input_list = [1, 2, 3, 4]
assert combine(input_list, never_combine) == input_list
|