def test_perform_one_indexed_mutation(): assert mutate( Context(source='1+1', mutation_id=MutationID(line='1+1', index=0, line_number=0))) == ('2+1', 1) assert mutate( Context(source='1+1', mutation_id=MutationID('1+1', 1, line_number=0))) == ('1-1', 1) assert mutate( Context(source='1+1', mutation_id=MutationID('1+1', 2, line_number=0))) == ('1+2', 1)
def test_function(): source = "def capitalize(s):\n return s[0].upper() + s[1:] if s else s\n" assert mutate( Context(source=source, mutate_id=(source.split('\n')[1], 0)) ) == ("def capitalize(s):\n return s[1].upper() + s[1:] if s else s\n", 1) assert mutate( Context(source=source, mutate_id=(source.split('\n')[1], 1)) ) == ("def capitalize(s):\n return s[0].upper() - s[1:] if s else s\n", 1) assert mutate( Context(source=source, mutate_id=(source.split('\n')[1], 2)) ) == ("def capitalize(s):\n return s[0].upper() + s[2:] if s else s\n", 1)
def test_mutate_body_of_function_with_return_type_annotation(): source = """ def foo() -> int: return 0 """ assert mutate(Context(source=source, mutation_id=ALL))[0] == source.replace('0', '1')
def test_basic_mutations(original, expected): actual, number_of_performed_mutations = mutate( Context(source=original, mutation_id=ALL, dict_synonyms=['Struct', 'FooBarDict'])) assert actual == expected, 'Performed {} mutations for original "{}"'.format( number_of_performed_mutations, original)
def test_mutate_dict(): source = "dict(a=b, c=d)" assert mutate( Context(source=source, mutation_id=MutationID(source, 1, line_number=0))) == ("dict(a=b, cXX=d)", 1)
def test_mutate_dict2(): source = "dict(a=b, c=d, e=f, g=h)" assert mutate( Context(source=source, mutation_id=MutationID( source, 3, line_number=0))) == ("dict(a=b, c=d, e=f, gXX=h)", 1)
def get_unified_diff(argument, dict_synonyms, update_cache=True, source=None): filename, mutation_id = filename_and_mutation_id_from_pk(argument) if update_cache: update_line_numbers(filename) if source is None: with open(filename) as f: source = f.read() context = Context( source=source, filename=filename, mutation_id=mutation_id, dict_synonyms=dict_synonyms, ) mutated_source, number_of_mutations_performed = mutate(context) if not number_of_mutations_performed: return "" output = "" for line in unified_diff(source.split('\n'), mutated_source.split('\n'), fromfile=filename, tofile=filename, lineterm=''): output += line + "\n" return output
def test_multiline_dunder_whitelist(): source = """ __all__ = [ 1, 2, ] """ assert mutate(Context(source=source)) == (source, 0)
def test_simple_apply(): CliRunner().invoke(main, [ 'foo.py', '--apply', '--mutation', mutation_id_separator.join([file_to_mutate_lines[0], '0']) ]) with open('foo.py') as f: assert f.read() == mutate( Context(source=file_to_mutate_contents, mutate_id=(file_to_mutate_lines[0], 0)))[0]
def test_function_with_annotation(): source = "def capitalize(s : str):\n return s[0].upper() + s[1:] if s else s\n" assert mutate( Context(source=source, mutation_id=MutationID(source.split('\n')[1], 0, line_number=1)) ) == ( "def capitalize(s : str):\n return s[1].upper() + s[1:] if s else s\n", 1)
def test_bug_github_issue_162(): source = """ primes: List[int] = [] foo = 'bar' """ assert mutate( Context(source=source, mutation_id=RelativeMutationID("foo = 'bar'", 0, 2))) == (source.replace( "'bar'", "'XXbarXX'"), 1)
def test_function(): source = "def capitalize(s):\n return s[0].upper() + s[1:] if s else s\n" assert mutate( Context(source=source, mutation_id=RelativeMutationID(source.split('\n')[1], 0, line_number=1)) ) == ("def capitalize(s):\n return s[1].upper() + s[1:] if s else s\n", 1) assert mutate( Context(source=source, mutation_id=RelativeMutationID(source.split('\n')[1], 1, line_number=1)) ) == ("def capitalize(s):\n return s[0].upper() - s[1:] if s else s\n", 1) assert mutate( Context(source=source, mutation_id=RelativeMutationID(source.split('\n')[1], 2, line_number=1)) ) == ("def capitalize(s):\n return s[0].upper() + s[2:] if s else s\n", 1)
def test_mutate_decorator(): source = """@foo\ndef foo():\n pass\n""" assert mutate(Context(source=source, mutation_id=ALL)) == (source.replace('@foo', ''), 1)
def test_pragma_no_mutate_and_no_cover(): source = """def foo():\n return 1+1 # pragma: no cover, no mutate\n""" assert mutate(Context(source=source, mutation_id=ALL)) == (source, 0)
def test_mutate_dict2(): source = "dict(a=b, c=d, e=f, g=h)" assert mutate(Context(source=source, mutate_id=(source, 3))) == ("dict(a=b, c=d, e=f, gXX=h)", 1)
def test_bug_github_issue_30(): source = """ def from_checker(cls: Type['BaseVisitor'], checker) -> 'BaseVisitor': pass """ assert mutate(Context(source=source)) == (source, 0)
def test_bug_github_issue_19(): source = """key = lambda a: "foo" filters = dict((key(field), False) for field in fields)""" mutate(Context(source=source))
def test_mutate_all(): assert mutate('def foo():\n return 1+1', ALL) == ('def foo():\n return 2-2\n', 3)
def test_pragma_no_mutate(): source = """def foo():\n return 1+1 # pragma: no mutate\n""" assert mutate(source, ALL) == (source, 0)
def test_performed_mutation_ids(): source = "dict(a=b, c=d)" context = Context(source=source) mutate(context) # we found two mutation points: mutate "a" and "c" assert context.performed_mutation_ids == [(source, 0), (source, 1)]
def test_mutate_dict(): source = "dict(a=b, c=d)" assert mutate(Context(source=source, mutate_id=(source, 1))) == ("dict(a=b, cXX=d)", 1)
def test_mutate_decorator(): source = """@foo\ndef foo():\n pass\n""" assert mutate(source, ALL) == (source.replace('@foo', ''), 1)
def test_mutate_both(): source = 'a = b + c' mutations = list_mutations(Context(source=source)) assert len(mutations) == 2 assert mutate(Context(source=source, mutate_id=mutations[0])) == ('a = b - c', 1) assert mutate(Context(source=source, mutate_id=mutations[1])) == ('a = None', 1)
def test_basic_mutations_python36(original, expected): actual = mutate( Context(source=original, mutation_id=ALL, dict_synonyms=['Struct', 'FooBarDict']))[0] assert actual == expected
def test_syntax_error(): with pytest.raises(Exception): mutate(Context(source=':!'))
def test_do_not_mutate_python3(source): actual = mutate( Context(source=source, mutation_id=ALL, dict_synonyms=['Struct', 'FooBarDict']))[0] assert actual == source
def test_bug_github_issue_26(): source = """ class ConfigurationOptions(Protocol): min_name_length: int """ mutate(Context(source=source))
def test_mutate_all(): assert mutate(Context(source='def foo():\n return 1+1', mutation_id=ALL)) == ('def foo():\n return 2-2', 3)
def test_perform_one_indexed_mutation(): assert mutate(Context(source='1+1', mutate_id=('1+1', 0))) == ('2+1', 1) assert mutate(Context(source='1+1', mutate_id=('1+1', 1))) == ('1-1', 1) assert mutate(Context(source='1+1', mutate_id=('1+1', 2))) == ('1+2', 1)
def test_context_exclude_line(): source = "__import__('pkg_resources').declare_namespace(__name__)\n" assert mutate(Context(source=source)) == (source, 0) source = "__all__ = ['hi']\n" assert mutate(Context(source=source)) == (source, 0)