Esempio n. 1
0
def test_seq_choose_option():
    xs: Iterable[Optional[int]] = seq.of(None, 42)

    chooser = seq.choose(option.of_optional)
    ys = pipe(xs, chooser)

    assert list(ys) == [42]
Esempio n. 2
0
def test_list_sort_with(xs: List[str]):
    expected = sorted(xs, key=lambda x: x[1])
    ys: FrozenList[str] = frozenlist.of_seq(xs)
    func: Callable[[str], str] = lambda x: x[1]
    result = pipe(ys, frozenlist.sort_with(func))

    assert list(result) == list(expected)
Esempio n. 3
0
def test_seq_take(xs: List[int], x: int):
    ys = seq.of_iterable(xs)
    try:
        zs = pipe(ys, seq.take(x))
        assert list(zs) == xs[:x]
    except ValueError:
        assert x > len(xs)
Esempio n. 4
0
def test_list_filter(xs: List[int], limit: int):
    expected = filter(lambda x: x < limit, xs)

    ys: FrozenList[int] = frozenlist.of_seq(xs)
    predicate: Callable[[int], bool] = lambda x: x < limit
    result = pipe(ys, frozenlist.filter(predicate))

    assert list(result) == list(expected)
Esempio n. 5
0
def test_seq_pipe_starmap(xs: List[Tuple[int, int]]):
    mapper: Callable[[int, int], int] = lambda x, y: x + y
    ys = pipe(
        frozenlist.of_seq(xs),
        frozenlist.starmap(mapper),
    )

    assert isinstance(ys, FrozenList)
    assert [y for y in ys] == [x + y for (x, y) in xs]
Esempio n. 6
0
def test_seq_pipe_map3(xs: List[Tuple[int, int, int]]):
    mapper: Callable[[int, int, int], int] = lambda x, y, z: x + y + z
    ys = pipe(
        frozenlist.of_seq(xs),
        frozenlist.map3(mapper),
    )

    assert isinstance(ys, FrozenList)
    assert [y for y in ys] == [x + y + z for (x, y, z) in xs]
Esempio n. 7
0
def test_list_unfold(x: int):
    def unfolder(state: int) -> Option[Tuple[int, int]]:
        if state < x:
            return Some((state, state + 1))
        return Nothing

    result = pipe(0, seq.unfold(unfolder))

    assert list(result) == list(range(x))
Esempio n. 8
0
def test_list_fold(xs: List[int]):
    def folder(x: int, y: int) -> int:
        return x + y

    expected: int = functools.reduce(folder, xs, 0)

    ys: FrozenList[int] = frozenlist.of_seq(xs)
    result = pipe(ys, frozenlist.fold(folder, 0))

    assert result == expected
Esempio n. 9
0
def test_map_remove(xs: Dict[str, int]):
    items: ItemsView[str, int] = xs.items()
    m = Map.of_seq(items)

    keys = xs.keys()
    count = len(m)
    for key in keys:
        m = pipe(m, map.remove(key))
        count -= 1
    assert len(m) == count == 0
Esempio n. 10
0
def test_map_to_seq(xs: Dict[str, int]):
    items: List[Tuple[str, int]] = list(xs.items())
    ys = map.of_list(items)
    zs = pipe(ys, map.to_seq)
    assert sorted(list(items)) == list(zs)
Esempio n. 11
0
def test_seq_pipe_map3(xs: List[Tuple[int, int, int]]):
    mapper: Callable[[int, int, int], int] = lambda x, y, z: x + y + z
    ys = pipe(xs, seq.map3(mapper))

    assert isinstance(ys, Iterable)
    assert [y for y in ys] == [x + y + z for (x, y, z) in xs]
Esempio n. 12
0
def test_seq_pipe_map(xs: List[int]):
    mapper: Callable[[int], int] = lambda x: x + 1
    ys = pipe(xs, seq.map(mapper))

    assert isinstance(ys, Iterable)
    assert [y for y in ys] == [x + 1 for x in xs]
