|
3 | 3 | from contextlib import redirect_stdout |
4 | 4 |
|
5 | 5 | import pytest |
| 6 | +import full_match |
6 | 7 |
|
7 | | -from transfunctions import superfunction, sync_context, async_context, generator_context |
| 8 | +from transfunctions import superfunction, sync_context, async_context, generator_context, await_it, WrongDecoratorSyntaxError |
8 | 9 |
|
9 | 10 | """ |
10 | 11 | Что нужно проверить: |
@@ -153,3 +154,97 @@ def function(a, b, c=4, d=3): |
153 | 154 |
|
154 | 155 | with pytest.raises(ValueError): |
155 | 156 | ~function(2, 3, d=5) |
| 157 | + |
| 158 | + |
| 159 | +def test_return_value_from_async_simple_superfunction(): |
| 160 | + @superfunction |
| 161 | + def function(): |
| 162 | + return 1 |
| 163 | + |
| 164 | + assert run(function()) == 1 |
| 165 | + |
| 166 | + |
| 167 | +def test_return_awaited_value_from_async_simple_superfunction(): |
| 168 | + async def another_one(): |
| 169 | + return 1 |
| 170 | + |
| 171 | + @superfunction |
| 172 | + def function(): |
| 173 | + return await_it(another_one()) |
| 174 | + |
| 175 | + assert run(function()) == 1 |
| 176 | + |
| 177 | + |
| 178 | +def test_return_value_from_async_superfunction_with_arguments(): |
| 179 | + @superfunction |
| 180 | + def function(a, b=5, c=10): |
| 181 | + return a + b + c |
| 182 | + |
| 183 | + assert run(function(2, b=3)) == 15 |
| 184 | + |
| 185 | + |
| 186 | +def test_return_awaited_value_from_async_superfunction_with_arguments(): |
| 187 | + async def another_one(a, b, c): |
| 188 | + return a + b + c |
| 189 | + |
| 190 | + @superfunction |
| 191 | + def function(a, b=5, c=10): |
| 192 | + return await_it(another_one(a, b, c)) |
| 193 | + |
| 194 | + assert run(function(2, b=3)) == 15 |
| 195 | + |
| 196 | + |
| 197 | +def test_call_superfunction_with_tilda_multiple_times(): |
| 198 | + @superfunction |
| 199 | + def function(): |
| 200 | + return 4 |
| 201 | + |
| 202 | + assert ~function() == 4 |
| 203 | + assert ~function() == 4 |
| 204 | + assert ~function() == 4 |
| 205 | + |
| 206 | + |
| 207 | +def test_async_call_superfunction_multiple_times(): |
| 208 | + @superfunction |
| 209 | + def function(): |
| 210 | + return 4 |
| 211 | + |
| 212 | + assert run(function()) == 4 |
| 213 | + assert run(function()) == 4 |
| 214 | + assert run(function()) == 4 |
| 215 | + |
| 216 | + |
| 217 | +def test_generator_call_superfunction_multiple_times(): |
| 218 | + @superfunction |
| 219 | + def function(): |
| 220 | + yield 4 |
| 221 | + |
| 222 | + assert list(function()) == [4] |
| 223 | + assert list(function()) == [4] |
| 224 | + assert list(function()) == [4] |
| 225 | + |
| 226 | + |
| 227 | +def test_combine_with_other_decorator_before(): |
| 228 | + def other_decorator(function): |
| 229 | + return function |
| 230 | + |
| 231 | + @superfunction |
| 232 | + @other_decorator |
| 233 | + def template(): |
| 234 | + pass |
| 235 | + |
| 236 | + with pytest.raises(WrongDecoratorSyntaxError, match=full_match('The @superfunction decorator cannot be used in conjunction with other decorators.')): |
| 237 | + ~template() |
| 238 | + |
| 239 | + |
| 240 | +def test_combine_with_other_decorator_after(): |
| 241 | + def other_decorator(function): |
| 242 | + return function |
| 243 | + |
| 244 | + @other_decorator |
| 245 | + @superfunction |
| 246 | + def template(): |
| 247 | + pass |
| 248 | + |
| 249 | + with pytest.raises(WrongDecoratorSyntaxError, match=full_match('The @superfunction decorator cannot be used in conjunction with other decorators.')): |
| 250 | + ~template() |
0 commit comments