Back to index

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

List of all members.

Public Member Functions

def test_Set
def test_hash_Set
def test_MutableSet
def test_issue_5647
def test_issue_4920
def test_issue8750
def test_Mapping
def test_MutableMapping
def test_MutableMapping_subclass
def test_Sequence
def test_ByteString
def test_MutableSequence
def validate_abstract_methods
def validate_isinstance
def validate_comparison

Public Attributes

 contents
 right_side

Private Attributes

 __s

Detailed Description

Definition at line 563 of file test_collections.py.


Member Function Documentation

Definition at line 711 of file test_collections.py.

00711 
00712     def test_ByteString(self):
00713         for sample in [bytes, bytearray]:
00714             self.assertIsInstance(sample(), ByteString)
00715             self.assertTrue(issubclass(sample, ByteString))
00716         for sample in [str, list, tuple]:
00717             self.assertNotIsInstance(sample(), ByteString)
00718             self.assertFalse(issubclass(sample, ByteString))
00719         self.assertNotIsInstance(memoryview(b""), ByteString)
00720         self.assertFalse(issubclass(memoryview, ByteString))

Here is the call graph for this function:

Definition at line 583 of file test_collections.py.

00583 
00584     def test_hash_Set(self):
00585         class OneTwoThreeSet(Set):
00586             def __init__(self):
00587                 self.contents = [1, 2, 3]
00588             def __contains__(self, x):
00589                 return x in self.contents
00590             def __len__(self):
00591                 return len(self.contents)
00592             def __iter__(self):
00593                 return iter(self.contents)
00594             def __hash__(self):
00595                 return self._hash()
00596         a, b = OneTwoThreeSet(), OneTwoThreeSet()
00597         self.assertTrue(hash(a) == hash(b))

Here is the call graph for this function:

Definition at line 639 of file test_collections.py.

00639 
00640     def test_issue8750(self):
00641         empty = WithSet()
00642         full = WithSet(range(10))
00643         s = WithSet(full)
00644         s -= s
00645         self.assertEqual(s, empty)
00646         s = WithSet(full)
00647         s ^= s
00648         self.assertEqual(s, empty)
00649         s = WithSet(full)
00650         s &= s
00651         self.assertEqual(s, full)
00652         s |= s
00653         self.assertEqual(s, full)

Here is the call graph for this function:

Definition at line 612 of file test_collections.py.

00612 
00613     def test_issue_4920(self):
00614         # MutableSet.pop() method did not work
00615         class MySet(MutableSet):
00616             __slots__=['__s']
00617             def __init__(self,items=None):
00618                 if items is None:
00619                     items=[]
00620                 self.__s=set(items)
00621             def __contains__(self,v):
00622                 return v in self.__s
00623             def __iter__(self):
00624                 return iter(self.__s)
00625             def __len__(self):
00626                 return len(self.__s)
00627             def add(self,v):
00628                 result=v not in self.__s
00629                 self.__s.add(v)
00630                 return result
00631             def discard(self,v):
00632                 result=v in self.__s
00633                 self.__s.discard(v)
00634                 return result
00635             def __repr__(self):
00636                 return "MySet(%s)" % repr(list(self))
00637         s = MySet([5,43,2,1])
00638         self.assertEqual(s.pop(), 1)

Here is the call graph for this function:

Definition at line 606 of file test_collections.py.

00606 
00607     def test_issue_5647(self):
00608         # MutableSet.__iand__ mutated the set during iteration
00609         s = WithSet('abcd')
00610         s &= WithSet('cdef')            # This used to fail
00611         self.assertEqual(set(s), set('cd'))

Here is the call graph for this function:

Definition at line 654 of file test_collections.py.

00654 
00655     def test_Mapping(self):
00656         for sample in [dict]:
00657             self.assertIsInstance(sample(), Mapping)
00658             self.assertTrue(issubclass(sample, Mapping))
00659         self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__',
00660             '__getitem__')
00661         class MyMapping(Mapping):
00662             def __len__(self):
00663                 return 0
00664             def __getitem__(self, i):
00665                 raise IndexError
00666             def __iter__(self):
00667                 return iter(())
00668         self.validate_comparison(MyMapping())

