PageRenderTime 254ms CodeModel.GetById 60ms app.highlight 159ms RepoModel.GetById 22ms app.codeStats 0ms

/Lib/lib2to3/tests/data/py2_test_grammar.py

http://unladen-swallow.googlecode.com/
Python | 956 lines | 860 code | 43 blank | 53 comment | 92 complexity | e942fb49c8000cb9a6b412726618c3a1 MD5 | raw file
  1# Python 2's Lib/test/test_grammar.py (r66189)
  2
  3# Python test set -- part 1, grammar.
  4# This just tests whether the parser accepts them all.
  5
  6# NOTE: When you run this test as a script from the command line, you
  7# get warnings about certain hex/oct constants.  Since those are
  8# issued by the parser, you can't suppress them by adding a
  9# filterwarnings() call to this module.  Therefore, to shut up the
 10# regression test, the filterwarnings() call has been added to
 11# regrtest.py.
 12
 13from test.test_support import run_unittest, check_syntax_error
 14import unittest
 15import sys
 16# testing import *
 17from sys import *
 18
 19class TokenTests(unittest.TestCase):
 20
 21    def testBackslash(self):
 22        # Backslash means line continuation:
 23        x = 1 \
 24        + 1
 25        self.assertEquals(x, 2, 'backslash for line continuation')
 26
 27        # Backslash does not means continuation in comments :\
 28        x = 0
 29        self.assertEquals(x, 0, 'backslash ending comment')
 30
 31    def testPlainIntegers(self):
 32        self.assertEquals(0xff, 255)
 33        self.assertEquals(0377, 255)
 34        self.assertEquals(2147483647, 017777777777)
 35        # "0x" is not a valid literal
 36        self.assertRaises(SyntaxError, eval, "0x")
 37        from sys import maxint
 38        if maxint == 2147483647:
 39            self.assertEquals(-2147483647-1, -020000000000)
 40            # XXX -2147483648
 41            self.assert_(037777777777 > 0)
 42            self.assert_(0xffffffff > 0)
 43            for s in '2147483648', '040000000000', '0x100000000':
 44                try:
 45                    x = eval(s)
 46                except OverflowError:
 47                    self.fail("OverflowError on huge integer literal %r" % s)
 48        elif maxint == 9223372036854775807:
 49            self.assertEquals(-9223372036854775807-1, -01000000000000000000000)
 50            self.assert_(01777777777777777777777 > 0)
 51            self.assert_(0xffffffffffffffff > 0)
 52            for s in '9223372036854775808', '02000000000000000000000', \
 53                     '0x10000000000000000':
 54                try:
 55                    x = eval(s)
 56                except OverflowError:
 57                    self.fail("OverflowError on huge integer literal %r" % s)
 58        else:
 59            self.fail('Weird maxint value %r' % maxint)
 60
 61    def testLongIntegers(self):
 62        x = 0L
 63        x = 0l
 64        x = 0xffffffffffffffffL
 65        x = 0xffffffffffffffffl
 66        x = 077777777777777777L
 67        x = 077777777777777777l
 68        x = 123456789012345678901234567890L
 69        x = 123456789012345678901234567890l
 70
 71    def testFloats(self):
 72        x = 3.14
 73        x = 314.
 74        x = 0.314
 75        # XXX x = 000.314
 76        x = .314
 77        x = 3e14
 78        x = 3E14
 79        x = 3e-14
 80        x = 3e+14
 81        x = 3.e14
 82        x = .3e14
 83        x = 3.1e4
 84
 85    def testStringLiterals(self):
 86        x = ''; y = ""; self.assert_(len(x) == 0 and x == y)
 87        x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39)
 88        x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34)
 89        x = "doesn't \"shrink\" does it"
 90        y = 'doesn\'t "shrink" does it'
 91        self.assert_(len(x) == 24 and x == y)
 92        x = "does \"shrink\" doesn't it"
 93        y = 'does "shrink" doesn\'t it'
 94        self.assert_(len(x) == 24 and x == y)
 95        x = """
 96The "quick"
 97brown fox
 98jumps over
 99the 'lazy' dog.
100"""
101        y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
102        self.assertEquals(x, y)
103        y = '''
104The "quick"
105brown fox
106jumps over
107the 'lazy' dog.
108'''
109        self.assertEquals(x, y)
110        y = "\n\
111The \"quick\"\n\
112brown fox\n\
113jumps over\n\
114the 'lazy' dog.\n\
115"
116        self.assertEquals(x, y)
117        y = '\n\
118The \"quick\"\n\
119brown fox\n\
120jumps over\n\
121the \'lazy\' dog.\n\
122'
123        self.assertEquals(x, y)
124
125
126class GrammarTests(unittest.TestCase):
127
128    # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
129    # XXX can't test in a script -- this rule is only used when interactive
130
131    # file_input: (NEWLINE | stmt)* ENDMARKER
132    # Being tested as this very moment this very module
133
134    # expr_input: testlist NEWLINE
135    # XXX Hard to test -- used only in calls to input()
136
137    def testEvalInput(self):
138        # testlist ENDMARKER
139        x = eval('1, 0 or 1')
140
141    def testFuncdef(self):
142        ### 'def' NAME parameters ':' suite
143        ### parameters: '(' [varargslist] ')'
144        ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
145        ###            | ('**'|'*' '*') NAME)
146        ###            | fpdef ['=' test] (',' fpdef ['=' test])* [',']
147        ### fpdef: NAME | '(' fplist ')'
148        ### fplist: fpdef (',' fpdef)* [',']
149        ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
150        ### argument: [test '='] test   # Really [keyword '='] test
151        def f1(): pass
152        f1()
153        f1(*())
154        f1(*(), **{})
155        def f2(one_argument): pass
156        def f3(two, arguments): pass
157        def f4(two, (compound, (argument, list))): pass
158        def f5((compound, first), two): pass
159        self.assertEquals(f2.func_code.co_varnames, ('one_argument',))
160        self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments'))
161        if sys.platform.startswith('java'):
162            self.assertEquals(f4.func_code.co_varnames,
163                   ('two', '(compound, (argument, list))', 'compound', 'argument',
164                                'list',))
165            self.assertEquals(f5.func_code.co_varnames,
166                   ('(compound, first)', 'two', 'compound', 'first'))
167        else:
168            self.assertEquals(f4.func_code.co_varnames,
169                  ('two', '.1', 'compound', 'argument',  'list'))
170            self.assertEquals(f5.func_code.co_varnames,
171                  ('.0', 'two', 'compound', 'first'))
172        def a1(one_arg,): pass
173        def a2(two, args,): pass
174        def v0(*rest): pass
175        def v1(a, *rest): pass
176        def v2(a, b, *rest): pass
177        def v3(a, (b, c), *rest): return a, b, c, rest
178
179        f1()
180        f2(1)
181        f2(1,)
182        f3(1, 2)
183        f3(1, 2,)
184        f4(1, (2, (3, 4)))
185        v0()
186        v0(1)
187        v0(1,)
188        v0(1,2)
189        v0(1,2,3,4,5,6,7,8,9,0)
190        v1(1)
191        v1(1,)
192        v1(1,2)
193        v1(1,2,3)
194        v1(1,2,3,4,5,6,7,8,9,0)
195        v2(1,2)
196        v2(1,2,3)
197        v2(1,2,3,4)
198        v2(1,2,3,4,5,6,7,8,9,0)
199        v3(1,(2,3))
200        v3(1,(2,3),4)
201        v3(1,(2,3),4,5,6,7,8,9,0)
202
203        # ceval unpacks the formal arguments into the first argcount names;
204        # thus, the names nested inside tuples must appear after these names.
205        if sys.platform.startswith('java'):
206            self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
207        else:
208            self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
209        self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
210        def d01(a=1): pass
211        d01()
212        d01(1)
213        d01(*(1,))
214        d01(**{'a':2})
215        def d11(a, b=1): pass
216        d11(1)
217        d11(1, 2)
218        d11(1, **{'b':2})
219        def d21(a, b, c=1): pass
220        d21(1, 2)
221        d21(1, 2, 3)
222        d21(*(1, 2, 3))
223        d21(1, *(2, 3))
224        d21(1, 2, *(3,))
225        d21(1, 2, **{'c':3})
226        def d02(a=1, b=2): pass
227        d02()
228        d02(1)
229        d02(1, 2)
230        d02(*(1, 2))
231        d02(1, *(2,))
232        d02(1, **{'b':2})
233        d02(**{'a': 1, 'b': 2})
234        def d12(a, b=1, c=2): pass
235        d12(1)
236        d12(1, 2)
237        d12(1, 2, 3)
238        def d22(a, b, c=1, d=2): pass
239        d22(1, 2)
240        d22(1, 2, 3)
241        d22(1, 2, 3, 4)
242        def d01v(a=1, *rest): pass
243        d01v()
244        d01v(1)
245        d01v(1, 2)
246        d01v(*(1, 2, 3, 4))
247        d01v(*(1,))
248        d01v(**{'a':2})
249        def d11v(a, b=1, *rest): pass
250        d11v(1)
251        d11v(1, 2)
252        d11v(1, 2, 3)
253        def d21v(a, b, c=1, *rest): pass
254        d21v(1, 2)
255        d21v(1, 2, 3)
256        d21v(1, 2, 3, 4)
257        d21v(*(1, 2, 3, 4))
258        d21v(1, 2, **{'c': 3})
259        def d02v(a=1, b=2, *rest): pass
260        d02v()
261        d02v(1)
262        d02v(1, 2)
263        d02v(1, 2, 3)
264        d02v(1, *(2, 3, 4))
265        d02v(**{'a': 1, 'b': 2})
266        def d12v(a, b=1, c=2, *rest): pass
267        d12v(1)
268        d12v(1, 2)
269        d12v(1, 2, 3)
270        d12v(1, 2, 3, 4)
271        d12v(*(1, 2, 3, 4))
272        d12v(1, 2, *(3, 4, 5))
273        d12v(1, *(2,), **{'c': 3})
274        def d22v(a, b, c=1, d=2, *rest): pass
275        d22v(1, 2)
276        d22v(1, 2, 3)
277        d22v(1, 2, 3, 4)
278        d22v(1, 2, 3, 4, 5)
279        d22v(*(1, 2, 3, 4))
280        d22v(1, 2, *(3, 4, 5))
281        d22v(1, *(2, 3), **{'d': 4})
282        def d31v((x)): pass
283        d31v(1)
284        def d32v((x,)): pass
285        d32v((1,))
286
287        # keyword arguments after *arglist
288        def f(*args, **kwargs):
289            return args, kwargs
290        self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
291                                                    {'x':2, 'y':5}))
292        self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
293        self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
294
295        # Check ast errors in *args and *kwargs
296        check_syntax_error(self, "f(*g(1=2))")
297        check_syntax_error(self, "f(**g(1=2))")
298
299    def testLambdef(self):
300        ### lambdef: 'lambda' [varargslist] ':' test
301        l1 = lambda : 0
302        self.assertEquals(l1(), 0)
303        l2 = lambda : a[d] # XXX just testing the expression
304        l3 = lambda : [2 < x for x in [-1, 3, 0L]]
305        self.assertEquals(l3(), [0, 1, 0])
306        l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
307        self.assertEquals(l4(), 1)
308        l5 = lambda x, y, z=2: x + y + z
309        self.assertEquals(l5(1, 2), 5)
310        self.assertEquals(l5(1, 2, 3), 6)
311        check_syntax_error(self, "lambda x: x = 2")
312        check_syntax_error(self, "lambda (None,): None")
313
314    ### stmt: simple_stmt | compound_stmt
315    # Tested below
316
317    def testSimpleStmt(self):
318        ### simple_stmt: small_stmt (';' small_stmt)* [';']
319        x = 1; pass; del x
320        def foo():
321            # verify statments that end with semi-colons
322            x = 1; pass; del x;
323        foo()
324
325    ### small_stmt: expr_stmt | print_stmt  | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
326    # Tested below
327
328    def testExprStmt(self):
329        # (exprlist '=')* exprlist
330        1
331        1, 2, 3
332        x = 1
333        x = 1, 2, 3
334        x = y = z = 1, 2, 3
335        x, y, z = 1, 2, 3
336        abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
337
338        check_syntax_error(self, "x + 1 = 1")
339        check_syntax_error(self, "a + 1 = b + 2")
340
341    def testPrintStmt(self):
342        # 'print' (test ',')* [test]
343        import StringIO
344
345        # Can't test printing to real stdout without comparing output
346        # which is not available in unittest.
347        save_stdout = sys.stdout
348        sys.stdout = StringIO.StringIO()
349
350        print 1, 2, 3
351        print 1, 2, 3,
352        print
353        print 0 or 1, 0 or 1,
354        print 0 or 1
355
356        # 'print' '>>' test ','
357        print >> sys.stdout, 1, 2, 3
358        print >> sys.stdout, 1, 2, 3,
359        print >> sys.stdout
360        print >> sys.stdout, 0 or 1, 0 or 1,
361        print >> sys.stdout, 0 or 1
362
363        # test printing to an instance
364        class Gulp:
365            def write(self, msg): pass
366
367        gulp = Gulp()
368        print >> gulp, 1, 2, 3
369        print >> gulp, 1, 2, 3,
370        print >> gulp
371        print >> gulp, 0 or 1, 0 or 1,
372        print >> gulp, 0 or 1
373
374        # test print >> None
375        def driver():
376            oldstdout = sys.stdout
377            sys.stdout = Gulp()
378            try:
379                tellme(Gulp())
380                tellme()
381            finally:
382                sys.stdout = oldstdout
383
384        # we should see this once
385        def tellme(file=sys.stdout):
386            print >> file, 'hello world'
387
388        driver()
389
390        # we should not see this at all
391        def tellme(file=None):
392            print >> file, 'goodbye universe'
393
394        driver()
395
396        self.assertEqual(sys.stdout.getvalue(), '''\
3971 2 3
3981 2 3
3991 1 1
4001 2 3
4011 2 3
4021 1 1
403hello world
404''')
405        sys.stdout = save_stdout
406
407        # syntax errors
408        check_syntax_error(self, 'print ,')
409        check_syntax_error(self, 'print >> x,')
410
411    def testDelStmt(self):
412        # 'del' exprlist
413        abc = [1,2,3]
414        x, y, z = abc
415        xyz = x, y, z
416
417        del abc
418        del x, y, (z, xyz)
419
420    def testPassStmt(self):
421        # 'pass'
422        pass
423
424    # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
425    # Tested below
426
427    def testBreakStmt(self):
428        # 'break'
429        while 1: break
430
431    def testContinueStmt(self):
432        # 'continue'
433        i = 1
434        while i: i = 0; continue
435
436        msg = ""
437        while not msg:
438            msg = "ok"
439            try:
440                continue
441                msg = "continue failed to continue inside try"
442            except:
443                msg = "continue inside try called except block"
444        if msg != "ok":
445            self.fail(msg)
446
447        msg = ""
448        while not msg:
449            msg = "finally block not called"
450            try:
451                continue
452            finally:
453                msg = "ok"
454        if msg != "ok":
455            self.fail(msg)
456
457    def test_break_continue_loop(self):
458        # This test warrants an explanation. It is a test specifically for SF bugs
459        # #463359 and #462937. The bug is that a 'break' statement executed or
460        # exception raised inside a try/except inside a loop, *after* a continue
461        # statement has been executed in that loop, will cause the wrong number of
462        # arguments to be popped off the stack and the instruction pointer reset to
463        # a very small number (usually 0.) Because of this, the following test
464        # *must* written as a function, and the tracking vars *must* be function
465        # arguments with default values. Otherwise, the test will loop and loop.
466
467        def test_inner(extra_burning_oil = 1, count=0):
468            big_hippo = 2
469            while big_hippo:
470                count += 1
471                try:
472                    if extra_burning_oil and big_hippo == 1:
473                        extra_burning_oil -= 1
474                        break
475                    big_hippo -= 1
476                    continue
477                except:
478                    raise
479            if count > 2 or big_hippo <> 1:
480                self.fail("continue then break in try/except in loop broken!")
481        test_inner()
482
483    def testReturn(self):
484        # 'return' [testlist]
485        def g1(): return
486        def g2(): return 1
487        g1()
488        x = g2()
489        check_syntax_error(self, "class foo:return 1")
490
491    def testYield(self):
492        check_syntax_error(self, "class foo:yield 1")
493
494    def testRaise(self):
495        # 'raise' test [',' test]
496        try: raise RuntimeError, 'just testing'
497        except RuntimeError: pass
498        try: raise KeyboardInterrupt
499        except KeyboardInterrupt: pass
500
501    def testImport(self):
502        # 'import' dotted_as_names
503        import sys
504        import time, sys
505        # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
506        from time import time
507        from time import (time)
508        # not testable inside a function, but already done at top of the module
509        # from sys import *
510        from sys import path, argv
511        from sys import (path, argv)
512        from sys import (path, argv,)
513
514    def testGlobal(self):
515        # 'global' NAME (',' NAME)*
516        global a
517        global a, b
518        global one, two, three, four, five, six, seven, eight, nine, ten
519
520    def testExec(self):
521        # 'exec' expr ['in' expr [',' expr]]
522        z = None
523        del z
524        exec 'z=1+1\n'
525        if z != 2: self.fail('exec \'z=1+1\'\\n')
526        del z
527        exec 'z=1+1'
528        if z != 2: self.fail('exec \'z=1+1\'')
529        z = None
530        del z
531        import types
532        if hasattr(types, "UnicodeType"):
533            exec r"""if 1:
534            exec u'z=1+1\n'
535            if z != 2: self.fail('exec u\'z=1+1\'\\n')
536            del z
537            exec u'z=1+1'
538            if z != 2: self.fail('exec u\'z=1+1\'')"""
539        g = {}
540        exec 'z = 1' in g
541        if g.has_key('__builtins__'): del g['__builtins__']
542        if g != {'z': 1}: self.fail('exec \'z = 1\' in g')
543        g = {}
544        l = {}
545
546        import warnings
547        warnings.filterwarnings("ignore", "global statement", module="<string>")
548        exec 'global a; a = 1; b = 2' in g, l
549        if g.has_key('__builtins__'): del g['__builtins__']
550        if l.has_key('__builtins__'): del l['__builtins__']
551        if (g, l) != ({'a':1}, {'b':2}):
552            self.fail('exec ... in g (%s), l (%s)' %(g,l))
553
554    def testAssert(self):
555        # assert_stmt: 'assert' test [',' test]
556        assert 1
557        assert 1, 1
558        assert lambda x:x
559        assert 1, lambda x:x+1
560        try:
561            assert 0, "msg"
562        except AssertionError, e:
563            self.assertEquals(e.args[0], "msg")
564        else:
565            if __debug__:
566                self.fail("AssertionError not raised by assert 0")
567
568    ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
569    # Tested below
570
571    def testIf(self):
572        # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
573        if 1: pass
574        if 1: pass
575        else: pass
576        if 0: pass
577        elif 0: pass
578        if 0: pass
579        elif 0: pass
580        elif 0: pass
581        elif 0: pass
582        else: pass
583
584    def testWhile(self):
585        # 'while' test ':' suite ['else' ':' suite]
586        while 0: pass
587        while 0: pass
588        else: pass
589
590        # Issue1920: "while 0" is optimized away,
591        # ensure that the "else" clause is still present.
592        x = 0
593        while 0:
594            x = 1
595        else:
596            x = 2
597        self.assertEquals(x, 2)
598
599    def testFor(self):
600        # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
601        for i in 1, 2, 3: pass
602        for i, j, k in (): pass
603        else: pass
604        class Squares:
605            def __init__(self, max):
606                self.max = max
607                self.sofar = []
608            def __len__(self): return len(self.sofar)
609            def __getitem__(self, i):
610                if not 0 <= i < self.max: raise IndexError
611                n = len(self.sofar)
612                while n <= i:
613                    self.sofar.append(n*n)
614                    n = n+1
615                return self.sofar[i]
616        n = 0
617        for x in Squares(10): n = n+x
618        if n != 285:
619            self.fail('for over growing sequence')
620
621        result = []
622        for x, in [(1,), (2,), (3,)]:
623            result.append(x)
624        self.assertEqual(result, [1, 2, 3])
625
626    def testTry(self):
627        ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
628        ###         | 'try' ':' suite 'finally' ':' suite
629        ### except_clause: 'except' [expr [('as' | ',') expr]]
630        try:
631            1/0
632        except ZeroDivisionError:
633            pass
634        else:
635            pass
636        try: 1/0
637        except EOFError: pass
638        except TypeError as msg: pass
639        except RuntimeError, msg: pass
640        except: pass
641        else: pass
642        try: 1/0
643        except (EOFError, TypeError, ZeroDivisionError): pass
644        try: 1/0
645        except (EOFError, TypeError, ZeroDivisionError), msg: pass
646        try: pass
647        finally: pass
648
649    def testSuite(self):
650        # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
651        if 1: pass
652        if 1:
653            pass
654        if 1:
655            #
656            #
657            #
658            pass
659            pass
660            #
661            pass
662            #
663
664    def testTest(self):
665        ### and_test ('or' and_test)*
666        ### and_test: not_test ('and' not_test)*
667        ### not_test: 'not' not_test | comparison
668        if not 1: pass
669        if 1 and 1: pass
670        if 1 or 1: pass
671        if not not not 1: pass
672        if not 1 and 1 and 1: pass
673        if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
674
675    def testComparison(self):
676        ### comparison: expr (comp_op expr)*
677        ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
678        if 1: pass
679        x = (1 == 1)
680        if 1 == 1: pass
681        if 1 != 1: pass
682        if 1 <> 1: pass
683        if 1 < 1: pass
684        if 1 > 1: pass
685        if 1 <= 1: pass
686        if 1 >= 1: pass
687        if 1 is 1: pass
688        if 1 is not 1: pass
689        if 1 in (): pass
690        if 1 not in (): pass
691        if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
692
693    def testBinaryMaskOps(self):
694        x = 1 & 1
695        x = 1 ^ 1
696        x = 1 | 1
697
698    def testShiftOps(self):
699        x = 1 << 1
700        x = 1 >> 1
701        x = 1 << 1 >> 1
702
703    def testAdditiveOps(self):
704        x = 1
705        x = 1 + 1
706        x = 1 - 1 - 1
707        x = 1 - 1 + 1 - 1 + 1
708
709    def testMultiplicativeOps(self):
710        x = 1 * 1
711        x = 1 / 1
712        x = 1 % 1
713        x = 1 / 1 * 1 % 1
714
715    def testUnaryOps(self):
716        x = +1
717        x = -1
718        x = ~1
719        x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
720        x = -1*1/1 + 1*1 - ---1*1
721
722    def testSelectors(self):
723        ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
724        ### subscript: expr | [expr] ':' [expr]
725
726        import sys, time
727        c = sys.path[0]
728        x = time.time()
729        x = sys.modules['time'].time()
730        a = '01234'
731        c = a[0]
732        c = a[-1]
733        s = a[0:5]
734        s = a[:5]
735        s = a[0:]
736        s = a[:]
737        s = a[-5:]
738        s = a[:-1]
739        s = a[-4:-3]
740        # A rough test of SF bug 1333982.  http://python.org/sf/1333982
741        # The testing here is fairly incomplete.
742        # Test cases should include: commas with 1 and 2 colons
743        d = {}
744        d[1] = 1
745        d[1,] = 2
746        d[1,2] = 3
747        d[1,2,3] = 4
748        L = list(d)
749        L.sort()
750        self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
751
752    def testAtoms(self):
753        ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
754        ### dictmaker: test ':' test (',' test ':' test)* [',']
755
756        x = (1)
757        x = (1 or 2 or 3)
758        x = (1 or 2 or 3, 2, 3)
759
760        x = []
761        x = [1]
762        x = [1 or 2 or 3]
763        x = [1 or 2 or 3, 2, 3]
764        x = []
765
766        x = {}
767        x = {'one': 1}
768        x = {'one': 1,}
769        x = {'one' or 'two': 1 or 2}
770        x = {'one': 1, 'two': 2}
771        x = {'one': 1, 'two': 2,}
772        x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
773
774        x = `x`
775        x = `1 or 2 or 3`
776        self.assertEqual(`1,2`, '(1, 2)')
777
778        x = x
779        x = 'x'
780        x = 123
781
782    ### exprlist: expr (',' expr)* [',']
783    ### testlist: test (',' test)* [',']
784    # These have been exercised enough above
785
786    def testClassdef(self):
787        # 'class' NAME ['(' [testlist] ')'] ':' suite
788        class B: pass
789        class B2(): pass
790        class C1(B): pass
791        class C2(B): pass
792        class D(C1, C2, B): pass
793        class C:
794            def meth1(self): pass
795            def meth2(self, arg): pass
796            def meth3(self, a1, a2): pass
797        # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
798        # decorators: decorator+
799        # decorated: decorators (classdef | funcdef)
800        def class_decorator(x):
801            x.decorated = True
802            return x
803        @class_decorator
804        class G:
805            pass
806        self.assertEqual(G.decorated, True)
807
808    def testListcomps(self):
809        # list comprehension tests
810        nums = [1, 2, 3, 4, 5]
811        strs = ["Apple", "Banana", "Coconut"]
812        spcs = ["  Apple", " Banana ", "Coco  nut  "]
813
814        self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco  nut'])
815        self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
816        self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
817        self.assertEqual([(i, s) for i in nums for s in strs],
818                         [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
819                          (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
820                          (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
821                          (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
822                          (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
823        self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
824                         [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
825                          (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
826                          (5, 'Banana'), (5, 'Coconut')])
827        self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
828                         [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
829
830        def test_in_func(l):
831            return [None < x < 3 for x in l if x > 2]
832
833        self.assertEqual(test_in_func(nums), [False, False, False])
834
835        def test_nested_front():
836            self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
837                             [[1, 2], [3, 4], [5, 6]])
838
839        test_nested_front()
840
841        check_syntax_error(self, "[i, s for i in nums for s in strs]")
842        check_syntax_error(self, "[x if y]")
843
844        suppliers = [
845          (1, "Boeing"),
846          (2, "Ford"),
847          (3, "Macdonalds")
848        ]
849
850        parts = [
851          (10, "Airliner"),
852          (20, "Engine"),
853          (30, "Cheeseburger")
854        ]
855
856        suppart = [
857          (1, 10), (1, 20), (2, 20), (3, 30)
858        ]
859
860        x = [
861          (sname, pname)
862            for (sno, sname) in suppliers
863              for (pno, pname) in parts
864                for (sp_sno, sp_pno) in suppart
865                  if sno == sp_sno and pno == sp_pno
866        ]
867
868        self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
869                             ('Macdonalds', 'Cheeseburger')])
870
871    def testGenexps(self):
872        # generator expression tests
873        g = ([x for x in range(10)] for x in range(1))
874        self.assertEqual(g.next(), [x for x in range(10)])
875        try:
876            g.next()
877            self.fail('should produce StopIteration exception')
878        except StopIteration:
879            pass
880
881        a = 1
882        try:
883            g = (a for d in a)
884            g.next()
885            self.fail('should produce TypeError')
886        except TypeError:
887            pass
888
889        self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
890        self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
891
892        a = [x for x in range(10)]
893        b = (x for x in (y for y in a))
894        self.assertEqual(sum(b), sum([x for x in range(10)]))
895
896        self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
897        self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
898        self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
899        self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
900        self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
901        self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)]))
902        self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
903        check_syntax_error(self, "foo(x for x in range(10), 100)")
904        check_syntax_error(self, "foo(100, x for x in range(10))")
905
906    def testComprehensionSpecials(self):
907        # test for outmost iterable precomputation
908        x = 10; g = (i for i in range(x)); x = 5
909        self.assertEqual(len(list(g)), 10)
910
911        # This should hold, since we're only precomputing outmost iterable.
912        x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
913        x = 5; t = True;
914        self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
915
916        # Grammar allows multiple adjacent 'if's in listcomps and genexps,
917        # even though it's silly. Make sure it works (ifelse broke this.)
918        self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
919        self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
920
921        # verify unpacking single element tuples in listcomp/genexp.
922        self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
923        self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
924
925    def testIfElseExpr(self):
926        # Test ifelse expressions in various cases
927        def _checkeval(msg, ret):
928            "helper to check that evaluation of expressions is done correctly"
929            print x
930            return ret
931
932        self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
933        self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
934        self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True])
935        self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
936        self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
937        self.assertEqual((5 and 6 if 0 else 1), 1)
938        self.assertEqual(((5 and 6) if 0 else 1), 1)
939        self.assertEqual((5 and (6 if 1 else 1)), 6)
940        self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
941        self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
942        self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
943        self.assertEqual((not 5 if 1 else 1), False)
944        self.assertEqual((not 5 if 0 else 1), 1)
945        self.assertEqual((6 + 1 if 1 else 2), 7)
946        self.assertEqual((6 - 1 if 1 else 2), 5)
947        self.assertEqual((6 * 2 if 1 else 4), 12)
948        self.assertEqual((6 / 2 if 1 else 3), 3)
949        self.assertEqual((6 < 4 if 0 else 2), 2)
950
951
952def test_main():
953    run_unittest(TokenTests, GrammarTests)
954
955if __name__ == '__main__':
956    test_main()