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