Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_collections.TestOneTrickPonyABCs Class Reference
Inheritance diagram for test.test_collections.TestOneTrickPonyABCs:
Inheritance graph
[legend]
Collaboration diagram for test.test_collections.TestOneTrickPonyABCs:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_Hashable
def test_Iterable
def test_Iterator
def test_Sized
def test_Container
def test_Callable
def test_direct_subclassing
def test_registration
def validate_abstract_methods
def validate_isinstance
def validate_comparison

Public Attributes

 right_side

Detailed Description

Definition at line 394 of file test_collections.py.


Member Function Documentation

Definition at line 508 of file test_collections.py.

00508 
00509     def test_Callable(self):
00510         non_samples = [None, 42, 3.14, 1j,
00511                        "", b"", (), [], {}, set(),
00512                        (lambda: (yield))(),
00513                        (x for x in []),
00514                        ]
00515         for x in non_samples:
00516             self.assertNotIsInstance(x, Callable)
00517             self.assertFalse(issubclass(type(x), Callable), repr(type(x)))
00518         samples = [lambda: None,
00519                    type, int, object,
00520                    len,
00521                    list.append, [].append,
00522                    ]
00523         for x in samples:
00524             self.assertIsInstance(x, Callable)
00525             self.assertTrue(issubclass(type(x), Callable), repr(type(x)))
00526         self.validate_abstract_methods(Callable, '__call__')
00527         self.validate_isinstance(Callable, '__call__')

Here is the call graph for this function:

Definition at line 490 of file test_collections.py.

00490 
00491     def test_Container(self):
00492         non_samples = [None, 42, 3.14, 1j,
00493                        (lambda: (yield))(),
00494                        (x for x in []),
00495                        ]
00496         for x in non_samples:
00497             self.assertNotIsInstance(x, Container)
00498             self.assertFalse(issubclass(type(x), Container), repr(type(x)))
00499         samples = [bytes(), str(),
00500                    tuple(), list(), set(), frozenset(), dict(),
00501                    dict().keys(), dict().items(),
00502                    ]
00503         for x in samples:
00504             self.assertIsInstance(x, Container)
00505             self.assertTrue(issubclass(type(x), Container), repr(type(x)))
00506         self.validate_abstract_methods(Container, '__contains__')
00507         self.validate_isinstance(Container, '__contains__')

Here is the call graph for this function:

Definition at line 528 of file test_collections.py.

00528 
00529     def test_direct_subclassing(self):
00530         for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
00531             class C(B):
00532                 pass
00533             self.assertTrue(issubclass(C, B))
00534             self.assertFalse(issubclass(int, C))

Definition at line 396 of file test_collections.py.

00396 
00397     def test_Hashable(self):
00398         # Check some non-hashables
00399         non_samples = [bytearray(), list(), set(), dict()]
00400         for x in non_samples:
00401             self.assertNotIsInstance(x, Hashable)
00402             self.assertFalse(issubclass(type(x), Hashable), repr(type(x)))
00403         # Check some hashables
00404         samples = [None,
00405                    int(), float(), complex(),
00406                    str(),
00407                    tuple(), frozenset(),
00408                    int, list, object, type, bytes()
00409                    ]
00410         for x in samples:
00411             self.assertIsInstance(x, Hashable)
00412             self.assertTrue(issubclass(type(x), Hashable), repr(type(x)))
00413         self.assertRaises(TypeError, Hashable)
00414         # Check direct subclassing
00415         class H(Hashable):
00416             def __hash__(self):
00417                 return super().__hash__()
00418         self.assertEqual(hash(H()), 0)
00419         self.assertFalse(issubclass(int, H))
00420         self.validate_abstract_methods(Hashable, '__hash__')
00421         self.validate_isinstance(Hashable, '__hash__')

Here is the call graph for this function:

Definition at line 422 of file test_collections.py.

00422 
00423     def test_Iterable(self):
00424         # Check some non-iterables
00425         non_samples = [None, 42, 3.14, 1j]
00426         for x in non_samples:
00427             self.assertNotIsInstance(x, Iterable)
00428             self.assertFalse(issubclass(type(x), Iterable), repr(type(x)))
00429         # Check some iterables
00430         samples = [bytes(), str(),
00431                    tuple(), list(), set(), frozenset(), dict(),
00432                    dict().keys(), dict().items(), dict().values(),
00433                    (lambda: (yield))(),
00434                    (x for x in []),
00435                    ]
00436         for x in samples:
00437             self.assertIsInstance(x, Iterable)
00438             self.assertTrue(issubclass(type(x), Iterable), repr(type(x)))
00439         # Check direct subclassing
00440         class I(Iterable):
00441             def __iter__(self):
00442                 return super().__iter__()
00443         self.assertEqual(list(I()), [])
00444         self.assertFalse(issubclass(str, I))
00445         self.validate_abstract_methods(Iterable, '__iter__')
00446         self.validate_isinstance(Iterable, '__iter__')

