|
1 | 1 | import ast |
2 | 2 | from python_minifier import unparse |
3 | 3 | from python_minifier.ast_compare import compare_ast |
4 | | -from python_minifier.transforms.implicit_return_none import implicit_return_none |
| 4 | +from python_minifier.transforms.remove_explicit_return_none import RemoveExplicitReturnNone |
5 | 5 |
|
6 | 6 |
|
7 | 7 | def remove_return_none(source): |
8 | 8 | module = ast.parse(source, 'remove_return_none') |
9 | 9 |
|
10 | | - return implicit_return_none(module) |
| 10 | + return RemoveExplicitReturnNone()(module) |
11 | 11 |
|
12 | | -def test_remove_return_none(): |
| 12 | +def test_trailing_remove_return_none(): |
| 13 | + source = 'def a():a=4;return None' |
| 14 | + expected = 'def a():a=4' |
| 15 | + |
| 16 | + expected_ast = ast.parse(expected) |
| 17 | + actual_ast = remove_return_none(source) |
| 18 | + compare_ast(expected_ast, actual_ast) |
| 19 | + |
| 20 | + assert unparse(actual_ast) == expected |
| 21 | + |
| 22 | +def test_trailing_implicit_return_none(): |
| 23 | + source = 'def a():a=4;return' |
| 24 | + expected = 'def a():a=4' |
| 25 | + expected_ast = ast.parse(expected) |
| 26 | + actual_ast = remove_return_none(source) |
| 27 | + compare_ast(expected_ast, actual_ast) |
| 28 | + assert unparse(actual_ast) == expected |
| 29 | + |
| 30 | + |
| 31 | +def test_trailing_remove_return_none_empty_suite(): |
13 | 32 | source = 'def a():return None' |
14 | | - expected = 'def a():return' |
| 33 | + expected = 'def a():0' |
15 | 34 |
|
16 | 35 | expected_ast = ast.parse(expected) |
17 | 36 | actual_ast = remove_return_none(source) |
18 | 37 | compare_ast(expected_ast, actual_ast) |
19 | 38 |
|
20 | 39 | assert unparse(actual_ast) == expected |
21 | 40 |
|
22 | | -def test_implicit_return_none(): |
| 41 | +def test_trailing_implicit_return_none_empty_suite(): |
23 | 42 | source = 'def a():return' |
| 43 | + expected = 'def a():0' |
| 44 | + expected_ast = ast.parse(expected) |
| 45 | + actual_ast = remove_return_none(source) |
| 46 | + compare_ast(expected_ast, actual_ast) |
| 47 | + assert unparse(actual_ast) == expected |
| 48 | + |
| 49 | +def test_trailing_return_value_unchanged(): |
| 50 | + source = 'def a():return 0' |
24 | 51 | expected = source |
25 | 52 | expected_ast = ast.parse(expected) |
26 | 53 | actual_ast = remove_return_none(source) |
27 | 54 | compare_ast(expected_ast, actual_ast) |
28 | 55 | assert unparse(actual_ast) == expected |
29 | 56 |
|
| 57 | +def test_remove_return_none(): |
| 58 | + source = ''' |
| 59 | +def a(): |
| 60 | + if a: return None |
| 61 | + return None |
| 62 | +''' |
| 63 | + expected = 'def a():\n\tif a:return' |
| 64 | + |
| 65 | + expected_ast = ast.parse(expected) |
| 66 | + actual_ast = remove_return_none(source) |
| 67 | + compare_ast(expected_ast, actual_ast) |
| 68 | + |
| 69 | + assert unparse(actual_ast) == expected |
| 70 | + |
| 71 | +def test_implicit_return_none(): |
| 72 | + source = ''' |
| 73 | +def a(): |
| 74 | + if a: return |
| 75 | + return |
| 76 | +''' |
| 77 | + expected = 'def a():\n\tif a:return' |
| 78 | + expected_ast = ast.parse(expected) |
| 79 | + actual_ast = remove_return_none(source) |
| 80 | + compare_ast(expected_ast, actual_ast) |
| 81 | + assert unparse(actual_ast) == expected |
| 82 | + |
30 | 83 | def test_return_value_unchanged(): |
31 | | - source = 'def a():return 0' |
| 84 | + source = ''' |
| 85 | +def a(): |
| 86 | + if a: return 1 |
| 87 | + return 3 |
| 88 | +''' |
| 89 | + expected = 'def a():\n\tif a:return 1\n\treturn 3' |
| 90 | + expected_ast = ast.parse(expected) |
| 91 | + actual_ast = remove_return_none(source) |
| 92 | + compare_ast(expected_ast, actual_ast) |
| 93 | + assert unparse(actual_ast) == expected |
| 94 | + |
| 95 | +def test_async_trailing_remove_return_none(): |
| 96 | + source = 'async def a():a=4;return None' |
| 97 | + expected = 'async def a():a=4' |
| 98 | + |
| 99 | + expected_ast = ast.parse(expected) |
| 100 | + actual_ast = remove_return_none(source) |
| 101 | + compare_ast(expected_ast, actual_ast) |
| 102 | + |
| 103 | + assert unparse(actual_ast) == expected |
| 104 | + |
| 105 | +def test_async_trailing_implicit_return_none(): |
| 106 | + source = 'async def a():a=4;return' |
| 107 | + expected = 'async def a():a=4' |
| 108 | + expected_ast = ast.parse(expected) |
| 109 | + actual_ast = remove_return_none(source) |
| 110 | + compare_ast(expected_ast, actual_ast) |
| 111 | + assert unparse(actual_ast) == expected |
| 112 | + |
| 113 | + |
| 114 | +def test_async_trailing_remove_return_none_empty_suite(): |
| 115 | + source = 'async def a():return None' |
| 116 | + expected = 'async def a():0' |
| 117 | + |
| 118 | + expected_ast = ast.parse(expected) |
| 119 | + actual_ast = remove_return_none(source) |
| 120 | + compare_ast(expected_ast, actual_ast) |
| 121 | + |
| 122 | + assert unparse(actual_ast) == expected |
| 123 | + |
| 124 | +def test_async_trailing_implicit_return_none_empty_suite(): |
| 125 | + source = 'async def a():return' |
| 126 | + expected = 'async def a():0' |
| 127 | + expected_ast = ast.parse(expected) |
| 128 | + actual_ast = remove_return_none(source) |
| 129 | + compare_ast(expected_ast, actual_ast) |
| 130 | + assert unparse(actual_ast) == expected |
| 131 | + |
| 132 | +def test_async_trailing_return_value_unchanged(): |
| 133 | + source = 'async def a():return 0' |
32 | 134 | expected = source |
33 | 135 | expected_ast = ast.parse(expected) |
34 | 136 | actual_ast = remove_return_none(source) |
35 | 137 | compare_ast(expected_ast, actual_ast) |
36 | 138 | assert unparse(actual_ast) == expected |
37 | 139 |
|
| 140 | +def test_async_remove_return_none(): |
| 141 | + source = ''' |
| 142 | +async def a(): |
| 143 | + if a: return None |
| 144 | + return None |
| 145 | +''' |
| 146 | + expected = 'async def a():\n\tif a:return' |
| 147 | + |
| 148 | + expected_ast = ast.parse(expected) |
| 149 | + actual_ast = remove_return_none(source) |
| 150 | + compare_ast(expected_ast, actual_ast) |
| 151 | + |
| 152 | + assert unparse(actual_ast) == expected |
| 153 | + |
| 154 | +def test_async_implicit_return_none(): |
| 155 | + source = ''' |
| 156 | +async def a(): |
| 157 | + if a: return |
| 158 | + return |
| 159 | +''' |
| 160 | + expected = 'async def a():\n\tif a:return' |
| 161 | + expected_ast = ast.parse(expected) |
| 162 | + actual_ast = remove_return_none(source) |
| 163 | + compare_ast(expected_ast, actual_ast) |
| 164 | + assert unparse(actual_ast) == expected |
| 165 | + |
| 166 | +def test_async_return_value_unchanged(): |
| 167 | + source = ''' |
| 168 | +async def a(): |
| 169 | + if a: return 1 |
| 170 | + return 3 |
| 171 | +''' |
| 172 | + expected = 'async def a():\n\tif a:return 1\n\treturn 3' |
| 173 | + expected_ast = ast.parse(expected) |
| 174 | + actual_ast = remove_return_none(source) |
| 175 | + compare_ast(expected_ast, actual_ast) |
| 176 | + assert unparse(actual_ast) == expected |
0 commit comments