def test_invalid_syntax(self): python_string = textwrap.dedent(""" a )= 5 """) with self.assertRaises(SyntaxError): parser.get_ast_node_from_string(python_string)
def test_valid_syntax(self): python_string = textwrap.dedent(""" a = 5 """) result = parser.get_ast_node_from_string(python_string) expected = ast.Module self.assertIsInstance(result, expected)
def test_get_module_classes_empty(self): python_string = textwrap.dedent(""" def func(arg1): print("Hi") """) node = parser.get_ast_node_from_string(python_string) result = parser.get_module_classes(node) self.assertEmpty(result)
def test_get_module_classes_single(self): python_string = textwrap.dedent(""" class Cls(object): pass """) node = parser.get_ast_node_from_string(python_string) classes = parser.get_module_classes(node) result = [cls.name for cls in classes] expected = ["Cls"] self.assertCountEqual(result, expected)
def test_get_instance_variables_from_class_avoid_class_variable(self): python_string = textwrap.dedent(""" class Cls(object): attr = 5 """) node = parser.get_ast_node_from_string(python_string) instance_variables = parser.get_instance_variables(node) result = [ instance_variable.attr for instance_variable in instance_variables ] self.assertEmpty(result)
def test_get_class_methods_empty(self): python_string = textwrap.dedent(""" class Cls(object): pass """) node = parser.get_ast_node_from_string(python_string) result = [ method for cls in parser.get_module_classes(node) for method in parser.get_class_methods(cls) ] self.assertEmpty(result)
def test_get_instance_variables_from_class_multiple_same_line(self): python_string = textwrap.dedent(""" class Cls(object): def __init__(self): self.attr1 = self.attr2 = 5 """) node = parser.get_ast_node_from_string(python_string) instance_variables = parser.get_instance_variables(node) result = [ instance_variable.attr for instance_variable in instance_variables ] expected = ["attr1", "attr2"] self.assertCountEqual(result, expected)
def test_bound_method_unbound(self): python_string = textwrap.dedent(""" class Cls(object): def func(arg1): pass """) node = parser.get_ast_node_from_string(python_string) methods = [ method for cls in parser.get_module_classes(node) for method in parser.get_class_methods(cls) if parser.is_class_method_bound(method) ] result = [method.name for method in methods] self.assertEmpty(result)
def test_get_class_variables_from_class_avoid_instance_variable(self): python_string = textwrap.dedent(""" class Cls(object): def __init__(self): self.attr = 5 """) node = parser.get_ast_node_from_string(python_string) classes = parser.get_module_classes(node) class_variables = [ class_variable for cls in classes for class_variable in parser.get_class_variables(cls) ] result = [class_variable.id for class_variable in class_variables] self.assertEmpty(result)
def test_get_class_methods_single(self): python_string = textwrap.dedent(""" class Cls(object): def func1(self, arg1): pass """) node = parser.get_ast_node_from_string(python_string) methods = [ method for cls in parser.get_module_classes(node) for method in parser.get_class_methods(cls) ] result = [method.name for method in methods] expected = ["func1"] self.assertCountEqual(result, expected)
def test_get_all_class_variable_names_just_instance(self): python_string = textwrap.dedent(""" class Cls(object): def __init__(self): self.attr = 6 """) node = parser.get_ast_node_from_string(python_string) classes = parser.get_module_classes(node) result = [ name for cls in classes for name in parser.get_all_class_variable_names(cls) ] expected = ['attr'] self.assertCountEqual(result, expected)
def test_get_class_variables_from_class_multiple_targets(self): python_string = textwrap.dedent(""" class Cls(object): attr1 = attr2 = 5 """) node = parser.get_ast_node_from_string(python_string) classes = parser.get_module_classes(node) class_variables = [ class_variable for cls in classes for class_variable in parser.get_class_variables(cls) ] result = [class_variable.id for class_variable in class_variables] expected = ["attr1", "attr2"] self.assertCountEqual(result, expected)
def test_bound_method_non_default_name(self): python_string = textwrap.dedent(""" class Cls(object): def func(this, arg1): pass """) node = parser.get_ast_node_from_string(python_string) methods = [ method for cls in parser.get_module_classes(node) for method in parser.get_class_methods(cls) if parser.is_class_method_bound(method, arg_name="this") ] result = [method.name for method in methods] expected = ["func"] self.assertCountEqual(result, expected)
def test_is_class_method_staticmethod_other_decorator_with_arguments(self): python_string = textwrap.dedent(""" class Cls(object): @other_decorator("argument") def func(self, arg1): pass """) node = parser.get_ast_node_from_string(python_string) methods = [ method for cls in parser.get_module_classes(node) for method in parser.get_class_methods(cls) if parser.is_class_method_staticmethod(method) ] result = [method.name for method in methods] self.assertEmpty(result)
def test_get_all_class_variable_names_ensure_no_method_names(self): python_string = textwrap.dedent(""" class Cls(object): attr = 6 def func(self): pass """) node = parser.get_ast_node_from_string(python_string) classes = parser.get_module_classes(node) result = [ name for cls in classes for name in parser.get_all_class_variable_names(cls) ] # Ensure 'func' isn't in the list of included names expected = ['attr'] self.assertCountEqual(result, expected)
def test_get_all_class_variable_names_used_in_method(self): python_string = textwrap.dedent(""" class Cls(object): attr1 = 5 def func(self): self.attr2 = 6 """) node = parser.get_ast_node_from_string(python_string) class_methods = [ method for cls in parser.get_module_classes(node) for method in parser.get_class_methods(cls) ] result = [ name for method in class_methods for name in parser.get_all_class_variable_names_used_in_method(method) ] expected = ['attr2'] self.assertCountEqual(result, expected)
def test_ensure_unbound_attribute_not_considered_instance_variable(self): python_string = textwrap.dedent(""" class Cls(object): def func(self): self.attr1 = 5 otherclass.attr2 = 6 """) node = parser.get_ast_node_from_string(python_string) class_methods = [ method for cls in parser.get_module_classes(node) for method in parser.get_class_methods(cls) ] result = [ name for method in class_methods for name in parser.get_all_class_variable_names_used_in_method(method) ] # Ensure 'attr2' isn't in the list of included names expected = ['attr1'] self.assertCountEqual(result, expected)