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_doesnt_include_relevant_objects_affected_by_side_effects_in_the_output(self): alist2 = create(SequenceObject) se = SideEffect([self.alist, alist2], []) create_parent_call_with_side_effects(self.call, [se]) put_on_timeline(self.alist, alist2, se, self.call, self.aline) assert_equal([(self.alist, 2)], object_usage_counts(self.aline))
def test_generates_full_test_case_for_a_call(self): alist = create(SequenceObject) call = create(FunctionCall, args={}, output=alist) put_on_timeline(call, alist) code_string = generate_test_case(call, template=unittest_template) assert_equal_strings("self.assertEqual([], function())\n", code_string) assert_equal(set([('module', 'function')]), code_string.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_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_removes_all_immutable_objects(self): obj = create(ImmutableObject) call = create(FunctionCall, args={'x': obj}, output=obj, definition=create(Function, args=['x'])) put_on_timeline(obj, call) assert_equal([call], remove_objects_unworthy_of_naming([obj, call]))
def test_doesnt_include_relevant_objects_affected_by_side_effects_in_the_output( self): alist2 = create(SequenceObject) se = SideEffect([self.alist, alist2], []) create_parent_call_with_side_effects(self.call, [se]) put_on_timeline(self.alist, alist2, se, self.call, self.aline) assert_equal([(self.alist, 2)], object_usage_counts(self.aline))
def test_assertion_gets_timestamp_075_higher_than_the_last_call_action(self): se = SideEffect([self.alist], []) self.call.add_side_effect(se) put_on_timeline(self.call, self.alist, se) assertion_lines = all_of_type(assertions_for_interaction(self.call), Line) assertion = assert_one_element_and_return(assertion_lines) assert_equal(se.timestamp+0.75, assertion.timestamp)
def test_returns_one_assertion_if_output_object_didnt_exist_before_the_call(self): put_on_timeline(self.call, self.alist) assertion_lines = all_of_type(assertions_for_interaction(self.call), Line) assertion = assert_one_element_and_return(assertion_lines) assert_is_equal_assertion_line(assertion, expected_a_copy=True, expected=self.call.output, actual=self.call) assert assertion.timestamp > self.call.timestamp
def test_assertion_gets_timestamp_075_higher_than_the_last_call_action( self): se = SideEffect([self.alist], []) self.call.add_side_effect(se) put_on_timeline(self.call, self.alist, se) assertion_lines = all_of_type(assertions_for_interaction(self.call), Line) assertion = assert_one_element_and_return(assertion_lines) assert_equal(se.timestamp + 0.75, assertion.timestamp)
def test_keeps_objects_affected_by_side_effects(self): output = create(SequenceObject) seq = create(SequenceObject, obj=[1]) call = create(FunctionCall, output=output) se = SideEffect([output, seq], []) put_on_timeline(seq, se, output, call) assert_equal([seq, se, output, call], remove_objects_unworthy_of_naming([seq, se, output, call]))
def test_keeps_objects_used_more_than_once(self): alist = create(SequenceObject) call = create(FunctionCall, args={ 'x': alist, 'y': alist }, definition=create(Function, args=['x', 'y'])) put_on_timeline(alist, call) assert_equal([alist, call], remove_objects_unworthy_of_naming([alist, call]))
def test_returns_two_assertions_if_output_object_existed_before_the_call(self): put_on_timeline(self.alist, self.call) assertion_lines = all_of_type(assertions_for_interaction(self.call), Line) assert_length(assertion_lines, 2) assert_is_equal_assertion_line(assertion_lines[0], expected=self.call.output, actual=self.call) assert_is_equal_assertion_line(assertion_lines[1], expected_a_copy=True, expected=self.call.output, actual=self.call.output) assert assertion_lines[0].timestamp < assertion_lines[1].timestamp
def test_removes_objects_only_referenced_by_side_effects(self): seq = create(SequenceObject, obj=[1]) output = create(SequenceObject) se = SideEffect([output], [seq]) call = create(FunctionCall, args={'x': seq}, output=output, definition=create(Function, args=['x'])) put_on_timeline(seq, output, se, call) assert_equal([output, se, call], remove_objects_unworthy_of_naming([seq, output, se, call]))
def test_names_objects_appropriatelly(self): obj = create(SequenceObject) call = create(FunctionCall, args={'x': obj}, output=obj, definition=create(Function, args=['x'])) obj2 = create(SequenceObject) put_on_timeline(obj, call, obj2) timeline = name_objects_on_timeline([obj, call, obj2]) assert_assignment(timeline[0], 'alist1', obj) assert_equal(call, timeline[1]) assert_assignment(timeline[2], 'alist2', obj2)
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_returns_one_assertion_if_output_object_didnt_exist_before_the_call( self): put_on_timeline(self.call, self.alist) assertion_lines = all_of_type(assertions_for_interaction(self.call), Line) assertion = assert_one_element_and_return(assertion_lines) assert_is_equal_assertion_line(assertion, expected_a_copy=True, expected=self.call.output, actual=self.call) assert assertion.timestamp > self.call.timestamp
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_object_copy_includes_its_side_effects(self): se = SideEffect([self.alist], []) self.call.add_side_effect(se) put_on_timeline(self.alist, se, self.call) timeline = assertions_for_interaction(self.call) alists = all_of_type(timeline, SequenceObject) assert_length(alists, 2) assert alists[0] is not alists[1] side_effects = all_of_type(timeline, SideEffect) side_effect = assert_one_element_and_return(side_effects) assert alists[1] in side_effect.affected_objects
def test_generates_full_test_case_for_a_call_with_side_effects_and_two_assertions_required(self): alist = create(SequenceObject) call = create(FunctionCall, args={'x': alist}, output=alist, definition=create(Function, args=['x'])) se = ListAppend(alist, create(ImmutableObject, obj=1)) call.add_side_effect(se) put_on_timeline(alist, call, se) assert_equal_strings("alist1 = []\n" "alist2 = []\n" "self.assertEqual(alist1, function(alist1))\n" "alist2.append(1)\n" "self.assertEqual(alist2, alist1)\n", generate_test_case(call, template=unittest_template))
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_returns_two_assertions_if_output_object_existed_before_the_call( self): put_on_timeline(self.alist, self.call) assertion_lines = all_of_type(assertions_for_interaction(self.call), Line) assert_length(assertion_lines, 2) assert_is_equal_assertion_line(assertion_lines[0], expected=self.call.output, actual=self.call) assert_is_equal_assertion_line(assertion_lines[1], expected_a_copy=True, expected=self.call.output, actual=self.call.output) assert assertion_lines[0].timestamp < assertion_lines[1].timestamp
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_generates_full_test_case_for_a_call_with_side_effects_and_two_assertions_required( self): alist = create(SequenceObject) call = create(FunctionCall, args={'x': alist}, output=alist, definition=create(Function, args=['x'])) se = ListAppend(alist, create(ImmutableObject, obj=1)) call.add_side_effect(se) put_on_timeline(alist, call, se) assert_equal_strings( "alist1 = []\n" "alist2 = []\n" "self.assertEqual(alist1, function(alist1))\n" "alist2.append(1)\n" "self.assertEqual(alist2, alist1)\n", generate_test_case(call, template=unittest_template))
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_includes_relevant_side_effects_in_the_output(self): se = SideEffect([self.alist], []) create_parent_call_with_side_effects(self.call, [se]) put_on_timeline(self.alist, se, self.call, self.aline) assert_equal([(self.alist, 2)], object_usage_counts(self.aline))
def test_removes_objects_used_only_once(self): alist = create(SequenceObject) call = create(FunctionCall, args={'x': alist}) put_on_timeline(alist, call) assert_equal([call], remove_objects_unworthy_of_naming([alist, call]))
def test_returns_objects_in_assertion_sorted_by_timestamp(self): put_on_timeline(self.alist, self.call, self.aline) assert_equal([(self.alist, 2)], object_usage_counts(self.aline))
def test_keeps_objects_used_more_than_once(self): alist = create(SequenceObject) call = create(FunctionCall, args={'x': alist, 'y': alist}, definition=create(Function, args=['x', 'y'])) put_on_timeline(alist, call) assert_equal([alist, call], remove_objects_unworthy_of_naming([alist, call]))