コード例 #1
0
def test_bare_raise_inside_generators_yield_from(
    assert_errors,
    parse_ast_tree,
    code,
    statement,
    default_options,
):
    """Testing that bare `raise` is allowed inside generators."""
    tree = parse_ast_tree(code.format(statement, ''))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #2
0
def test_correct_function_defaults(
    assert_errors,
    parse_ast_tree,
    default_options,
    code,
    mode,
):
    """Testing that correct function defaults passes validation."""
    tree = parse_ast_tree(mode(function_with_defaults.format(code)))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #3
0
def test_wrong_function_defaults(
    assert_errors,
    parse_ast_tree,
    default_options,
    code,
    mode,
):
    """Testing that wrong function defaults are forbidden."""
    tree = parse_ast_tree(mode(function_with_defaults.format(code)))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [ComplexDefaultValuesViolation])
コード例 #4
0
def test_wrong_super_call(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
    mode,
):
    """Testing that naming and using variables have limitations."""
    tree = parse_ast_tree(mode(code))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [UnusedVariableIsUsedViolation])
コード例 #5
0
def test_correct_variables(
    assert_errors,
    parse_ast_tree,
    default_options,
    code,
    mode,
):
    """Testing that correct usage of variables is allowed."""
    tree = parse_ast_tree(mode(code))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #6
0
def test_method_decorators_incorrect(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
    mode,
):
    """Testing that too large amount of decorators works."""
    tree = parse_ast_tree(mode(code))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [WrongDescriptorDecoratorViolation])
コード例 #7
0
def test_stop_iteration_in_generators_yield_from(
    assert_errors,
    parse_ast_tree,
    code,
    statement,
    exception,
    default_options,
):
    """Testing that `raise StopIteration` is restricted inside generators."""
    tree = parse_ast_tree(code.format(statement, exception))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [StopIterationInsideGeneratorViolation])
コード例 #8
0
def test_other_exc_in_generators_yield_from(
    assert_errors,
    parse_ast_tree,
    code,
    statement,
    exception,
    default_options,
):
    """Testing that `raise` of other exceptions is allowed inside generators."""
    tree = parse_ast_tree(code.format(statement, exception))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #9
0
def test_stop_iteration_inside_bare_functions(
    assert_errors,
    parse_ast_tree,
    code,
    statement,
    exception,
    default_options,
    mode,
):
    """Testing that `raise StopIteration` is allowed inside bare functions."""
    tree = parse_ast_tree(mode(code.format(statement, exception)))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #10
0
def test_double_wrong_variables(
    assert_errors,
    parse_ast_tree,
    default_options,
    mode,
):
    """Testing that it is possible to have two violations with wrong vars."""
    tree = parse_ast_tree(mode("""
    def some_function():
        _should_not_be_used = 1
        print(_should_not_be_used)
        print(_should_not_be_used)
    """))

    visitor = FunctionDefinitionVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [
        UnusedVariableIsUsedViolation,
        UnusedVariableIsUsedViolation,
    ])