|
| 1 | +# |
| 2 | +# Monic Framework |
| 3 | +# |
| 4 | +# Copyright (c) 2024-2025 Cognica, Inc. |
| 5 | +# |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from monic.expressions import ExpressionsParser, ExpressionsInterpreter |
| 10 | + |
| 11 | + |
| 12 | +def test_awaitable_value(): |
| 13 | + """Test that AwaitableValue.__await__ works correctly.""" |
| 14 | + # Create a simple async function that returns a value |
| 15 | + code = """ |
| 16 | +async def test_func(): |
| 17 | + return 42 |
| 18 | +
|
| 19 | +await test_func() |
| 20 | +""" |
| 21 | + |
| 22 | + parser = ExpressionsParser() |
| 23 | + interpreter = ExpressionsInterpreter() |
| 24 | + |
| 25 | + # Execute the code |
| 26 | + result = interpreter.execute(parser.parse(code)) |
| 27 | + |
| 28 | + # The result should be 42 |
| 29 | + assert result == 42 |
| 30 | + |
| 31 | + |
| 32 | +def test_awaitable_value_with_error(): |
| 33 | + """Test that AwaitableValue.__await__ handles errors correctly.""" |
| 34 | + # Create an async function that raises an error |
| 35 | + code = """ |
| 36 | +async def test_func(): |
| 37 | + raise ValueError("test error") |
| 38 | +
|
| 39 | +await test_func() |
| 40 | +""" |
| 41 | + |
| 42 | + parser = ExpressionsParser() |
| 43 | + interpreter = ExpressionsInterpreter() |
| 44 | + |
| 45 | + # The execution should raise ValueError |
| 46 | + with pytest.raises(ValueError, match="test error"): |
| 47 | + interpreter.execute(parser.parse(code)) |
| 48 | + |
| 49 | + |
| 50 | +def test_awaitable_value_return(): |
| 51 | + """Test that AwaitableValue.__await__ returns the correct value.""" |
| 52 | + # Create an async function that returns a value through StopIteration |
| 53 | + code = """ |
| 54 | +async def test_func(): |
| 55 | + # This will make __await__ return the value through StopIteration |
| 56 | + return await test_func() if False else 42 |
| 57 | +
|
| 58 | +await test_func() |
| 59 | +""" |
| 60 | + |
| 61 | + parser = ExpressionsParser() |
| 62 | + interpreter = ExpressionsInterpreter() |
| 63 | + |
| 64 | + # Execute the code |
| 65 | + result = interpreter.execute(parser.parse(code)) |
| 66 | + |
| 67 | + # The result should be 42 |
| 68 | + assert result == 42 |
| 69 | + |
| 70 | + |
| 71 | +def test_awaitable_value_direct_return(): |
| 72 | + """Test that AwaitableValue.__await__ directly returns its value.""" |
| 73 | + # Create an async function that returns a value directly |
| 74 | + code = """ |
| 75 | +async def test_func(): |
| 76 | + # This will make __await__ directly return its value |
| 77 | + return 42 |
| 78 | +
|
| 79 | +# Call the function multiple times to ensure the value is returned correctly |
| 80 | +result1 = await test_func() |
| 81 | +result2 = await test_func() |
| 82 | +result1 + result2 # This should be 84 |
| 83 | +""" |
| 84 | + |
| 85 | + parser = ExpressionsParser() |
| 86 | + interpreter = ExpressionsInterpreter() |
| 87 | + |
| 88 | + # Execute the code |
| 89 | + result = interpreter.execute(parser.parse(code)) |
| 90 | + |
| 91 | + # The result should be 84 (42 + 42) |
| 92 | + assert result == 84 |
| 93 | + |
| 94 | + |
| 95 | +def test_awaitable_value_direct(): |
| 96 | + """ |
| 97 | + Test that AwaitableValue.__await__ directly returns its value by creating |
| 98 | + an awaitable value directly. |
| 99 | + """ |
| 100 | + code = """ |
| 101 | +async def create_awaitable(): |
| 102 | + # This will create an awaitable value that will be awaited directly |
| 103 | + return 42 |
| 104 | +
|
| 105 | +# Get the awaitable value |
| 106 | +awaitable = create_awaitable() |
| 107 | +# Await it directly |
| 108 | +result1 = await awaitable |
| 109 | +# Await it again to ensure it works multiple times |
| 110 | +result2 = await awaitable |
| 111 | +result1 + result2 # This should be 84 |
| 112 | +""" |
| 113 | + |
| 114 | + parser = ExpressionsParser() |
| 115 | + interpreter = ExpressionsInterpreter() |
| 116 | + |
| 117 | + # Execute the code |
| 118 | + result = interpreter.execute(parser.parse(code)) |
| 119 | + |
| 120 | + # The result should be 84 (42 + 42) |
| 121 | + assert result == 84 |
| 122 | + |
| 123 | + |
| 124 | +def test_awaitable_value_complex(): |
| 125 | + """ |
| 126 | + Test that AwaitableValue.__await__ returns its value in a more complex |
| 127 | + scenario. |
| 128 | + """ |
| 129 | + code = """ |
| 130 | +async def nested_awaitable(): |
| 131 | + # This will create a chain of awaitable values |
| 132 | + async def inner(): |
| 133 | + return 42 |
| 134 | +
|
| 135 | + # First await will yield the value |
| 136 | + value = await inner() |
| 137 | + # Second await will return the value |
| 138 | + return await inner() |
| 139 | +
|
| 140 | +# Execute the nested awaitable |
| 141 | +result = await nested_awaitable() |
| 142 | +result # This should be 42 |
| 143 | +""" |
| 144 | + |
| 145 | + parser = ExpressionsParser() |
| 146 | + interpreter = ExpressionsInterpreter() |
| 147 | + |
| 148 | + # Execute the code |
| 149 | + result = interpreter.execute(parser.parse(code)) |
| 150 | + |
| 151 | + # The result should be 42 |
| 152 | + assert result == 42 |
0 commit comments