Skip to content

Commit 7a2da6b

Browse files
Евгений БлиновЕвгений Блинов
authored andcommitted
new tests
1 parent 6eb24a4 commit 7a2da6b

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

tests/units/test_decorator.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
1616
1 фаза:
1717
18-
8. Работают замыкания.
19-
9. Работают чтение глобальных переменных.
18+
9. Работает чтение глобальных переменных.
2019
10. Работает директива nonlocal.
2120
11. Работает директива global.
2221
12. Декоратор @transfunction можно использовать на методах (в т.ч. асинк и генераторных).
@@ -45,6 +44,7 @@
4544
13. В декоратор @transfunction нельзя скормить лямбду или число.
4645
17. Нельзя вызывать трансформер напрямую. При попытке это сделать вылетает информативное исключение, причем как при передаче аргументов, так и нет.
4746
7. Декоратор @transfunction нельзя использовать дважды на одной функции.
47+
8. Работает чтение из замыканий (в том числе для функций с аргументами).
4848
"""
4949

5050
@transfunction
@@ -328,3 +328,71 @@ def make():
328328
function = make.get_usual_function()
329329

330330
assert function() == 1
331+
332+
333+
def test_read_closures_with_usual_function_with_arguments():
334+
nonlocal_variable = 1
335+
336+
@transfunction
337+
def make(some_number):
338+
#nonlocal nonlocal_variable
339+
return nonlocal_variable + some_number
340+
341+
function = make.get_usual_function()
342+
343+
assert function(1) == 2
344+
assert function(2) == 3
345+
346+
347+
def test_read_closures_with_async_function():
348+
nonlocal_variable = 1
349+
350+
@transfunction
351+
def make():
352+
#nonlocal nonlocal_variable
353+
return nonlocal_variable
354+
355+
function = make.get_async_function()
356+
357+
assert run(function()) == 1
358+
359+
360+
def test_read_closures_with_async_function_with_arguments():
361+
nonlocal_variable = 1
362+
363+
@transfunction
364+
def make(some_number):
365+
#nonlocal nonlocal_variable
366+
return nonlocal_variable + some_number
367+
368+
function = make.get_async_function()
369+
370+
assert run(function(1)) == 2
371+
assert run(function(2)) == 3
372+
373+
374+
def test_read_closures_with_generator_function():
375+
nonlocal_variable = 1
376+
377+
@transfunction
378+
def make():
379+
#nonlocal nonlocal_variable
380+
yield nonlocal_variable
381+
382+
function = make.get_generator_function()
383+
384+
assert list(function()) == [1]
385+
386+
387+
def test_read_closures_with_generator_function_with_arguments():
388+
nonlocal_variable = 1
389+
390+
@transfunction
391+
def make(some_number):
392+
#nonlocal nonlocal_variable
393+
yield nonlocal_variable + some_number
394+
395+
function = make.get_generator_function()
396+
397+
assert list(function(1)) == [2]
398+
assert list(function(2)) == [3]

transfunctions/transformer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ def visit_FunctionDef(self, node: Expr) -> Optional[Union[AST, List[AST]]]:
131131
RewriteContexts().visit(tree)
132132
DeleteDecorator().visit(tree)
133133

134-
135-
136134
if addictional_transformers is not None:
137135
for addictional_transformer in addictional_transformers:
138136
addictional_transformer.visit(tree)

0 commit comments

Comments
 (0)