コード例 #1
0
def test_locals_wrong_count(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    options,
    code,
    mode,
):
    """
    Testing that local variables are counted correctly.

    Regression test for #74.
    See: https://github.com/wemake-services/wemake-python-styleguide/issues/74

    Regression test for #247
    See: https://github.com/wemake-services/wemake-python-styleguide/issues/247
    """
    option_values = options(max_local_variables=1)
    tree = parse_ast_tree(mode(code))

    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyLocalsViolation])
    assert_error_text(visitor, '2', option_values.max_local_variables)
コード例 #2
0
def test_elif_incorrect_count(assert_errors, parse_ast_tree, code, options):
    """Testing that incorrect number of `elif` stuff is restricted."""
    tree = parse_ast_tree(code)

    option_values = options(max_elifs=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyElifsViolation])
コード例 #3
0
def test_returns_wrong_count(assert_errors, parse_ast_tree, options, code):
    """Testing that many returns raises a warning."""
    tree = parse_ast_tree(code)

    option_values = options(max_returns=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyReturnsViolation])
コード例 #4
0
def test_expressions_correct_count(
    assert_errors, parse_ast_tree, code, default_options, mode,
):
    """Testing that expressions counted correctly."""
    tree = parse_ast_tree(code.format(mode))

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

    assert_errors(visitor, [])
コード例 #5
0
def test_expressions_wrong_count(
    assert_errors, parse_ast_tree, options, code, mode,
):
    """Testing that many expressions raises a warning."""
    tree = parse_ast_tree(code.format(mode))

    option_values = options(max_expressions=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyExpressionsViolation])
コード例 #6
0
def test_elif_correct_count(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that all `if`/`elif`/`else` stuff is allowed."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [])
コード例 #7
0
def test_correct_arguments_count(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Ensures that lambda functions with correct argument count works."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [])
コード例 #8
0
def test_awaits_correct_count(
    assert_errors,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that awaits counted correctly."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [])
コード例 #9
0
def test_single_argument_count_valid(
    assert_errors,
    parse_ast_tree,
    code,
    options,
):
    """Ensures that functions raise violation when there are multiple args."""
    tree = parse_ast_tree(code)

    option_values = options(max_arguments=0)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyArgumentsViolation])
コード例 #10
0
def test_single_argument_count(
    assert_errors,
    parse_ast_tree,
    single_argument,
    options,
):
    """Ensures that functions with correct argument count works."""
    tree = parse_ast_tree(single_argument)

    option_values = options(max_arguments=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #11
0
def test_no_arguments_error(
    assert_errors,
    parse_ast_tree,
    code,
    options,
):
    """Ensures that lambda functions with multiple arguments raise an error."""
    tree = parse_ast_tree(code)

    option_values = options(max_arguments=0)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyArgumentsViolation])
コード例 #12
0
def test_locals_correct_count(assert_errors, parse_ast_tree, options, code):
    """
    Testing that local variables are counted correctly.

    Regression test for #74.
    See: https://github.com/wemake-services/wemake-python-styleguide/issues/74
    """
    option_values = options(max_local_variables=2)
    tree = parse_ast_tree(code)

    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #13
0
def test_asserts_correct_count1(
    assert_errors,
    parse_ast_tree,
    options,
    code,
):
    """Testing that raises counted correctly."""
    tree = parse_ast_tree(code)

    option_values = options(max_raises=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #14
0
def test_no_arguments(
    assert_errors,
    parse_ast_tree,
    code,
    options,
):
    """Ensures that lambda functions with no arguments work."""
    tree = parse_ast_tree(code)

    option_values = options(max_arguments=0)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #15
0
def test_correct_arguments_count_custom_option(
    assert_errors,
    parse_ast_tree,
    code,
    options,
):
    """Ensures that lambda functions with correct argument count works."""
    tree = parse_ast_tree(code)

    option_values = options(max_arguments=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
コード例 #16
0
def test_elif_incorrect_count(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that incorrect number of `elif` is restricted."""
    tree = parse_ast_tree(code)

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

    assert_errors(visitor, [TooManyElifsViolation])
    assert_error_text(visitor, '4')
コード例 #17
0
def test_asserts_wrong_count(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    options,
    code,
):
    """Testing that many asserts raises a warning."""
    tree = parse_ast_tree(code)

    option_values = options(max_asserts=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyAssertsViolation])
    assert_error_text(visitor, '2')
コード例 #18
0
def test_two_arguments_count_invalid(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    two_arguments,
    options,
    mode,
):
    """Ensures that functions raise violation when there are multiple args."""
    tree = parse_ast_tree(mode(two_arguments))

    option_values = options(max_arguments=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyArgumentsViolation])
    assert_error_text(visitor, '2', option_values.max_arguments)
コード例 #19
0
def test_raises_correct_count2(
    assert_errors,
    parse_ast_tree,
    context,
    first,
    second,
    third,
    default_options,
    mode,
):
    """Testing that raises are counted correctly."""
    test_instance = context.format(first, second, third)
    tree = parse_ast_tree(mode(test_instance))

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

    assert_errors(visitor, [])
コード例 #20
0
def test_raises_wrong_count(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    options,
    context,
    first,
    second,
    third,
    mode,
):
    """Testing that many raises raises a warning."""
    test_instance = function_template.format(first, second, third)
    tree = parse_ast_tree(mode(test_instance))

    option_values = options(max_raises=1)
    visitor = FunctionComplexityVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [TooManyRaisesViolation])
    assert_error_text(visitor, '2', option_values.max_raises)