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

1from typing import Callable 

2from ..quicklog import tracelog, log 

3from ..composable import Composable 

4from ..extensions import Api as f 

5 

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" 

12 

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" 

19 

20 result1 = get1 & "2" 

21 assert result1 == "012" 

22 

23 result2 = cat2 & "1" & "2" 

24 assert result2 == "012" 

25 

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" 

31 

32 get2 = cat3 & "1" & "2" 

33 assert get2("3") == "0123" 

34 

35 get3 = cat3 & "1" & "2" & "3" 

36 assert get3() == "0123" 

37 

38@tracelog("test_partial_3_param_func_curried") 

39def test_partial_multi_param_func(): 

40 

41 #this test demonstrates that when functions are already curried, they are not within the composable's domain 

42 

43 cat3 = f(lambda a: lambda b: lambda c: "0" + a + b + c) 

44 get1 = cat3("1") 

45 assert get1("2")("3") == "0123" 

46 

47 #NOT composable! 

48 assert not isinstance(get1, Composable) 

49 

50 get2 = cat3("1")("2") 

51 assert get2("3") == "0123" 

52 

53 get3 = cat3("1")("2")("3") 

54 assert get3 == "0123" 

55 

56@tracelog("test_partial_on_no_param_throws") 

57def test_partial_on_no_param_throws(): 

58 zero_param_func = f(lambda: "hi") 

59 

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" 

67 

68@tracelog("test_partial_on_curried_composable_func") 

69def test_partial_on_curried_composable_func(): 

70 

71 curried_add = f(lambda x: f(lambda y: x + y)) 

72 

73 curried_add_1 = curried_add & 1 

74 

75 assert curried_add_1(1) == 2 

76 

77@tracelog("test_partial_on_curried_composable_func_with_composition") 

78def test_partial_on_curried_composable_func_with_composition(): 

79 

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] 

83 

84 actual = plus2comp(data) 

85 

86 assert actual == expected 

87 

88 

89@tracelog("test_partial_callable_class") 

90def test_partial_callable_class(): 

91 

92 data = [1, 2, 3] 

93 plus1 = f(map) & (lambda x: x + 1) 

94 expected = [2, 3, 4] 

95 

96 actual = list(plus1(data)) 

97 

98 assert actual == expected 

99 

100@tracelog("test_partial_callable_class_2") 

101def test_partial_callable_class(): 

102 

103 data = [1, 2, 3] 

104 plus2 = f(map) & (lambda x: x + 1) | f(map) & (lambda x: x + 1) 

105 expected = [3, 4, 5] 

106 

107 actual = list(plus2(data)) 

108 

109 assert actual == expected