Coverage for auttcomp/testing/test_partial_app.py: 80%
75 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-24 12:00 -0600
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-24 12:00 -0600
1from typing import Callable
2from ..quicklog import tracelog, log
3from ..composable import Composable
4from ..extensions import Api as f
6@tracelog("test_partial_1_param_func")
7def test_partial_1_param_func():
8 cat1 = f(lambda x: "0" + x)
9 result = cat1 & "1"
10 assert isinstance(result, str)
11 assert result == "01"
13@tracelog("test_partial_2_param_func")
14def test_partial_2_param_func():
15 cat2_l:Callable[[str, str], str] = lambda a, b: "0" + a + b
16 cat2 = f(cat2_l)
17 get1 = cat2 & "1"
18 assert get1("2") == "012"
20 result1 = get1 & "2"
21 assert result1 == "012"
23 result2 = cat2 & "1" & "2"
24 assert result2 == "012"
26@tracelog("test_partial_3_param_func")
27def test_partial_multi_param_func():
28 cat3 = f(lambda a, b, c: "0" + a + b + c)
29 get1 = cat3 & "1"
30 assert get1("2", "3") == "0123"
32 get2 = cat3 & "1" & "2"
33 assert get2("3") == "0123"
35 get3 = cat3 & "1" & "2" & "3"
36 assert get3() == "0123"
38@tracelog("test_partial_3_param_func_curried")
39def test_partial_multi_param_func():
41 #this test demonstrates that when functions are already curried, they are not within the composable's domain
43 cat3 = f(lambda a: lambda b: lambda c: "0" + a + b + c)
44 get1 = cat3("1")
45 assert get1("2")("3") == "0123"
47 #NOT composable!
48 assert not isinstance(get1, Composable)
50 get2 = cat3("1")("2")
51 assert get2("3") == "0123"
53 get3 = cat3("1")("2")("3")
54 assert get3 == "0123"
56@tracelog("test_partial_on_no_param_throws")
57def test_partial_on_no_param_throws():
58 zero_param_func = f(lambda: "hi")
60 try:
61 zero_param_func & "1"
62 assert False, "expected to throw!"
63 except TypeError:
64 pass
65 except Exception:
66 assert False, "wrong exception type"
68@tracelog("test_partial_on_curried_composable_func")
69def test_partial_on_curried_composable_func():
71 curried_add = f(lambda x: f(lambda y: x + y))
73 curried_add_1 = curried_add & 1
75 assert curried_add_1(1) == 2
77@tracelog("test_partial_on_curried_composable_func_with_composition")
78def test_partial_on_curried_composable_func_with_composition():
80 data = [1, 2, 3]
81 plus2comp = f.map & (lambda x: x + 1) | f.map & (lambda x: x + 1) | list
82 expected = [3, 4, 5]
84 actual = plus2comp(data)
86 assert actual == expected
89@tracelog("test_partial_callable_class")
90def test_partial_callable_class():
92 data = [1, 2, 3]
93 plus1 = f(map) & (lambda x: x + 1)
94 expected = [2, 3, 4]
96 actual = list(plus1(data))
98 assert actual == expected
100@tracelog("test_partial_callable_class_2")
101def test_partial_callable_class():
103 data = [1, 2, 3]
104 plus2 = f(map) & (lambda x: x + 1) | f(map) & (lambda x: x + 1)
105 expected = [3, 4, 5]
107 actual = list(plus2(data))
109 assert actual == expected