def test_reconstructs_builtin_frozenset(self): # Frozenset builtin was added in Python 2.4. if sys.version_info < (2, 4): raise SkipTest call_string = constructor_as_string(self.serialize(frozenset([1, 2, 3]))) assert_equal_strings("frozenset([1, 2, 3])", call_string) assert_equal_sets([], call_string.imports)
def variable_assignment_line(left, right, already_assigned_names): if isinstance(right, ModuleVariableReference): constructor = code_string_from_module_variable_reference(right) elif isinstance(right, str): constructor = CodeString(right) elif isinstance(right, (Call, MethodCallContext)): constructor = call_in_test(right, already_assigned_names) # Associate the name with the call's output, not the call itself. already_assigned_names[right.output] = left else: constructor = constructor_as_string(right, already_assigned_names) already_assigned_names[right] = left return combine(left, constructor, "%s = %s")
def variable_assignment_line(left, right, already_assigned_names): if isinstance(right, ModuleVariableReference): constructor = code_string_from_module_variable_reference(right) elif isinstance(right, str): constructor = CodeString(right) elif isinstance(right, (Call, MethodCallContext)): constructor = call_in_test(right, already_assigned_names) # Associate the name with the call's output, not the call itself. already_assigned_names[right.output] = left else: constructor = constructor_as_string(right, already_assigned_names) already_assigned_names[right] = left return combine(left, constructor, "%s = %s")
def generate_test_contents(events, template): contents = CodeString("") all_uncomplete = False already_assigned_names = {} for event in events: if isinstance(event, Assign): line = variable_assignment_line(event.name, event.obj, already_assigned_names) elif isinstance(event, BindingChange): if event.name.obj in already_assigned_names.keys(): already_assigned_names[ event.obj] = code_string_from_object_attribute_reference( event.name, already_assigned_names) continue # This is not a real test line, so just go directly to the next line. elif isinstance(event, EqualAssertionLine): expected = constructor_as_string(event.expected, already_assigned_names) if isinstance(event.actual, (Call, MethodCallContext)): actual = call_in_test(event.actual, already_assigned_names) elif isinstance(event.actual, ModuleVariableReference): actual = code_string_from_module_variable_reference( event.actual) elif isinstance(event.actual, ObjectAttributeReference): actual = code_string_from_object_attribute_reference( event.actual, already_assigned_names) elif isinstance(event.actual, str): actual = CodeString(event.actual) else: actual = constructor_as_string(event.actual, already_assigned_names) if expected.uncomplete: expected = type_as_string(event.expected) actual = type_of(actual) line = template.equal_assertion(expected, actual) elif isinstance(event, GeneratorAssertionLine): call = event.generator_call yields = generator_object_yields(call) expected = constructor_as_string(yields, already_assigned_names) actual = call_in_test(call, already_assigned_names) if expected.uncomplete: expected = type_as_string(yields) actual = map_types(actual) actual = addimport(actual, 'types') line = template.equal_assertion(expected, actual) elif isinstance(event, RaisesAssertionLine): actual = call_in_test(event.call, already_assigned_names) actual = in_lambda(actual) if is_serialized_string(event.expected_exception): exception = todo_value(event.expected_exception.reconstructor) else: exception = CodeString(event.expected_exception.type_name) exception = addimport(exception, event.expected_exception.type_import) line = template.raises_assertion(exception, actual) elif isinstance(event, CommentLine): line = CodeString(event.comment) elif isinstance(event, SkipTestLine): line = template.skip_test() elif isinstance(event, EqualAssertionStubLine): line = template.equal_assertion( CodeString('expected', uncomplete=True), event.actual) elif isinstance(event, BuiltinMethodWithPositionArgsSideEffect): # All objects affected by side effects are named. object_name = already_assigned_names[event.obj] line = call_as_string_for( "%s.%s" % (object_name, event.definition.name), event.args_mapping(), event.definition, already_assigned_names) elif isinstance(event, AttributeRebind): # All objects affected by side effects are named. object_name = already_assigned_names[event.obj] line = attribute_assignment_line( "%s.%s" % (object_name, event.name), event.value, already_assigned_names) else: raise TypeError( "Don't know how to generate test contents for event %r." % event) if line.uncomplete: all_uncomplete = True if all_uncomplete and not isinstance(event, SkipTestLine): line = combine("# ", line) contents = combine(contents, add_newline(line)) return contents
def attribute_assignment_line(left, right, already_assigned_names): try: constructor = CodeString(already_assigned_names[right]) except KeyError: constructor = constructor_as_string(right, already_assigned_names) return combine(left, constructor, "%s = %s")
def generate_test_contents(events, template): contents = CodeString("") all_uncomplete = False already_assigned_names = {} for event in events: if isinstance(event, Assign): line = variable_assignment_line(event.name, event.obj, already_assigned_names) elif isinstance(event, BindingChange): if event.name.obj in already_assigned_names.keys(): already_assigned_names[event.obj] = code_string_from_object_attribute_reference(event.name, already_assigned_names) continue # This is not a real test line, so just go directly to the next line. elif isinstance(event, EqualAssertionLine): expected = constructor_as_string(event.expected, already_assigned_names) if isinstance(event.actual, (Call, MethodCallContext)): actual = call_in_test(event.actual, already_assigned_names) elif isinstance(event.actual, ModuleVariableReference): actual = code_string_from_module_variable_reference(event.actual) elif isinstance(event.actual, ObjectAttributeReference): actual = code_string_from_object_attribute_reference(event.actual, already_assigned_names) elif isinstance(event.actual, str): actual = CodeString(event.actual) else: actual = constructor_as_string(event.actual, already_assigned_names) if expected.uncomplete: expected = type_as_string(event.expected) actual = type_of(actual) line = template.equal_assertion(expected, actual) elif isinstance(event, GeneratorAssertionLine): call = event.generator_call yields = generator_object_yields(call) expected = constructor_as_string(yields, already_assigned_names) actual = call_in_test(call, already_assigned_names) if expected.uncomplete: expected = type_as_string(yields) actual = map_types(actual) actual = addimport(actual, 'types') line = template.equal_assertion(expected, actual) elif isinstance(event, RaisesAssertionLine): actual = call_in_test(event.call, already_assigned_names) actual = in_lambda(actual) if is_serialized_string(event.expected_exception): exception = todo_value(event.expected_exception.reconstructor) else: exception = CodeString(event.expected_exception.type_name) exception = addimport(exception, event.expected_exception.type_import) line = template.raises_assertion(exception, actual) elif isinstance(event, CommentLine): line = CodeString(event.comment) elif isinstance(event, SkipTestLine): line = template.skip_test() elif isinstance(event, EqualAssertionStubLine): line = template.equal_assertion(CodeString('expected', uncomplete=True), event.actual) elif isinstance(event, BuiltinMethodWithPositionArgsSideEffect): # All objects affected by side effects are named. object_name = already_assigned_names[event.obj] line = call_as_string_for("%s.%s" % (object_name, event.definition.name), event.args_mapping(), event.definition, already_assigned_names) elif isinstance(event, AttributeRebind): # All objects affected by side effects are named. object_name = already_assigned_names[event.obj] line = attribute_assignment_line("%s.%s" % (object_name, event.name), event.value, already_assigned_names) else: raise TypeError("Don't know how to generate test contents for event %r." % event) if line.uncomplete: all_uncomplete = True if all_uncomplete and not isinstance(event, SkipTestLine): line = combine("# ", line) contents = combine(contents, add_newline(line)) return contents
def attribute_assignment_line(left, right, already_assigned_names): try: constructor = CodeString(already_assigned_names[right]) except KeyError: constructor = constructor_as_string(right, already_assigned_names) return combine(left, constructor, "%s = %s")
def test_reconstructs_floating_point_arrays(self): call_string = constructor_as_string(self.serialize(array.array('d', [1, 2, 3, 4]))) assert_equal_strings("array.array('d', [1.0, 2.0, 3.0, 4.0])", call_string) assert_equal_sets(['array'], call_string.imports)
def test_reconstructs_integer_arrays(self): call_string = constructor_as_string(self.serialize(array.array('I', [1, 2, 3, 4]))) assert_equal_strings("array.array('I', [1L, 2L, 3L, 4L])", call_string) assert_equal_sets(['array'], call_string.imports)
def test_reconstructs_immutable_set_from_sets_module(self): call_string = constructor_as_string(self.serialize(sets.ImmutableSet([1, 2, 3]))) assert_equal_strings("ImmutableSet([1, 2, 3])", call_string) assert_equal_sets([("sets", "ImmutableSet")], call_string.imports)