def init(self, get_cands): self.mock_get_cands = MagicMock(side_effect=get_cands) def get_cands(args): return self.mock_get_cands(args) self.completer = Completer(get_cands, operators=('&', 'and', '|', 'or'))
async def do(self, get_cands, exp_cats): completer = Completer(get_cands) result = await completer._get_candidates_wrapper(('', )) self.assertIsInstance(result, Categories) for c in result: self.assertIsInstance(c, Candidates) self.assertIsInstance(result[0], SingleCandidate) self.assertEqual(tupleize(result[1:]), tupleize(exp_cats))
async def do(self, categories, cmdline, curpos, exp_cur_user_input): completer = Completer(lambda *args, **kwargs: categories) await completer.update(cmdline, curpos) self.assertEqual(completer.current_user_input, exp_cur_user_input)
async def init(self, cmdline, curpos, get_cands): self.completer = Completer(get_cands, operators=('&', 'and', '|', 'or')) await self.completer.update(cmdline, curpos)
class TestCompleter_complete_next_prev(asynctest.TestCase): async def init(self, cmdline, curpos, get_cands): self.completer = Completer(get_cands, operators=('&', 'and', '|', 'or')) await self.completer.update(cmdline, curpos) def do_next(self, exp_cmdline, exp_curpos): self.assertEqual(self.completer.complete_next(), (exp_cmdline, exp_curpos)) def do_prev(self, exp_cmdline, exp_curpos): self.assertEqual(self.completer.complete_prev(), (exp_cmdline, exp_curpos)) async def test_empty_command_line(self): def get_cands(args): return Candidates(('foo', 'ba', 'bazz')) await self.init('', 0, get_cands) for _ in range(3): self.do_next('ba', 2) self.do_next('bazz', 4) self.do_next('foo', 3) self.do_next('', 0) for _ in range(3): self.do_prev('foo', 3) self.do_prev('bazz', 4) self.do_prev('ba', 2) self.do_prev('', 0) async def test_complete_at_end_of_command_line(self): def get_cands(args): return Candidates(('-a', '-be', '-cee')) await self.init('foo ', 4, get_cands) for _ in range(3): self.do_next('foo -a', 6) self.do_next('foo -be', 7) self.do_next('foo -cee', 8) self.do_next('foo ', 4) for _ in range(3): self.do_prev('foo -cee', 8) self.do_prev('foo -be', 7) self.do_prev('foo -a', 6) self.do_prev('foo ', 4) async def test_complete_in_middle_of_command_line(self): def get_cands(args): return Candidates(('-a', '-be', '-cee')) await self.init('foo & bar', 5, get_cands) for _ in range(3): self.do_next('foo -a & bar', 7) self.do_next('foo -be & bar', 8) self.do_next('foo -cee & bar', 9) self.do_next('foo & bar', 5) for _ in range(3): self.do_prev('foo -cee & bar', 9) self.do_prev('foo -be & bar', 8) self.do_prev('foo -a & bar', 7) self.do_prev('foo & bar', 5) async def test_complete_at_beginning_of_command_line(self): def get_cands(args): return Candidates(('foo', 'ba', 'bazz')) await self.init(' -a', 0, get_cands) for _ in range(3): self.do_next('ba -a', 2) self.do_next('bazz -a', 4) self.do_next('foo -a', 3) self.do_next(' -a', 0) for _ in range(3): self.do_prev('foo -a', 3) self.do_prev('bazz -a', 4) self.do_prev('ba -a', 2) self.do_prev(' -a', 0) async def test_complete_at_beginning_of_command_line_with_leading_space( self): def get_cands(args): return Candidates(('foo', 'ba', 'bazz')) await self.init(' -a', 2, get_cands) for _ in range(3): self.do_next(' ba -a', 4) self.do_next(' bazz -a', 6) self.do_next(' foo -a', 5) self.do_next(' -a', 2) for _ in range(3): self.do_prev(' foo -a', 5) self.do_prev(' bazz -a', 6) self.do_prev(' ba -a', 4) self.do_prev(' -a', 2) async def test_follow_user_style_with_special_characters(self): def get_cands(args): return Candidates(('foo or bar ', )) await self.init("'foo ", 5, get_cands) self.do_next('"foo or bar "', 13) await self.init(r'foo\ ', 5, get_cands) self.do_next(r'foo\ or\ bar\ ', 14) async def test_non_space_delimiter(self): def get_cands(args): return Candidates(('b ', r'b\r', 'f"oo'), curarg_seps=('/', )) await self.init(r'ls "path to"/f\" -x', 16, get_cands) self.do_next(r'ls "path to"/f\"oo -x', 18) await self.init(r'ls "path to"/b -x', 14, get_cands) self.do_next(r'ls "path to"/"b " -x', 17) self.do_next(r'ls "path to"/"b\r" -x', 18) async def test_non_space_delimiter_with_no_common_prefix(self): def get_cands(args): return Candidates(('b ', r'b\r', 'f"oo'), curarg_seps=('/', )) await self.init(r'ls "path to"/ -x', 13, get_cands) self.do_next(r'ls "path to"/"b " -x', 17) async def test_non_space_delimiter_with_cursor_on_previous_candidate(self): def get_cands(args): return Candidates(('b ', r'b\r', 'f"oo'), curarg_seps=('/', )) await self.init(r'''ls "path to"/f/abc -x''', 14, get_cands) self.do_next(r'''ls "path to"/'f"oo'/abc -x''', 19) await self.init(r'''ls "path to"/f/abc/def/ -x''', 14, get_cands) self.do_next(r'''ls "path to"/'f"oo'/abc/def/ -x''', 19) async def test_non_space_delimiter_with_cursor_on_first_part(self): def get_cands(args): return Candidates(('b ', r'b\r', 'f"oo'), curarg_seps=('/', )) await self.init(r'''ls abc/def/ghi -x''', 3, get_cands) self.do_next(r'''ls "b "/def/ghi -x''', 7) self.do_next(r'''ls "b\r"/def/ghi -x''', 8) self.do_next(r'''ls 'f"oo'/def/ghi -x''', 9)