|
5 | 5 | except ImportError:
|
6 | 6 | import unittest
|
7 | 7 |
|
8 |
| -from bpython.curtsiesfrontend import interpreter |
9 | 8 | from curtsies.fmtfuncs import bold, green, magenta, cyan, red, plain
|
10 | 9 |
|
| 10 | +from bpython.curtsiesfrontend import interpreter |
| 11 | +from bpython._py3compat import py3 |
| 12 | +from bpython.test import mock |
| 13 | + |
11 | 14 |
|
12 | 15 | class TestInterpreter(unittest.TestCase):
|
13 | 16 | def test_syntaxerror(self):
|
@@ -51,3 +54,42 @@ def g():
|
51 | 54 |
|
52 | 55 | self.assertEquals(str(plain('').join(a)), str(expected))
|
53 | 56 | self.assertEquals(plain('').join(a), expected)
|
| 57 | + |
| 58 | + @unittest.skipIf(py3, "runsource() accepts only unicode in Python 3") |
| 59 | + def test_runsource_bytes(self): |
| 60 | + i = interpreter.Interp() |
| 61 | + i.encoding = 'latin-1' |
| 62 | + |
| 63 | + i.runsource(b"a = b'\xfe'") |
| 64 | + self.assertIsInstance(i.locals['a'], str) |
| 65 | + self.assertEqual(i.locals['a'], b"\xfe") |
| 66 | + |
| 67 | + i.runsource(b"b = u'\xfe'") |
| 68 | + self.assertIsInstance(i.locals['b'], unicode) |
| 69 | + self.assertEqual(i.locals['b'], u"\xfe") |
| 70 | + |
| 71 | + @unittest.skipUnless(py3, "Only a syntax error in Python 3") |
| 72 | + @mock.patch.object(interpreter.Interp, 'showsyntaxerror') |
| 73 | + def test_runsource_bytes_over_128_syntax_error(self): |
| 74 | + i = interpreter.Interp() |
| 75 | + i.encoding = 'latin-1' |
| 76 | + |
| 77 | + i.runsource(u"a = b'\xfe'") |
| 78 | + i.showsyntaxerror.assert_called_with() |
| 79 | + |
| 80 | + @unittest.skipIf(py3, "only ASCII allowed in bytestrings in Python 3") |
| 81 | + def test_runsource_bytes_over_128_syntax_error(self): |
| 82 | + i = interpreter.Interp() |
| 83 | + i.encoding = 'latin-1' |
| 84 | + |
| 85 | + i.runsource(u"a = b'\xfe'") |
| 86 | + self.assertIsInstance(i.locals['a'], type(b'')) |
| 87 | + self.assertEqual(i.locals['a'], b"\xfe") |
| 88 | + |
| 89 | + def test_runsource_unicode(self): |
| 90 | + i = interpreter.Interp() |
| 91 | + i.encoding = 'latin-1' |
| 92 | + |
| 93 | + i.runsource(u"a = u'\xfe'") |
| 94 | + self.assertIsInstance(i.locals['a'], type(u'')) |
| 95 | + self.assertEqual(i.locals['a'], u"\xfe") |
0 commit comments