gh-109485: Further improve test_future_stmt tests (#109486)

Add assertSyntaxError() which run tests with an additional docstring
and without docstring, and checks for the error message.
This commit is contained in:
Nikita Sobolev 2023-09-19 15:01:59 +03:00 committed by GitHub
parent 59f32a785f
commit 94c95d42a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 106 additions and 115 deletions

View file

@ -1,10 +0,0 @@
"""This is a test"""
from __future__ import nested_scopes
from __future__ import rested_snopes
def f(x):
def g(y):
return x + y
return g
result = f(2)(4)

View file

@ -1,10 +0,0 @@
"""This is a test"""
import __future__
from __future__ import nested_scopes
def f(x):
def g(y):
return x + y
return g
result = f(2)(4)

View file

@ -1,12 +0,0 @@
"""This is a test"""
from __future__ import nested_scopes
import foo
from __future__ import nested_scopes
def f(x):
def g(y):
return x + y
return g
result = f(2)(4)

View file

@ -1,10 +0,0 @@
"""This is a test"""
"this isn't a doc string"
from __future__ import nested_scopes
def f(x):
def g(y):
return x + y
return g
result = f(2)(4)

View file

@ -1,11 +0,0 @@
"""This is a test"""
from __future__ import nested_scopes; import string; from __future__ import \
nested_scopes
def f(x):
def g(y):
return x + y
return g
result = f(2)(4)

View file

@ -1,10 +0,0 @@
"""This is a test"""
from __future__ import *
def f(x):
def g(y):
return x + y
return g
print(f(2)(4))

View file

@ -1,10 +0,0 @@
"""This is a test"""
from __future__ import nested_scopes, braces
def f(x):
def g(y):
return x + y
return g
print(f(2)(4))

View file

@ -10,6 +10,8 @@ import os
import re import re
import sys import sys
TOP_LEVEL_MSG = 'from __future__ imports must occur at the beginning of the file'
rx = re.compile(r'\((\S+).py, line (\d+)') rx = re.compile(r'\((\S+).py, line (\d+)')
def get_error_location(msg): def get_error_location(msg):
@ -18,21 +20,48 @@ def get_error_location(msg):
class FutureTest(unittest.TestCase): class FutureTest(unittest.TestCase):
def check_syntax_error(self, err, basename, lineno, offset=1): def check_syntax_error(self, err, basename,
self.assertIn('%s.py, line %d' % (basename, lineno), str(err)) *,
self.assertEqual(os.path.basename(err.filename), basename + '.py') lineno,
message=TOP_LEVEL_MSG, offset=1):
if basename != '<string>':
basename += '.py'
self.assertEqual(f'{message} ({basename}, line {lineno})', str(err))
self.assertEqual(os.path.basename(err.filename), basename)
self.assertEqual(err.lineno, lineno) self.assertEqual(err.lineno, lineno)
self.assertEqual(err.offset, offset) self.assertEqual(err.offset, offset)
def test_future1(self): def assertSyntaxError(self, code,
with import_helper.CleanImport('test.test_future_stmt.future_test1'): *,
from test.test_future_stmt import future_test1 lineno=1,
self.assertEqual(future_test1.result, 6) message=TOP_LEVEL_MSG, offset=1,
parametrize_docstring=True):
code = dedent(code.lstrip('\n'))
for add_docstring in ([False, True] if parametrize_docstring else [False]):
with self.subTest(code=code, add_docstring=add_docstring):
if add_docstring:
code = '"""Docstring"""\n' + code
lineno += 1
with self.assertRaises(SyntaxError) as cm:
exec(code)
self.check_syntax_error(cm.exception, "<string>",
lineno=lineno,
message=message,
offset=offset)
def test_future2(self): def test_import_nested_scope_twice(self):
with import_helper.CleanImport('test.test_future_stmt.future_test2'): # Import the name nested_scopes twice to trigger SF bug #407394
from test.test_future_stmt import future_test2 with import_helper.CleanImport(
self.assertEqual(future_test2.result, 6) 'test.test_future_stmt.import_nested_scope_twice',
):
from test.test_future_stmt import import_nested_scope_twice
self.assertEqual(import_nested_scope_twice.result, 6)
def test_nested_scope(self):
with import_helper.CleanImport('test.test_future_stmt.nested_scope'):
from test.test_future_stmt import nested_scope
self.assertEqual(nested_scope.result, 6)
def test_future_single_import(self): def test_future_single_import(self):
with import_helper.CleanImport( with import_helper.CleanImport(
@ -52,45 +81,80 @@ class FutureTest(unittest.TestCase):
): ):
from test.test_future_stmt import test_future_multiple_features from test.test_future_stmt import test_future_multiple_features
def test_badfuture3(self): def test_unknown_future_flag(self):
with self.assertRaises(SyntaxError) as cm: code = """
from test.test_future_stmt import badsyntax_future3 from __future__ import nested_scopes
self.check_syntax_error(cm.exception, "badsyntax_future3", 3) from __future__ import rested_snopes # typo error here: nested => rested
"""
self.assertSyntaxError(
code, lineno=2,
message='future feature rested_snopes is not defined',
)
def test_badfuture4(self): def test_future_import_not_on_top(self):
with self.assertRaises(SyntaxError) as cm: code = """
from test.test_future_stmt import badsyntax_future4 import some_module
self.check_syntax_error(cm.exception, "badsyntax_future4", 3) from __future__ import annotations
"""
self.assertSyntaxError(code, lineno=2)
def test_badfuture5(self): code = """
with self.assertRaises(SyntaxError) as cm: import __future__
from test.test_future_stmt import badsyntax_future5 from __future__ import annotations
self.check_syntax_error(cm.exception, "badsyntax_future5", 4) """
self.assertSyntaxError(code, lineno=2)
def test_badfuture6(self): code = """
with self.assertRaises(SyntaxError) as cm: from __future__ import absolute_import
from test.test_future_stmt import badsyntax_future6 "spam, bar, blah"
self.check_syntax_error(cm.exception, "badsyntax_future6", 3) from __future__ import print_function
"""
self.assertSyntaxError(code, lineno=3)
def test_badfuture7(self): def test_future_import_with_extra_string(self):
with self.assertRaises(SyntaxError) as cm: code = """
from test.test_future_stmt import badsyntax_future7 '''Docstring'''
self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 54) "this isn't a doc string"
from __future__ import nested_scopes
"""
self.assertSyntaxError(code, lineno=3, parametrize_docstring=False)
def test_badfuture8(self): def test_multiple_import_statements_on_same_line(self):
with self.assertRaises(SyntaxError) as cm: # With `\`:
from test.test_future_stmt import badsyntax_future8 code = """
self.check_syntax_error(cm.exception, "badsyntax_future8", 3) from __future__ import nested_scopes; import string; from __future__ import \
nested_scopes
"""
self.assertSyntaxError(code, offset=54)
def test_badfuture9(self): # Without `\`:
with self.assertRaises(SyntaxError) as cm: code = """
from test.test_future_stmt import badsyntax_future9 from __future__ import nested_scopes; import string; from __future__ import nested_scopes
self.check_syntax_error(cm.exception, "badsyntax_future9", 3) """
self.assertSyntaxError(code, offset=54)
def test_badfuture10(self): def test_future_import_star(self):
code = """
from __future__ import *
"""
self.assertSyntaxError(code, message='future feature * is not defined')
def test_future_import_braces(self):
code = """
from __future__ import braces
"""
# Congrats, you found an easter egg!
self.assertSyntaxError(code, message='not a chance')
code = """
from __future__ import nested_scopes, braces
"""
self.assertSyntaxError(code, message='not a chance')
def test_module_with_future_import_not_on_top(self):
with self.assertRaises(SyntaxError) as cm: with self.assertRaises(SyntaxError) as cm:
from test.test_future_stmt import badsyntax_future10 from test.test_future_stmt import badsyntax_future
self.check_syntax_error(cm.exception, "badsyntax_future10", 3) self.check_syntax_error(cm.exception, "badsyntax_future", lineno=3)
def test_ensure_flags_dont_clash(self): def test_ensure_flags_dont_clash(self):
# bpo-39562: test that future flags and compiler flags doesn't clash # bpo-39562: test that future flags and compiler flags doesn't clash