def test_match(self): f = mocks.FE() w = messager.Messager(f) self.assertFalse(w.match('foo')) f.context.backends._messages[0]._display = chunks.Chunk([((), 'foo\n') ]) self.assertTrue(w.match('foo'))
def test_quit_hook_starks(self): f = mocks.FE() w = messager.Messager(f) self.assertIsNotNone(w.replymsg()) self.assertFalse(f.context.starks) w.quit_hook() self.assertTrue(f.context.starks)
def test_move_backwards_target(self): f = mocks.FE() w = messager.Messager(f) w.move(-1) self.assertIsNotNone(f.context.backends._target) self.assertLess(f.context.backends._target, 0) self.assertTrue(math.isinf(f.context.backends._target))
def test_modeline(self): f = mocks.FE() w = messager.Messager(f) w.renderer = mocks.Renderer() self.assertEqual(w.modeline(), ([(set(), 'filter default')], [({'right'}, '1')])) m = f.context.backends._messages[0] m.time = 0.0 w.renderer = mocks.Renderer((m, m)) os.environ['TZ'] = 'GMT' self.assertEqual(w.modeline(), ([({'dim'}, '1970-01-01 00:00'), (set(), ' filter default')], [({'right'}, '1')])) m.time = float('NaN') class mockdatetime(datetime.datetime): @staticmethod def now(): return datetime.datetime.fromtimestamp(60) with patch('datetime.datetime', mockdatetime): self.assertEqual( w.modeline(), ([({'dim'}, '00:01'), (set(), ' filter default')], [({'right'}, '1')]))
def test_mark(self): f = mocks.FE() w = messager.Messager(f) self.assertIsNone(w.the_mark) m = f.context.backends._messages m.append(mocks.Message()) m.append(mocks.Message()) w.set_mark() self.assertEqual(w.the_mark, w.cursor) w.set_mark(m[-1]) self.assertEqual(w.the_mark, m[-1]) self.assertEqual(w.mark_ring[-1], m[0]) where = w.cursor there = w.the_mark anywhere = w.mark_ring[-1] w.set_mark(prefix=[4]) self.assertEqual(w.mark_ring[0], where) self.assertEqual(w.the_mark, anywhere) self.assertEqual(w.cursor, there) o = object() self.assertIs(o, w.make_mark(o)) # pass through self.assertNotEqual(w.cursor, m[0]) w.go_mark(m[0]) self.assertEqual(w.cursor, m[0]) w.the_mark = m[-1] w.exchange_point_and_mark() self.assertEqual(w.cursor, m[-1]) self.assertEqual(w.the_mark, m[0])
def test_view_bad_message(self): f = mocks.FE() w = messager.Messager(f) f.context.backends._messages[0]._display = [()] # malformed chunk self.assertEqual( ({'visible', 'bar'}, '[()]\n'), [chunk.tagsets() for (mark, chunk) in w.view(0)][0][0])
def test_keyseqprompt(self): result = None def cb(s): nonlocal result result = s w = prompt.KeySeqPrompt(mocks.FE(), prompt=': ', keymap=keymap.Keymap({'a': 0}), callback=cb) w.keyecho('a') # how do you test that somethign did nothing? w.echo_keystroke('a') self.assertEqual(w.keystrokes, ['a']) self.assertEqual(w.input(), 'a ') w.runcallback() self.assertEqual(result, (['a'], None)) self.assertEqual([(0, [ ({'bold'}, ': '), ((), 'a '), ({'cursor', 'visible'}, ''), ])], [(int(mark), chunk.tagsets()) for (mark, chunk) in w.view(0)])
def test_yank(self): e = snipe.editor.Editor(None) e.fe = mocks.FE() e.insert_region('foo') self.assertEqual('foo', str(e.buf)) e.kill_region() self.assertEqual('', str(e.buf)) e.yank() self.assertEqual('foo', str(e.buf)) e.kill_region() self.assertEqual('', str(e.buf)) e.insert_region('bar') self.assertEqual('bar', str(e.buf)) e.kill_region() self.assertEqual('', str(e.buf)) e.yank(2) self.assertEqual('foo', str(e.buf)) self.assertEqual(3, e.cursor) self.assertEqual(0, e.the_mark) e.kill_region() e.yank([]) self.assertEqual('foo', str(e.buf)) self.assertEqual(0, e.cursor) self.assertEqual(3, e.the_mark) e.kill_region() e.yank() self.assertEqual('foo', str(e.buf)) e.whine = Mock() e.yank_pop() e.whine.assert_called() e.last_command = 'yank' e.yank_pop(2) self.assertEqual('bar', str(e.buf))
def test_focus(self): w = messager.Messager(mocks.FE()) c = w.cursor w.secondary, w.cursor = c, None self.assertTrue(w.focus()) self.assertIs(w.cursor, c) self.assertIsNone(w.secondary)
async def test_send(self): f = mocks.FE() m = mocks.Message() m.omega = True f.context.backends._messages.append(m) w = messager.Messager(f) w.renderer = mocks.Renderer((f.context.backends._messages[0], f.context.backends._messages[-1])) c = w.cursor _read_args = None _read_kw = None _read_result = 'foo\nbar' async def read_string(*args, **kw): nonlocal _read_args, _read_kw _read_args, _read_kw = args, kw return _read_result w.read_string = read_string await w.send() self.assertEqual(f.context.backends._sent[-1], ('foo', 'bar')) self.assertNotIn('modes', _read_kw) self.assertEqual(w.secondary, c) w.cursor = c _read_result = 'foo' await w.send(msg=c) self.assertIn('modes', _read_kw) self.assertEqual(f.context.backends._sent[-1], ('foo', ''))
def test_go(self): w = repl.REPL(mocks.FE()) w.log.debug('A: %s', repr(w.buf[:])) w.insert('2 + 2') w.log.debug('B: %s', repr(w.buf[:])) earlier = w.cursor.point w.go() w.log.debug('C: %s', repr(w.buf[:])) result = '\n' + str(4) + '\n' + w.state['ps1'] self.assertEqual(w.buf[-len(result):], result) self.assertEqual(w.state['in'][0], '2 + 2') self.assertEqual(w.state['out'][0], 4) t = 'def flurb():' w.insert(t) w.log.debug('D: %s', repr(w.buf[:])) w.go() self.assertEqual(w.context._message, 'incomplete input') w.log.debug('E before go2: %s', repr(w.buf[:])) w.go2() w.log.debug('F after go2: %s', repr(w.buf[:])) self.assertEqual(w.buf[-(len(t) + 1):], t + '\n') w.cursor.point = earlier w.go() result = result + t + '\n' self.assertEqual(w.buf[-len(result):], result) self.assertEqual(w.state['in'][1], '2 + 2') self.assertEqual(w.state['out'][1], 4)
def test_pageup_backfill(self): f = mocks.FE() w = messager.Messager(f) w.renderer = mocks.Renderer(range=tuple(f.context.backends._messages * 2)) self.assertIsNone(f.context.backends._target) w.pageup() self.assertTrue(math.isinf(f.context.backends._target))
async def test_filter_sender(self): f = mocks.FE() w = messager.Messager(f) w.read_string = returning('green') await w.filter_sender() self.assertEqual(str(w.filter), 'filter default and sender = "green"')
def test_evaller_2(self): e = snipe.editor.Editor(None) e.fe = mocks.FE() e.set_mark() e.insert('if True:\n') self.assertEqual('if True:\n', str(e.buf)) e.eval_region(arg=['x']) self.assertIn('notify', e.fe.called) self.assertEqual('if True:\n', str(e.buf))
def test_cursor_set_walk(self): f = mocks.FE() w = messager.Messager(f) m = mocks.Message() m.omega = True f.context.backends._messages.append(m) self.assertEqual(w.cursor, f.context.backends._messages[0]) w.cursor_set_walk_mark(None, False) self.assertEqual(w.cursor, f.context.backends._messages[1])
def test_move_cleverness_arg(self): f = mocks.FE() w = messager.Messager(f) w.move_cleverly(True, None) self.assertEqual(w.move_cleverly_state, 0) w.move_cleverly(True, 1) self.assertEqual(w.move_cleverly_state, 1) w.move_cleverly(True, 'xx') self.assertEqual(w.move_cleverly_state, 2)
def test_redisplay(self): s = SyntheticBackend(mocks.Context()) s.context.ui = mocks.FE() self.assertNotIn('redisplay', s.context.ui.called) s.redisplay(None, None) self.assertIn('redisplay', s.context.ui.called) s.context.ui.redisplay = lambda: None with self.assertLogs(s.log.name, level='ERROR'): s.redisplay(None, None)
def test_filter_clear_decorate(self): f = mocks.FE() w = messager.Messager(f) w.filter = filters.Yes() decor = {'foreground': 'white', 'background': 'blue'} w.filter_clear_decorate(decor) self.assertEqual(f.context.conf['rule'], [('yes', decor)]) self.assertEqual([[({'visible', 'bar'}, '\n')]], [chunk.tagsets() for (mark, chunk) in w.view(0)])
def test_next_prev_day(self): f = mocks.FE() w = messager.Messager(f) target = None def mock_goto_time(*args): nonlocal target target = args w.goto_time = mock_goto_time class MockDate: TODAY = datetime.date(1970, 1, 1) @classmethod def today(klass, *args): return klass.TODAY f.context.backends._messages[0].omega = True os.environ['TZ'] = 'GMT' try: # pypy with patch('datetime.date.today', MockDate.today): w.prev_day() except TypeError: # cpython with patch('datetime.date', MockDate): w.prev_day() self.assertEqual(target, (0.0, )) f.context.backends._messages[0].omega = False f.context.backends._messages[0].time = \ datetime.datetime(1970, 1, 2).timestamp() target = None w.prev_day() self.assertEqual(target, (0.0, )) target = None w.next_day(-1) self.assertEqual(target, (0.0, )) target = None f.context.backends._messages[0].omega = True w.next_day() self.assertIsNone(target) f.context.backends._messages[0].omega = False f.context.backends._messages[0].time = \ datetime.datetime(1969, 12, 31).timestamp() w.next_day() self.assertEqual(target, (0.0, )) target = None w.prev_day(-1) self.assertEqual(target, (0.0, ))
async def test_filter_class(self): f = mocks.FE() w = messager.Messager(f) w.read_string = returning('green') await w.filter_class() self.assertEqual( str(w.filter), 'filter default and backend == "roost" and class = "green"')
def test_after_command(self): f = mocks.FE() w = messager.Messager(f) m = mocks.Message() m.omega = True f.context.backends._messages.append(m) w.next_message() w.after_command() self.assertEqual(f.context.starks[-1], f.context.backends._messages[0].time)
def test_move_cleverly(self): f = mocks.FE() w = messager.Messager(f) f.context.backends._messages.append(mocks.Message()) self.assertIs(w.cursor, f.context.backends._messages[0]) w.next_message_cleverly() self.assertIs(w.cursor, f.context.backends._messages[1]) w.prev_message_cleverly() self.assertIs(w.cursor, f.context.backends._messages[0])
def test_set_stark(self): w = messager.Messager(mocks.FE()) m = w.context.backends._messages self.assertFalse(w.context.starks) m[0].omega = True w.set_stark() self.assertFalse(w.context.starks) m[0].omega = False w.set_stark() self.assertEqual(w.context.starks, [m[0].time])
def test(self): fe = mocks.FE() v = snipe.editor.PopViewer(fe, content='', name='test') v.renderer = mocks.Renderer() v.renderer._range = [0, 0] self.assertEqual( (([(set(), '- test')]), ([({'right'}, '1')])), v.modeline()) name = v.buf.name self.assertIn(name, v.buf.registry) v.destroy() self.assertNotIn(name, v.buf.registry)
def test_list_destinations(self): w = messager.Messager(mocks.FE()) out = None def mock_show(text): nonlocal out out = text w.show = mock_show w.list_destinations() self.assertEqual(out, '')
def test_end_beginning(self): f = mocks.FE() w = messager.Messager(f) m = mocks.Message() m.omega = True f.context.backends._messages.append(m) self.assertEqual(w.cursor, f.context.backends._messages[0]) w.end() self.assertEqual(w.cursor, f.context.backends._messages[1]) w.beginning() self.assertEqual(w.cursor, f.context.backends._messages[0])
def test_check_redisplay_hint(self): f = mocks.FE() w = messager.Messager(f) w.renderer = mocks.Renderer() self.assertFalse(w.check_redisplay_hint({})) hint = w.redisplay_hint() self.assertTrue(w.check_redisplay_hint(hint)) r = (f.context.backends._messages[0], f.context.backends._messages[-1]) self.assertFalse(w.check_redisplay_hint({'messages': r})) w.filter = filters.Yes() w.renderer = mocks.Renderer(range=r) self.assertTrue(w.check_redisplay_hint({'messages': r}))
def test_replace_cursor(self): w = prompt.Search(mocks.FE(), forward=False, prompt='search ', history='test_search', target=mocks.Window([])) w.insert('fo') mark = w.make_mark(w.cursor) w.target.find_ret = False w.insert('o') self.assertEqual(w.cursor.point, mark.point + 1)
def test_view_search(self): f = mocks.FE() w = messager.Messager(f) f.context.backends._messages[0]._display = chunks.Chunk([ ((), 'bar\nfoo\n') ]) w.search_re = re.compile('foo') self.assertEqual([ ({'visible', 'bar'}, 'bar\n'), ({'reverse'}, 'foo'), ((), '\n'), ], [chunk.tagsets() for (mark, chunk) in w.view(0)][0])
def test_filter_push(self): f = mocks.FE() w = messager.Messager(f) w.filter = None f0 = filters.Yes() w.filter_push(f0) self.assertIs(w.filter, f0) self.assertEqual(w.filter_stack, []) f1 = filters.No() w.filter_push(f1) self.assertEqual(w.filter, filters.And(f0, f1)) self.assertEqual(w.filter_stack, [f0])