Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 1 | import __future__ |
| 2 | import unittest |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3 | import sys |
Victor Stinner | 1def775 | 2020-04-23 03:03:24 +0200 | [diff] [blame] | 4 | from test import support |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 5 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 6 | |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 7 | class FLUFLTests(unittest.TestCase): |
| 8 | |
| 9 | def test_barry_as_bdfl(self): |
Serhiy Storchaka | aba24ff | 2018-07-23 23:41:11 +0300 | [diff] [blame] | 10 | code = "from __future__ import barry_as_FLUFL\n2 {0} 3" |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 11 | compile(code.format('<>'), '<BDFL test>', 'exec', |
| 12 | __future__.CO_FUTURE_BARRY_AS_BDFL) |
Serhiy Storchaka | aba24ff | 2018-07-23 23:41:11 +0300 | [diff] [blame] | 13 | with self.assertRaises(SyntaxError) as cm: |
| 14 | compile(code.format('!='), '<FLUFL test>', 'exec', |
| 15 | __future__.CO_FUTURE_BARRY_AS_BDFL) |
| 16 | self.assertRegex(str(cm.exception), |
| 17 | "with Barry as BDFL, use '<>' instead of '!='") |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 18 | self.assertIn('2 != 3', cm.exception.text) |
Serhiy Storchaka | aba24ff | 2018-07-23 23:41:11 +0300 | [diff] [blame] | 19 | self.assertEqual(cm.exception.filename, '<FLUFL test>') |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 20 | |
| 21 | self.assertTrue(cm.exception.lineno, 2) |
| 22 | # The old parser reports the end of the token and the new |
| 23 | # parser reports the start of the token |
| 24 | self.assertEqual(cm.exception.offset, 4 if support.use_old_parser() else 3) |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 25 | |
| 26 | def test_guido_as_bdfl(self): |
| 27 | code = '2 {0} 3' |
| 28 | compile(code.format('!='), '<BDFL test>', 'exec') |
Serhiy Storchaka | aba24ff | 2018-07-23 23:41:11 +0300 | [diff] [blame] | 29 | with self.assertRaises(SyntaxError) as cm: |
| 30 | compile(code.format('<>'), '<FLUFL test>', 'exec') |
| 31 | self.assertRegex(str(cm.exception), "invalid syntax") |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 32 | self.assertIn('2 <> 3', cm.exception.text) |
Serhiy Storchaka | aba24ff | 2018-07-23 23:41:11 +0300 | [diff] [blame] | 33 | self.assertEqual(cm.exception.filename, '<FLUFL test>') |
| 34 | self.assertEqual(cm.exception.lineno, 1) |
Pablo Galindo | 2b74c83 | 2020-04-27 18:02:07 +0100 | [diff] [blame] | 35 | # The old parser reports the end of the token and the new |
| 36 | # parser reports the start of the token |
| 37 | self.assertEqual(cm.exception.offset, 4 if support.use_old_parser() else 3) |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 38 | |
| 39 | |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 40 | if __name__ == '__main__': |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 41 | unittest.main() |