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])
Esempio n. 2
0
    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
        ])
Esempio n. 3
0
    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_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
Esempio n. 6
0
    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_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
Esempio n. 8
0
    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
Esempio n. 9
0
    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_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_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
Esempio n. 13
0
    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')
Esempio n. 14
0
    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_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')
Esempio n. 16
0
    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
Esempio n. 17
0
    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')
Esempio n. 18
0
def generate_test_case(testable_interaction, template):
    """This functions binds all other functions from generator submodules
    together (assertions, cleaner, optimizer, objects_namer and builder),
    implementing full test generation process, from a testable interaction
    object to a test case string.

    Call|UserObject|Method|Function -> assertions_for_interaction ->
      [Event] -> remove_objects_unworthy_of_naming ->
        [Event] -> optimize ->
          [Event] -> name_objects_on_timeline ->
            [Event] -> generate_test_contents ->
              CodeString
    """
    return \
        generate_test_contents(
            name_objects_on_timeline(
                optimize(
                    remove_objects_unworthy_of_naming(
                        assertions_for_interaction(testable_interaction)))),
            template)
Esempio n. 19
0
def generate_test_case(testable_interaction, template):
    """This functions binds all other functions from generator submodules
    together (assertions, cleaner, optimizer, objects_namer and builder),
    implementing full test generation process, from a testable interaction
    object to a test case string.

    Call|UserObject|Method|Function -> assertions_for_interaction ->
      [Event] -> remove_objects_unworthy_of_naming ->
        [Event] -> optimize ->
          [Event] -> name_objects_on_timeline ->
            [Event] -> generate_test_contents ->
              CodeString
    """
    return \
        generate_test_contents(
            name_objects_on_timeline(
                optimize(
                    remove_objects_unworthy_of_naming(
                        assertions_for_interaction(testable_interaction)))),
            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')