Back to index

python3.2  3.2.2
Public Member Functions
test.test_abc.TestABC Class Reference

List of all members.

Public Member Functions

def test_abstractmethod_basics
def test_abstractproperty_basics
def test_abstractclassmethod_basics
def test_abstractstaticmethod_basics
def test_abstractmethod_integration
def test_metaclass_abc
def test_registration_basics
def test_isinstance_invalidation
def test_registration_builtins
def test_registration_edge_cases
def test_register_non_class
def test_registration_transitiveness
def test_all_new_methods_are_called

Detailed Description

Definition at line 13 of file test_abc.py.


Member Function Documentation

Definition at line 37 of file test_abc.py.

00037 
00038     def test_abstractclassmethod_basics(self):
00039         @abc.abstractclassmethod
00040         def foo(cls): pass
00041         self.assertTrue(foo.__isabstractmethod__)
00042         @classmethod
00043         def bar(cls): pass
00044         self.assertFalse(hasattr(bar, "__isabstractmethod__"))
00045 
00046         class C(metaclass=abc.ABCMeta):
00047             @abc.abstractclassmethod
00048             def foo(cls): return cls.__name__
00049         self.assertRaises(TypeError, C)
00050         class D(C):
00051             @classmethod
00052             def foo(cls): return super().foo()
00053         self.assertEqual(D.foo(), 'D')
00054         self.assertEqual(D().foo(), 'D')

Here is the call graph for this function:

Definition at line 15 of file test_abc.py.

00015 
00016     def test_abstractmethod_basics(self):
00017         @abc.abstractmethod
00018         def foo(self): pass
00019         self.assertTrue(foo.__isabstractmethod__)
00020         def bar(self): pass
00021         self.assertFalse(hasattr(bar, "__isabstractmethod__"))

Here is the call graph for this function:

Definition at line 73 of file test_abc.py.

00073 
00074     def test_abstractmethod_integration(self):
00075         for abstractthing in [abc.abstractmethod, abc.abstractproperty,
00076                               abc.abstractclassmethod,
00077                               abc.abstractstaticmethod]:
00078             class C(metaclass=abc.ABCMeta):
00079                 @abstractthing
00080                 def foo(self): pass  # abstract
00081                 def bar(self): pass  # concrete
00082             self.assertEqual(C.__abstractmethods__, {"foo"})
00083             self.assertRaises(TypeError, C)  # because foo is abstract
00084             self.assertTrue(isabstract(C))
00085             class D(C):
00086                 def bar(self): pass  # concrete override of concrete
00087             self.assertEqual(D.__abstractmethods__, {"foo"})
00088             self.assertRaises(TypeError, D)  # because foo is still abstract
00089             self.assertTrue(isabstract(D))
00090             class E(D):
00091                 def foo(self): pass
00092             self.assertEqual(E.__abstractmethods__, set())
00093             E()  # now foo is concrete, too
00094             self.assertFalse(isabstract(E))
00095             class F(E):
00096                 @abstractthing
00097                 def bar(self): pass  # abstract override of concrete
00098             self.assertEqual(F.__abstractmethods__, {"bar"})
00099             self.assertRaises(TypeError, F)  # because bar is abstract now
00100             self.assertTrue(isabstract(F))

Here is the call graph for this function:

Definition at line 22 of file test_abc.py.

00022 
00023     def test_abstractproperty_basics(self):
00024         @abc.abstractproperty
00025         def foo(self): pass
00026         self.assertTrue(foo.__isabstractmethod__)
00027         def bar(self): pass
00028         self.assertFalse(hasattr(bar, "__isabstractmethod__"))
00029 
00030         class C(metaclass=abc.ABCMeta):
00031             @abc.abstractproperty
00032             def foo(self): return 3
00033         class D(C):
00034             @property
00035             def foo(self): return super().foo
00036         self.assertEqual(D().foo, 3)

Here is the call graph for this function:

Definition at line 55 of file test_abc.py.

00055 
00056     def test_abstractstaticmethod_basics(self):
00057         @abc.abstractstaticmethod
00058         def foo(): pass
00059         self.assertTrue(foo.__isabstractmethod__)
00060         @staticmethod
00061         def bar(): pass
00062         self.assertFalse(hasattr(bar, "__isabstractmethod__"))
00063 
00064         class C(metaclass=abc.ABCMeta):
00065             @abc.abstractstaticmethod
00066             def foo(): return 3
00067         self.assertRaises(TypeError, C)
00068         class D(C):
00069             @staticmethod
00070             def foo(): return 4
00071         self.assertEqual(D.foo(), 4)
00072         self.assertEqual(D().foo(), 4)

Here is the call graph for this function:

Definition at line 230 of file test_abc.py.

00230 
00231     def test_all_new_methods_are_called(self):
00232         class A(metaclass=abc.ABCMeta):
00233             pass
00234         class B(object):
00235             counter = 0
00236             def __new__(cls):
00237                 B.counter += 1
00238                 return super().__new__(cls)
00239         class C(A, B):
00240             pass
00241         self.assertEqual(B.counter, 0)
00242         C()
00243         self.assertEqual(B.counter, 1)
00244 

Definition at line 137 of file test_abc.py.

