def function_calling_methods_with_strangely_named_self(): class Class(object): def strange_method(s): pass def another_strange_method(*args): pass Class().strange_method() Class().another_strange_method()
def function_calling_method_which_calls_other_method(): class Class(object): def method(self): self.other_method() def other_method(self): pass Class().method()
def test_doesnt_include_side_effects_of_method_calls_into_user_object_test_case( self): klass = Class("UserClass") user_obj = UserObject(None, klass) init = Method("__init__", klass=klass) init_call = MethodCall(definition=init, args={}, output=user_obj) method1 = Method("method1", klass=klass) method1_call = MethodCall(definition=method1, args={}, output=create(ImmutableObject)) se1 = GlobalRebind('mod', 'var', ImmutableObject('new_value')) method1_call.add_side_effect(se1) method2 = Method("method2", klass=klass) method2_call = MethodCall(definition=method2, args={}, output=create(ImmutableObject)) se2 = AttributeRebind(user_obj, 'attr', create(ImmutableObject, obj=1)) method2_call.add_side_effect(se2) user_obj.add_call(init_call) user_obj.add_call(method1_call) user_obj.add_call(method2_call) put_on_timeline(user_obj, init_call, method1_call, se1, method2_call, se2) assert_equal_types(assertions_for_interaction(user_obj), [ UserObject, EqualAssertionLine, EqualAssertionLine, EqualAssertionLine, EqualAssertionLine, ImmutableObject, ImmutableObject ])
def test_generates_side_effect_line_for_user_object_attribute_change(self): klass = Class("UserClass", module=Module(None, 'user_module')) user_obj = UserObject(None, klass) assign = Assign('user_obj', user_obj, 1) se = AttributeRebind(user_obj, 'attr', create(ImmutableObject, obj=1)) assert_equal_strings("user_obj = UserClass()\nuser_obj.attr = 1\n", generate_test_contents([assign, se], None))
def test_code_of_method(self): klass = Class('Class', module=self.module) method = Method('method', klass=klass) method_code = object() self.code_tree.add_object(method, method_code) assert_equal(method_code, code_of(method))
def visit_class(self, name, bases, body): visitor = descend(body.children, ClassVisitor) if is_test_class(name, bases): methods = [TestMethod(n, c) for (n, a, c) in visitor.methods] klass = TestClass(name=name, test_cases=methods, code=body) else: methods = [create_definition(n, a, c, Method) for (n, a, c) in visitor.methods] klass = Class(name=name, methods=methods, bases=bases) self.objects.append(klass) self.past_imports = True
def test_properly_gathers_all_input_and_output_values_of_a_method_call(self): self._init_project("class SomeClass:\n def some_method(self, x): return x + 1\n", "from module import SomeClass\nSomeClass().some_method(42)\n") method = Method("some_method") klass = Class("SomeClass", methods=[method]) self.project["module"].objects = [klass] inspect_point_of_entry(self.poe) user_object = assert_one_element_and_return(klass.user_objects) call = assert_one_element_and_return(user_object.calls) assert_call({'x': 42}, 43, call)
def test_clear_previous_run_removes_user_objects_from_classes(self): klass = Class('SomeClass') self._create_project_with_two_points_of_entry(klass) obj1 = inject_user_object(self.first, 1, klass) obj2 = inject_user_object(self.first, 2, klass) obj3 = inject_user_object(self.second, 1, klass) self.first.clear_previous_run() # Only the UserObject from the second POE remains. assert_equal_sets([obj3], klass.user_objects)
def ClassWithMethods(classname, methods, call_type='output'): """call_type has to be one of 'output', 'exception' or 'generator'. """ execution = EmptyProjectExecution() method_objects = [] method_calls = [] klass = Class(classname, methods=method_objects) user_object = UserObject(None, klass) for name, calls in methods: method = Method(name, ['self'] + flatten([a.keys() for a,_ in calls]), is_generator=(call_type=='generator')) method_objects.append(method) for args, output in calls: method_calls.append(create_method_call(method, args, output, call_type, execution, user_object)) klass.add_methods(method_objects) user_object.calls = method_calls klass.add_user_object(user_object) return klass
def test_clear_previous_run_removes_generator_objects_from_functions(self): function = Function('generator', is_generator=True) method = Method('generator_method', is_generator=True) klass = Class('ClassWithGenerators', methods=[method]) self._create_project_with_two_points_of_entry(function, klass) user_object = inject_user_object(self.first, 1, klass) inject_generator_object(self.first, 2, function, {}, function) inject_generator_object(self.first, 3, method, {}, user_object) self.first.clear_previous_run() assert_equal([], klass.user_objects) assert_equal([], function.calls)
def test_can_be_saved_and_restored_from_file(self): project = ProjectInDirectory(self.tmpdir).with_modules( ["good_module.py", "bad_module.py"]) project['good_module'].add_objects( [Class("AClass", [Method("amethod")]), Function("afunction")]) project['bad_module'].errors = ["Syntax error"] project.save() project = Project.from_directory(project.path) assert_equal(2, len(project.get_modules())) assert_equal(2, len(project['good_module'].objects)) assert_equal(["AClass"], get_names(project['good_module'].classes)) assert_equal(["amethod"], get_names(project['good_module'].classes[0].methods)) assert_equal(["afunction"], get_names(project['good_module'].functions)) assert_equal(["Syntax error"], project['bad_module'].errors)
def test_handles_generator_methods(self): def function_calling_generator_method(): class GenClass(object): def genmeth(self): yield 0 yield 1 yield 0 [x for x in GenClass().genmeth()] execution = TestingProject()\ .with_all_catch_module()\ .with_object(Class("GenClass", methods=[Method("genmeth", is_generator=True)]))\ .make_new_execution() inspect_code_in_context(function_calling_generator_method, execution) klass = execution.project.find_object(Class, "GenClass") user_object = assert_one_element_and_return(klass.user_objects) assert_instance(user_object, UserObject) gobject = assert_one_element_and_return(user_object.calls) assert_generator_object({}, [0, 1, 0], gobject)
def top(): obj = Class() first(2) obj.do_something()
def test_code_of_class(self): klass = Class('Class', module=self.module) class_code = object() self.code_tree.add_object(klass, class_code) assert_equal(class_code, code_of(klass))
def __init__(self, name): Class.__init__(self, name) self._methods = {}
def function_calling_a_method(): class Class(object): def method(self): pass Class().method()
def setUp(self): project = EmptyProject() self.module = Module(project=project, subpath='module.py') self.klass = Class('Klass', module=self.module) self.tclass = TestClass('TClass', parent=self.module)