blob: c4f5a58a946de660c57f56050d4e7f35b8d5e2fe [file] [log] [blame]
Guido van Rossum0874f7f1997-10-27 22:15:06 +00001# Tests for the 'tokenize' module.
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +03002# Large bits stolen from test_grammar.py.
Guido van Rossum0874f7f1997-10-27 22:15:06 +00003
4# Comments
5"#"
6#'
7#"
8#\
9 #
10 # abc
11'''#
12#'''
13
14x = 1 #
15
16# Balancing continuation
17
18a = (3, 4,
19 5, 6)
20y = [3, 4,
21 5]
22z = {'a':5,
23 'b':6}
Brett Cannon0b70cca2006-08-25 02:59:59 +000024x = (len(repr(y)) + 5*x - a[
Guido van Rossum0874f7f1997-10-27 22:15:06 +000025 3 ]
26 - x + len({
27 }
28 )
29 )
30
31# Backslash means line continuation:
32x = 1 \
33+ 1
34
35# Backslash does not means continuation in comments :\
36x = 0
37
38# Ordinary integers
Guido van Rossumb053cd82006-08-24 03:53:23 +0000390xff != 255
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000400o377 != 255
412147483647 != 0o17777777777
42-2147483647-1 != 0o20000000000
430o37777777777 != -1
440xffffffff != -1; 0o37777777777 != -1; -0o1234567 == 0O001234567; 0b10101 == 0B00010101
Guido van Rossum0874f7f1997-10-27 22:15:06 +000045
46# Long integers
Guido van Rossumcd16bf62007-06-13 18:07:49 +000047x = 0
48x = 0
49x = 0xffffffffffffffff
50x = 0xffffffffffffffff
51x = 0o77777777777777777
52x = 0B11101010111111111
53x = 123456789012345678901234567890
54x = 123456789012345678901234567890
Guido van Rossum0874f7f1997-10-27 22:15:06 +000055
56# Floating-point numbers
57x = 3.14
58x = 314.
59x = 0.314
60# XXX x = 000.314
61x = .314
62x = 3e14
63x = 3E14
64x = 3e-14
65x = 3e+14
66x = 3.e14
67x = .3e14
68x = 3.1e4
69
70# String literals
71x = ''; y = "";
72x = '\''; y = "'";
73x = '"'; y = "\"";
74x = "doesn't \"shrink\" does it"
75y = 'doesn\'t "shrink" does it'
76x = "does \"shrink\" doesn't it"
77y = 'does "shrink" doesn\'t it'
78x = """
79The "quick"
80brown fox
81jumps over
82the 'lazy' dog.
83"""
84y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
85y = '''
86The "quick"
87brown fox
88jumps over
89the 'lazy' dog.
90''';
91y = "\n\
92The \"quick\"\n\
93brown fox\n\
94jumps over\n\
95the 'lazy' dog.\n\
96";
97y = '\n\
98The \"quick\"\n\
99brown fox\n\
100jumps over\n\
101the \'lazy\' dog.\n\
102';
103x = r'\\' + R'\\'
104x = r'\'' + ''
105y = r'''
106foo bar \\
107baz''' + R'''
108foo'''
109y = r"""foo
110bar \\ baz
111""" + R'''spam
112'''
Guido van Rossum4fe72f92007-11-12 17:40:10 +0000113x = b'abc' + B'ABC'
114y = b"abc" + B"ABC"
115x = br'abc' + Br'ABC' + bR'ABC' + BR'ABC'
116y = br"abc" + Br"ABC" + bR"ABC" + BR"ABC"
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +0100117x = rb'abc' + rB'ABC' + Rb'ABC' + RB'ABC'
118y = rb"abc" + rB"ABC" + Rb"ABC" + RB"ABC"
Guido van Rossum4fe72f92007-11-12 17:40:10 +0000119x = br'\\' + BR'\\'
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +0100120x = rb'\\' + RB'\\'
Guido van Rossum4fe72f92007-11-12 17:40:10 +0000121x = br'\'' + ''
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +0100122x = rb'\'' + ''
Guido van Rossum4fe72f92007-11-12 17:40:10 +0000123y = br'''
Ka-Ping Yee1ff08b12001-01-15 22:04:30 +0000124foo bar \\
Guido van Rossum4fe72f92007-11-12 17:40:10 +0000125baz''' + BR'''
Ka-Ping Yee1ff08b12001-01-15 22:04:30 +0000126foo'''
Guido van Rossum4fe72f92007-11-12 17:40:10 +0000127y = Br"""foo
Ka-Ping Yee1ff08b12001-01-15 22:04:30 +0000128bar \\ baz
Guido van Rossum4fe72f92007-11-12 17:40:10 +0000129""" + bR'''spam
Ka-Ping Yee1ff08b12001-01-15 22:04:30 +0000130'''
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +0100131y = rB"""foo
132bar \\ baz
133""" + Rb'''spam
134'''
Guido van Rossum0874f7f1997-10-27 22:15:06 +0000135
136# Indentation
137if 1:
138 x = 2
139if 1:
Tim Peters3b5de4d2003-02-19 02:44:12 +0000140 x = 2
Guido van Rossum0874f7f1997-10-27 22:15:06 +0000141if 1:
142 while 0:
Tim Peters3b5de4d2003-02-19 02:44:12 +0000143 if 0:
144 x = 2
145 x = 2
Guido van Rossum0874f7f1997-10-27 22:15:06 +0000146if 0:
Tim Peters3b5de4d2003-02-19 02:44:12 +0000147 if 2:
148 while 0:
149 if 1:
150 x = 2
Guido van Rossum0874f7f1997-10-27 22:15:06 +0000151
152# Operators
153
154def d22(a, b, c=1, d=2): pass
Jeremy Hyltona4553c02001-04-13 14:36:51 +0000155def d01v(a=1, *restt, **restd): pass
Guido van Rossum0874f7f1997-10-27 22:15:06 +0000156
Guido van Rossumb053cd82006-08-24 03:53:23 +0000157(x, y) != ({'a':1}, {'b':2})
Guido van Rossum0874f7f1997-10-27 22:15:06 +0000158
159# comparison
Guido van Rossumb053cd82006-08-24 03:53:23 +0000160if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 != 1 in 1 not in 1 is 1 is not 1: pass
Guido van Rossum0874f7f1997-10-27 22:15:06 +0000161
162# binary
163x = 1 & 1
164x = 1 ^ 1
165x = 1 | 1
166
167# shift
168x = 1 << 1 >> 1
169
170# additive
171x = 1 - 1 + 1 - 1 + 1
172
173# multiplicative
174x = 1 / 1 * 1 % 1
175
176# unary
177x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
178x = -1*1/1 + 1*1 - ---1*1
179
180# selector
181import sys, time
182x = sys.modules['time'].time()
Tim Peters3b5de4d2003-02-19 02:44:12 +0000183
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000184@staticmethod
185def foo(): pass
186
Neal Norwitzc1505362006-12-28 06:47:50 +0000187@staticmethod
188def foo(x:1)->1: pass
189