|
| 1 | +import ast |
| 2 | +import sys |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from python_minifier.rename import add_namespace, bind_names, resolve_names |
| 7 | +from python_minifier.ast_compare import compare_ast |
| 8 | +from python_minifier.transforms.remove_exception_brackets import remove_no_arg_exception_call |
| 9 | + |
| 10 | + |
| 11 | +def remove_brackets(source): |
| 12 | + module = ast.parse(source, 'remove_brackets') |
| 13 | + |
| 14 | + add_namespace(module) |
| 15 | + bind_names(module) |
| 16 | + resolve_names(module) |
| 17 | + return remove_no_arg_exception_call(module) |
| 18 | + |
| 19 | + |
| 20 | +def test_exception_brackets(): |
| 21 | + """This is a buitin so remove the brackets""" |
| 22 | + source = 'def a(): raise Exception()' |
| 23 | + expected = 'def a(): raise Exception' |
| 24 | + |
| 25 | + expected_ast = ast.parse(expected) |
| 26 | + actual_ast = remove_brackets(source) |
| 27 | + compare_ast(expected_ast, actual_ast) |
| 28 | + |
| 29 | +def test_zero_division_error_brackets(): |
| 30 | + """This is a buitin so remove the brackets""" |
| 31 | + source = 'def a(): raise ZeroDivisionError()' |
| 32 | + expected = 'def a(): raise ZeroDivisionError' |
| 33 | + |
| 34 | + expected_ast = ast.parse(expected) |
| 35 | + actual_ast = remove_brackets(source) |
| 36 | + compare_ast(expected_ast, actual_ast) |
| 37 | + |
| 38 | +def test_builtin_with_arg(): |
| 39 | + """This has an arg so dont' remove the brackets""" |
| 40 | + source = 'def a(): raise Exception(1)' |
| 41 | + expected = 'def a(): raise Exception(1)' |
| 42 | + |
| 43 | + expected_ast = ast.parse(expected) |
| 44 | + actual_ast = remove_brackets(source) |
| 45 | + compare_ast(expected_ast, actual_ast) |
| 46 | + |
| 47 | +def test_one_division_error_brackets(): |
| 48 | + """This is not a builtin so don't remove the brackets even though it's not defined in the module""" |
| 49 | + source = 'def a(): raise OneDivisionError()' |
| 50 | + expected = source |
| 51 | + |
| 52 | + expected_ast = ast.parse(expected) |
| 53 | + actual_ast = remove_brackets(source) |
| 54 | + compare_ast(expected_ast, actual_ast) |
| 55 | + |
| 56 | +def test_redefined(): |
| 57 | + """This is usually a builtin, but don't remove brackets if it's been redefined""" |
| 58 | + source = ''' |
| 59 | +def a(): |
| 60 | + raise ZeroDivisionError() |
| 61 | +def b(): |
| 62 | + ZeroDivisionError = blag |
| 63 | + raise ZeroDivisionError() |
| 64 | +''' |
| 65 | + expected = ''' |
| 66 | +def a(): |
| 67 | + raise ZeroDivisionError |
| 68 | +def b(): |
| 69 | + ZeroDivisionError = blag |
| 70 | + raise ZeroDivisionError() |
| 71 | +''' |
| 72 | + expected_ast = ast.parse(expected) |
| 73 | + actual_ast = remove_brackets(source) |
| 74 | + compare_ast(expected_ast, actual_ast) |
| 75 | + |
| 76 | +def test_raise_from(): |
| 77 | + """This is a builtin so remove the brackets""" |
| 78 | + if sys.version_info < (3, 0): |
| 79 | + pytest.skip('raise from not supported in this version of python') |
| 80 | + source = 'def a(): raise Exception() from Exception()' |
| 81 | + expected = 'def a(): raise Exception from Exception' |
| 82 | + |
| 83 | + expected_ast = ast.parse(expected) |
| 84 | + actual_ast = remove_brackets(source) |
| 85 | + compare_ast(expected_ast, actual_ast) |
| 86 | + |
| 87 | +def test_raise_from_only(): |
| 88 | + """This is a builtin so remove the brackets""" |
| 89 | + if sys.version_info < (3, 0): |
| 90 | + pytest.skip('raise from not supported in this version of python') |
| 91 | + |
| 92 | + source = 'def a(): raise Hello() from Exception()' |
| 93 | + expected = 'def a(): raise Hello() from Exception' |
| 94 | + |
| 95 | + expected_ast = ast.parse(expected) |
| 96 | + actual_ast = remove_brackets(source) |
| 97 | + compare_ast(expected_ast, actual_ast) |
| 98 | + |
| 99 | +def test_raise_from_arg(): |
| 100 | + """This is a builtin so remove the brackets""" |
| 101 | + if sys.version_info < (3, 0): |
| 102 | + pytest.skip('raise from not supported in this version of python') |
| 103 | + |
| 104 | + source = 'def a(): raise Hello() from Exception(1)' |
| 105 | + expected = 'def a(): raise Hello() from Exception(1)' |
| 106 | + |
| 107 | + expected_ast = ast.parse(expected) |
| 108 | + actual_ast = remove_brackets(source) |
| 109 | + compare_ast(expected_ast, actual_ast) |
0 commit comments