Commit afdcd4f4 authored by Georg Brandl's avatar Georg Brandl

Convert test_global, test_scope and test_grammar to unittest.

I tried to enclose all tests which must be run at the toplevel
(instead of inside a method) in exec statements.
parent a9fbc26b
...@@ -379,8 +379,8 @@ test_support provides the following useful objects: ...@@ -379,8 +379,8 @@ test_support provides the following useful objects:
point numbers when you expect them to only be approximately equal point numbers when you expect them to only be approximately equal
withing a fuzz factor (``test_support.FUZZ``, which defaults to 1e-6). withing a fuzz factor (``test_support.FUZZ``, which defaults to 1e-6).
* ``check_syntax(statement)`` - make sure that the statement is *not* * ``check_syntax_error(testcase, statement)`` - make sure that the
correct Python syntax. statement is *not* correct Python syntax.
Python and C statement coverage results are currently available at Python and C statement coverage results are currently available at
......
test_global
got SyntaxError as expected
got SyntaxError as expected
got SyntaxError as expected
as expected, no SyntaxError
test_grammar
1. Parser
1.1 Tokens
1.1.1 Backslashes
1.1.2 Numeric literals
1.1.2.1 Plain integers
1.1.2.2 Long integers
1.1.2.3 Floating point
1.1.3 String literals
1.2 Grammar
single_input
file_input
expr_input
eval_input
funcdef
lambdef
simple_stmt
expr_stmt
print_stmt
1 2 3
1 2 3
1 1 1
extended print_stmt
1 2 3
1 2 3
1 1 1
hello world
del_stmt
pass_stmt
flow_stmt
break_stmt
continue_stmt
continue + try/except ok
continue + try/finally ok
testing continue and break in try/except in loop
return_stmt
yield_stmt
raise_stmt
import_name
import_from
global_stmt
exec_stmt
assert_stmt
if_stmt
while_stmt
for_stmt
try_stmt
suite
test
comparison
binary mask ops
shift ops
additive ops
multiplicative ops
unary ops
selectors
[1, (1,), (1, 2), (1, 2, 3)]
atoms
classdef
['Apple', 'Banana', 'Coco nut']
[3, 6, 9, 12, 15]
[3, 4, 5]
[(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]
[(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), (5, 'Banana'), (5, 'Coconut')]
[[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]
[False, False, False]
[[1, 2], [3, 4], [5, 6]]
[('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), ('Macdonalds', 'Cheeseburger')]
test_scope
1. simple nesting
2. extra nesting
3. simple nesting + rebinding
4. nesting with global but no free
5. nesting through class
6. nesting plus free ref to global
7. nearest enclosing scope
8. mixed freevars and cellvars
9. free variable in method
10. recursion
11. unoptimized namespaces
12. lambdas
13. UnboundLocal
14. complex definitions
15. scope of global statements
16. check leaks
17. class and global
18. verify that locals() works
19. var is bound and free in class
20. interaction with trace function
20. eval and exec with free variables
21. list comprehension with local variables
22. eval with free variables
"""Verify that warnings are issued for global statements following use.""" """Verify that warnings are issued for global statements following use."""
from test.test_support import check_syntax from test.test_support import run_unittest, check_syntax_error
import unittest
import warnings import warnings
warnings.filterwarnings("error", module="<test string>")
warnings.filterwarnings("error", module="<test code>") class GlobalTests(unittest.TestCase):
def compile_and_check(text, should_fail=1): def test1(self):
try: prog_text_1 = """\
compile(text, "<test code>", "exec")
except SyntaxError, msg:
if should_fail:
print "got SyntaxError as expected"
else:
print "raised unexpected SyntaxError:", text
else:
if should_fail:
print "should have raised SyntaxError:", text
else:
print "as expected, no SyntaxError"
prog_text_1 = """
def wrong1(): def wrong1():
a = 1 a = 1
b = 2 b = 2
global a global a
global b global b
""" """
compile_and_check(prog_text_1) check_syntax_error(self, prog_text_1)
prog_text_2 = """ def test2(self):
prog_text_2 = """\
def wrong2(): def wrong2():
print x print x
global x global x
""" """
compile_and_check(prog_text_2) check_syntax_error(self, prog_text_2)
prog_text_3 = """ def test3(self):
prog_text_3 = """\
def wrong3(): def wrong3():
print x print x
x = 2 x = 2
global x global x
""" """
compile_and_check(prog_text_3) check_syntax_error(self, prog_text_3)
prog_text_4 = """ def test4(self):
prog_text_4 = """\
global x global x
x = 2 x = 2
""" """
compile_and_check(prog_text_4, 0) # this should work
compile(prog_text_4, "<test string>", "exec")
def test_main():
run_unittest(GlobalTests)
if __name__ == "__main__":
test_main()
This diff is collapsed.
This diff is collapsed.
...@@ -245,13 +245,13 @@ def sortdict(dict): ...@@ -245,13 +245,13 @@ def sortdict(dict):
withcommas = ", ".join(reprpairs) withcommas = ", ".join(reprpairs)
return "{%s}" % withcommas return "{%s}" % withcommas
def check_syntax(statement): def check_syntax_error(testcase, statement):
try: try:
compile(statement, '<string>', 'exec') compile(statement, '<test string>', 'exec')
except SyntaxError: except SyntaxError:
pass pass
else: else:
print 'Missing SyntaxError: "%s"' % statement testcase.fail('Missing SyntaxError: "%s"' % statement)
def open_urlresource(url): def open_urlresource(url):
import urllib, urlparse import urllib, urlparse
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment