tests/basics: Improve test coverage for generators.

This commit is contained in:
Rami Ali 2016-12-20 12:57:39 +11:00 committed by Damien George
parent 1e7a801e2d
commit 5d06a74303
6 changed files with 47 additions and 1 deletions

View File

@ -85,3 +85,17 @@ try:
print(next(g))
except StopIteration:
print("StopIteration")
# case where generator ignores the close request and yields instead
def gen7():
try:
yield 123
except GeneratorExit:
yield 456
g = gen7()
print(next(g))
try:
g.close()
except RuntimeError:
print('RuntimeError')

View File

@ -17,3 +17,14 @@ try:
except TypeError:
print("got TypeError from downstream!")
# case where generator doesn't intercept the thrown/injected exception
def gen3():
yield 123
yield 456
g3 = gen3()
print(next(g3))
try:
g3.throw(StopIteration)
except StopIteration:
print('got StopIteration from downstream!')

View File

@ -0,0 +1,15 @@
# uPy differs from CPython for this test
# generator ignored GeneratorExit
def gen():
try:
yield 123
except GeneratorExit:
print('GeneratorExit')
yield 456
g = gen()
print(next(g))
try:
g.throw(GeneratorExit)
except RuntimeError:
print('RuntimeError')

View File

@ -0,0 +1,3 @@
123
GeneratorExit
RuntimeError

View File

@ -20,3 +20,6 @@ for val in f(3):
#print(gen.__next__())
#print(gen.__next__())
#print(gen.__next__())
# test printing, but only the first chars that match CPython
print(repr(f(0))[0:17])

View File

@ -271,7 +271,7 @@ def run_tests(pyb, tests, args):
# Some tests are known to fail with native emitter
# Remove them from the below when they work
if args.emit == 'native':
skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from gen_yield_from_close gen_yield_from_ducktype gen_yield_from_exc gen_yield_from_iter gen_yield_from_send gen_yield_from_stopped gen_yield_from_throw generator1 generator2 generator_args generator_close generator_closure generator_exc generator_return generator_send'.split()}) # require yield
skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from gen_yield_from_close gen_yield_from_ducktype gen_yield_from_exc gen_yield_from_iter gen_yield_from_send gen_yield_from_stopped gen_yield_from_throw gen_yield_from_throw2 generator1 generator2 generator_args generator_close generator_closure generator_exc generator_return generator_send'.split()}) # require yield
skip_tests.update({'basics/%s.py' % t for t in 'bytes_gen class_store_class globals_del string_join'.split()}) # require yield
skip_tests.update({'basics/async_%s.py' % t for t in 'def await await2 for for2 with with2'.split()}) # require yield
skip_tests.update({'basics/%s.py' % t for t in 'try_reraise try_reraise2'.split()}) # require raise_varargs