def test_multiline_dictionary_literal(): assert get_flakes( '#py foo = {\n' ' "bar": "baz",\n' '}\n' '$foo\n' ) == ()
def test_indented_multiline_dictionary_literal(): assert get_flakes('#if True\n' ' #py foo = {\n' ' "bar": "baz",\n' ' }\n' ' $foo\n' '#end if\n') == ()
def test_unicode_literals(): assert get_flakes("$_(u'hi ☃')", ) == (( 1, 'P001', 'unicode literal prefix is unnecessary (assumed) in cheetah ' "templates: u'hi ☃'", ), )
def test_redefinition_of_unused_name_block_def(): assert get_flakes( '#block foo\n' '#end block\n' '#def foo()\n' '#end def\n') == ((3, 'F811', "redefinition of unused 'foo' from line 1"), )
def test_cannot_determine_line_number(): assert get_flakes( '$foo(\n' ' $bar == True,\n' ')') == (( 0, 'E712', "comparison to True should be 'if cond is True:' or 'if cond:'"), )
def test_context_manager(): assert get_flakes('#import contextlib\n' '#@contextlib.contextmanager\n' '#def foo()\n' ' before\n' ' #yield\n' ' after\n' '#end def\n') == ()
def test_comparison_to_true(): assert get_flakes('#if $foo == True: herp') == ( ( 1, "E712 comparison to True should be " "'if cond is True:' or 'if cond:'", ), )
def test_indents_with_tabs(): assert get_flakes( '#if True:\n' '\tHello world\n' '#end if\n', ) == ( (2, 'T003 Indentation contains tabs'), )
def test_indents_not_four_spaces(): assert get_flakes( '#if True:\n' ' Hello world\n' '#end if\n' ) == ( (2, 'T004 Indentation is not a multiple of 4'), )
def test_redefinition_of_unused_name(): assert get_flakes( '#import foo.bar\n' '#import foo.bar\n' '$foo.baz()' ) == ( (2, "F811 redefinition of unused 'foo' from line 1"), )
def test_oneline_directive_followed_by_directive(): assert get_flakes( '#if True\n' ' #def title()##end def#\n' ' #py foo = "bar"\n' ' $foo\n' '#end if\n' ) == ()
def test_list_comprehension_redefines_name(): assert get_flakes( '#set foo = $bar\n' '#for bar in [foo for foo in (1, 2, 3)]\n' ' $bar\n' '#end for\n' ) == ( (2, "F812 list comprehension redefines 'foo' from line 1"), )
def test_module_shadowed_by_loop_variable(): assert get_flakes( '#import foo\n' '\n' '$foo\n' '#for foo in (1, 2, 3)\n' ' $foo\n' '#end for\n') == (( 4, 'F402', "import 'foo' from line 1 shadowed by loop variable"), )
def test_star_import(): assert get_flakes('#from foo import *') == ( (1, 'F401', "'foo.*' imported but unused"), ( 1, 'F403', "'from foo import *' used; unable to detect undefined names", ), )
def test_redefinition_of_unused_name_block_def(): assert get_flakes( '#block foo\n' '#end block\n' '#def foo()\n' '#end def\n' ) == ( (3, "F811 redefinition of unused 'foo' from line 1"), )
def test_indented_multiline_dictionary_literal(): assert get_flakes( '#if True\n' ' #py foo = {\n' ' "bar": "baz",\n' ' }\n' ' $foo\n' '#end if\n' ) == ()
def test_module_imported_but_unused_lots_of_lines(): assert get_flakes( '#import foo\n' '#def womp()\n' ' $bar\n' '#end def\n' ) == ( (1, "F401 'foo' imported but unused"), )
def test_indented_thrice_multiline(): assert get_flakes('#def foo()\n' ' <div>\n' ' <div>\n' ' $bar(\n' ' $baz,\n' ' )\n' ' </div>\n' ' </div>\n' '#end def\n') == ()
def test_unicode_literals(): assert get_flakes( "$_(u'hi ☃')", ) == ( ( 1, 'P001 unicode literal prefix is unnecessary (assumed) in cheetah ' "templates: u'hi ☃'" ), )
def test_cannot_determine_line_number(): assert get_flakes( '#set foo = "bar"\n' '#def foo()\n' ' $herp\n' '#end def\n' ) == ( # The current reverse-parsing strategy doesn't find this assignment. (0, "F841 local variable 'foo' is assigned to but never used"), )
def test_context_manager(): assert get_flakes( '#import contextlib\n' '#@contextlib.contextmanager\n' '#def foo()\n' ' before\n' ' #yield\n' ' after\n' '#end def\n' ) == ()
def test_list_comprehension_redefines_name(): # python 3 makes this not an issue expected = (((1, 'F841', "local variable 'foo' is assigned to but never used"), ) if five.PY3 else ((2, 'F812', "list comprehension redefines 'foo' from line 1"), )) assert get_flakes('#py foo = $bar\n' '#for bar in [foo for foo in (1, 2, 3)]\n' ' $bar\n' '#end for\n') == expected
def test_module_shadowed_by_loop_variable(): assert get_flakes( '#import foo\n' '\n' '$foo\n' '#for foo in (1, 2, 3)\n' ' $foo\n' '#end for\n' ) == ( (4, "F402 import 'foo' from line 1 shadowed by loop variable"), )
def test_cannot_determine_line_number(): assert get_flakes( '$foo(\n' ' $bar == True,\n' ')' ) == ( ( 0, "E712 comparison to True should be " "'if cond is True:' or 'if cond:'" ), )
def test_indented_thrice_multiline(): assert get_flakes( '#def foo()\n' ' <div>\n' ' <div>\n' ' $bar(\n' ' $baz,\n' ' )\n' ' </div>\n' ' </div>\n' '#end def\n' ) == ()
def test_list_comprehension_redefines_name(): # python 3 makes this not an issue expected = ( ((1, "F841 local variable 'foo' is assigned to but never used"),) if five.PY3 else ((2, "F812 list comprehension redefines 'foo' from line 1"),) ) assert get_flakes( '#py foo = $bar\n' '#for bar in [foo for foo in (1, 2, 3)]\n' ' $bar\n' '#end for\n' ) == expected
def test_duplicated_key_name(): assert get_flakes( '#py x = 1\n' '#py y = {x: 1, x: 2}\n' '$y\n', ) == ( ( 2, 'F602', 'dictionary key variable x repeated with different values', ), ( 2, 'F602', 'dictionary key variable x repeated with different values', ), )
def test_comparison_to_true(): assert get_flakes('#if $foo == True: herp') == (( 1, 'E712', "comparison to True should be 'if cond is True:' or 'if cond:'", ), )
def test_compare_against_literal(): assert get_flakes('#py x = 5\n${x is 5}') == (( 2, 'F632', 'use ==/!= to compare str, bytes, and int literals'), )
def test_invalid_escape_sequence(): with pytest.warns(None): # in py3 the compilation of this causes a warning assert get_flakes(r'${"\q"}') == ((1, 'W605', r"invalid escape sequence '\q'"), )
def test_extends_cheetah_template(): assert get_flakes('#extends Cheetah.Template') == (( 1, 'T002', "'#extends Cheetah.Template' is assumed without '#extends'", ), )
def test_implements_respond_with_extend(): assert get_flakes('#extends foo\n#implements respond') == ()
def test_extends_something_else(): assert get_flakes('#extends foo') == ()
def test_syntaxerror(): assert get_flakes('#if foo = "bar": herp') == ( (1, "E901 SyntaxError: invalid syntax"), )
def test_identity(): assert get_flakes('#py bar = foo = None\n#if not $bar is $foo: herp') == ( (2, "E714 test for object identity should be 'is not'"), )
def test_implements_respond_with_extend(): assert get_flakes('#extends foo\n#implements respond') == ()
def test_unused_local_variable(): assert get_flakes('#py foo = $bar') == ( (1, "F841 local variable 'foo' is assigned to but never used"), )
def test_membership(): assert get_flakes('#py baz = foo = None\n#if not $baz in $foo: herp') == (( 2, 'E713', "test for membership should be 'not in'"), )
def test_compiler_settings(): assert get_flakes( '#compiler-settings#useLegacyImportMode = True#end compiler-settings#', ) == ()
def test_identity(): assert get_flakes('#py bar = foo = None\n#if not $bar is $foo: herp') == (( 2, 'E714', "test for object identity should be 'is not'"), )
def test_indents_not_four_spaces(): assert get_flakes( '#if True:\n' ' Hello world\n' '#end if\n') == ((2, 'T004', 'Indentation is not a multiple of 4'), )
def test_indents_with_tabs(): assert get_flakes('#if True:\n' '\tHello world\n' '#end if\n', ) == ((2, 'T003', 'Indentation contains tabs'), )
def test_comparison_to_none(): assert get_flakes('#if $foo == None: herp') == ( (1, "E711 comparison to None should be 'if cond is None:'"), )
def test_comparison_to_none(): assert get_flakes('#if $foo == None: herp') == (( 1, 'E711', "comparison to None should be 'if cond is None:'"), )
def test_membership(): assert get_flakes('#py baz = foo = None\n#if not $baz in $foo: herp') == ( (2, "E713 test for membership should be 'not in'"), )
def test_unused_local_variable(): assert get_flakes('#py foo = $bar') == (( 1, 'F841', "local variable 'foo' is assigned to but never used"), )
def test_has_key(): assert get_flakes('#if $foo.has_key($bar): herp') == ( (1, "W601 .has_key() is deprecated, use 'in'"), )
def test_extends_cheetah_template(): assert get_flakes('#extends Cheetah.Template') == ( (1, "T002 '#extends Cheetah.Template' is assumed without '#extends'"), )
def test_has_key(): assert get_flakes('#if $foo.has_key($bar): herp') == (( 1, 'W601', ".has_key() is deprecated, use 'in'"), )
def test_duplicated_key_const(): assert get_flakes('#py x = {True: 1, True: 2}\n$x') == ( (1, 'F601', 'dictionary key True repeated with different values'), (1, 'F601', 'dictionary key True repeated with different values'), )
def test_implements_respond_no_extend(): assert get_flakes('#implements respond') == ( (1, "T001 '#implements respond' is assumed without '#extends'"), )
def test_implements_respond_no_extend(): assert get_flakes('#implements respond') == (( 1, 'T001', "'#implements respond' is assumed without '#extends'"), )
def test_implements_non_respond(): assert get_flakes('#implements foo') == ()
def test_implements_non_respond(): assert get_flakes('#implements foo') == ()
def test_extends_something_else(): assert get_flakes('#extends foo') == ()
def test_star_import(): assert get_flakes('#from foo import *') == ( (1, "F401 'foo.*' imported but unused"), (1, "F403 'from foo import *' used; unable to detect undefined names"), )
def test_module_imported_but_unused_lots_of_lines(): assert get_flakes('#import foo\n' '#def womp()\n' ' $bar\n' '#end def\n') == ((1, 'F401', "'foo' imported but unused"), )
def test_redefinition_of_unused_name(): assert get_flakes( '#import foo.bar\n' '#import foo.bar\n' '$foo.baz()') == ((2, 'F811', "redefinition of unused 'foo' from line 1"), )
def test_compiler_settings(): assert get_flakes( '#compiler-settings#useLegacyImportMode = True#end compiler-settings#', ) == ()
def test_syntaxerror(): assert get_flakes('#if foo = "bar": herp') == (( 1, 'E999', "SyntaxError: invalid syntax"), )