Here is the call graph for this function:

Definition at line 669 of file test_collections.py.

00669 
00670     def test_MutableMapping(self):
00671         for sample in [dict]:
00672             self.assertIsInstance(sample(), MutableMapping)
00673             self.assertTrue(issubclass(sample, MutableMapping))
00674         self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__', '__len__',
00675             '__getitem__', '__setitem__', '__delitem__')

Here is the call graph for this function:

Definition at line 676 of file test_collections.py.

00676 
00677     def test_MutableMapping_subclass(self):
00678         # Test issue 9214
00679         mymap = UserDict()
00680         mymap['red'] = 5
00681         self.assertIsInstance(mymap.keys(), Set)
00682         self.assertIsInstance(mymap.keys(), KeysView)
00683         self.assertIsInstance(mymap.items(), Set)
00684         self.assertIsInstance(mymap.items(), ItemsView)
00685 
00686         mymap = UserDict()
00687         mymap['red'] = 5
00688         z = mymap.keys() | {'orange'}
00689         self.assertIsInstance(z, set)
00690         list(z)
00691         mymap['blue'] = 7               # Shouldn't affect 'z'
00692         self.assertEqual(sorted(z), ['orange', 'red'])
00693 
00694         mymap = UserDict()
00695         mymap['red'] = 5
00696         z = mymap.items() | {('orange', 3)}
00697         self.assertIsInstance(z, set)
00698         list(z)
00699         mymap['blue'] = 7               # Shouldn't affect 'z'
00700         self.assertEqual(sorted(z), [('orange', 3), ('red', 5)])

Here is the call graph for this function:

Definition at line 721 of file test_collections.py.

00721 
00722     def test_MutableSequence(self):
00723         for sample in [tuple, str, bytes]:
00724             self.assertNotIsInstance(sample(), MutableSequence)
00725             self.assertFalse(issubclass(sample, MutableSequence))
00726         for sample in [list, bytearray]:
00727             self.assertIsInstance(sample(), MutableSequence)
00728             self.assertTrue(issubclass(sample, MutableSequence))
00729         self.assertFalse(issubclass(str, MutableSequence))
00730         self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
00731             '__len__', '__getitem__', '__setitem__', '__delitem__', 'insert')
00732 

Here is the call graph for this function:

Definition at line 598 of file test_collections.py.

00598 
00599     def test_MutableSet(self):
00600         self.assertIsInstance(set(), MutableSet)
00601         self.assertTrue(issubclass(set, MutableSet))
00602         self.assertNotIsInstance(frozenset(), MutableSet)
00603         self.assertFalse(issubclass(frozenset, MutableSet))
00604         self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__',
00605             'add', 'discard')

Here is the call graph for this function:

Definition at line 701 of file test_collections.py.

00701 
00702     def test_Sequence(self):
00703         for sample in [tuple, list, bytes, str]:
00704             self.assertIsInstance(sample(), Sequence)
00705             self.assertTrue(issubclass(sample, Sequence))
00706         self.assertIsInstance(range(10), Sequence)
00707         self.assertTrue(issubclass(range, Sequence))
00708         self.assertTrue(issubclass(str, Sequence))
00709         self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__',
00710             '__getitem__')

Here is the call graph for this function:

Definition at line 569 of file test_collections.py.

00569 
00570     def test_Set(self):
00571         for sample in [set, frozenset]:
00572             self.assertIsInstance(sample(), Set)
00573             self.assertTrue(issubclass(sample, Set))
00574         self.validate_abstract_methods(Set, '__contains__', '__iter__', '__len__')
00575         class MySet(Set):
00576             def __contains__(self, x):
00577                 return False
00578             def __len__(self):
00579                 return 0
00580             def __iter__(self):
00581                 return iter([])
00582         self.validate_comparison(MySet())

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 619 of file test_collections.py.

Definition at line 586 of file test_collections.py.

Definition at line 372 of file test_collections.py.


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