Home | History | Annotate | Download | only in test
      1 doctests = """
      2 ########### Tests borrowed from or inspired by test_genexps.py ############
      3 
      4 Test simple loop with conditional
      5 
      6     >>> sum([i*i for i in range(100) if i&1 == 1])
      7     166650
      8 
      9 Test simple nesting
     10 
     11     >>> [(i,j) for i in range(3) for j in range(4)]
     12     [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
     13 
     14 Test nesting with the inner expression dependent on the outer
     15 
     16     >>> [(i,j) for i in range(4) for j in range(i)]
     17     [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
     18 
     19 Make sure the induction variable is not exposed
     20 
     21     >>> i = 20
     22     >>> sum([i*i for i in range(100)])
     23     328350
     24 
     25     >>> i
     26     20
     27 
     28 Verify that syntax error's are raised for listcomps used as lvalues
     29 
     30     >>> [y for y in (1,2)] = 10          # doctest: +IGNORE_EXCEPTION_DETAIL
     31     Traceback (most recent call last):
     32        ...
     33     SyntaxError: ...
     34 
     35     >>> [y for y in (1,2)] += 10         # doctest: +IGNORE_EXCEPTION_DETAIL
     36     Traceback (most recent call last):
     37        ...
     38     SyntaxError: ...
     39 
     40 
     41 ########### Tests borrowed from or inspired by test_generators.py ############
     42 
     43 Make a nested list comprehension that acts like range()
     44 
     45     >>> def frange(n):
     46     ...     return [i for i in range(n)]
     47     >>> frange(10)
     48     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     49 
     50 Same again, only as a lambda expression instead of a function definition
     51 
     52     >>> lrange = lambda n:  [i for i in range(n)]
     53     >>> lrange(10)
     54     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     55 
     56 Generators can call other generators:
     57 
     58     >>> def grange(n):
     59     ...     for x in [i for i in range(n)]:
     60     ...         yield x
     61     >>> list(grange(5))
     62     [0, 1, 2, 3, 4]
     63 
     64 
     65 Make sure that None is a valid return value
     66 
     67     >>> [None for i in range(10)]
     68     [None, None, None, None, None, None, None, None, None, None]
     69 
     70 ########### Tests for various scoping corner cases ############
     71 
     72 Return lambdas that use the iteration variable as a default argument
     73 
     74     >>> items = [(lambda i=i: i) for i in range(5)]
     75     >>> [x() for x in items]
     76     [0, 1, 2, 3, 4]
     77 
     78 Same again, only this time as a closure variable
     79 
     80     >>> items = [(lambda: i) for i in range(5)]
     81     >>> [x() for x in items]
     82     [4, 4, 4, 4, 4]
     83 
     84 Another way to test that the iteration variable is local to the list comp
     85 
     86     >>> items = [(lambda: i) for i in range(5)]
     87     >>> i = 20
     88     >>> [x() for x in items]
     89     [4, 4, 4, 4, 4]
     90 
     91 And confirm that a closure can jump over the list comp scope
     92 
     93     >>> items = [(lambda: y) for i in range(5)]
     94     >>> y = 2
     95     >>> [x() for x in items]
     96     [2, 2, 2, 2, 2]
     97 
     98 We also repeat each of the above scoping tests inside a function
     99 
    100     >>> def test_func():
    101     ...     items = [(lambda i=i: i) for i in range(5)]
    102     ...     return [x() for x in items]
    103     >>> test_func()
    104     [0, 1, 2, 3, 4]
    105 
    106     >>> def test_func():
    107     ...     items = [(lambda: i) for i in range(5)]
    108     ...     return [x() for x in items]
    109     >>> test_func()
    110     [4, 4, 4, 4, 4]
    111 
    112     >>> def test_func():
    113     ...     items = [(lambda: i) for i in range(5)]
    114     ...     i = 20
    115     ...     return [x() for x in items]
    116     >>> test_func()
    117     [4, 4, 4, 4, 4]
    118 
    119     >>> def test_func():
    120     ...     items = [(lambda: y) for i in range(5)]
    121     ...     y = 2
    122     ...     return [x() for x in items]
    123     >>> test_func()
    124     [2, 2, 2, 2, 2]
    125 
    126 """
    127 
    128 
    129 __test__ = {'doctests' : doctests}
    130 
    131 def test_main(verbose=None):
    132     import sys
    133     from test import support
    134     from test import test_listcomps
    135     support.run_doctest(test_listcomps, verbose)
    136 
    137     # verify reference counting
    138     if verbose and hasattr(sys, "gettotalrefcount"):
    139         import gc
    140         counts = [None] * 5
    141         for i in range(len(counts)):
    142             support.run_doctest(test_genexps, verbose)
    143             gc.collect()
    144             counts[i] = sys.gettotalrefcount()
    145         print(counts)
    146 
    147 if __name__ == "__main__":
    148     test_main(verbose=True)
    149