Coverage for auttcomp/testing/test_composable.py: 100%

86 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-24 12:00 -0600

1from ..quicklog import tracelog, log 

2from ..composable import Composable as f 

3 

4#to examine support for type hinting 

5def increment(value:int) -> int: 

6 return value + 1 

7 

8inc = f(increment) 

9inc_pass = f(lambda x,y: (x+1, y+1)) 

10power = f(lambda x,y: x**y) 

11split_num = f(lambda x: (x/2, x/2)) 

12with_str = f(lambda x: (x, str(x))) 

13str_len = f(lambda x,y: len(y)) 

14pass_thru = f(lambda x: x) 

15 

16def pass_many_params(a, b, c, d): 

17 return (a, b, c, d) 

18 

19@tracelog("test_minimal_single_param") 

20def test_minimal_single_param(): 

21 assert inc(1) == 2 

22 

23@tracelog("test_basic_comp") 

24def test_basic_comp(): 

25 inc2 = inc | inc 

26 assert inc2(1) == 3 

27 

28@tracelog("test_long_comp") 

29def test_long_comp(): 

30 inc4 = inc | inc | inc | inc 

31 assert inc4(1) == 5 

32 

33@tracelog("test_single_multi_param") 

34def test_single_multi_param(): 

35 (r0, r1) = inc_pass(1, 1) 

36 assert r0 == 2 and r1 == 2 

37 

38@tracelog("test_multi_param") 

39def test_multi_param(): 

40 inc_pass4 = inc_pass | inc_pass | inc_pass | inc_pass 

41 (r0, r1) = inc_pass4(1, 1) 

42 assert r0 == 5 and r1 == 5 

43 

44@tracelog("test_various_param") 

45def test_various_param(): 

46 func = inc_pass | power | with_str | str_len 

47 assert func(3, 3) == 3 

48 

49@tracelog("test_inverse_mixmatch") 

50def test_inverse_mixmatch(): 

51 func = inc_pass | power | with_str | str_len 

52 assert func(3, 3) == 3 

53 func2 = power | split_num | inc_pass | f(lambda x,y: (x/2) + (x/2)) | with_str | str_len 

54 assert func2(4, 4) == 5 

55 

56@tracelog("test_collections") 

57def test_collections(): 

58 pass3 = pass_thru | pass_thru | pass_thru 

59 assert pass3([1, 2, 3]) == [1, 2, 3] 

60 

61def range_factory(x): 

62 for i in range(1, x): 

63 yield i 

64 

65@tracelog("test_iterables") 

66def test_iterables(): 

67 rf = f(range_factory) 

68 evens = f(lambda r: filter(lambda x: x % 2 == 0, r)) 

69 to_list = f(lambda r: list(r)) 

70 avg = f(lambda r: sum(r) / len(r)) 

71 

72 func = rf | evens | to_list | avg 

73 

74 assert func(10) == 5 

75 

76def void_func(): 

77 log(f"not input, not output") 

78 

79@tracelog("test_void") 

80def test_void(): 

81 vf = f(void_func) 

82 func = vf | vf | vf 

83 func() 

84 assert True, "does not throw" 

85 

86@tracelog("test_dynamic_wrapping") 

87def test_dynamic_wrapping(): 

88 

89 #test_iterables without f-wrap 

90 rf = f(range_factory) 

91 evens = lambda r: filter(lambda x: x % 2 == 0, r) 

92 to_list = lambda r: list(r) 

93 avg = lambda r: sum(r) / len(r) 

94 func = rf | evens | to_list | avg 

95 assert func(10) == 5 

96 

97@tracelog("test_kargs") 

98def test_kargs(): 

99 

100 comp = f(pass_many_params) 

101 

102 assert comp(1, 2, 3, 4) == (1, 2, 3, 4) 

103 assert comp(a=1, b=2, c=3, d=4) == (1, 2, 3, 4) 

104 assert comp(1, 2, c=3, d=4) == (1, 2, 3, 4) 

105 assert comp(1, 2, d=4, c=3) == (1, 2, 3, 4) 

106 

107 comp3 = comp | comp | comp 

108 assert comp3(1, 2, d=4, c=3) == (1, 2, 3, 4) 

109 

110 def func4to1(a, b, c, d): return (d-c)+(b-a) 

111 comp4 = comp | comp | f(func4to1) 

112 assert comp4(1, 2, d=4, c=3) == 2