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

Method test_Generator

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

Source from the content-addressed store, hash-verified

1169 self.assertNotIsInstance(NextOnly(), Iterator)
1170
1171 def test_Generator(self):
1172 class NonGen1:
1173 def __iter__(self): return self
1174 def __next__(self): return None
1175 def close(self): pass
1176 def throw(self, typ, val=None, tb=None): pass
1177
1178 class NonGen2:
1179 def __iter__(self): return self
1180 def __next__(self): return None
1181 def close(self): pass
1182 def send(self, value): return value
1183
1184 class NonGen3:
1185 def close(self): pass
1186 def send(self, value): return value
1187 def throw(self, typ, val=None, tb=None): pass
1188
1189 non_samples = [
1190 None, 42, 3.14, 1j, b"", "", (), [], {}, set(),
1191 iter(()), iter([]), NonGen1(), NonGen2(), NonGen3()]
1192 for x in non_samples:
1193 self.assertNotIsInstance(x, Generator)
1194 self.assertNotIsSubclass(type(x), Generator)
1195
1196 class Gen:
1197 def __iter__(self): return self
1198 def __next__(self): return None
1199 def close(self): pass
1200 def send(self, value): return value
1201 def throw(self, typ, val=None, tb=None): pass
1202
1203 class MinimalGen(Generator):
1204 def send(self, value):
1205 return value
1206 def throw(self, typ, val=None, tb=None):
1207 super().throw(typ, val, tb)
1208
1209 def gen():
1210 yield 1
1211
1212 samples = [gen(), (lambda: (yield))(), Gen(), MinimalGen()]
1213 for x in samples:
1214 self.assertIsInstance(x, Iterator)
1215 self.assertIsInstance(x, Generator)
1216 self.assertIsSubclass(type(x), Generator)
1217 self.validate_abstract_methods(Generator, 'send', 'throw')
1218
1219 # mixin tests
1220 mgen = MinimalGen()
1221 self.assertIs(mgen, iter(mgen))
1222 self.assertIs(mgen.send(None), next(mgen))
1223 self.assertEqual(2, mgen.send(2))
1224 self.assertIsNone(mgen.close())
1225 self.assertRaises(ValueError, mgen.throw, ValueError)
1226 self.assertRaisesRegex(ValueError, "^huhu$",
1227 mgen.throw, ValueError, ValueError("huhu"))
1228 self.assertRaises(StopIteration, mgen.throw, StopIteration())

Callers

nothing calls this directly

Calls 15

sendMethod · 0.95
setFunction · 0.85
iterFunction · 0.85
NonGen1Class · 0.85
NonGen2Class · 0.85
NonGen3Class · 0.85
MinimalGenClass · 0.85
nextFunction · 0.85
FailOnCloseClass · 0.85
IgnoreGeneratorExitClass · 0.85
assertNotIsInstanceMethod · 0.80
assertIsInstanceMethod · 0.80

Tested by

no test coverage detected