22
33import unittest
44from test import support
5+ import os
56import re
67
78rx = re .compile ('\((\S+).py, line (\d+)' )
@@ -12,6 +13,12 @@ def get_error_location(msg):
1213
1314class FutureTest (unittest .TestCase ):
1415
16+ def check_syntax_error (self , err , basename , lineno , offset = 0 ):
17+ self .assertIn ('%s.py, line %d' % (basename , lineno ), str (err ))
18+ self .assertEqual (os .path .basename (err .filename ), basename + '.py' )
19+ self .assertEqual (err .lineno , lineno )
20+ self .assertEqual (err .offset , offset )
21+
1522 def test_future1 (self ):
1623 with support .CleanImport ('future_test1' ):
1724 from test import future_test1
@@ -27,68 +34,44 @@ def test_future3(self):
2734 from test import test_future3
2835
2936 def test_badfuture3 (self ):
30- try :
37+ with self . assertRaises ( SyntaxError ) as cm :
3138 from test import badsyntax_future3
32- except SyntaxError as msg :
33- self .assertEqual (get_error_location (msg ), ("badsyntax_future3" , '3' ))
34- else :
35- self .fail ("expected exception didn't occur" )
39+ self .check_syntax_error (cm .exception , "badsyntax_future3" , 3 )
3640
3741 def test_badfuture4 (self ):
38- try :
42+ with self . assertRaises ( SyntaxError ) as cm :
3943 from test import badsyntax_future4
40- except SyntaxError as msg :
41- self .assertEqual (get_error_location (msg ), ("badsyntax_future4" , '3' ))
42- else :
43- self .fail ("expected exception didn't occur" )
44+ self .check_syntax_error (cm .exception , "badsyntax_future4" , 3 )
4445
4546 def test_badfuture5 (self ):
46- try :
47+ with self . assertRaises ( SyntaxError ) as cm :
4748 from test import badsyntax_future5
48- except SyntaxError as msg :
49- self .assertEqual (get_error_location (msg ), ("badsyntax_future5" , '4' ))
50- else :
51- self .fail ("expected exception didn't occur" )
49+ self .check_syntax_error (cm .exception , "badsyntax_future5" , 4 )
5250
5351 def test_badfuture6 (self ):
54- try :
52+ with self . assertRaises ( SyntaxError ) as cm :
5553 from test import badsyntax_future6
56- except SyntaxError as msg :
57- self .assertEqual (get_error_location (msg ), ("badsyntax_future6" , '3' ))
58- else :
59- self .fail ("expected exception didn't occur" )
54+ self .check_syntax_error (cm .exception , "badsyntax_future6" , 3 )
6055
6156 def test_badfuture7 (self ):
62- try :
57+ with self . assertRaises ( SyntaxError ) as cm :
6358 from test import badsyntax_future7
64- except SyntaxError as msg :
65- self .assertEqual (get_error_location (msg ), ("badsyntax_future7" , '3' ))
66- else :
67- self .fail ("expected exception didn't occur" )
59+ self .check_syntax_error (cm .exception , "badsyntax_future7" , 3 , 53 )
6860
6961 def test_badfuture8 (self ):
70- try :
62+ with self . assertRaises ( SyntaxError ) as cm :
7163 from test import badsyntax_future8
72- except SyntaxError as msg :
73- self .assertEqual (get_error_location (msg ), ("badsyntax_future8" , '3' ))
74- else :
75- self .fail ("expected exception didn't occur" )
64+ self .check_syntax_error (cm .exception , "badsyntax_future8" , 3 )
7665
7766 def test_badfuture9 (self ):
78- try :
67+ with self . assertRaises ( SyntaxError ) as cm :
7968 from test import badsyntax_future9
80- except SyntaxError as msg :
81- self .assertEqual (get_error_location (msg ), ("badsyntax_future9" , '3' ))
82- else :
83- self .fail ("expected exception didn't occur" )
69+ self .check_syntax_error (cm .exception , "badsyntax_future9" , 3 , 0 )
8470
8571 def test_badfuture10 (self ):
86- try :
72+ with self . assertRaises ( SyntaxError ) as cm :
8773 from test import badsyntax_future10
88- except SyntaxError as msg :
89- self .assertEqual (get_error_location (msg ), ("badsyntax_future10" , '3' ))
90- else :
91- self .fail ("expected exception didn't occur" )
74+ self .check_syntax_error (cm .exception , "badsyntax_future10" , 3 , 0 )
9275
9376 def test_parserhack (self ):
9477 # test that the parser.c::future_hack function works as expected
0 commit comments