Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_codeop.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | be7683a6f521 |
| children | e6e100efbc7d |
| rev | line source |
|---|---|
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
1 """ |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
2 Test cases for codeop.py |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
3 Nick Mathewson |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
4 """ |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
5 import unittest |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
6 from test.test_support import run_unittest |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
7 |
|
27878
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
8 from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
9 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
10 class CodeopTests(unittest.TestCase): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
11 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
12 def assertValid(self, str, symbol='single'): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
13 '''succeed iff str is a valid piece of code''' |
|
27878
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
14 expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT) |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
15 self.assertEquals( compile_command(str, "<input>", symbol), expected) |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18878
diff
changeset
|
16 |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
17 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
18 def assertIncomplete(self, str, symbol='single'): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
19 '''succeed iff str is the start of a valid piece of code''' |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
20 self.assertEquals( compile_command(str, symbol=symbol), None) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
21 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
22 def assertInvalid(self, str, symbol='single', is_syntax=1): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
23 '''succeed iff str is the start of an invalid piece of code''' |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
24 try: |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
25 compile_command(str,symbol=symbol) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
26 self.fail("No exception thrown for invalid code") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
27 except SyntaxError: |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
28 self.assert_(is_syntax) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
29 except OverflowError: |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
30 self.assert_(not is_syntax) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
31 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
32 def test_valid(self): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
33 av = self.assertValid |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
34 av("a = 1\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
35 av("def x():\n pass\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
36 av("pass\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
37 av("3**3\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
38 av("if 9==3:\n pass\nelse:\n pass\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
39 av("#a\n#b\na = 3\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
40 av("#a\n\n \na=3\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
41 av("a=3\n\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
42 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
43 # special case |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18878
diff
changeset
|
44 self.assertEquals(compile_command(""), |
|
27878
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
45 compile("pass", "<input>", 'single', |
|
be7683a6f521
- Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
46 PyCF_DONT_IMPLY_DEDENT)) |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
47 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
48 av("3**3","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
49 av("(lambda z: \n z**3)","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
50 av("#a\n#b\na**3","eval") |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18878
diff
changeset
|
51 |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
52 def test_incomplete(self): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
53 ai = self.assertIncomplete |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
54 ai("(a **") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
55 ai("def x():\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
56 ai("(a,b,") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
57 ai("(a,b,(") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
58 ai("(a,b,(") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
59 ai("if 9==3:\n pass\nelse:\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
60 ai("if 9==3:\n pass\nelse:\n pass") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
61 ai("a = (") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
62 ai("a = 9+ \\") |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18878
diff
changeset
|
63 |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
64 ai("(","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
65 ai("(\n\n\n","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
66 ai("(9+","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
67 ai("9+ \\","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
68 ai("lambda z: \\","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
69 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
70 def test_invalid(self): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
71 ai = self.assertInvalid |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
72 ai("a b") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
73 ai("a = ") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
74 ai("a = 9 +") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
75 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
76 ai("a = 1","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
77 ai("a = (","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
78 ai("]","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
79 ai("())","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
80 ai("[}","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
81 ai("9+","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
82 ai("lambda z:","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
83 ai("a b","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
84 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
85 def test_filename(self): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
86 self.assertEquals(compile_command("a = 1\n", "abc").co_filename, |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
87 compile("a = 1\n", "abc", 'single').co_filename) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
88 self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename, |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
89 compile("a = 1\n", "def", 'single').co_filename) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
90 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
91 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
92 def test_main(): |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
93 run_unittest(CodeopTests) |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
94 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
95 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
96 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
97 test_main() |
