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 remove_objects_unworthy_of_naming(events): new_events = list(events) side_effects = all_of_type(events, SideEffect) affected_objects = objects_affected_by_side_effects(side_effects) invoked_objects = objects_with_method_calls(events) + objects_with_attribute_references(events) for obj, usage_count in object_usage_counts(events): # ImmutableObjects don't need to be named, as their identity is # always unambiguous. if not isinstance(obj, ImmutableObject): # Anything mentioned more than once have to be named. if usage_count > 1: continue # Anything affected by side effects is also worth naming. if obj in affected_objects: continue # All user objects with method calls should also get names for # readability. if obj in invoked_objects: continue try: while True: new_events.remove(obj) except ValueError: pass # If the element wasn't on the timeline, even better. return new_events
def function_code_from_definition(definition): """Return a code object of a given function definition. Can raise SyntaxError if the definition is not valid. """ consts = compile_without_warnings(unindent(str(definition))).co_consts return all_of_type(consts, types.CodeType)[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_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_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 _get_test_classes(self): return all_of_type(self.objects, TestClass)
def _get_functions(self): return all_of_type(self.objects, Function)
def find_object(self, type, name): for obj in all_of_type(self.objects, type): if obj.name == name: return obj
def side_effects_that_affect_object(events, obj): "Filter out side effects that are irrelevant to given object." for side_effect in all_of_type(events, SideEffect): if obj in side_effect.affected_objects: yield side_effect
def iter_captured_generator_objects(self): return all_of_type(self.captured_objects.values(), GeneratorObject)
def objects_only(events): return all_of_type(events, SerializedObject)
def get_those_and_contained_objects(objs): return all_of_type(objs, SerializedObject) + get_contained_objects(objs)