def get_name_base_for_object(obj): common_names = {'list': 'alist', 'dict': 'adict', 'array.array': 'array', 'datetime': 'dt', # we can't name it 'datetime', because that is module's name 'types.FunctionType': 'function', 'types.GeneratorType': 'generator'} return common_names.get(obj.type_name, underscore(obj.type_name))
def test_timeline_for_method(method): object_name = underscore(method.klass.name) init_stub = "# %s = %s" % (object_name, class_init_stub(method.klass)) timeline = [CommentLine(init_stub, 1)] # Generate assertion stub, but only for non-creational methods. if not method.is_creational(): actual = call_with_args("%s.%s" % (object_name, method.name), method.get_call_args()) timeline.append(EqualAssertionStubLine(actual, 2)) timeline.append(SkipTestLine(3)) return timeline
def test_timeline_for_method(method): object_name = underscore(method.klass.name) init_stub = '# %s = %s' % (object_name, class_init_stub(method.klass)) timeline = [CommentLine(init_stub, 1)] # Generate assertion stub, but only for non-creational methods. if not method.is_creational(): actual = call_with_args("%s.%s" % (object_name, method.name), method.get_call_args()) timeline.append(EqualAssertionStubLine(actual, 2)) timeline.append(SkipTestLine(3)) return timeline
def get_human_readable_id(obj): # Get human readable id based on object's value, if obj is True: return 'true' elif obj is False: return 'false' # ... based on object's type, objclass = class_of(obj) mapping = { list: 'list', dict: 'dict', tuple: 'tuple', six.text_type: 'unicode_string', types.GeneratorType: 'generator' } objid = mapping.get(objclass) if objid: return objid # ... or based on its supertype. if isinstance(obj, Exception): return underscore(objclass.__name__) elif isinstance(obj, RePatternType): return "%s_pattern" % string2id(obj.pattern) elif isinstance(obj, types.FunctionType): if obj.__name__ == '<lambda>': return "function" return "%s_function" % obj.__name__ else: # str() may raise an exception. try: string = str(obj) except: string = "<>" # Looks like an instance without a custom __str__ defined. if string.startswith("<"): return "%s_instance" % underscore(objclass.__name__) else: return string2id(string)
def get_human_readable_id(obj): # Get human readable id based on object's value, if obj is True: return 'true' elif obj is False: return 'false' # ... based on object's type, objclass = class_of(obj) mapping = {list: 'list', dict: 'dict', tuple: 'tuple', unicode: 'unicode_string', types.GeneratorType: 'generator'} objid = mapping.get(objclass) if objid: return objid # ... or based on its supertype. if isinstance(obj, Exception): return underscore(objclass.__name__) elif isinstance(obj, RePatternType): return "%s_pattern" % string2id(obj.pattern) elif isinstance(obj, types.FunctionType): if obj.func_name == '<lambda>': return "function" return "%s_function" % obj.func_name else: # str() may raise an exception. try: string = str(obj) except: string = "<>" # Looks like an instance without a custom __str__ defined. if string.startswith("<"): return "%s_instance" % underscore(objclass.__name__) else: return string2id(string)
def exccall2testname(object_name, args, exception): """Generate a test method name that describes given object call raising an exception. >>> exccall2testname('do_this', {}, UnknownObject(Exception())) 'test_do_this_raises_exception' >>> exccall2testname('square', {'x': ImmutableObject('a string')}, UnknownObject(TypeError())) 'test_square_raises_type_error_for_a_string' """ if args: call_description = "%s_for_%s" % (object2id(exception), arguments_as_string(args)) else: call_description = object2id(exception) return "test_%s_raises_%s" % (underscore(object_name), call_description)
def gencall2testname(object_name, args, yields): """Generate a test method name that describes given generator object call yielding some values. >>> gencall2testname('generate', {}, []) 'test_generate_yields_nothing' >>> gencall2testname('generate', {}, [ImmutableObject(1), ImmutableObject(2), ImmutableObject(3)]) 'test_generate_yields_1_then_2_then_3' >>> gencall2testname('backwards', {'x': ImmutableObject(321)}, [ImmutableObject('one'), ImmutableObject('two'), ImmutableObject('three')]) 'test_backwards_yields_one_then_two_then_three_for_321' """ if args: call_description = "%s_for_%s" % (objects_list_to_id(yields), arguments_as_string(args)) else: call_description = objects_list_to_id(yields) return "test_%s_yields_%s" % (underscore(object_name), call_description)
def objcall2testname(object_name, args, output): """Generate a test method name that describes given object call. >>> from test.helper import make_fresh_serialize >>> serialize = make_fresh_serialize() >>> objcall2testname('do_this', {}, serialize(True)) 'test_do_this_returns_true' >>> objcall2testname('compute', {}, serialize('whatever you say')) 'test_compute_returns_whatever_you_say' >>> objcall2testname('square', {'x': serialize(7)}, serialize(49)) 'test_square_returns_49_for_7' >>> objcall2testname('capitalize', {'str': serialize('a word.')}, serialize('A word.')) 'test_capitalize_returns_A_word_for_a_word' Two or more arguments are mentioned by name. >>> objcall2testname('ackermann', {'m': serialize(3), 'n': serialize(2)}, serialize(29)) 'test_ackermann_returns_29_for_m_equal_3_and_n_equal_2' Will sort arguments alphabetically. >>> objcall2testname('concat', {'s1': serialize('Hello '), 's2': serialize('world!')}, serialize('Hello world!')) 'test_concat_returns_Hello_world_for_s1_equal_Hello_and_s2_equal_world' Always starts and ends a word with a letter or number. >>> objcall2testname('strip', {'n': serialize(1), 's': serialize(' A bit of whitespace ')}, serialize(' A bit of whitespace ')) 'test_strip_returns_A_bit_of_whitespace_for_n_equal_1_and_s_equal_A_bit_of_whitespace' Uses argument name when argument is used as a return value. >>> alist = serialize([]) >>> objcall2testname('identity', {'x': alist}, alist) 'test_identity_returns_x_for_x_equal_list' """ if args: # If return value is present in arguments list, use its name as an # identifier. output_name = key_for_value(args, output) if output_name: call_description = "%s_for_%s" % ( output_name, arguments_as_string(args, always_use_argnames=True)) else: call_description = "%s_for_%s" % (object2id(output), arguments_as_string(args)) else: call_description = object2id(output) return "test_%s_returns_%s" % (underscore(object_name), call_description)
def _generate_test_method_descriptions_for_function(self, function, module): if testable_calls(function.calls): log.debug("Detected %s in function %s." % \ (pluralize("testable call", len(testable_calls(function.calls))), function.name)) # We're calling the function, so we have to make sure it will # be imported in the test self.ensure_import((module.locator, function.name)) # We have at least one call registered, so use it. return self._method_descriptions_from_function(function) else: # No calls were traced, so we'll go for a single test stub. log.debug("Detected _no_ testable calls in function %s." % function.name) name = name2testname(underscore(function.name)) return [TestMethodDescription(name, generate_test_case(function, self.template))]
def objcall2testname(object_name, args, output): """Generate a test method name that describes given object call. >>> from test.helper import make_fresh_serialize >>> serialize = make_fresh_serialize() >>> objcall2testname('do_this', {}, serialize(True)) 'test_do_this_returns_true' >>> objcall2testname('compute', {}, serialize('whatever you say')) 'test_compute_returns_whatever_you_say' >>> objcall2testname('square', {'x': serialize(7)}, serialize(49)) 'test_square_returns_49_for_7' >>> objcall2testname('capitalize', {'str': serialize('a word.')}, serialize('A word.')) 'test_capitalize_returns_A_word_for_a_word' Two or more arguments are mentioned by name. >>> objcall2testname('ackermann', {'m': serialize(3), 'n': serialize(2)}, serialize(29)) 'test_ackermann_returns_29_for_m_equal_3_and_n_equal_2' Will sort arguments alphabetically. >>> objcall2testname('concat', {'s1': serialize('Hello '), 's2': serialize('world!')}, serialize('Hello world!')) 'test_concat_returns_Hello_world_for_s1_equal_Hello_and_s2_equal_world' Always starts and ends a word with a letter or number. >>> objcall2testname('strip', {'n': serialize(1), 's': serialize(' A bit of whitespace ')}, serialize(' A bit of whitespace ')) 'test_strip_returns_A_bit_of_whitespace_for_n_equal_1_and_s_equal_A_bit_of_whitespace' Uses argument name when argument is used as a return value. >>> alist = serialize([]) >>> objcall2testname('identity', {'x': alist}, alist) 'test_identity_returns_x_for_x_equal_list' """ if args: # If return value is present in arguments list, use its name as an # identifier. output_name = key_for_value(args, output) if output_name: call_description = "%s_for_%s" % (output_name, arguments_as_string(args, always_use_argnames=True)) else: call_description = "%s_for_%s" % (object2id(output), arguments_as_string(args)) else: call_description = object2id(output) return "test_%s_returns_%s" % (underscore(object_name), call_description)