def test_alphabet_as_string_violation(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    code,
    default_options,
):
    """Testing that the strings violate the rules."""
    tree = parse_ast_tree('"{0}"'.format(code))

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

    assert_errors(visitor, [StringConstantRedefinedViolation])
    assert_error_text(visitor, code)
Пример #2
0
def test_modulo_formatting(
    assert_errors,
    parse_ast_tree,
    code,
    prefix,
    default_options,
):
    """Testing that the strings violate the rules."""
    tree = parse_ast_tree('{0}"{1}"'.format(prefix, code))

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

    assert_errors(visitor, [ModuloStringFormatViolation],
                  (FormattedStringViolation, ))
Пример #3
0
def test_string_overuse(
    assert_errors,
    assert_error_text,
    parse_ast_tree,
    default_options,
    strings,
    string_value,
):
    """Ensures that over-used strings raise violations."""
    tree = parse_ast_tree(strings.format(string_value))

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

    assert_errors(visitor, [OverusedStringViolation])
    assert_error_text(visitor, string_value.replace('"', '') or "''")
Пример #4
0
def test_string_overuse_settings(
    assert_errors,
    parse_ast_tree,
    options,
    strings,
    string_value,
    mode,
):
    """Ensures that settings for string over-use work."""
    tree = parse_ast_tree(mode(strings.format(string_value)))

    option_values = options(max_string_usages=4)
    visitor = WrongStringVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [])
def test_alphabet_as_fstring_violation(
    assert_errors,
    parse_ast_tree,
    default_options,
):
    """Testing that the fstrings violate the rules."""
    tree = parse_ast_tree('f"{0}"'.format(string.ascii_letters))

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

    assert_errors(
        visitor,
        [StringConstantRedefinedViolation],
        ignored_types=FormattedStringViolation,
    )
def test_string_type_annotations(
    assert_errors,
    parse_ast_tree,
    options,
    strings,
    string_value,
    mode,
):
    """Ensures that type annotations do not raise violations."""
    tree = parse_ast_tree(mode(strings.format(string_value)))

    option_values = options(max_string_usages=0)
    visitor = WrongStringVisitor(option_values, tree=tree)
    visitor.run()

    assert_errors(visitor, [])