Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests should assert types #41

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions functionalpy/test_sequence.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from collections.abc import Mapping, Sequence
from typing import Literal

from functionalpy._sequence import Seq


# TODO: https://github.com/MartinBernstorff/FunctionalPy/issues/40 tests: add hypothesis tests where it makes sense
def test_chaining():
sequence = Seq([1, 2])
result = (
result: list[int] = (
sequence.filter(lambda x: x % 2 == 0)
.map(lambda x: x * 2)
.to_list()
Expand All @@ -14,66 +17,64 @@ def test_chaining():

def test_map():
sequence = Seq([1, 2])
result = sequence.map(lambda x: x * 2).to_list()
result: list[int] = sequence.map(lambda x: x * 2).to_list()
assert result == [2, 4]


def test_filter():
sequence = Seq((1, 2))
result = sequence.filter(lambda x: x % 2 == 0).to_list()
sequence = Seq([1, 2])
result: list[int] = sequence.filter(
lambda x: x % 2 == 0
).to_list()
assert result == [2]


def test_reduce():
sequence = Seq([1, 2])
result = sequence.reduce(lambda x, y: x + y)
result: int = sequence.reduce(lambda x, y: x + y)
assert result == 3


class TestGroupby:
def test_grouped_filter(self):
sequence = Seq([1, 2, 3, 4])

def is_even(num: int) -> str:
if num % 2 == 0:
return "even"
return "odd"

grouped = sequence.groupby(is_even)
assert grouped == {
"odd": [1, 3],
"even": [2, 4],
}
def test_grouped_filter():
sequence = Seq([1, 2, 3, 4])

def test_grouped_map(self):
...
def is_even(num: int) -> str:
if num % 2 == 0:
return "even"
return "odd"

def test_grouped_reduce(self):
...
grouped: Mapping[str, Sequence[int]] = sequence.groupby(is_even)
assert grouped == {
"odd": [1, 3],
"even": [2, 4],
}


def test_flatten():
test_input = ((1, 2), (3, 4))
test_input: list[list[int]] = [[1, 2], [3, 4]]
sequence = Seq(test_input)
result = sequence.flatten()
result: Seq[int] = sequence.flatten()
assert result.to_list() == [1, 2, 3, 4]


class TestFlattenTypes:
def test_flatten_tuple(self):
test_input = ((1, 2), (3, 4))
test_input: Sequence[tuple[int, ...]] = (
(1, 2),
(3, 4),
)
sequence = Seq(test_input)
result = sequence.flatten()
result: Seq[Literal[1, 2, 3, 4]] = sequence.flatten()
assert result.to_list() == [1, 2, 3, 4]

def test_flatten_list(self):
test_input = [[1, 2], [3, 4]]
test_input: list[list[int]] = [[1, 2], [3, 4]]
sequence = Seq(test_input)
result = sequence.flatten()
result: Seq[int] = sequence.flatten()
assert result.to_list() == [1, 2, 3, 4]

def test_flatten_str(self):
test_input = ["abcd"]
test_input: list[str] = ["abcd"]
sequence = Seq(test_input)
result = sequence.flatten()
result: Seq[str] = sequence.flatten()
assert result.to_list() == ["a", "b", "c", "d"]
Loading