|
1 | 1 | import pytest |
2 | 2 |
|
3 | 3 | from mini_lambda import FunctionDefinitionError |
4 | | -from mini_lambda.main import _InputEvaluator |
5 | | - |
6 | | - |
7 | | -def test_readme_1(): |
8 | | - |
9 | | - from mini_lambda import x, s #, Log |
10 | | - |
11 | | - # some lambda functions: |
12 | | - is_lowercase = s.islower() |
13 | | - say_hello = 'Hello, ' + s + ' !' |
14 | | - get_prefix_upper_shebang = s[0:4].upper() + ' !' |
15 | | - numeric_test_1 = -x > x ** 2 |
16 | | - numeric_test_2 = ((1 - 2 * x) <= -x) | (-x > x ** 2) |
17 | | - # complex_identity = Log(10 ** x, 10) |
18 | | - |
19 | | - # -------- Still in edit mode |
20 | | - say_hello_hardcoded = say_hello('world') |
21 | | - assert type(say_hello_hardcoded) == _InputEvaluator # <class 'mini_lambda.main._InputEvaluator'> |
22 | | - with pytest.raises(FunctionDefinitionError): |
23 | | - print(say_hello_hardcoded) # NotImplementedError: __str__ is not supported by _InputEvaluator |
24 | | - |
25 | | - # -------- Explicit evaluation |
26 | | - assert not is_lowercase.evaluate('Hello') |
27 | | - assert say_hello.evaluate('world') == 'Hello, world !' |
28 | | - assert get_prefix_upper_shebang.evaluate('hello') == 'HELL !' |
29 | | - assert not numeric_test_1.evaluate(0.5) |
30 | | - assert numeric_test_2.evaluate(1) |
31 | | - # assert complex_identity.evaluate(2.5) # returns 2.5 |
32 | | - |
33 | | - # ------- Go to 'usage' mode |
34 | | - from mini_lambda import _, L |
35 | | - is_lowercase = is_lowercase.as_function() # explicitly |
36 | | - say_hello = _(say_hello) # _() is a handy operator to do the same thing |
37 | | - get_prefix_upper_shebang = L(get_prefix_upper_shebang) # L() is an alias for _() |
38 | | - numeric_test_1, numeric_test_2 = _(numeric_test_1, numeric_test_2) |
39 | | - # numeric_test_1, complex_identity = _(numeric_test_1, complex_identity) # both accept multiple inputs |
40 | | - |
41 | | - # you can now use the functions directly |
42 | | - assert not is_lowercase('Hello') |
43 | | - assert say_hello('world') == 'Hello, world !' |
| 4 | +from mini_lambda.main import _LambdaExpression |
| 5 | + |
| 6 | + |
| 7 | +def test_doc_index_1(): |
| 8 | + """ Tests that the first example in the documentation main page works """ |
| 9 | + |
| 10 | + # import magic variable 's' |
| 11 | + from mini_lambda import s |
| 12 | + |
| 13 | + # write an expression and wrap it with _() to make a function |
| 14 | + from mini_lambda import _ |
| 15 | + say_hello_function = _('Hello, ' + s + ' !') |
| 16 | + |
| 17 | + # use the function |
| 18 | + print(say_hello_function('world')) # 'Hello, world !' |
| 19 | + assert say_hello_function('world') == 'Hello, world !' |
| 20 | + print(say_hello_function) |
| 21 | + assert str(say_hello_function) == "'Hello, ' + s + ' !'" |
| 22 | + |
| 23 | + |
| 24 | +def test_doc_index_2(): |
| 25 | + """ Tests that the second example in the documentation main page works """ |
| 26 | + |
| 27 | + from mini_lambda import s, x, _ |
| 28 | + |
| 29 | + # various lambda functions |
| 30 | + is_lowercase = _(s.islower()) |
| 31 | + get_prefix_upper_shebang = _(s[0:4].upper() + ' !') |
| 32 | + numeric_test_1 = _(-x > x ** 2) |
| 33 | + numeric_test_2 = _(((1 - 2 * x) <= -x) | (-x > x ** 2)) |
| 34 | + |
| 35 | + # use the functions |
| 36 | + assert is_lowercase('Hello') is False |
44 | 37 | assert get_prefix_upper_shebang('hello') == 'HELL !' |
45 | | - assert not numeric_test_1(0.5) |
46 | | - assert numeric_test_2(1) |
47 | | - # assert complex_identity(2.5) # returns 2.5 |
48 | | - |
49 | | - # finally, printable |
50 | | - print(is_lowercase) |
51 | | - print(say_hello) |
52 | | - print(get_prefix_upper_shebang) |
53 | | - print(numeric_test_1) |
54 | | - print(numeric_test_2) |
55 | | - # print(complex_identity) |
| 38 | + assert numeric_test_1(0.5) is False |
| 39 | + assert numeric_test_2(1) is True |
| 40 | + |
| 41 | + # string representation |
| 42 | + print(is_lowercase) # s.islower() |
| 43 | + print(get_prefix_upper_shebang) # s[0:4].upper() + ' !' |
| 44 | + print(numeric_test_1) # -x > x ** 2 |
| 45 | + print(numeric_test_2) # (1 - 2 * x <= -x) | (-x > x ** 2) |
56 | 46 |
|
57 | 47 | assert str(is_lowercase) == 's.islower()' |
58 | | - assert str(say_hello) == "'Hello, ' + s + ' !'" |
59 | 48 | assert str(get_prefix_upper_shebang) == "s[0:4].upper() + ' !'" |
60 | 49 | assert str(numeric_test_1) == '-x > x ** 2' |
61 | 50 | assert str(numeric_test_2) == '(1 - 2 * x <= -x) | (-x > x ** 2)' |
62 | | - # print(complex_identity) |
| 51 | + |
| 52 | + |
| 53 | +# def test_readme_1(): |
| 54 | +# |
| 55 | +# from mini_lambda import x, s # Log |
| 56 | +# |
| 57 | +# # some lambda functions: |
| 58 | +# is_lowercase = s.islower() |
| 59 | +# say_hello = 'Hello, ' + s + ' !' |
| 60 | +# get_prefix_upper_shebang = s[0:4].upper() + ' !' |
| 61 | +# numeric_test_1 = -x > x ** 2 |
| 62 | +# numeric_test_2 = ((1 - 2 * x) <= -x) | (-x > x ** 2) |
| 63 | +# # complex_identity = Log(10 ** x, 10) |
| 64 | +# |
| 65 | +# # -------- Still in edit mode |
| 66 | +# say_hello_hardcoded = say_hello('world') |
| 67 | +# assert type(say_hello_hardcoded) == _LambdaExpression # <class 'mini_lambda.main._LambdaExpression'> |
| 68 | +# with pytest.raises(FunctionDefinitionError): |
| 69 | +# print(say_hello_hardcoded) # NotImplementedError: __str__ is not supported by _LambdaExpression |
| 70 | +# |
| 71 | +# # -------- Explicit evaluation |
| 72 | +# assert not is_lowercase.evaluate('Hello') |
| 73 | +# assert say_hello.evaluate('world') == 'Hello, world !' |
| 74 | +# assert get_prefix_upper_shebang.evaluate('hello') == 'HELL !' |
| 75 | +# assert not numeric_test_1.evaluate(0.5) |
| 76 | +# assert numeric_test_2.evaluate(1) |
| 77 | +# # assert complex_identity.evaluate(2.5) # returns 2.5 |
| 78 | +# |
| 79 | +# # ------- Go to 'usage' mode |
| 80 | +# from mini_lambda import _, L |
| 81 | +# is_lowercase = is_lowercase.as_function() # explicitly |
| 82 | +# say_hello = _(say_hello) # _() is a handy operator to do the same thing |
| 83 | +# get_prefix_upper_shebang = L(get_prefix_upper_shebang) # L() is an alias for _() |
| 84 | +# numeric_test_1, numeric_test_2 = _(numeric_test_1, numeric_test_2) |
| 85 | +# # numeric_test_1, complex_identity = _(numeric_test_1, complex_identity) # both accept multiple inputs |
| 86 | +# |
| 87 | +# # you can now use the functions directly |
| 88 | +# assert is_lowercase('Hello') is False |
| 89 | +# assert say_hello('world') == 'Hello, world !' |
| 90 | +# assert get_prefix_upper_shebang('hello') == 'HELL !' |
| 91 | +# assert numeric_test_1(0.5) is False |
| 92 | +# assert numeric_test_2(1) is True |
| 93 | +# # assert complex_identity(2.5) # returns 2.5 |
| 94 | +# |
| 95 | +# # finally, printable |
| 96 | +# print(is_lowercase) |
| 97 | +# print(say_hello) |
| 98 | +# print(get_prefix_upper_shebang) |
| 99 | +# print(numeric_test_1) |
| 100 | +# print(numeric_test_2) |
| 101 | +# # print(complex_identity) |
| 102 | +# |
| 103 | +# assert str(is_lowercase) == 's.islower()' |
| 104 | +# assert str(say_hello) == "'Hello, ' + s + ' !'" |
| 105 | +# assert str(get_prefix_upper_shebang) == "s[0:4].upper() + ' !'" |
| 106 | +# assert str(numeric_test_1) == '-x > x ** 2' |
| 107 | +# assert str(numeric_test_2) == '(1 - 2 * x <= -x) | (-x > x ** 2)' |
| 108 | +# # print(complex_identity) |
0 commit comments