Here is the call graph for this function:

Definition at line 447 of file test_collections.py.

00447 
00448     def test_Iterator(self):
00449         non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()]
00450         for x in non_samples:
00451             self.assertNotIsInstance(x, Iterator)
00452             self.assertFalse(issubclass(type(x), Iterator), repr(type(x)))
00453         samples = [iter(bytes()), iter(str()),
00454                    iter(tuple()), iter(list()), iter(dict()),
00455                    iter(set()), iter(frozenset()),
00456                    iter(dict().keys()), iter(dict().items()),
00457                    iter(dict().values()),
00458                    (lambda: (yield))(),
00459                    (x for x in []),
00460                    ]
00461         for x in samples:
00462             self.assertIsInstance(x, Iterator)
00463             self.assertTrue(issubclass(type(x), Iterator), repr(type(x)))
00464         self.validate_abstract_methods(Iterator, '__next__', '__iter__')
00465 
00466         # Issue 10565
00467         class NextOnly:
00468             def __next__(self):
00469                 yield 1
00470                 raise StopIteration
00471         self.assertNotIsInstance(NextOnly(), Iterator)

Here is the call graph for this function:

Definition at line 535 of file test_collections.py.

00535 
00536     def test_registration(self):
00537         for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
00538             class C:
00539                 __hash__ = None  # Make sure it isn't hashable by default
00540             self.assertFalse(issubclass(C, B), B.__name__)
00541             B.register(C)
00542             self.assertTrue(issubclass(C, B))

Here is the call graph for this function:

Definition at line 472 of file test_collections.py.

00472 
00473     def test_Sized(self):
00474         non_samples = [None, 42, 3.14, 1j,
00475                        (lambda: (yield))(),
00476                        (x for x in []),
00477                        ]
00478         for x in non_samples:
00479             self.assertNotIsInstance(x, Sized)
00480             self.assertFalse(issubclass(type(x), Sized), repr(type(x)))
00481         samples = [bytes(), str(),
00482                    tuple(), list(), set(), frozenset(), dict(),
00483                    dict().keys(), dict().items(), dict().values(),
00484                    ]
00485         for x in samples:
00486             self.assertIsInstance(x, Sized)
00487             self.assertTrue(issubclass(type(x), Sized), repr(type(x)))
00488         self.validate_abstract_methods(Sized, '__len__')
00489         self.validate_isinstance(Sized, '__len__')

Here is the call graph for this function:

def test.test_collections.ABCTestCase.validate_abstract_methods (   self,
  abc,
  names 
) [inherited]

Definition at line 337 of file test_collections.py.

00337 
00338     def validate_abstract_methods(self, abc, *names):
00339         methodstubs = dict.fromkeys(names, lambda s, *args: 0)
00340 
00341         # everything should work will all required methods are present
00342         C = type('C', (abc,), methodstubs)
00343         C()
00344 
00345         # instantiation should fail if a required method is missing
00346         for name in names:
00347             stubs = methodstubs.copy()
00348             del stubs[name]
00349             C = type('C', (abc,), stubs)
00350             self.assertRaises(TypeError, C, name)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_collections.ABCTestCase.validate_comparison (   self,
  instance 
) [inherited]

Definition at line 363 of file test_collections.py.

00363 
00364     def validate_comparison(self, instance):
00365         ops = ['lt', 'gt', 'le', 'ge', 'ne', 'or', 'and', 'xor', 'sub']
00366         operators = {}
00367         for op in ops:
00368             name = '__' + op + '__'
00369             operators[name] = getattr(operator, name)
00370 
00371         class Other:
00372             def __init__(self):
00373                 self.right_side = False
00374             def __eq__(self, other):
00375                 self.right_side = True
00376                 return True
00377             __lt__ = __eq__
00378             __gt__ = __eq__
00379             __le__ = __eq__
00380             __ge__ = __eq__
00381             __ne__ = __eq__
00382             __ror__ = __eq__
00383             __rand__ = __eq__
00384             __rxor__ = __eq__
00385             __rsub__ = __eq__
00386 
00387         for name, op in operators.items():
00388             if not hasattr(instance, name):
00389                 continue
00390             other = Other()
00391             op(instance, other)
00392             self.assertTrue(other.right_side,'Right side not called for %s.%s'
00393                             % (type(instance), name))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_collections.ABCTestCase.validate_isinstance (   self,
  abc,
  name 
) [inherited]

Definition at line 351 of file test_collections.py.

00351 
00352     def validate_isinstance(self, abc, name):
00353         stub = lambda s, *args: 0
00354 
00355         C = type('C', (object,), {'__hash__': None})
00356         setattr(C, name, stub)
00357         self.assertIsInstance(C(), abc)
00358         self.assertTrue(issubclass(C, abc))
00359 
00360         C = type('C', (object,), {'__hash__': None})
00361         self.assertNotIsInstance(C(), abc)
00362         self.assertFalse(issubclass(C, abc))

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 372 of file test_collections.py.


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