|
| 1 | +import os |
| 2 | +import inspect |
| 3 | + |
| 4 | + |
| 5 | +files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("functions")] |
| 6 | +for f in files: |
| 7 | + exec("import " + f[:-3] + " as " + f[:-3]) |
| 8 | + |
| 9 | + |
| 10 | +def test_names(): |
| 11 | + for f in files: |
| 12 | + assert "custom_power" in dir(eval(f[:-3])), "custom_power is not defined in " + f[:-3] |
| 13 | + assert "custom_equation" in dir(eval(f[:-3])), "custom_equation is not defined in " + f[:-3] |
| 14 | + assert "fn_w_counter" in dir(eval(f[:-3])), "fn_w_counter is not defined in " + f[:-3] |
| 15 | + |
| 16 | +def test_callables(): |
| 17 | + for f in files: |
| 18 | + assert callable(eval(f[:-3]).custom_power), "custom_power is not callable in " + f[:-3] |
| 19 | + assert callable(eval(f[:-3]).custom_equation), "custom_equation is not callable in " + f[:-3] |
| 20 | + assert callable(eval(f[:-3]).fn_w_counter), "fn_w_counter is not callable in " + f[:-3] |
| 21 | + |
| 22 | +def test_custom_power(): |
| 23 | + for f in files: |
| 24 | + sig = inspect.signature(eval(f[:-3]).custom_power) |
| 25 | + assert eval(f[:-3]).custom_power.__name__ == "<lambda>", \ |
| 26 | + "custom_power is not a lambda function in " + f[:-3] |
| 27 | + assert 'x' in sig.parameters, \ |
| 28 | + "custom_power has wrong parameters in " + f[:-3] |
| 29 | + assert 'e' in sig.parameters, \ |
| 30 | + "custom_power has wrong parameters in " + f[:-3] |
| 31 | + assert sig.parameters['x'].kind == inspect._POSITIONAL_ONLY, \ |
| 32 | + "custom_power has wrong kind in " + f[:-3] |
| 33 | + assert sig.parameters['e'].kind == inspect._POSITIONAL_OR_KEYWORD, \ |
| 34 | + "custom_power has wrong kind in " + f[:-3] |
| 35 | + assert sig.parameters['x'].default == 0, \ |
| 36 | + "custom_power has wrong default value in " + f[:-3] |
| 37 | + assert sig.parameters['e'].default == 1, \ |
| 38 | + "custom_power has wrong default value in " + f[:-3] |
| 39 | + assert eval(f[:-3]).custom_power(2) == 2, \ |
| 40 | + "custom_power is not working in " + f[:-3] |
| 41 | + assert eval(f[:-3]).custom_power(2, 3) == 8, \ |
| 42 | + "custom_power is not working in " + f[:-3] |
| 43 | + assert eval(f[:-3]).custom_power(2, e=3) == 8, \ |
| 44 | + "custom_power is not working in " + f[:-3] |
| 45 | + assert eval(f[:-3]).custom_power(49, e=0.5) == 7, \ |
| 46 | + "custom_power is not working in " + f[:-3] |
| 47 | + |
| 48 | +def test_custom_equation(): |
| 49 | + for f in files: |
| 50 | + sig = inspect.signature(eval(f[:-3]).custom_equation) |
| 51 | + assert eval(f[:-3]).custom_equation.__doc__ is not None, \ |
| 52 | + "custom_equation has no docstring in " + f[:-3] |
| 53 | + assert eval(f[:-3]).custom_equation.__doc__.count(":param") == 5, \ |
| 54 | + "custom_equation has wrong docstring in " + f[:-3] + " (param)" |
| 55 | + assert eval(f[:-3]).custom_equation.__doc__.count(":return") == 1, \ |
| 56 | + "custom_equation has wrong docstring in " + f[:-3] + " (return)" |
| 57 | + assert sig.return_annotation == float, \ |
| 58 | + "custom_equation has wrong return annotation in " + f[:-3] |
| 59 | + assert 'x' in sig.parameters, \ |
| 60 | + "custom_equation has wrong parameters in " + f[:-3] |
| 61 | + assert 'y' in sig.parameters, \ |
| 62 | + "custom_equation has wrong parameters in " + f[:-3] |
| 63 | + assert 'a' in sig.parameters, \ |
| 64 | + "custom_equation has wrong parameters in " + f[:-3] |
| 65 | + assert 'b' in sig.parameters, \ |
| 66 | + "custom_equation has wrong parameters in " + f[:-3] |
| 67 | + assert 'c' in sig.parameters, \ |
| 68 | + "custom_equation has wrong parameters in " + f[:-3] |
| 69 | + assert sig.parameters['x'].kind == inspect._POSITIONAL_ONLY, \ |
| 70 | + "custom_equation has wrong kind for x in " + f[:-3] |
| 71 | + assert sig.parameters['y'].kind == inspect._POSITIONAL_ONLY, \ |
| 72 | + "custom_equation has wrong kind for y in " + f[:-3] |
| 73 | + assert sig.parameters['a'].kind == inspect._POSITIONAL_OR_KEYWORD, \ |
| 74 | + "custom_equation has wrong kind for a in " + f[:-3] |
| 75 | + assert sig.parameters['b'].kind == inspect._POSITIONAL_OR_KEYWORD, \ |
| 76 | + "custom_equation has wrong kind for b in " + f[:-3] |
| 77 | + assert sig.parameters['c'].kind == inspect._KEYWORD_ONLY, \ |
| 78 | + "custom_equation has wrong kind for c in " + f[:-3] |
| 79 | + assert sig.parameters['x'].annotation == int, \ |
| 80 | + "custom_equation has wrong annotation for x in " + f[:-3] |
| 81 | + assert sig.parameters['y'].annotation == int, \ |
| 82 | + "custom_equation has wrong annotation for y in " + f[:-3] |
| 83 | + assert sig.parameters['a'].annotation == int, \ |
| 84 | + "custom_equation has wrong annotation for a in " + f[:-3] |
| 85 | + assert sig.parameters['b'].annotation == int, \ |
| 86 | + "custom_equation has wrong annotation for b in " + f[:-3] |
| 87 | + assert sig.parameters['c'].annotation == int, \ |
| 88 | + "custom_equation has wrong annotation for c in " + f[:-3] |
| 89 | + assert sig.parameters['x'].default == 0, \ |
| 90 | + "custom_equation has wrong default value for x in " + f[:-3] |
| 91 | + assert sig.parameters['y'].default == 0, \ |
| 92 | + "custom_equation has wrong default value for y in " + f[:-3] |
| 93 | + assert sig.parameters['a'].default == 1, \ |
| 94 | + "custom_equation has wrong default value for a in " + f[:-3] |
| 95 | + assert sig.parameters['b'].default == 1, \ |
| 96 | + "custom_equation has wrong default value for b in " + f[:-3] |
| 97 | + assert sig.parameters['c'].default == 1, \ |
| 98 | + "custom_equation has wrong default value for c in " + f[:-3] |
| 99 | + try: |
| 100 | + eval(f[:-3]).custom_equation(1.5, 2.5) |
| 101 | + except TypeError as e: |
| 102 | + assert str(e).count("must") > 0, \ |
| 103 | + "custom_equation has wrong raises in " + f[:-3] |
| 104 | + try: |
| 105 | + eval(f[:-3]).custom_equation(1, 2, c=1.5) |
| 106 | + except TypeError as e: |
| 107 | + assert str(e).count("must") > 0, \ |
| 108 | + "custom_equation has wrong raises in " + f[:-3] |
| 109 | + assert eval(f[:-3]).custom_equation(1, 2) == 3, \ |
| 110 | + "custom_equation is not working in " + f[:-3] |
| 111 | + assert eval(f[:-3]).custom_equation(1, 2, 3) == 3, \ |
| 112 | + "custom_equation is not working in " + f[:-3] |
| 113 | + assert eval(f[:-3]).custom_equation(1, 2, 3, 4) == 17, \ |
| 114 | + "custom_equation is not working in " + f[:-3] |
| 115 | + assert eval(f[:-3]).custom_equation(1, 2, 3, 4, c=5) == 3.4, \ |
| 116 | + "custom_equation is not working in " + f[:-3] |
| 117 | + assert eval(f[:-3]).custom_equation(1, 2, 3, c=5, b=6) == 13, \ |
| 118 | + "custom_equation is not working in " + f[:-3] |
| 119 | + |
| 120 | +def test_fn_w_counter(): |
| 121 | + for f in files: |
| 122 | + sig = inspect.signature(eval(f[:-3]).fn_w_counter) |
| 123 | + assert sig.return_annotation == (int, dict[str, int]), \ |
| 124 | + "fn_w_counter has wrong return annotation in " + f[:-3] |
| 125 | + # call the function multiple times |
| 126 | + for i in range(1, 10): |
| 127 | + assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \ |
| 128 | + "fn_w_counter is not working in " + f[:-3] |
| 129 | + for i in range(10, 97): |
| 130 | + assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \ |
| 131 | + "fn_w_counter is not working in " + f[:-3] |
| 132 | + for i in range(97, 100): |
| 133 | + assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \ |
| 134 | + "fn_w_counter is not working in " + f[:-3] |
0 commit comments