def test_nonmatching_class( assert_errors, parse_ast_tree, default_options, access, attribute_name, annotation, method_name, assignment, mode, ): """Testing that non matching attribute and getter/setter is allowed.""" test_instance = class_attribute_template.format( access, attribute_name, assignment, annotation, method_name, ) tree = parse_ast_tree(mode(test_instance)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_class_attributes_getter_setter( assert_errors, parse_ast_tree, default_options, attribute_name, access, annotation, method_name, assignment, mode, ): """Testing that using getter/setters with class attributes is prohibited.""" test_instance = class_attribute_template.format( access, attribute_name, assignment, annotation, method_name, ) tree = parse_ast_tree(mode(test_instance)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [UnpythonicGetterSetterViolation])
def test_wrong_base_class(assert_errors, parse_ast_tree, default_options): """Testing that not using explicit base class is forbiden.""" tree = parse_ast_tree('class WithoutBase: ...') visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [RequiredBaseClassViolation])
def test_staticmethod_used(assert_errors, parse_ast_tree, default_options): """Testing that some built-in functions are restricted as decorators.""" tree = parse_ast_tree(decorated_method.format('staticmethod')) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [StaticMethodViolation])
def test_regular_decorator_used( assert_errors, parse_ast_tree, decorator, default_options, ): """Testing that other decorators are allowed.""" tree = parse_ast_tree(decorated_method.format(decorator)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_bad_number_default_option( assert_errors, parse_ast_tree, code, default_options, ): """Testing of base classes number with default options.""" tree = parse_ast_tree(code) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [TooManyBaseClassesViolation])
def test_correct_count( assert_errors, parse_ast_tree, code, default_options, ): """Testing of correct base classes number.""" tree = parse_ast_tree(code) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_bad_number_custom_option( assert_errors, parse_ast_tree, code, options, ): """Testing of base classes number with custom options.""" tree = parse_ast_tree(code) options = options(max_base_classes=5) visitor = WrongClassVisitor(options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_base_exception_subclass( assert_errors, parse_ast_tree, default_options, ): """Testing that it is not possible to subclass `BaseException`.""" tree = parse_ast_tree(class_with_base.format('BaseException')) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [BaseExceptionSubclassViolation])
def test_regular_base_classes( assert_errors, parse_ast_tree, base, default_options, ): """Testing that regular base classes work.""" tree = parse_ast_tree('class Example({0}): ...'.format(base)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_incorrect_body_items( assert_errors, parse_ast_tree, default_options, code, ): """Testing that incorrect body nodes are prohibited.""" tree = parse_ast_tree(class_body_template.format(code)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [WrongClassBodyContentViolation])
def test_wrong_class_definition_multiple_parent( assert_errors, parse_ast_tree, code, default_options, ): """Testing wrong class definition.""" tree = parse_ast_tree(code) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [ObjectInBaseClassesListViolation])
def test_correct_class_definitions( assert_errors, parse_ast_tree, code, default_options, ): """Testing correct class definition with single parent.""" tree = parse_ast_tree(code) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_regular_method_used( assert_errors, parse_ast_tree, method, default_options, ): """Testing that other methods are working fine.""" tree = parse_ast_tree(magic_method.format(method)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_init_generator( assert_errors, parse_ast_tree, default_options, mode, ): """Testing that `__init__` without `yield` is prohibited.""" tree = parse_ast_tree(mode(init_with_yield)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [YieldInsideInitViolation])
def test_regular_subclass( assert_errors, parse_ast_tree, super_class, default_options, ): """Testing that it is possible to subclass regulars.""" tree = parse_ast_tree(class_with_base.format(super_class)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_init_regular( assert_errors, parse_ast_tree, code, default_options, ): """Testing that `__init__` without `yield` is allowed.""" tree = parse_ast_tree(code) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_builtin_subclass( assert_errors, parse_ast_tree, super_class, default_options, ): """Testing that it is not possible to subclass builtins.""" tree = parse_ast_tree(class_with_base.format(super_class)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [BuiltinSubclassViolation])
def test_correct_base_classes( assert_errors, parse_ast_tree, base, default_options, ): """Testing that it is possible to use correct nodes as bases.""" tree = parse_ast_tree(class_with_base.format(base)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_base_class_expression( assert_errors, parse_ast_tree, base, default_options, ): """Testing that it is not possible to use any incorrect nodes as bases.""" tree = parse_ast_tree(class_with_base.format(base)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [WrongBaseClassViolation])
def test_wrong_magic_used( assert_errors, parse_ast_tree, method, default_options, ): """Testing that some magic methods are restricted.""" tree = parse_ast_tree(magic_method.format(method)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [BadMagicMethodViolation])
def test_body_correct_items( assert_errors, parse_ast_tree, code, default_options, mode, ): """Testing correct body items are allowed.""" tree = parse_ast_tree(mode(class_body_template.format(code))) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_valid_getter_and_setter( assert_errors, parse_ast_tree, default_options, code, mode, ): """Testing that correct usage of getter/setter is allowed.""" tree = parse_ast_tree(mode(code)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_wrong_base_class_nested( assert_errors, parse_ast_tree, default_options, ): """Testing that not using explicit base class on nested is forbiden.""" tree = parse_ast_tree(""" class Model(object): class Meta: ... """) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [RequiredBaseClassViolation])
def test_wrong_base_class( assert_errors, assert_error_text, parse_ast_tree, code, default_options, ): """Testing that not using explicit base class is forbiden.""" tree = parse_ast_tree(code) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [RequiredBaseClassViolation]) assert_error_text(visitor, 'Meta')
def test_correct_base_class_nested( assert_errors, parse_ast_tree, default_options, ): """Testing that using explicit base class on nested works.""" tree = parse_ast_tree(""" class Model(object): class Meta(object): ... """) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])
def test_invalid_getter_and_setter( assert_errors, parse_ast_tree, default_options, code, mode, ): """Testing that wrong use of getter/setter is prohibited.""" tree = parse_ast_tree(mode(code)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [ UnpythonicGetterSetterViolation, UnpythonicGetterSetterViolation, ])
def test_class_mixed( assert_errors, parse_ast_tree, default_options, access, first, second, third, mode, ): """Testing correct use of methods with get/set in name.""" test_instance = class_mixed.format(access, first, second, third) tree = parse_ast_tree(mode(test_instance)) visitor = WrongClassVisitor(default_options, tree=tree) visitor.run() assert_errors(visitor, [])