Lichen

pyparser/test/expressions.py

788:9ec1872daea4
2017-03-28 Paul Boddie Changed the location notations and added output of alias information.
     1 """     2 list of tested expressions / suites (used by test_parser and test_astbuilder)     3 """     4      5 constants = [     6     "0",     7     "7",     8     "-3",     9     "053",    10     "0x18",    11     "14L",    12     "1.0",    13     "3.9",    14     "-3.6",    15     "1.8e19",    16     "90000000000000",    17     "90000000000000.",    18     "3j"    19     ]    20     21 expressions = [    22     "x = a + 1",    23     "x = 1 - a",    24     "x = a * b",    25     "x = a ** 2",    26     "x = a / b",    27     "x = a & b",    28     "x = a | b",    29     "x = a ^ b",    30     "x = a // b",    31     "x = a * b + 1",    32     "x = a + 1 * b",    33     "x = a * b / c",    34     "x = a * (1 + c)",    35     "x, y, z = 1, 2, 3",    36     "x = 'a' 'b' 'c'",    37     "del foo",    38     "del foo[bar]",    39     "del foo.bar",    40     "l[0]",    41     "k[v,]",    42     "m[a,b]",    43     "a.b.c[d]",    44     "file('some.txt').read()",    45     "a[0].read()",    46     "a[1:1].read()",    47     "f('foo')('bar')('spam')",    48     "f('foo')('bar')('spam').read()[0]",    49     "a.b[0][0]",    50     "a.b[0][:]",    51     "a.b[0][::]",    52     "a.b[0][0].pop()[0].push('bar')('baz').spam",    53     "a.b[0].read()[1][2].foo().spam()[0].bar",    54     "a**2",    55     "a**2**2",    56     "a.b[0]**2",    57     "a.b[0].read()[1][2].foo().spam()[0].bar ** 2",    58     "l[start:end] = l2",    59     "l[::] = l2",    60     "a = `s`",    61     "a = `1 + 2 + f(3, 4)`",    62     "[a, b] = c",    63     "(a, b) = c",    64     "[a, (b,c), d] = e",    65     "a, (b, c), d = e",    66     ]    67     68 # We do not export the following tests because we would have to implement 2.5    69 # features in the stable compiler (other than just building the AST).    70 expressions_inbetweenversions = expressions + [    71     "1 if True else 2",    72     "1 if False else 2",    73     ]    74     75 funccalls = [    76     "l = func()",    77     "l = func(10)",    78     "l = func(10, 12, a, b=c, *args)",    79     "l = func(10, 12, a, b=c, **kwargs)",    80     "l = func(10, 12, a, b=c, *args, **kwargs)",    81     "l = func(10, 12, a, b=c)",    82     "e = l.pop(3)",    83     "e = k.l.pop(3)",    84     "simplefilter('ignore', category=PendingDeprecationWarning, append=1)",    85     """methodmap = dict(subdirs=phase4,    86                         same_files=phase3, diff_files=phase3, funny_files=phase3,    87                         common_dirs = phase2, common_files=phase2, common_funny=phase2,    88                         common=phase1, left_only=phase1, right_only=phase1,    89                         left_list=phase0, right_list=phase0)""",    90     "odata = b2a_qp(data, quotetabs = quotetabs, header = header)",    91     ]    92     93 listmakers = [    94     "l = []",    95     "l = [1, 2, 3]",    96     "l = [i for i in range(10)]",    97     "l = [i for i in range(10) if i%2 == 0]",    98     "l = [i for i in range(10) if i%2 == 0 or i%2 == 1]", # <--    99     "l = [i for i in range(10) if i%2 == 0 and i%2 == 1]",   100     "l = [i for j in range(10) for i in range(j)]",   101     "l = [i for j in range(10) for i in range(j) if j%2 == 0]",   102     "l = [i for j in range(10) for i in range(j) if j%2 == 0 and i%2 == 0]",   103     "l = [(a, b) for (a,b,c) in l2]",   104     "l = [{a:b} for (a,b,c) in l2]",   105     "l = [i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0]",   106     ]   107    108 genexps = [   109     "l = (i for i in j)",   110     "l = (i for i in j if i%2 == 0)",   111     "l = (i for j in k for i in j)",   112     "l = (i for j in k for i in j if j%2==0)",   113     "l = (i for j in k if j%2 == 0 if j*2 < 20 for i in j if i%2==0)",   114     "l = (i for i in [ j*2 for j in range(10) ] )",   115     "l = [i for i in ( j*2 for j in range(10) ) ]",   116     "l = (i for i in [ j*2 for j in ( k*3 for k in range(10) ) ] )",   117     "l = [i for j in ( j*2 for j in [ k*3 for k in range(10) ] ) ]",   118     "l = f(i for i in j)",   119     ]   120    121    122 dictmakers = [   123     "l = {a : b, 'c' : 0}",   124     "l = {}",   125     ]   126    127 backtrackings = [   128     "f = lambda x: x+1",   129     "f = lambda x,y: x+y",   130     "f = lambda x,y=1,z=t: x+y",   131     "f = lambda x,y=1,z=t,*args,**kwargs: x+y",   132     "f = lambda x,y=1,z=t,*args: x+y",   133     "f = lambda x,y=1,z=t,**kwargs: x+y",   134     "f = lambda: 1",   135     "f = lambda *args: 1",   136     "f = lambda **kwargs: 1",   137     ]   138    139 comparisons = [   140     "a < b",   141     "a > b",   142     "a not in b",   143     "a is not b",   144     "a in b",   145     "a is b",   146     "3 < x < 5",   147     "(3 < x) < 5",   148     "a < b < c < d",   149     "(a < b) < (c < d)",   150     "a < (b < c) < d",   151     ]   152    153 multiexpr = [   154     'a = b; c = d;',   155     'a = b = c = d',   156     ]   157    158 attraccess = [   159     'a.b = 2',   160     'x = a.b',   161     ]   162    163 slices = [   164     "l[:]",   165     "l[::]",   166     "l[1:2]",   167     "l[1:]",   168     "l[:2]",   169     "l[1::]",   170     "l[:1:]",   171     "l[::1]",   172     "l[1:2:]",   173     "l[:1:2]",   174     "l[1::2]",   175     "l[0:1:2]",   176     "a.b.l[:]",   177     "a.b.l[1:2]",   178     "a.b.l[1:]",   179     "a.b.l[:2]",   180     "a.b.l[0:1:2]",   181     "a[1:2:3, 100]",   182     "a[:2:3, 100]",   183     "a[1::3, 100,]",   184     "a[1:2:, 100]",   185     "a[1:2, 100]",   186     "a[1:, 100,]",   187     "a[:2, 100]",   188     "a[:, 100]",   189     "a[100, 1:2:3,]",   190     "a[100, :2:3]",   191     "a[100, 1::3]",   192     "a[100, 1:2:,]",   193     "a[100, 1:2]",   194     "a[100, 1:]",   195     "a[100, :2,]",   196     "a[100, :]",   197     ]   198    199 imports = [   200     'import os',   201     'import sys, os',   202     'import os.path',   203     'import os.path, sys',   204     'import sys, os.path as osp',   205     'import os.path as osp',   206     'import os.path as osp, sys as _sys',   207     'import a.b.c.d',   208     'import a.b.c.d as abcd',   209     'from os import path',   210     'from os import path, system',   211     ]   212    213 imports_newstyle = [   214     'from os import path, system',   215     'from os import path as P, system as S',   216     'from os import (path as P, system as S,)',   217     'from os import *',   218     ]   219    220 if_stmts = [   221     "if a == 1: a+= 2",   222     """if a == 1:   223     a += 2   224 elif a == 2:   225     a += 3   226 else:   227     a += 4   228 """,   229     "if a and not b == c: pass",   230     "if a and not not not b == c: pass",   231     "if 0: print 'foo'"   232     ]   233    234 asserts = [   235     'assert False',   236     'assert a == 1',   237     'assert a == 1 and b == 2',   238     'assert a == 1 and b == 2, "assertion failed"',   239     ]   240    241 execs = [   242     'exec a',   243     'exec "a=b+3"',   244     'exec a in f()',   245     'exec a in f(), g()',   246     ]   247    248 prints = [   249     'print',   250     'print a',   251     'print a,',   252     'print a, b',   253     'print a, "b", c',   254     'print >> err',   255     'print >> err, "error"',   256     'print >> err, "error",',   257     'print >> err, "error", a',   258     ]   259    260 globs = [   261     'global a',   262     'global a,b,c',   263     ]   264    265 raises_ = [      # NB. 'raises' creates a name conflict with py.test magic   266     'raise',   267     'raise ValueError',   268     'raise ValueError("error")',   269     'raise ValueError, "error"',   270     'raise ValueError, "error", foo',   271     ]   272    273 tryexcepts = [   274     """try:   275     a   276     b   277 except:   278     pass   279 """,   280     """try:   281     a   282     b   283 except NameError:   284     pass   285 """,   286     """try:   287     a   288     b   289 except NameError, err:   290     pass   291 """,   292     """try:   293     a   294     b   295 except (NameError, ValueError):   296     pass   297 """,   298     """try:   299     a   300     b   301 except (NameError, ValueError), err:   302     pass   303 """,   304     """try:   305     a   306 except NameError, err:   307     pass   308 except ValueError, err:   309     pass   310 """,   311     """def f():   312     try:   313         a   314     except NameError, err:   315         a = 1   316         b = 2   317     except ValueError, err:   318         a = 2   319         return a   320 """   321     """try:   322     a   323 except NameError, err:   324     a = 1   325 except ValueError, err:   326     a = 2   327 else:   328     a += 3   329 """,   330     """try:   331     a   332 finally:   333     b   334 """,   335     """def f():   336     try:   337         return a   338     finally:   339         a = 3   340         return 1   341 """,   342    343     ]   344        345 one_stmt_funcdefs = [   346     "def f(): return 1",   347     "def f(x): return x+1",   348     "def f(x,y): return x+y",   349     "def f(x,y=1,z=t): return x+y",   350     "def f(x,y=1,z=t,*args,**kwargs): return x+y",   351     "def f(x,y=1,z=t,*args): return x+y",   352     "def f(x,y=1,z=t,**kwargs): return x+y",   353     "def f(*args): return 1",   354     "def f(**kwargs): return 1",   355     "def f(t=()): pass",   356     "def f(a, b, (c, d), e): pass",   357     "def f(a, b, (c, (d, e), f, (g, h))): pass",   358     "def f(a, b, (c, (d, e), f, (g, h)), i): pass",   359     "def f((a)): pass",   360     ]   361    362 one_stmt_classdefs = [   363     "class Pdb(bdb.Bdb, cmd.Cmd): pass",   364     "class A: pass",   365     ]   366    367 docstrings = [   368     '''def foo(): return 1''',   369     '''class Foo: pass''',   370     '''class Foo: "foo"''',   371     '''def foo():   372     """foo docstring"""   373     return 1   374 ''',   375     '''def foo():   376     """foo docstring"""   377     a = 1   378     """bar"""   379     return a   380 ''',   381     '''def foo():   382     """doc"""; print 1   383     a=1   384 ''',   385     '''"""Docstring""";print 1''',   386     ]   387    388 returns = [   389     'def f(): return',   390     'def f(): return 1',   391     'def f(): return a.b',   392     'def f(): return a',   393     'def f(): return a,b,c,d',   394     #'return (a,b,c,d)',      --- this one makes no sense, as far as I can tell   395     ]   396    397 augassigns = [   398     'a=1;a+=2',   399     'a=1;a-=2',   400     'a=1;a*=2',   401     'a=1;a/=2',   402     'a=1;a//=2',   403     'a=1;a%=2',   404     'a=1;a**=2',   405     'a=1;a>>=2',   406     'a=1;a<<=2',   407     'a=1;a&=2',   408     'a=1;a^=2',   409     'a=1;a|=2',   410        411     'a=A();a.x+=2',   412     'a=A();a.x-=2',   413     'a=A();a.x*=2',   414     'a=A();a.x/=2',   415     'a=A();a.x//=2',   416     'a=A();a.x%=2',   417     'a=A();a.x**=2',   418     'a=A();a.x>>=2',   419     'a=A();a.x<<=2',   420     'a=A();a.x&=2',   421     'a=A();a.x^=2',   422     'a=A();a.x|=2',   423    424     'a=A();a[0]+=2',   425     'a=A();a[0]-=2',   426     'a=A();a[0]*=2',   427     'a=A();a[0]/=2',   428     'a=A();a[0]//=2',   429     'a=A();a[0]%=2',   430     'a=A();a[0]**=2',   431     'a=A();a[0]>>=2',   432     'a=A();a[0]<<=2',   433     'a=A();a[0]&=2',   434     'a=A();a[0]^=2',   435     'a=A();a[0]|=2',   436    437     'a=A();a[0:2]+=2',   438     'a=A();a[0:2]-=2',   439     'a=A();a[0:2]*=2',   440     'a=A();a[0:2]/=2',   441     'a=A();a[0:2]//=2',   442     'a=A();a[0:2]%=2',   443     'a=A();a[0:2]**=2',   444     'a=A();a[0:2]>>=2',   445     'a=A();a[0:2]<<=2',   446     'a=A();a[0:2]&=2',   447     'a=A();a[0:2]^=2',   448     'a=A();a[0:2]|=2',   449     ]   450    451 PY23_TESTS = [   452     constants,   453     expressions,   454     augassigns,   455     comparisons,   456     funccalls,   457     backtrackings,   458     listmakers, # ERRORS   459     dictmakers,   460     multiexpr,   461     attraccess,   462     slices,   463     imports,   464     execs,   465     prints,   466     globs,   467     raises_,   468    469     ]   470    471 OPTIONAL_TESTS = [   472     # expressions_inbetweenversions,    473     genexps,   474     imports_newstyle,   475     asserts,   476     ]   477    478 TESTS = PY23_TESTS + OPTIONAL_TESTS   479    480    481 ## TESTS = [   482 ##     ["l = [i for i in range(10) if i%2 == 0 or i%2 == 1]"],   483 ##     ]   484    485 CHANGES_25_INPUTS = [   486     ["class A(): pass"],   487     ["def f(): x = yield 3"]   488     ]   489    490 EXEC_INPUTS = [   491     one_stmt_classdefs,   492     one_stmt_funcdefs,   493     if_stmts,   494     tryexcepts,   495     docstrings,   496     returns,   497     ]   498    499 SINGLE_INPUTS = [   500    one_stmt_funcdefs,   501    ['\t # hello\n',   502     'print 6*7',   503     'if 1:  x\n',   504     'x = 5',   505     'x = 5 ',   506     '''"""Docstring""";print 1''',   507     '''"Docstring"''',   508     '''"Docstring" "\\x00"''',   509     ]   510 ]