MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_Collection

Method test_Collection

Lib/test/test_collections.py:1059–1143  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1057 self.assertNotIsInstance(RevRevBlocked(), Reversible)
1058
1059 def test_Collection(self):
1060 # Check some non-collections
1061 non_collections = [None, 42, 3.14, 1j, lambda x: 2*x]
1062 for x in non_collections:
1063 self.assertNotIsInstance(x, Collection)
1064 self.assertNotIsSubclass(type(x), Collection)
1065 # Check some non-collection iterables
1066 non_col_iterables = [_test_gen(), iter(b''), iter(bytearray()),
1067 (x for x in [])]
1068 for x in non_col_iterables:
1069 self.assertNotIsInstance(x, Collection)
1070 self.assertNotIsSubclass(type(x), Collection)
1071 # Check some collections
1072 samples = [set(), frozenset(), dict(), bytes(), str(), tuple(),
1073 list(), dict().keys(), dict().items(), dict().values()]
1074 for x in samples:
1075 self.assertIsInstance(x, Collection)
1076 self.assertIsSubclass(type(x), Collection)
1077 # Check also Mapping, MutableMapping, etc.
1078 self.assertIsSubclass(Sequence, Collection)
1079 self.assertIsSubclass(Mapping, Collection)
1080 self.assertIsSubclass(MutableMapping, Collection)
1081 self.assertIsSubclass(Set, Collection)
1082 self.assertIsSubclass(MutableSet, Collection)
1083 self.assertIsSubclass(Sequence, Collection)
1084 # Check direct subclassing
1085 class Col(Collection):
1086 def __iter__(self):
1087 return iter(list())
1088 def __len__(self):
1089 return 0
1090 def __contains__(self, item):
1091 return False
1092 class DerCol(Col): pass
1093 self.assertEqual(list(iter(Col())), [])
1094 self.assertNotIsSubclass(list, Col)
1095 self.assertNotIsSubclass(set, Col)
1096 self.assertNotIsSubclass(float, Col)
1097 self.assertEqual(list(iter(DerCol())), [])
1098 self.assertNotIsSubclass(list, DerCol)
1099 self.assertNotIsSubclass(set, DerCol)
1100 self.assertNotIsSubclass(float, DerCol)
1101 self.validate_abstract_methods(Collection, '__len__', '__iter__',
1102 '__contains__')
1103 # Check sized container non-iterable (which is not Collection) etc.
1104 class ColNoIter:
1105 def __len__(self): return 0
1106 def __contains__(self, item): return False
1107 class ColNoSize:
1108 def __iter__(self): return iter([])
1109 def __contains__(self, item): return False
1110 class ColNoCont:
1111 def __iter__(self): return iter([])
1112 def __len__(self): return 0
1113 self.assertNotIsSubclass(ColNoIter, Collection)
1114 self.assertNotIsInstance(ColNoIter(), Collection)
1115 self.assertNotIsSubclass(ColNoSize, Collection)
1116 self.assertNotIsInstance(ColNoSize(), Collection)

Callers

nothing calls this directly

Calls 15

_test_genFunction · 0.85
iterFunction · 0.85
setFunction · 0.85
strFunction · 0.85
listClass · 0.85
ColClass · 0.85
DerColClass · 0.85
ColNoIterClass · 0.85
ColNoSizeClass · 0.85
ColNoContClass · 0.85
SizeBlockClass · 0.85
IterBlockClass · 0.85

Tested by

no test coverage detected