00137 
00138     def test_isinstance_invalidation(self):
00139         class A(metaclass=abc.ABCMeta):
00140             pass
00141         class B:
00142             pass
00143         b = B()
00144         self.assertFalse(isinstance(b, A))
00145         self.assertFalse(isinstance(b, (A,)))
00146         A.register(B)
00147         self.assertTrue(isinstance(b, A))
00148         self.assertTrue(isinstance(b, (A,)))

Definition at line 101 of file test_abc.py.

00101 
00102     def test_metaclass_abc(self):
00103         # Metaclasses can be ABCs, too.
00104         class A(metaclass=abc.ABCMeta):
00105             @abc.abstractmethod
00106             def x(self):
00107                 pass
00108         self.assertEqual(A.__abstractmethods__, {"x"})
00109         class meta(type, A):
00110             def x(self):
00111                 return 1
00112         class C(metaclass=meta):
00113             pass

Definition at line 185 of file test_abc.py.

00185 
00186     def test_register_non_class(self):
00187         class A(metaclass=abc.ABCMeta):
00188             pass
00189         self.assertRaisesRegex(TypeError, "Can only register classes",
00190                                A.register, 4)

Here is the call graph for this function:

Definition at line 114 of file test_abc.py.

00114 
00115     def test_registration_basics(self):
00116         class A(metaclass=abc.ABCMeta):
00117             pass
00118         class B(object):
00119             pass
00120         b = B()
00121         self.assertFalse(issubclass(B, A))
00122         self.assertFalse(issubclass(B, (A,)))
00123         self.assertNotIsInstance(b, A)
00124         self.assertNotIsInstance(b, (A,))
00125         A.register(B)
00126         self.assertTrue(issubclass(B, A))
00127         self.assertTrue(issubclass(B, (A,)))
00128         self.assertIsInstance(b, A)
00129         self.assertIsInstance(b, (A,))
00130         class C(B):
00131             pass
00132         c = C()
00133         self.assertTrue(issubclass(C, A))
00134         self.assertTrue(issubclass(C, (A,)))
00135         self.assertIsInstance(c, A)
00136         self.assertIsInstance(c, (A,))

Definition at line 149 of file test_abc.py.

00149 
00150     def test_registration_builtins(self):
00151         class A(metaclass=abc.ABCMeta):
00152             pass
00153         A.register(int)
00154         self.assertIsInstance(42, A)
00155         self.assertIsInstance(42, (A,))
00156         self.assertTrue(issubclass(int, A))
00157         self.assertTrue(issubclass(int, (A,)))
00158         class B(A):
00159             pass
00160         B.register(str)
00161         class C(str): pass
00162         self.assertIsInstance("", A)
00163         self.assertIsInstance("", (A,))
00164         self.assertTrue(issubclass(str, A))
00165         self.assertTrue(issubclass(str, (A,)))
00166         self.assertTrue(issubclass(C, A))
00167         self.assertTrue(issubclass(C, (A,)))

Here is the call graph for this function:

Definition at line 168 of file test_abc.py.

00168 
00169     def test_registration_edge_cases(self):
00170         class A(metaclass=abc.ABCMeta):
00171             pass
00172         A.register(A)  # should pass silently
00173         class A1(A):
00174             pass
00175         self.assertRaises(RuntimeError, A1.register, A)  # cycles not allowed
00176         class B(object):
00177             pass
00178         A1.register(B)  # ok
00179         A1.register(B)  # should pass silently
00180         class C(A):
00181             pass
00182         A.register(C)  # should pass silently
00183         self.assertRaises(RuntimeError, C.register, A)  # cycles not allowed
00184         C.register(B)  # ok

Definition at line 191 of file test_abc.py.

00191 
00192     def test_registration_transitiveness(self):
00193         class A(metaclass=abc.ABCMeta):
00194             pass
00195         self.assertTrue(issubclass(A, A))
00196         self.assertTrue(issubclass(A, (A,)))
00197         class B(metaclass=abc.ABCMeta):
00198             pass
00199         self.assertFalse(issubclass(A, B))
00200         self.assertFalse(issubclass(A, (B,)))
00201         self.assertFalse(issubclass(B, A))
00202         self.assertFalse(issubclass(B, (A,)))
00203         class C(metaclass=abc.ABCMeta):
00204             pass
00205         A.register(B)
00206         class B1(B):
00207             pass
00208         self.assertTrue(issubclass(B1, A))
00209         self.assertTrue(issubclass(B1, (A,)))
00210         class C1(C):
00211             pass
00212         B1.register(C1)
00213         self.assertFalse(issubclass(C, B))
00214         self.assertFalse(issubclass(C, (B,)))
00215         self.assertFalse(issubclass(C, B1))
00216         self.assertFalse(issubclass(C, (B1,)))
00217         self.assertTrue(issubclass(C1, A))
00218         self.assertTrue(issubclass(C1, (A,)))
00219         self.assertTrue(issubclass(C1, B))
00220         self.assertTrue(issubclass(C1, (B,)))
00221         self.assertTrue(issubclass(C1, B1))
00222         self.assertTrue(issubclass(C1, (B1,)))
00223         C1.register(int)
00224         class MyInt(int):
00225             pass
00226         self.assertTrue(issubclass(MyInt, A))
00227         self.assertTrue(issubclass(MyInt, (A,)))
00228         self.assertIsInstance(42, A)
00229         self.assertIsInstance(42, (A,))

Here is the call graph for this function:


The documentation for this class was generated from the following file: