def test_no_change(): patterns = get_patterns(dbl_quote_docstring) text = """ def derp(): '''inside\"\"\"str''' """ assert process(patterns, text) == (text)
def test_nl_at_eof(): patterns = get_patterns(nl_at_eof) text = """ herp derp""" assert process(patterns, text) == (""" herp derp """)
def test_get_patterns(): # patterns = [[(grammar, replace), (HEADER, extra_replace)]] patterns = get_patterns(attribute_to_function) [patternset] = patterns [pattern1, pattern2] = patternset (grammar1, replace1) = pattern1 (grammar2, replace2) = pattern2 assert grammar2 is HEADER
def test_file_without_unicode_literals(): patterns = get_patterns(remove_needless_u_specifier) text = """ from __future__ import print_statement, absolute_import derp herp u"some string" herp derp """ assert process(patterns, text) == text
def test_function_used(): patterns = get_patterns(remove_unused_import) text = """ from function_lives_here import function derp herp function(args) herp derp """ assert process(patterns, text) == text
def test_match_method_but_not_import(): patterns = get_patterns(method_to_function) text = """ from function_lives_here import function obj.method().derp """ assert process(patterns, text) == (""" from function_lives_here import function function(obj).derp """)
def test_exec_function(): patterns = get_patterns(exec_function) text = """ exec stra in globals() exec strb in globals(), locals() """ assert process(patterns, text) == (""" exec(stra, globals()) exec(strb, globals(), locals()) """)
def test_dbl_quote_docstring(): patterns = get_patterns(dbl_quote_docstring) text = """ def derp(): '''doc''' """ assert process(patterns, text) == (''' def derp(): """doc""" ''')
def test_file_with_unicode_literals(): patterns = get_patterns(remove_needless_u_specifier) text = """ from __future__ import unicode_literals derp herp u"some string" herp derp """ assert process(patterns, text) == (""" from __future__ import unicode_literals derp herp "some string" herp derp """)
def test_attribute_to_function(): patterns = get_patterns(attribute_to_function) text = """ obj.attribute h(obj.a.b(c).d[e].attribute) """ assert process(patterns, text) == ("""from function_lives_here import function function(obj) h(function(obj.a.b(c).d[e])) """)
def test_ignore_u_at_end_of_string(): patterns = get_patterns(remove_needless_u_specifier) text = """ from __future__ import unicode_literals subprocess.check_call(('mysql', '-u', 'test_user',)) TITLE_LABELS = { "en": ("Subject", ), "ru": ("Тема", ), "fr": ("Objet", "Sujet"), } """ assert process(patterns, text) == text
def test_hex_to_bitshift(): patterns = get_patterns(hex_to_bitshift) text = """ 0x001 0x002 0x003 0x004 """ assert process(patterns, text) == (""" 1 << 0 1 << 1 0x003 1 << 2 """)
def test_sqla_count(): patterns = get_patterns(sqla_count) text = """ session.query( derp, ).filter( herp, ).count() """ assert process(patterns, text) == ( """import sqlalchemy session.query( sqlalchemy.func.count(), ).filter( herp, ).scalar() """)
def test_no_match(): patterns = get_patterns(attribute_to_function) text = """ derp.herp """ assert process(patterns, text) == text
def test_contextlib_nested(): patterns = get_patterns(contextlib_nested) text = """ import mock import contextlib from contextlib import nested with contextlib.nested( mock.patch( 'os.path.join'), mock.patch('os.mkdir'), mock.patch('os.chdir'), ): pass with nested( mock.patch( 'os.path.join' ), mock.patch('os.mkdir'), mock.patch('os.chdir'), ): pass def a_function(): with contextlib.nested( mock.patch( 'lots of stuff', 'in this mock', return_value=None ), mock.patch( 'reformatting really stinks' ), ): return None with contextlib.nested(a, b, c) as (x, y, z): pass """ assert process(patterns, text) == (""" import mock import contextlib from contextlib import nested with mock.patch( 'os.path.join'), \\ mock.patch('os.mkdir'), \\ mock.patch('os.chdir'): pass with mock.patch( 'os.path.join' ), \\ mock.patch('os.mkdir'), \\ mock.patch('os.chdir'): pass def a_function(): with mock.patch( 'lots of stuff', 'in this mock', return_value=None ), \\ mock.patch( 'reformatting really stinks' ): return None with a as x, \\ b as y, \\ c as z: pass """)
def test_no_import(): patterns = get_patterns(remove_unused_import) text = """ derp herp function(args) herp derp """ assert process(patterns, text) == text