def test_get_subclasses(self): """Test the get_subclasses function. All types are found when looking for subclasses of object, except for the old style classes on Python 2.x. """ all_classes = get_subclasses(object) for typ, new in CLASSES: self.assertTrue(typ in get_subclasses(typ)) if new or OLD_CLASS_SUPPORT: self.assertTrue(typ in all_classes) else: self.assertFalse(typ in all_classes) self.assertFalse(0 in all_classes)
def test_type_name(self): """Test TYPE_NAME.""" regex = r"^" + re.TYPE_NAME + r"$" real_types = set(c.__name__ for c in get_subclasses(object)) types = ['str', 'int', 'method-wrapper'] + list(real_types) for type_ in types: self.assertRegexpMatches(type_, regex)
def test_attr_name(self): """Test ATTR_NAME.""" regex = r"^" + re.ATTR_NAME + r"$" real_attrs = set(att for o in get_subclasses(object) for att in dir(o)) attrs = ['do_stuff', '__magic__'] + list(real_attrs) for attr in attrs: self.assertRegexpMatches(attr, regex) for attr in ['1a']: self.assertNotRegexpMatches(attr, regex)
def test_attr_name(self): """Test ATTR_NAME.""" regex = r"^" + re.ATTR_NAME + r"$" real_attrs = set() for o in get_subclasses(object): try: real_attrs.update(dir(o)) except AttributeError: pass attrs = ['do_stuff', '__magic__'] + list(real_attrs) for attr in attrs: self.assertRegexp(attr, regex) for attr in ['1a']: self.assertNotRegexp(attr, regex)
def test_type_name(self): """Test TYPE_NAME.""" regex = r"^" + re.TYPE_NAME + r"$" real_types = set(c.__name__ for c in get_subclasses(object)) types = [ 'str', 'int', 'method-wrapper', 'builtin_function', 'builtin_function_or_method' ] + list(real_types) for type_ in types: if type_ != 'symtable entry': self.assertRegexp(type_, regex)
def generate_add_string_to_exc_tests(): """Generate tests for add_string_to_exception. This function dynamically creates tests cases for the function add_string_to_exception for as many Exception subclasses as possible. This is not used at the moment because the generated classes need to be added in the global namespace and there is no good way to do this. However, it may be a good idea to call this when new versions of Python are released to ensure we handle all exceptions properly (and find the tests to be added manually if need be). """ for klass in get_subclasses(Exception): r = get_instance(klass) if r is not None: _, p = r class_name = ("NameForAddStringToExcFromInstanceTest" + klass.__name__ + str(id(klass))) assert class_name not in globals(), class_name globals()[class_name] = type( class_name, (AddStringToExcFromInstanceTest, unittest2.TestCase), {'exc_type': klass, 'args': p})