def test_correct_multiline_conditions( assert_errors, parse_ast_tree, code, default_options, mode, ): """Testing multiline conditions.""" tree = parse_ast_tree(mode(code)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_not_simplifiable_elif( assert_errors, parse_ast_tree, comparators, default_options, ): """Statements with elif are not simplifiable.""" tree = parse_ast_tree(elif_statement.format(*comparators)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [UselessReturningElseViolation])
def test_complex_else( assert_errors, parse_ast_tree, comparators, default_options, ): """This if is not simplifiable, although the else is useless.""" tree = parse_ast_tree(complex_else.format(*comparators)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [UselessReturningElseViolation])
def test_not_simplifiable_only_if( assert_errors, parse_ast_tree, comparators, default_options, ): """Statements with only if and empty parent are not simplifiable.""" tree = parse_ast_tree(only_if.format(*comparators)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_correctly_negated_complex_conditions( code, assert_errors, parse_ast_tree, default_options, ): """Testing correctly negated complex conditions.""" tree = parse_ast_tree(complex_conditions.format(code)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_regression_for_else( assert_errors, default_options, parse_ast_tree, code, ): """Tests for nested ``if`` statement in ``for else`` statement.""" tree = parse_ast_tree(code) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_wrong_negated_complex_conditions( code, assert_errors, parse_ast_tree, default_options, ): """Testing complex conditions with nagated ``if`` condition.""" tree = parse_ast_tree(complex_conditions.format(code)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [NegatedConditionsViolation])
def test_useless_len_call( assert_errors, parse_ast_tree, code, default_options, ): """Testing that incorrect code raises a violation.""" tree = parse_ast_tree(code) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [UselessLenCompareViolation])
def test_negated_simple_conditions( code, assert_errors, parse_ast_tree, default_options, ): """Testing simple conditions.""" tree = parse_ast_tree(simple_conditions.format(code)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_negated_complex_elif_conditions( code, assert_errors, parse_ast_tree, default_options, ): """Testing complex conditions without else expression.""" tree = parse_ast_tree(complex_elif_conditions.format(code)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_useful_len_call( assert_errors, parse_ast_tree, code, default_options, ): """Testing that correct code works.""" tree = parse_ast_tree(code) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_else_that_can_be_removed_in_module( assert_errors, parse_ast_tree, code, default_options, ): """Testing that extra ``else`` blocks can be removed.""" tree = parse_ast_tree(module_level_condition.format(code)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [UselessReturningElseViolation])
def test_simplifiable_early_returning_if( assert_errors, parse_ast_tree, comparators, code, default_options, ): """These early returning ifs are simplifiable.""" tree = parse_ast_tree(code.format(*comparators)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [SimplifiableReturningIfViolation])
def test_else_that_can_not_be_removed( assert_errors, parse_ast_tree, code, default_options, mode, ): """Testing that extra ``else`` blocks cannot be removed.""" tree = parse_ast_tree(mode(code)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_complex_early_returning_if_inside( assert_errors, parse_ast_tree, template, comparators, default_options, ): """These more complex early returning ifs can not be simplified.""" tree = parse_ast_tree(template.format(*comparators)) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_else_that_can_be_removed_and_complex_if( assert_errors, parse_ast_tree, code, template, default_options, mode, ): """Testing that extra ``else`` blocks can be removed.""" tree = parse_ast_tree(mode(template.format(code, code))) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [UselessReturningElseViolation])
def test_else_can_be_removed_and_simplifiable_if( assert_errors, parse_ast_tree, code, template, default_options, mode, ): """Extra ``else`` blocks can be removed, plus the ``if`` is simplifiable.""" tree = parse_ast_tree(mode(template.format(code, code))) visitor = IfStatementVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [ UselessReturningElseViolation, SimplifiableReturningIfViolation, ])