diff --git a/Lib/test/test_genexps.py b/Lib/test/test_genexps.py new file mode 100644 index 0000000000..ae16318ff4 --- /dev/null +++ b/Lib/test/test_genexps.py @@ -0,0 +1,294 @@ +import sys +import doctest +import unittest + + +doctests = """ + +Test simple loop with conditional + + >>> sum(i*i for i in range(100) if i&1 == 1) + 166650 + +Test simple nesting + + >>> list((i,j) for i in range(3) for j in range(4) ) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + +Test nesting with the inner expression dependent on the outer + + >>> list((i,j) for i in range(4) for j in range(i) ) + [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] + +Test the idiom for temporary variable assignment in comprehensions. + + >>> list((j*j for i in range(4) for j in [i+1])) + [1, 4, 9, 16] + >>> list((j*k for i in range(4) for j in [i+1] for k in [j+1])) + [2, 6, 12, 20] + >>> list((j*k for i in range(4) for j, k in [(i+1, i+2)])) + [2, 6, 12, 20] + +Not assignment + + >>> list((i*i for i in [*range(4)])) + [0, 1, 4, 9] + >>> list((i*i for i in (*range(4),))) + [0, 1, 4, 9] + +Make sure the induction variable is not exposed + + >>> i = 20 + >>> sum(i*i for i in range(100)) + 328350 + >>> i + 20 + +Test first class + + >>> g = (i*i for i in range(4)) + >>> type(g) + + >>> list(g) + [0, 1, 4, 9] + +Test direct calls to next() + + >>> g = (i*i for i in range(3)) + >>> next(g) + 0 + >>> next(g) + 1 + >>> next(g) + 4 + >>> next(g) + Traceback (most recent call last): + File "", line 1, in -toplevel- + next(g) + StopIteration + +Does it stay stopped? + + >>> next(g) + Traceback (most recent call last): + File "", line 1, in -toplevel- + next(g) + StopIteration + >>> list(g) + [] + +Test running gen when defining function is out of scope + + >>> def f(n): + ... return (i*i for i in range(n)) + >>> list(f(10)) + [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + + >>> def f(n): + ... return ((i,j) for i in range(3) for j in range(n)) + >>> list(f(4)) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + >>> def f(n): + ... return ((i,j) for i in range(3) for j in range(4) if j in range(n)) + >>> list(f(4)) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + >>> list(f(2)) + [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)] + +Verify that parenthesis are required in a statement + + >>> def f(n): + ... return i*i for i in range(n) + Traceback (most recent call last): + ... + SyntaxError: invalid syntax + +Verify that parenthesis are required when used as a keyword argument value + + >>> dict(a = i for i in range(10)) # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE + Traceback (most recent call last): + ... + SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? + +Verify that parenthesis are required when used as a keyword argument value + + >>> dict(a = (i for i in range(10))) #doctest: +ELLIPSIS + {'a': at ...>} + +Verify early binding for the outermost for-expression + + >>> x=10 + >>> g = (i*i for i in range(x)) + >>> x = 5 + >>> list(g) + [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + +Verify late binding for the outermost if-expression + + >>> include = (2,4,6,8) + >>> g = (i*i for i in range(10) if i in include) + >>> include = (1,3,5,7,9) + >>> list(g) + [1, 9, 25, 49, 81] + +Verify that the outermost for-expression makes an immediate check +for iterability + >>> (i for i in 6) + Traceback (most recent call last): + File "", line 1, in -toplevel- + (i for i in 6) + TypeError: 'int' object is not iterable + +Verify late binding for the innermost for-expression + + >>> g = ((i,j) for i in range(3) for j in range(x)) + >>> x = 4 + >>> list(g) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + +Verify re-use of tuples (a side benefit of using genexps over listcomps) + + >>> tupleids = list(map(id, ((i,i) for i in range(10)))) + >>> int(max(tupleids) - min(tupleids)) + 0 + +Verify that syntax error's are raised for genexps used as lvalues + + >>> (y for y in (1,2)) = 10 # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE + Traceback (most recent call last): + ... + SyntaxError: cannot assign to generator expression + + >>> (y for y in (1,2)) += 10 # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE + Traceback (most recent call last): + ... + SyntaxError: 'generator expression' is an illegal expression for augmented assignment + + +########### Tests borrowed from or inspired by test_generators.py ############ + +Make a generator that acts like range() + + >>> yrange = lambda n: (i for i in range(n)) + >>> list(yrange(10)) + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +Generators always return to the most recent caller: + + >>> def creator(): + ... r = yrange(5) + ... print("creator", next(r)) + ... return r + >>> def caller(): + ... r = creator() + ... for i in r: + ... print("caller", i) + >>> caller() + creator 0 + caller 1 + caller 2 + caller 3 + caller 4 + +Generators can call other generators: + + >>> def zrange(n): + ... for i in yrange(n): + ... yield i + >>> list(zrange(5)) + [0, 1, 2, 3, 4] + + +Verify that a gen exp cannot be resumed while it is actively running: + + >>> g = (next(me) for i in range(10)) + >>> me = g + >>> next(me) + Traceback (most recent call last): + File "", line 1, in -toplevel- + next(me) + File "", line 1, in + g = (next(me) for i in range(10)) + ValueError: generator already executing + +Verify exception propagation + + >>> g = (10 // i for i in (5, 0, 2)) + >>> next(g) + 2 + >>> next(g) + Traceback (most recent call last): + File "", line 1, in -toplevel- + next(g) + File "", line 1, in + g = (10 // i for i in (5, 0, 2)) + ZeroDivisionError: division by zero + >>> next(g) + Traceback (most recent call last): + File "", line 1, in -toplevel- + next(g) + StopIteration + +Make sure that None is a valid return value + + >>> list(None for i in range(10)) + [None, None, None, None, None, None, None, None, None, None] + +Check that generator attributes are present + + >>> g = (i*i for i in range(3)) + >>> expected = set(['gi_frame', 'gi_running']) + >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected + True + + >>> from test.support import HAVE_DOCSTRINGS + >>> print(g.__next__.__doc__ if HAVE_DOCSTRINGS else 'Implement next(self).') # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE + Implement next(self). + >>> import types + >>> isinstance(g, types.GeneratorType) + True + +Check the __iter__ slot is defined to return self + + >>> iter(g) is g + True + +Verify that the running flag is set properly + + >>> g = (me.gi_running for i in (0,1)) + >>> me = g + >>> me.gi_running + 0 + >>> next(me) + 1 + >>> me.gi_running + 0 + +Verify that genexps are weakly referencable + + >>> import weakref + >>> g = (i*i for i in range(4)) + >>> wr = weakref.ref(g) + >>> wr() is g + True + >>> p = weakref.proxy(g) + >>> list(p) + [0, 1, 4, 9] + + +""" + +# Trace function can throw off the tuple reuse test. +if hasattr(sys, 'gettrace') and sys.gettrace(): + __test__ = {} +else: + __test__ = {'doctests' : doctests} + +def load_tests(loader, tests, pattern): + from test.support.rustpython import DocTestChecker # TODO: RUSTPYTHON + tests.addTest(doctest.DocTestSuite(checker=DocTestChecker())) # TODO: RUSTPYTHON + return tests + + +if __name__ == "__main__": + unittest.main() diff --git a/Lib/test/test_metaclass.py b/Lib/test/test_metaclass.py index 1707df9075..1d3d6d1329 100644 --- a/Lib/test/test_metaclass.py +++ b/Lib/test/test_metaclass.py @@ -128,8 +128,7 @@ Check for duplicate keywords. - # TODO: RUSTPYTHON - >>> class C(metaclass=type, metaclass=type): pass # doctest: +SKIP + >>> class C(metaclass=type, metaclass=type): pass # TODO: RUSTPYTHON # doctest: +EXPECTED_FAILURE ... Traceback (most recent call last): [...] @@ -139,9 +138,7 @@ Another way. >>> kwds = {'metaclass': type} - - # TODO: RUSTPYTHON - >>> class C(metaclass=type, **kwds): pass # doctest: +SKIP + >>> class C(metaclass=type, **kwds): pass ... Traceback (most recent call last): [...] @@ -160,9 +157,7 @@ ... def __prepare__(name, bases): ... return LoggingDict() ... - - # TODO: RUSTPYTHON - >>> class C(metaclass=Meta): # doctest: +SKIP + >>> class C(metaclass=Meta): ... foo = 2+2 ... foo = 42 ... bar = 123 @@ -184,22 +179,16 @@ ... print("kw:", sorted(kwds.items())) ... return namespace ... - - # TODO: RUSTPYTHON - >>> class C(metaclass=meta): # doctest: +SKIP + >>> class C(metaclass=meta): ... a = 42 ... b = 24 ... meta: C () ns: [('__firstlineno__', 1), ('__module__', 'test.test_metaclass'), ('__qualname__', 'C'), ('__static_attributes__', ()), ('a', 42), ('b', 24)] kw: [] - - # TODO: RUSTPYTHON - >>> type(C) is dict # doctest: +SKIP + >>> type(C) is dict True - - # TODO: RUSTPYTHON - >>> print(sorted(C.items())) # doctest: +SKIP + >>> print(sorted(C.items())) [('__firstlineno__', 1), ('__module__', 'test.test_metaclass'), ('__qualname__', 'C'), ('__static_attributes__', ()), ('a', 42), ('b', 24)] >>> @@ -210,9 +199,7 @@ ... return LoggingDict() ... >>> meta.__prepare__ = prepare - - # TODO: RUSTPYTHON - >>> class C(metaclass=meta, other="booh"): # doctest: +SKIP + >>> class C(metaclass=meta, other="booh"): ... a = 1 ... a = 2 ... b = 3 @@ -281,23 +268,17 @@ ... >>> Base.value 1 - - # TODO: RUSTPYTHON; AttributeError: type object 'WeirdClass' has no attribute 'value' - >>> WeirdClass.value # doctest: +SKIP + >>> WeirdClass.value # TODO: RUSTPYTHON; AttributeError: type object 'WeirdClass' has no attribute 'value' # doctest: +SKIP 1 >>> Base.value = 2 >>> Base.value 2 - - # TODO: RUSTPYTHON; AttributeError: type object 'WeirdClass' has no attribute 'value' - >>> WeirdClass.value # doctest: +SKIP + >>> WeirdClass.value # TODO: RUSTPYTHON; AttributeError: type object 'WeirdClass' has no attribute 'value' # doctest: +SKIP 2 >>> Base.value = 3 >>> Base.value 3 - - # TODO: RUSTPYTHON; AttributeError: type object 'WeirdClass' has no attribute 'value' - >>> WeirdClass.value # doctest: +SKIP + >>> WeirdClass.value # TODO: RUSTPYTHON; AttributeError: type object 'WeirdClass' has no attribute 'value' # doctest: +SKIP 3 """ @@ -311,7 +292,8 @@ __test__ = {'doctests' : doctests} def load_tests(loader, tests, pattern): - tests.addTest(doctest.DocTestSuite()) + from test.support.rustpython import DocTestChecker # TODO: RUSTPYTHON + tests.addTest(doctest.DocTestSuite(checker=DocTestChecker())) return tests