Coverage for tests/test_pipable.py: 100%
60 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-10 02:26 +0800
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-10 02:26 +0800
1from pipable import Pipe
2import pytest
5# == fixture ==
6def kebab(*args):
7 return "-".join(args)
10# == test ==
11def test_builtn_with_single_arg():
12 list_ = Pipe(list)
13 assert "ab" | list_ == ["a", "b"]
16def test_precedent_assign_to_last_pos_args():
17 assert "c" | Pipe(kebab) == "c"
18 assert "c" | Pipe(kebab, "a") == "a-c"
19 assert "c" | Pipe(kebab, "a", "b") == "a-b-c"
22def test_assign_1st_arg():
23 base2 = Pipe(pow, 2)
24 assert 3 | base2 == 8
27def test_raise_pos_and_keyword_arg_conflict():
28 """
29 precedent will append to the positional argument list
30 ∴ partial can't assign 1st arg as keyword
31 """
32 base2 = Pipe(pow, base=2)
33 with pytest.raises(Exception):
34 3 | base2
37def test_decorator_with_single_arg():
38 @Pipe
39 def hi(name):
40 return f"hi {name}"
42 assert "May" | hi == "hi May"
45def test_decorator_with_multiple_arg():
46 @Pipe
47 def power(base, exp):
48 return base**exp
50 assert 3 | power(2) == 8
51 assert 3 | power(exp=2) == 9
52 # same reason with test_raise_pos_and_keyword_arg_conflict
53 with pytest.raises(Exception):
54 3 | power(base=2)
57def test_wrap_with_partial():
58 base2 = Pipe(lambda x: pow(2, x))
59 assert 3 | base2 == 8
62def test_variable_positional_args(capsys):
63 print_ = Pipe(print, "a", "b")
64 "c" | print_
65 stdout: str = capsys.readouterr().out
66 assert stdout.splitlines()[-1] == "a b c"
69def test_prepend_args_with_wrapper():
70 def wrapper(first, others):
71 return kebab(first, *others)
73 assert "c" | Pipe(wrapper, others=["a", "b"]) == "c-a-b"
76def test_decorator_prepend_args():
77 @Pipe
78 def wrapper(first, args):
79 return kebab(first, *args)
81 assert "c" | wrapper(args=["a", "b"]) == "c-a-b"
84def test_reassign_with_wrapper():
85 def kebab(*args):
86 return "-".join(args)
88 def wrapper(first, others):
89 return kebab(first, *others)
91 # works
92 assert "a" | Pipe(wrapper, others=["b", "c"]) == "a-b-c"
94 # works with other var name
95 kebab_ = Pipe(wrapper, others=["b", "c"])
96 assert "a" | kebab_ == "a-b-c"
98 # not work, ∵ original name already re-assigned when wrapper is being invoked
99 # it become a Pipe object instead
100 kebab = Pipe(wrapper, others=["b", "c"])
101 result = "a" | kebab
102 assert result != "a-b-c"
103 assert isinstance(result, Pipe)