Esempio n. 13
0
def test_seq_append_3(xs: List[int], ys: List[int], zs: List[int]):
    value = pipe(xs, seq.append(ys, zs))

    assert list(value) == xs + ys + zs
Esempio n. 14
0
def test_list_non_empty():
    xs = frozenlist.singleton(42)
    assert len(xs) == 1
    assert xs
    assert not pipe(xs, frozenlist.is_empty)
Esempio n. 15
0
def test_list_empty():
    xs = frozenlist.empty
    assert len(xs) == 0
    assert not xs
    assert pipe(xs, frozenlist.is_empty)
Esempio n. 16
0
def test_list_sort(xs: List[int]):
    expected = sorted(xs)
    ys: FrozenList[int] = frozenlist.of_seq(xs)
    result = pipe(ys, frozenlist.sort())

    assert list(result) == list(expected)
Esempio n. 17
0
def test_seq_scan_pipe(xs: List[int], s: int):
    func: Callable[[int, int], int] = lambda s, v: s + v
    value = pipe(seq.of_iterable(xs), seq.scan(func, s))

    expected: Iterable[int] = accumulate(xs, func, initial=s)
    assert list(value) == list(expected)
Esempio n. 18
0
def test_list_cons_head(value: Any):
    x = pipe(frozenlist.empty.cons(value), frozenlist.head)
    assert x == value
Esempio n. 19
0
def test_seq_collect(xs: List[int]):
    collector = seq.collect(seq.singleton)
    ys = pipe(xs, collector)

    assert list(xs) == list(ys)
Esempio n. 20
0
def test_pipe_fn(x: int):
    value = pipe(x, lambda x: x + 1)
    assert value == x + 1
Esempio n. 21
0
def test_seq_length(xs: List[int]):
    ys = seq.of_iterable(xs)
    n = pipe(ys, seq.length)
    assert n == len(xs)
Esempio n. 22
0
def test_pipe_fn_gn(x: int, y: int, z: int):
    gn: Callable[[int], int] = lambda g: g * y
    fn: Callable[[int], int] = lambda x: x + z
    value = pipe(x, fn, gn)

    assert value == gn(fn(x))
Esempio n. 23
0
def test_seq_infinite(xs: List[int]):
    ys = pipe(xs, seq.zip(seq.infinite))

    expected = list(enumerate(xs))
    assert expected == list(ys)
Esempio n. 24
0
def test_pipe_id(x: int):
    value = pipe(x)
    assert value == x
Esempio n. 25
0
def test_seq_pipe_starmap(xs: List[Tuple[int, int]]):
    mapper: Callable[[int, int], int] = lambda x, y: x + y
    ys = pipe(xs, seq.starmap(mapper))

    assert isinstance(ys, Iterable)
    assert [y for y in ys] == [x + y for (x, y) in xs]
Esempio n. 26
0
def test_seq_head_pipe(xs: List[int]):
    value = pipe(xs, seq.head)

    assert value == xs[0]
Esempio n. 27
0
def test_seq_pipe_mapi(xs: List[int]):
    mapper: Callable[[int, int], int] = lambda i, x: x + i
    ys = pipe(xs, seq.mapi(mapper))

    assert isinstance(ys, Iterable)
    assert [y for y in ys] == [x + i for i, x in enumerate(xs)]
Esempio n. 28
0
def test_seq_head_empty_source():
    with pytest.raises(ValueError):
        pipe(Seq.empty(), seq.head)
Esempio n. 29
0
def test_seq_fold_pipe(xs: List[int], s: int):
    folder: Callable[[int, int], int] = lambda s, v: s + v
    value = pipe(seq.of_iterable(xs), seq.fold(folder, s))

    assert value == sum(xs) + s
Esempio n. 30
0
def test_option_some():
    xs = Some(42)

    assert isinstance(xs, Option)
    assert pipe(xs, option.is_some) is True
    assert pipe(xs, option.is_none) is False