Lichen

Annotated pyparser/test/expressions.py

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