def __new__(meta, name, bases, dict_): """Create the new subclass of :class:`TestCase`.""" super_ = (bases[0] if bases else object).__mro__[0] # for every test stage, gather methods adorned with its decorator, # sort them by definition order and construct final stage method for stage in meta.CLASS_STAGES + meta.INSTANCE_STAGES: stage_method_wrappers = [ mw for mw in dicts.itervalues(dict_) if isinstance(mw, _StageMethod) and mw.stage == stage ] if not stage_method_wrappers: continue # no stage methods, may be custom setUp/tearDown/etc. # if setUp/tearDown/etc. method was defined AND corresponding # decorator used, then it's impossible to resolve proper runtime # order of those two approaches, so we report that as an error if stage in dict_: raise RuntimeError( "ambiguous test stage: either define {stage}() method or " "use @{stage} decorator".format(stage=stage)) stage_method_wrappers.sort(key=attr_func('order')) methods = [mw.method for mw in stage_method_wrappers] dict_[stage] = meta._create_stage_method(stage, methods, super_) return super(TestCaseMetaclass, meta).__new__(meta, name, bases, dict_)
def __new__(meta, name, bases, dict_): """Create the new subclass of :class:`TestCase`.""" super_ = (bases[0] if bases else object).__mro__[0] # for every test stage, gather methods adorned with its decorator, # sort them by definition order and construct final stage method for stage in meta.CLASS_STAGES + meta.INSTANCE_STAGES: stage_method_wrappers = [ mw for mw in dicts.itervalues(dict_) if isinstance(mw, _StageMethod) and mw.stage == stage ] if not stage_method_wrappers: continue # no stage methods, may be custom setUp/tearDown/etc. # if setUp/tearDown/etc. method was defined AND corresponding # decorator used, then it's impossible to resolve proper runtime # order of those two approaches, so we report that as an error if stage in dict_: raise RuntimeError( "ambiguous test stage: either define {stage}() method or " "use @{stage} decorator".format(stage=stage)) stage_method_wrappers.sort(key=attr_func('order')) methods = [mw.method for mw in stage_method_wrappers] dict_[stage] = meta._create_stage_method(stage, methods, super_) return super(TestCaseMetaclass, meta).__new__(meta, name, bases, dict_)
def retrieve_commit_history(directory, vcs_name=None, interval=None): """Retrieves history of commit for given repository. :return: List of Commit tuples """ vcs_name = vcs_name or detect_vcs(directory) if not vcs_name: raise ValueError("Could not find any known version control system " "in given directory") interval = interval or (None, None) history_func = globals().get(vcs_name + '_history') if not history_func: raise ValueError( "Version control system '%s' is not supported" % vcs_name) history = history_func(directory, interval) return sorted(history, key=attr_func('time'), reverse=True)
def retrieve_commit_history(directory, vcs_name=None, interval=None): """Retrieves history of commit for given repository. :return: List of Commit tuples """ vcs_name = vcs_name or detect_vcs(directory) if not vcs_name: raise ValueError("Could not find any known version control system " "in given directory") interval = interval or (None, None) history_func = globals().get(vcs_name + '_history') if not history_func: raise ValueError("Version control system '%s' is not supported" % vcs_name) history = history_func(directory, interval) return sorted(history, key=attr_func('time'), reverse=True)
def test_single_attr__with_dot(self): func = __unit__.attr_func('foo.bar') self.assertEquals( self.DOUBLY_NESTED_OBJECT.foo.bar, func(self.DOUBLY_NESTED_OBJECT))
def test_none(self): with self.assertRaises(TypeError): __unit__.attr_func(None)
def test_two_attrs__good__with_default(self): func = __unit__.attr_func('foo', 'bar', default=self.DEFAULT) self.assertEquals( self.DOUBLY_NESTED_OBJECT.foo.bar, func(self.DOUBLY_NESTED_OBJECT))
def test_two_attrs__bad__with_default(self): func = __unit__.attr_func('foo', 'doesnt_exist', default=self.DEFAULT) self.assertEquals(self.DEFAULT, func(self.DOUBLY_NESTED_OBJECT))
def test_single_attr__good__with_default(self): func = __unit__.attr_func('foo', default=self.DEFAULT) self.assertEquals( self.SINGLE_NESTED_OBJECT.foo, func(self.SINGLE_NESTED_OBJECT)) self.assertEquals( self.DOUBLY_NESTED_OBJECT.foo, func(self.DOUBLY_NESTED_OBJECT))
def test_single_attr__bad__with_default(self): func = __unit__.attr_func('doesnt_exist', default=self.DEFAULT) self.assertEquals(self.DEFAULT, func(self.SINGLE_NESTED_OBJECT)) self.assertEquals(self.DEFAULT, func(self.DOUBLY_NESTED_OBJECT))
def test_two_attrs__good(self): func = __unit__.attr_func('foo', 'bar') self.assertEquals( self.DOUBLY_NESTED_OBJECT.foo.bar, func(self.DOUBLY_NESTED_OBJECT))
def test_two_attrs__bad(self): func = __unit__.attr_func('doesnt_exist', 'foo') with self.assertRaises(AttributeError): func(self.DOUBLY_NESTED_OBJECT)
def test_single_attr__bad(self): func = __unit__.attr_func('doesnt_exist') with self.assertRaises(AttributeError): func(self.SINGLE_NESTED_OBJECT) with self.assertRaises(AttributeError): func(self.DOUBLY_NESTED_OBJECT)
def test_no_args(self): with self.assertRaises(TypeError): __unit__.attr_func()
def test_single_attr__good(self): func = __unit__.attr_func('foo') self.assertEquals( self.SINGLE_NESTED_OBJECT.foo, func(self.SINGLE_NESTED_OBJECT)) self.assertEquals( self.DOUBLY_NESTED_OBJECT.foo, func(self.DOUBLY_NESTED_OBJECT))
def test_string__number(self): with self._assertAttributeNameValueError(): __unit__.attr_func('42')
def test_string__with_spaces(self): with self._assertAttributeNameValueError(): __unit__.attr_func('foo bar')
def test_string__empty(self): with self._assertAttributeNameValueError(): __unit__.attr_func('')
def test_some_object(self): with self.assertRaises(TypeError): __unit__.attr_func(object())