diff --git a/tests/test_dumper.py b/tests/test_dumper.py index 6b3091ee..5ab3c550 100644 --- a/tests/test_dumper.py +++ b/tests/test_dumper.py @@ -193,6 +193,10 @@ def test_try_finally(): def test_try_except(): + check_dumps("try: pass\n\nexcept:\n pass\n") + + +def test_try_except_named(): check_dumps("try : pass\nexcept Exception : pass\n") diff --git a/tests/test_grammator_control_structures.py b/tests/test_grammator_control_structures.py index 92e94f71..3d50c950 100644 --- a/tests/test_grammator_control_structures.py +++ b/tests/test_grammator_control_structures.py @@ -1706,3 +1706,144 @@ def test_try_except_as_stmt_indent(): ], } ]) + +def test_try_excepts_stmt_empty_same_line(): + """ + try: pass + except: + pass + """ + parse_multi([ + ('TRY', 'try'), + ('COLON', ':'), + ('PASS', 'pass', [('SPACE', ' ')]), + ('ENDL', '\n'), + ('EXCEPT', 'except'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ], [ + { + "type": "try", + "first_formatting": [], + "second_formatting": [], + "else": {}, + "finally": {}, + "excepts": [ + { + "type": "except", + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "fifth_formatting": [], + "delimiter": "", + "target": {}, + "exception": {}, + "value": [ + { + "type": "endl", + "formatting": [], + "value": "\n", + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ] + } + ], + "value": [ + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ], + } + ]) + +def test_try_excepts_stmt_empty_same_line_spaced(): + """ + try: pass + + except: + pass + """ + parse_multi([ + ('TRY', 'try'), + ('COLON', ':'), + ('PASS', 'pass', [('SPACE', ' ')]), + ('ENDL', '\n'), + ('ENDL', '\n'), + ('EXCEPT', 'except'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ], [ + { + "type": "try", + "first_formatting": [], + "second_formatting": [], + "else": {}, + "finally": {}, + "excepts": [ + { + "type": "except", + "first_formatting": [], + "second_formatting": [], + "third_formatting": [], + "fourth_formatting": [], + "fifth_formatting": [], + "delimiter": "", + "target": {}, + "exception": {}, + "value": [ + { + "type": "endl", + "formatting": [], + "value": "\n", + "indent": " " + }, + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ] + } + ], + "value": [ + { + "type": "pass", + }, + { + "indent": "", + "formatting": [], + "type": "endl", + "value": "\n" + } + ], + } + ]) + diff --git a/tests/test_indentation_marker.py b/tests/test_indentation_marker.py index 482cb505..623c895f 100644 --- a/tests/test_indentation_marker.py +++ b/tests/test_indentation_marker.py @@ -275,8 +275,8 @@ def test_trailing_spaces(): """ if a: if b: - - + + pass """ check([ @@ -337,3 +337,38 @@ def test_tab_and_spaces_because_some_people_are_horrible(): ('PASS', 'pass'), ('DEDENT', ''), ]) + + +def test_try_pass_on_same_line(): + """ + try: pass + + except: + pass + """ + check([ + ('TRY', 'try'), + ('COLON', ':', [], [('SPACE', ' ')]), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('ENDL', '\n'), + ('EXCEPT', 'except'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('PASS', 'pass'), + ('ENDL', '\n'), + ], [ + ('TRY', 'try'), + ('COLON', ':', [], [('SPACE', ' ')]), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('ENDL', '\n'), + ('EXCEPT', 'except'), + ('COLON', ':'), + ('ENDL', '\n', [], [('SPACE', ' ')]), + ('INDENT', ''), + ('PASS', 'pass'), + ('ENDL', '\n'), + ('DEDENT', ''), + ]) + diff --git a/tests/test_spliter.py b/tests/test_spliter.py index 34402aa0..6413e404 100644 --- a/tests/test_spliter.py +++ b/tests/test_spliter.py @@ -367,6 +367,10 @@ def test_backslash_in_comment(): def test_regression(): assert split("(r'[\"\\'](.|\n|\r)*[\"\\']', 'STRING'),") == ["(", "r", "'[\"\\'](.|\n|\r)*[\"\\']'", ",", " ", "'STRING'", ")", ","] + +def test_try_pass(): + assert split("try: pass\n\nexcept:\n pass") == ['try', ':', ' ', 'pass', '\n', '\n', 'except', ':', '\n', ' ', 'pass'] + # TODO: make this test pass in python3 also # requires to remove dependency on ast.py if python_version == 2: