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_line_with_variable_reference(self): line = EqualAssertionLine(ImmutableObject('string'), ModuleVariableReference('mod', 'var', 1.5), 2) result = generate_test_contents([line], unittest_template) assert_equal_strings("self.assertEqual('string', mod.var)\n", result) assert_equal(set(['mod']), result.imports)
def test_creates_setup_and_teardown_for_two_different_global_read_side_effects( self): call = create(FunctionCall, args={}) old_value = ImmutableObject('old_value') se = GlobalRead('mod', 'var', old_value) se2 = GlobalRead('mod', 'other_var', old_value) call.add_side_effect(se) call.add_side_effect(se2) put_on_timeline(se, se2, call, call.output) timeline = assertions_for_interaction(call) varSetup1, varSetup2, varTeardown = assert_timeline_length_and_return_elements( filter_out_objects(timeline), 7, [2, 3, 5]) assert_assignment_with_module_variable_reference( varSetup1, 'old_mod_var', 'mod', 'var') assert_assignment(varSetup2, 'mod.var', old_value) assert_assignment(varTeardown, 'mod.var', 'old_mod_var') otherVarSetup1, otherVarSetup2, otherVarTeardown = assert_timeline_length_and_return_elements( filter_out_objects(timeline), 7, [0, 1, 6]) assert_assignment_with_module_variable_reference( otherVarSetup1, 'old_mod_other_var', 'mod', 'other_var') assert_assignment(otherVarSetup2, 'mod.other_var', old_value) assert_assignment(otherVarTeardown, 'mod.other_var', 'old_mod_other_var')
def test_creates_assertion_for_global_rebind_side_effect(self): call = create(FunctionCall, args={}) new_value = ImmutableObject('new_value') se = GlobalRebind('mod', 'var', new_value) call.add_side_effect(se) put_on_timeline(se, call, call.output) timeline = assertions_for_interaction(call) assert_length(timeline, 4) rebind_assertion = timeline[2] assert_equal(new_value, rebind_assertion.expected) assert_module_variable_reference(rebind_assertion.actual, 'mod', 'var')
def test_clear_previous_run_ignores_not_referenced_objects(self): function = Function('some_function') self._create_project_with_two_points_of_entry(function) args = { 'i': ImmutableObject(123), 'u': UnknownObject(None), 's': SequenceObject([], None), 'm': MapObject({}, None) } inject_function_call(self.first, function, args) self.first.clear_previous_run()
def _fix_generator_objects(self): """Remove last yielded values of generator objects, as those are just bogus Nones placed on generator stop. """ for gobject in self.iter_captured_generator_objects(): if is_exhaused_generator_object(gobject) \ and gobject.calls \ and gobject.calls[-1].output == ImmutableObject(None): removed_invocation = gobject.calls.pop() self.remove_call_from_call_graph(removed_invocation) # Once we know if the generator is active or not, we can discard it. if hasattr(gobject, '_generator'): del gobject._generator
def test_names_globals_from_submodules_properly(self): call = create(FunctionCall, args={}) old_value = ImmutableObject('old_value') se = GlobalRead('mod.submod', 'var', old_value) call.add_side_effect(se) put_on_timeline(se, call, call.output) timeline = assertions_for_interaction(call) setup_1, setup_2, teardown = assert_timeline_length_and_return_elements( filter_out_objects(timeline), 4, [0, 1, 3]) assert_assignment_with_module_variable_reference( setup_1, 'old_mod_submod_var', 'mod.submod', 'var') assert_assignment(setup_2, 'mod.submod.var', old_value) assert_assignment(teardown, 'mod.submod.var', 'old_mod_submod_var')
def test_creates_only_one_setup_and_teardown_for_multiple_global_read_side_effects_of_the_same_variable( self): call = create(FunctionCall, args={}) old_value = ImmutableObject('old_value') se = GlobalRead('mod', 'var', old_value) se2 = GlobalRead('mod', 'var', old_value) call.add_side_effect(se) call.add_side_effect(se2) put_on_timeline(se, se2, call, call.output) timeline = assertions_for_interaction(call) setup_1, setup_2, teardown = assert_timeline_length_and_return_elements( filter_out_objects(timeline), 4, [0, 1, 3]) assert_assignment_with_module_variable_reference( setup_1, 'old_mod_var', 'mod', 'var') assert_assignment(setup_2, 'mod.var', old_value) assert_assignment(teardown, 'mod.var', 'old_mod_var')
def test_handles_passing_user_objects_around(self): def fun(): class Something(object): pass def compare(x, y): return x is y obj = Something() compare(obj, obj) callables = inspect_returning_callables(fun) assert_length(callables, 2) user_object = findfirst(is_user_object, callables) function = findfirst(is_function, callables) call = assert_one_element_and_return(function.calls) assert_instance(call.output, ImmutableObject) assert_equal(ImmutableObject(True), call.output) assert call.input['x'] is call.input['y'] is user_object
def create_serialized_object(self, obj): # Generator object has been passed as a value. We don't have enough # information to create a complete GeneratorObject instance here, so # we create a stub to be activated later. if isinstance(obj, types.GeneratorType): return GeneratorObject(obj) user_object = self.create_serialized_user_object(obj) if user_object: return user_object elif is_immutable(obj): return ImmutableObject(obj) elif is_sequence(obj): return SequenceObject(obj, self.serialize) elif is_mapping(obj): return MapObject(obj, self.serialize) elif is_builtin_exception(obj): return BuiltinException(obj, self.serialize) elif is_library_object(obj): return LibraryObject(obj, self.serialize) else: return UnknownObject(obj)