| 1 |
import __future__ |
| 2 |
import unittest |
| 3 |
|
| 4 |
|
| 5 |
class FLUFLTests(unittest.TestCase): |
| 6 |
|
| 7 |
def test_barry_as_bdfl(self): |
| 8 |
code = "from __future__ import barry_as_FLUFL\n2 {0} 3" |
| 9 |
compile(code.format('<>'), '<BDFL test>', 'exec', |
| 10 |
__future__.CO_FUTURE_BARRY_AS_BDFL) |
| 11 |
with self.assertRaises(SyntaxError) as cm: |
| 12 |
compile(code.format('!='), '<FLUFL test>', 'exec', |
| 13 |
__future__.CO_FUTURE_BARRY_AS_BDFL) |
| 14 |
self.assertRegex(str(cm.exception), |
| 15 |
"with Barry as BDFL, use '<>' instead of '!='") |
| 16 |
self.assertIn('2 != 3', cm.exception.text) |
| 17 |
self.assertEqual(cm.exception.filename, '<FLUFL test>') |
| 18 |
|
| 19 |
self.assertEqual(cm.exception.lineno, 2) |
| 20 |
# The old parser reports the end of the token and the new |
| 21 |
# parser reports the start of the token |
| 22 |
self.assertEqual(cm.exception.offset, 3) |
| 23 |
|
| 24 |
def test_guido_as_bdfl(self): |
| 25 |
code = '2 {0} 3' |
| 26 |
compile(code.format('!='), '<BDFL test>', 'exec') |
| 27 |
with self.assertRaises(SyntaxError) as cm: |
| 28 |
compile(code.format('<>'), '<FLUFL test>', 'exec') |
| 29 |
self.assertRegex(str(cm.exception), "invalid syntax") |
| 30 |
self.assertIn('2 <> 3', cm.exception.text) |
| 31 |
self.assertEqual(cm.exception.filename, '<FLUFL test>') |
| 32 |
self.assertEqual(cm.exception.lineno, 1) |
| 33 |
# The old parser reports the end of the token and the new |
| 34 |
# parser reports the start of the token |
| 35 |
self.assertEqual(cm.exception.offset, 3) |
| 36 |
|
| 37 |
def test_barry_as_bdfl_look_ma_with_no_compiler_flags(self): |
| 38 |
# Check that the future import is handled by the parser |
| 39 |
# even if the compiler flags are not passed. |
| 40 |
code = "from __future__ import barry_as_FLUFL;2 {0} 3" |
| 41 |
compile(code.format('<>'), '<BDFL test>', 'exec') |
| 42 |
with self.assertRaises(SyntaxError) as cm: |
| 43 |
compile(code.format('!='), '<FLUFL test>', 'exec') |
| 44 |
self.assertRegex(str(cm.exception), "with Barry as BDFL, use '<>' instead of '!='") |
| 45 |
self.assertIn('2 != 3', cm.exception.text) |
| 46 |
self.assertEqual(cm.exception.filename, '<FLUFL test>') |
| 47 |
self.assertEqual(cm.exception.lineno, 1) |
| 48 |
self.assertEqual(cm.exception.offset, len(code) - 4) |
| 49 |
|
| 50 |
def test_barry_as_bdfl_relative_import(self): |
| 51 |
code = "from .__future__ import barry_as_FLUFL;2 {0} 3" |
| 52 |
compile(code.format('!='), '<FLUFL test>', 'exec') |
| 53 |
with self.assertRaises(SyntaxError) as cm: |
| 54 |
compile(code.format('<>'), '<BDFL test>', 'exec') |
| 55 |
self.assertRegex(str(cm.exception), "<BDFL test>") |
| 56 |
self.assertIn('2 <> 3', cm.exception.text) |
| 57 |
self.assertEqual(cm.exception.filename, '<BDFL test>') |
| 58 |
self.assertEqual(cm.exception.lineno, 1) |
| 59 |
self.assertEqual(cm.exception.offset, len(code) - 4) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
if __name__ == '__main__': |
| 65 |
unittest.main() |