|
| 1 | +# coding: utf8 |
| 2 | +import unittest |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | +from curtsies.formatstringarray import FormatStringTest, fsarray |
| 7 | + |
| 8 | +from curtsies.fmtfuncs import * |
| 9 | + |
| 10 | +from bpython import config |
| 11 | +from bpython.curtsiesfrontend.repl import Repl |
| 12 | +from bpython.repl import History |
| 13 | + |
| 14 | +def setup_config(): |
| 15 | + config_struct = config.Struct() |
| 16 | + config.loadini(config_struct, os.devnull) |
| 17 | + return config_struct |
| 18 | + |
| 19 | +class TestCurtsiesPainting(FormatStringTest): |
| 20 | + def setUp(self): |
| 21 | + self.refresh_requests = [] |
| 22 | + self.repl = Repl(config=setup_config()) |
| 23 | + self.repl.rl_history = History() # clear history |
| 24 | + self.repl.height, self.repl.width = (5, 10) |
| 25 | + |
| 26 | + def assert_paint(self, screen, cursor_row_col): |
| 27 | + array, cursor_pos = self.repl.paint() |
| 28 | + self.assertFSArraysEqual(array, screen) |
| 29 | + self.assertEqual(cursor_pos, cursor_row_col) |
| 30 | + |
| 31 | + def assert_paint_ignoring_formatting(self, screen, cursor_row_col): |
| 32 | + array, cursor_pos = self.repl.paint() |
| 33 | + self.assertFSArraysEqualIgnoringFormatting(array, screen) |
| 34 | + |
| 35 | + def test_startup(self): |
| 36 | + screen = fsarray([cyan('>>> '), cyan('Welcome to')]) |
| 37 | + self.assert_paint(screen, (0, 4)) |
| 38 | + |
| 39 | + def test_enter_text(self): |
| 40 | + [self.repl.add_normal_character(c) for c in '1 + 1'] |
| 41 | + screen = fsarray([cyan('>>> ') + bold(green('1')+cyan(' ')+ |
| 42 | + yellow('+') + cyan(' ') + green('1')), cyan('Welcome to')]) |
| 43 | + self.assert_paint(screen, (0, 9)) |
| 44 | + |
| 45 | + def test_run_line(self): |
| 46 | + try: |
| 47 | + orig_stdout = sys.stdout |
| 48 | + sys.stdout = self.repl.stdout |
| 49 | + [self.repl.add_normal_character(c) for c in '1 + 1'] |
| 50 | + self.repl.on_enter() |
| 51 | + screen = fsarray([u'>>> 1 + 1', '2', 'Welcome to']) |
| 52 | + self.assert_paint_ignoring_formatting(screen, (0, 9)) |
| 53 | + finally: |
| 54 | + sys.stdout = orig_stdout |
| 55 | + |
| 56 | + def test_completion(self): |
| 57 | + self.repl.height, self.repl.width = (5, 32) |
| 58 | + self.repl.current_line = 'se' |
| 59 | + self.cursor_offset = 2 |
| 60 | + screen = [u'>>> se', |
| 61 | + u'┌───────────────────────┐', |
| 62 | + u'│ set( setattr( │', |
| 63 | + u'└───────────────────────┘', |
| 64 | + u'', |
| 65 | + u'Welcome to bpython! Press <F1> f'] |
| 66 | + self.assert_paint_ignoring_formatting(screen, (0, 9)) |
0 commit comments