async def test_TUI_completion_candidates_for_posargs_with_two_args_on_first_arg(self, mock_fs_path, mock_torrent_filter): from stig.commands.tui import MoveTorrentsCmd mock_torrent_filter.return_value = (Candidates(('a', 'b', 'c')),) cands = await MoveTorrentsCmd.completion_candidates(Args(('move', 'foo', 'bar'), curarg_index=1)) mock_torrent_filter.assert_called_once_with('foo') mock_fs_path.assert_not_called() self.assertEqual(cands, (Candidates(('a', 'b', 'c')),))
def test_empty_strings_are_excluded(self): class Something(): def __str__(self): return '' self.assertEqual(tuple(Candidates(('a', '', 'b', Something()))), ('a', 'b')) self.assertEqual(tuple(Candidates(('', ))), ())
async def test_TUI_completion_candidates_for_posargs_with_two_args_on_second_arg(self, mock_fs_path, mock_torrent_filter): from stig.commands.tui import MoveTorrentsCmd mock_fs_path.return_value = Candidates(('d', 'e', 'f')) self.remotecfg['path.complete'] = '/some/path/' cands = await MoveTorrentsCmd.completion_candidates(Args(('move', 'foo', 'bar'), curarg_index=2)) mock_torrent_filter.assert_not_called() mock_fs_path.assert_called_once_with('bar', base=self.remotecfg['path.complete'], directories_only=True) self.assertEqual(cands, Candidates(('d', 'e', 'f')))
def test_equality(self): a = Candidates(('foo', 'bar', 'baz')) b = Candidates(('bar', 'baz', 'foo')) self.assertEqual(a, b) a.reduce('^ba') self.assertEqual(a, b) self.assertNotEqual(tuple(a), tuple(b))
def test_current_and_current_index_when_reduced(self): self.assert_focus(Candidates(), None, None) cands = Candidates(('bar', 'baz', 'foo')) cands.reduce(r'^f') cands.current_index = -1 self.assert_focus(cands, 0, 'foo') cands.current_index = 0 self.assert_focus(cands, 0, 'foo') cands.current_index = 1 self.assert_focus(cands, 0, 'foo')
async def test_get_candidates_returns_nested_iterables_and_iterators_of_Candidates( self): cands = Candidates(('a', 'b', 'c')) cands_iter = iter((cands, Candidates(('d', 'e', 'f')))) def get_cands(args): yield from (cands_iter, Candidates(('g', 'h', 'i'))) await self.do(get_cands, (('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i')))
async def test_no_path_given_completes_torrent_filter( self, mock_torrents, mock_torrent_filter): mock_torrent_filter.return_value = (Candidates( ('mock torrent_filter() candidates', ), curarg_seps=('.', ',')), ) cands = await candidates.torrent_path(Arg('id=foo/a/b/c', curpos=6)) exp_cands = (Candidates(('mock torrent_filter() candidates', ), curarg_seps=('.', ',', '/')), ) self.assertEqual(cands, exp_cands) mock_torrent_filter.assert_called_once_with('id=foo') mock_torrents.assert_not_called()
async def test_curpos_args_with_multiple_categories_with_different_curarg_seps( self): cats = (Candidates(('foo', 'bar'), curarg_seps=('/', )), Candidates(('foo', 'bang'), curarg_seps=('.', ))) await self.do(cats, 'foo/ba', 3, 'foo') await self.do(cats, 'foo/ba', 4, '') await self.do(cats, 'foo/ba', 5, 'b') await self.do(cats, 'foo/ba', 6, 'ba') await self.do(cats, 'foo/bang', 8, 'foo/bang') await self.do(cats, 'foo.bang', 8, 'bang')
def test_len_with_some_reduced_candidate_lists(self): cats = Categories(Candidates(('boo', 'foo')), Candidates( ('bar', 'far')), Candidates(('faz', 'naz'))) self.assertEqual(len(cats), 3) self.reduce(cats, '^x') self.assertEqual(len(cats), 0) self.reduce(cats, '^f') self.assertEqual(len(cats), 3) self.reduce(cats, '^b') self.assertEqual(len(cats), 2)
async def test_completion_candidates_for_posargs(self, mock_fs_path, mock_torrent_filter): mock_torrent_filter.return_value = Candidates(('a', 'b', 'c')) cands = await RemoveTorrentsCmd.completion_candidates(Args(('remove', 'foo'), curarg_index=1)) mock_torrent_filter.assert_called_once_with('foo') self.assertEqual(cands, Candidates(('a', 'b', 'c'))) mock_torrent_filter.reset_mock() mock_torrent_filter.return_value = Candidates(('a', 'b', 'c')) cands = await RemoveTorrentsCmd.completion_candidates(Args(('remove', 'foo', 'bar'), curarg_index=2)) mock_torrent_filter.assert_called_once_with('bar') self.assertEqual(cands, Candidates(('a', 'b', 'c')))
async def test_curpos_args_with_multiple_categories_with_same_curarg_seps( self): cats = (Candidates(('foo', 'bar'), curarg_seps=('/', )), Candidates(('foo', 'bang'), curarg_seps=('/', ))) await self.do(cats, 'foo/bar', 3, 'foo') await self.do(cats, 'foo/bar', 4, '') await self.do(cats, 'foo/bar', 5, 'b') await self.do(cats, 'foo/bar', 6, 'ba') await self.do(cats, 'foo/bar', 7, 'bar') await self.do(cats, 'foo/ban', 7, 'ban') await self.do(cats, 'foo/bang/abc', 12, 'foo/bang/abc')
def test_current_and_current_index(self): self.assert_focus(Candidates(), None, None) cands = Candidates(('bar', 'baz', 'foo')) cands.current_index = -1 self.assert_focus(cands, 0, 'bar') cands.current_index = 0 self.assert_focus(cands, 0, 'bar') cands.current_index = 1 self.assert_focus(cands, 1, 'baz') cands.current_index = 2 self.assert_focus(cands, 2, 'foo') cands.current_index = 3 self.assert_focus(cands, 2, 'foo')
async def test_completion_candidates_when_completing_list_values( self, mock_candidates): mock_candidates.setting_names.return_value = Candidates(('foo', 'bar')) mock_candidates.setting_values.return_value = Candidates( ('a', 'b', 'c')) await self.assert_completion_candidates( SetCmd, Args(('set', 'foo', '_', '_', '_'), curarg_index=2), exp_cands=('a', 'b', 'c')) await self.assert_completion_candidates( SetCmd, Args(('set', 'bar', '_', '_', '_'), curarg_index=3), exp_cands=('a', 'b', 'c'))
async def test_get_candidates_returns_multiple_Candidates(self): test_value = (Candidates( ('fooo', 'barrr', 'bazzz')), Candidates(('a', 'b', 'c'))) def get_cands(args): return test_value await self.do(get_cands, test_value) async def get_cands(args): return test_value await self.do(get_cands, test_value)
async def test_focusing_filter_name(self, mock_filter_names, mock_get_filter_cls, mock_torrent_filter_values): mock_get_filter_cls.return_value.INVERT_CHAR = '!' mock_get_filter_cls.return_value.DEFAULT_FILTER = 'mock default filter' mock_filter_names.return_value = Candidates(('foo', 'bar', 'baz'), curarg_seps=('.', ':'), label='Mock Filter Names') mock_torrent_filter_values.return_value = 'mock torrent values' cands = await candidates.torrent_filter(Arg('bar=asdf', curpos=2)) mock_torrent_filter_values.assert_called_once_with('mock default filter') exp_cands = ( Candidates(('foo', 'bar', 'baz'), curarg_seps=('.', ':'), label='Mock Filter Names'), 'mock torrent values' ) self.assertEqual(cands, exp_cands)
def test_all_lists_are_reduced_to_nothing(self): cats = Categories(Candidates(('boo', 'foo')), Candidates( ('bar', 'far')), Candidates(('faz', 'naz'))) self.reduce(cats, 'x') def assert_state(): self.assertEqual(len(cats), 0) self.assertEqual(cats.current_index, None) self.assertEqual(cats.current, None) cats.next() assert_state() cats.prev() assert_state()
async def test_server_request_failed(self, mock_filter_takes_completable_values, mock_get_filter_spec, mock_get_filter_cls): mock_filter_takes_completable_values.return_value = True mock_get_filter_cls.return_value.return_value.needed_keys = ( 'mockkey1', 'mockkey2') mock_value_type = mock_get_filter_spec.return_value.value_type delattr(mock_value_type, 'valid_values') mock_objects_getter = asynctest.CoroutineMock() mock_items_getter = MagicMock() cands = await candidates._filter_values('MockFilter', 'mock filter name', mock_objects_getter, mock_items_getter) mock_get_filter_cls.assert_called_once_with('MockFilter') mock_get_filter_spec.assert_called_once_with(mock_get_filter_cls(), 'mock filter name') mock_objects_getter.assert_called_once_with(keys=('mockkey1', 'mockkey2')) mock_items_getter.assert_not_called() exp_cands = Candidates((), curarg_seps=('|', '&', '=', '!='), label='Mock Filter Label: mock filter name') self.assertEqual(cands, exp_cands)
async def test_item_value_is_mixed_string_and_iterable( self, mock_filter_takes_completable_values, mock_get_filter_spec, mock_get_filter_cls): mock_filter_takes_completable_values.return_value = True mock_get_filter_cls.return_value.return_value.needed_keys = ( 'mockkey2', ) mock_objects = ({ 'mockkey1': ('a', 'b'), 'mockkey2': 'b' }, { 'mockkey1': ('d', 'e'), 'mockkey2': ('f', 'g') }) mock_objects_getter = asynctest.CoroutineMock( return_value=mock_objects) mock_value_type = mock_get_filter_spec.return_value.value_type delattr(mock_value_type, 'valid_values') mock_value_getter = mock_get_filter_spec.return_value.value_getter mock_value_getter.side_effect = lambda item: item['mockkey2'] cands = await candidates._filter_values('MockFilter', 'mock filter name', mock_objects_getter, items_getter=None) mock_get_filter_cls.assert_called_once_with('MockFilter') mock_get_filter_spec.assert_called_once_with(mock_get_filter_cls(), 'mock filter name') assert mock_objects_getter.call_args_list == [ call(keys=('mockkey2', )) ] exp_cands = Candidates(('b', 'f', 'g'), curarg_seps=('|', '&', '=', '!='), label='Mock Filter Label: mock filter name') self.assertEqual(cands, exp_cands)
async def test_completion_candidates_torrent_filter(self, mock_candidates): mock_candidates.torrent_filter.return_value = Candidates( ('a', 'b', 'c')) await self.assert_completion_candidates( RateLimitCmd, Args(('ratelimit', 'up', '10MB', '_'), curarg_index=3), exp_cands=('a', 'b', 'c')) await self.assert_completion_candidates( RateLimitCmd, Args(('ratelimit', 'up', '10MB', '_', '_'), curarg_index=4), exp_cands=('a', 'b', 'c')) await self.assert_completion_candidates( RateLimitCmd, Args(('ratelimit', 'up', '10MB', '_', '_'), curarg_index=3), exp_cands=('a', 'b', 'c')) await self.assert_completion_candidates( RateLimitCmd, Args(('ratelimit', '-q', 'up', '10MB', '_', '_'), curarg_index=4), exp_cands=('a', 'b', 'c')) await self.assert_completion_candidates( RateLimitCmd, Args(('ratelimit', 'up', '-q', '10MB', '_', '_'), curarg_index=5), exp_cands=('a', 'b', 'c')) await self.assert_completion_candidates( RateLimitCmd, Args(('ratelimit', 'up', '10MB', '-q', '_', '_'), curarg_index=4), exp_cands=('a', 'b', 'c'))
async def test_curpos_args_with_single_category(self): cats = (Candidates(('foo', 'bar', 'baz'), curarg_seps=('/', )), ) await self.do(cats, 'foo/bar/', 3, 'foo') await self.do(cats, 'foo/bar/', 4, '') await self.do(cats, 'foo/bar/', 5, 'b') await self.do(cats, 'foo/bar/', 6, 'ba') await self.do(cats, 'foo/bar/', 7, 'bar') await self.do(cats, 'foo/bar/abc', 11, 'foo/bar/abc')
def test_if_possible_keep_selected_candidate_when_reduced(self): cands = Candidates(('abc', 'cde', 'efg')) cands.current_index = 1 self.assert_focus(cands, 1, 'cde') cands.reduce(r'e') self.assert_focus(cands, 0, 'cde') cands.reduce(r'f') self.assert_focus(cands, 0, 'efg')
async def test_completion_candidates(self, mock_candidates): mock_candidates.setting_names.return_value = Candidates( ('a', 'b', 'c')) await self.assert_completion_candidates(ResetCmd, Args(('reset', 'hey', 'ho'), curarg_index=2), exp_cands=('a', 'b', 'c')) mock_candidates.setting_names.assert_called_once_with()
async def test_completion_candidates_for_posargs(self, mock_torrent_filter): mock_torrent_filter.return_value = Candidates(('a', 'b', 'c')) await self.assert_completion_candidates(ListTorrentsCmd, Args(('ls', 'foo'), curarg_index=1), exp_cands=('a', 'b', 'c')) mock_torrent_filter.assert_called_once_with('foo') mock_torrent_filter.reset_mock() await self.assert_completion_candidates(ListTorrentsCmd, Args(('ls', 'foo', 'bar'), curarg_index=2), exp_cands=('a', 'b', 'c')) mock_torrent_filter.assert_called_once_with('bar')
async def test_completion_candidates_for_posargs(self, mock_fs_path): mock_fs_path.return_value = Candidates(('a', 'b', 'c')) await self.assert_completion_candidates(AddTorrentsCmd, Args(('add', 'foo'), curarg_index=1), exp_cands=('a', 'b', 'c')) mock_fs_path.assert_called_once_with('foo', glob='*.torrent') mock_fs_path.reset_mock() await self.assert_completion_candidates(AddTorrentsCmd, Args(('add', 'foo', 'bar'), curarg_index=2), exp_cands=('a', 'b', 'c')) mock_fs_path.assert_called_once_with('bar', glob='*.torrent')
async def test_completion_candidates_on_any_other_argument( self, mock_candidates): mock_candidates.fs_path.return_value = Candidates( ('foo', 'bar', 'baz')) await self.assert_completion_candidates(RcCmd, Args(('rc', 'hey', 'ho'), curarg_index=2), exp_cands=None) mock_candidates.fs_path.assert_not_called()
def test_prev(self): cands = Candidates(('bar', 'baz', 'foo')) for _ in range(3): self.assert_focus(cands, 0, 'bar') cands.prev() self.assert_focus(cands, 2, 'foo') cands.prev() self.assert_focus(cands, 1, 'baz') cands.prev() self.assert_focus(cands, 0, 'bar')
def test_reduce(self): cands = Candidates(('bar', 'baz', 'foo')) self.assert_focus(cands, 0, 'bar') cands.reduce(r'^b') self.assertEqual(tuple(cands), ('bar', 'baz')) self.assert_focus(cands, 0, 'bar') cands.next() self.assert_focus(cands, 1, 'baz') cands.next() self.assert_focus(cands, 0, 'bar')
def test_setting_is_a_Tuple(self, mock_rcfg, mock_lcfg): for mock_cfg,setting_name in ((mock_lcfg, 'foo'), (mock_rcfg, 'srv.foo')): mock_cfg.__iter__.return_value = (setting_name,) mock_cfg.__contains__.return_value = True mock_cfg.__getitem__.return_value = usertypes.Tuple('a', 'b', options=('a', 'b', 'c')) for i in (1, 2, 3): cmdline = Args((setting_name, '_', '_', '_'), curarg_index=i, curarg_curpos=0) cands = candidates.setting_values(cmdline) exp_cands = Candidates(('a', 'b', 'c'), label='%s options' % setting_name, curarg_seps=(',',)) self.assertEqual(cands, exp_cands)
def test_setting_is_an_Option(self, mock_rcfg, mock_lcfg): for mock_cfg,setting_name in ((mock_lcfg, 'foo'), (mock_rcfg, 'srv.foo')): mock_cfg.__iter__.return_value = (setting_name,) mock_cfg.__contains__.return_value = True mock_cfg.__getitem__.return_value = usertypes.Option('b', options=('a', 'b', 'c')) cmdline = Args((setting_name, '_', '_'), curarg_index=1, curarg_curpos=0) self.assertEqual(candidates.setting_values(cmdline), Candidates(('a', 'b', 'c'), label='%s options' % setting_name)) cmdline = Args((setting_name, '_', '_'), curarg_index=2, curarg_curpos=0) self.assertFalse(candidates.setting_values(cmdline))
async def test_completion_candidates__subcmd_parameters( self, mock_commands, mock_for_args): mock_for_args.return_value = Candidates(('foo', 'bar', 'baz')) args = Args(('tab', '--bar', 'a', '-b', 'c'), curarg_index=4) await self.assert_completion_candidates(TabCmd, args, exp_cands=('foo', 'bar', 'baz')) mock_for_args.assert_called_once_with( Args(('a', '-b', 'c'), curarg_index=2, curarg_curpos=0)) mock_commands.assert_not_called()