def setUp(self): self.cmdmgr = CommandManager() self.cmd_foo = make_cmdcls(name='foo', provides=('cli',)) self.cmd_bar = make_cmdcls(name='bar', provides=('tui',)) self.cmd_baz = make_cmdcls(name='baz', provides=('cli', 'tui')) for c in (self.cmd_foo, self.cmd_bar, self.cmd_baz): self.cmdmgr.register(c)
def setUp(self): # It's ok to use the default loop here because we're not using it in # this test class. self.cmdmgr = CommandManager(loop=asyncio.get_event_loop()) self.cmd_foo = make_cmdcls(name='foo', provides=('cli',)) self.cmd_bar = make_cmdcls(name='bar', provides=('tui',)) self.cmd_baz = make_cmdcls(name='baz', provides=('cli', 'tui')) for c in (self.cmd_foo, self.cmd_bar, self.cmd_baz): self.cmdmgr.register(c)
def setUp(self): self.cmdmgr = CommandManager() self.cmdmgr.register(make_cmdcls(name='dotui', provides=('tui', ))) self.cmdmgr.register(make_cmdcls(name='docli', provides=('cli', ))) self.cmdmgr.register( make_cmdcls(name='doboth', provides=('cli', 'tui'))) self.cmdmgr.register( make_cmdcls(name='dotorrent', provides=('cli', 'tui'), category='torrent'))
def setUp(self): self.cmdmgr = CommandManager(loop=self.loop) def sync_run(self_, A, B): assert isinstance(self_, _CommandBase) self.div_result = A / B return True async def async_run(self_, A, B): assert isinstance(self_, _CommandBase) await asyncio.sleep(0, loop=self_.loop) self.div_result = A / B return True argspecs = ({'names': ('A',), 'type': int, 'description': 'First number'}, {'names': ('B',), 'type': int, 'description': 'Second number'}) self.cmdmgr.register( make_cmdcls(name='div', run=sync_run, argspecs=argspecs, provides=('sync',)) ) self.cmdmgr.register( make_cmdcls(name='div', run=async_run, argspecs=argspecs, provides=('async',)) ) async def async_run_CmdError(self_, msg): await asyncio.sleep(0, loop=self_.loop) raise CmdError(msg) def sync_run_CmdError(self_, msg): raise CmdError(msg) argspecs = ({'names': ('msg',), 'type': str, 'description': 'Error message'},) self.cmdmgr.register( make_cmdcls(name='error', run=async_run_CmdError, argspecs=argspecs, provides=('async',)) ) self.cmdmgr.register( make_cmdcls(name='error', run=sync_run_CmdError, argspecs=argspecs, provides=('sync',)) ) async def async_run_Exception(self_): await asyncio.sleep(0, loop=self_.loop) 1/0 def sync_run_Exception(self_): 1/0 self.cmdmgr.register( make_cmdcls(name='raise', run=async_run_Exception, argspecs=(), provides=('async',)) ) self.cmdmgr.register( make_cmdcls(name='raise', run=sync_run_Exception, argspecs=(), provides=('sync',)) ) self.cb_error = Callback() self.cb_success = Callback()
def setUp(self): self.info_handler = Callback() self.error_handler = Callback() self.cmdmgr = CommandManager(loop=self.loop, info_handler=self.info_handler, error_handler=self.error_handler) def run_sync(self_, A, B): assert isinstance(self_, _CommandBase) self_.info(round(int(A) / int(B))) async def run_async(self_, A, B): assert isinstance(self_, _CommandBase) await asyncio.sleep(0, loop=self_.loop) self_.info(round(int(A) / int(B))) argspecs = ({'names': ('A',), 'type': int, 'description': 'First number'}, {'names': ('B',), 'type': int, 'description': 'Second number'}) self.cmdmgr.register( make_cmdcls(name='div', run=run_sync, argspecs=argspecs, provides=('sync',)) ) self.cmdmgr.register( make_cmdcls(name='div', run=run_async, argspecs=argspecs, provides=('async',)) ) def run_sync_CmdError(self_, msg): raise CmdError(msg) async def run_async_CmdError(self_, msg): await asyncio.sleep(0, loop=self_.loop) raise CmdError(msg) argspecs = ({'names': ('msg',), 'description': 'Error message'},) self.cmdmgr.register( make_cmdcls(name='error', run=run_sync_CmdError, argspecs=argspecs, provides=('sync',)) ) self.cmdmgr.register( make_cmdcls(name='error', run=run_async_CmdError, argspecs=argspecs, provides=('async',)) ) def run_sync_Exception(self_): 1/0 async def run_async_Exception(self_): await asyncio.sleep(0, loop=self_.loop) 1/0 self.cmdmgr.register( make_cmdcls(name='raise', run=run_sync_Exception, argspecs=(), provides=('sync',)) ) self.cmdmgr.register( make_cmdcls(name='raise', run=run_async_Exception, argspecs=(), provides=('async',)) )
class Test_guess_ui(unittest.TestCase): def setUp(self): self.cmdmgr = CommandManager() self.cmdmgr.register(make_cmdcls(name='dotui', provides=('tui', ))) self.cmdmgr.register(make_cmdcls(name='docli', provides=('cli', ))) self.cmdmgr.register( make_cmdcls(name='doboth', provides=('cli', 'tui'))) self.cmdmgr.register( make_cmdcls(name='dotorrent', provides=('cli', 'tui'), category='torrent')) def guess_ui(self, cmds): return guess_ui(cmds, self.cmdmgr) def test_guess_no_commands(self): self.assertEqual(self.guess_ui([]), 'tui') def test_guess_exclusive_tui_command(self): self.assertEqual(self.guess_ui([['dotui']]), 'tui') def test_guess_exclusive_cli_command(self): self.assertEqual(self.guess_ui([['docli']]), 'cli') def test_guess_universal_command(self): self.assertEqual(self.guess_ui([['doboth']]), 'cli') def test_guess_cli_and_tui_command(self): with self.assertRaises(UIGuessError): self.guess_ui([['dotui'], ['docli']]) def test_guess_torrent_command(self): self.assertEqual(self.guess_ui([['dotorrent']]), 'cli') self.assertEqual(self.guess_ui([['dotui'], ['dotorrent']]), 'tui') self.assertEqual(self.guess_ui([['docli'], ['dotorrent']]), 'cli')
class TestCommandManagerManagement(unittest.TestCase): def setUp(self): # It's ok to use the default loop here because we're not using it in # this test class. self.cmdmgr = CommandManager(loop=asyncio.get_event_loop()) self.cmd_foo = make_cmdcls(name='foo', provides=('cli',)) self.cmd_bar = make_cmdcls(name='bar', provides=('tui',)) self.cmd_baz = make_cmdcls(name='baz', provides=('cli', 'tui')) for c in (self.cmd_foo, self.cmd_bar, self.cmd_baz): self.cmdmgr.register(c) def test_metaclass_check(self): class InvalidCommandClass(): pass with self.assertRaises(RuntimeError) as cm: self.cmdmgr.register(InvalidCommandClass) self.assertIn('metaclass', str(cm.exception)) self.assertIn('InitCommand', str(cm.exception)) def test_active_interface(self): self.assertEqual(self.cmdmgr.active_interface, None) self.cmdmgr.active_interface = 'tui' self.assertEqual(self.cmdmgr.active_interface, 'tui') self.cmdmgr.active_interface = 'cli' self.assertEqual(self.cmdmgr.active_interface, 'cli') with self.assertRaises(ValueError) as cm: self.cmdmgr.active_interface = 'fooi' self.assertIn('fooi', str(cm.exception)) def test_all_commands(self): self.assertEqual(set(self.cmdmgr.all_commands), set((self.cmd_foo, self.cmd_bar, self.cmd_baz))) def test_active_commands(self): self.cmdmgr.active_interface = 'cli' self.assertEqual(set(self.cmdmgr.active_commands), set((self.cmd_foo, self.cmd_baz))) self.cmdmgr.active_interface = 'tui' self.assertEqual(set(self.cmdmgr.active_commands), set((self.cmd_bar, self.cmd_baz))) def test_get_cmdcls(self): self.cmdmgr.active_interface = 'cli' self.assertEqual(self.cmdmgr.get_cmdcls('bar', interface='ACTIVE', exclusive=False), None) # bar is 'tui' command self.assertEqual(self.cmdmgr.get_cmdcls('bar', interface='ANY', exclusive=False), self.cmd_bar) self.assertEqual(self.cmdmgr.get_cmdcls('baz', interface='cli', exclusive=True), None) # baz supports cli but not exclusively # Add another 'foo' command for 'tui' (the first one for 'cli' is added in setUp) cmd_foo_tui = make_cmdcls(name='foo', provides=('tui',)) self.cmdmgr.register(cmd_foo_tui) self.assertEqual(self.cmdmgr.get_cmdcls('foo', interface='ACTIVE', exclusive=False), self.cmd_foo) self.cmdmgr.active_interface = 'tui' self.assertEqual(self.cmdmgr.get_cmdcls('foo', interface='ACTIVE', exclusive=False), cmd_foo_tui) # Both commands support their interfaces exclusively self.assertEqual(self.cmdmgr.get_cmdcls('foo', interface='cli', exclusive=True), self.cmd_foo) self.assertEqual(self.cmdmgr.get_cmdcls('foo', interface='tui', exclusive=True), cmd_foo_tui) with self.assertRaises(ValueError) as cm: self.cmdmgr.get_cmdcls('foo', interface='brain') self.assertIn('brain', str(cm.exception)) self.assertIn('interface', str(cm.exception).lower())
class TestCommandManagerCallsBase(asynctest.ClockedTestCase): def setUp(self): self.info_handler = Callback() self.error_handler = Callback() self.cmdmgr = CommandManager(info_handler=self.info_handler, error_handler=self.error_handler) def run_sync(self_, A, B): assert isinstance(self_, _CommandBase) self_.info(round(int(A) / int(B))) async def run_async(self_, A, B): assert isinstance(self_, _CommandBase) await asyncio.sleep(0) self_.info(round(int(A) / int(B))) argspecs = ({'names': ('A',), 'type': int, 'description': 'First number'}, {'names': ('B',), 'type': int, 'description': 'Second number'}) self.cmdmgr.register( make_cmdcls(name='div', run=run_sync, argspecs=argspecs, provides=('sync',)) ) self.cmdmgr.register( make_cmdcls(name='div', run=run_async, argspecs=argspecs, provides=('async',)) ) def run_sync_CmdError(self_, msg): raise CmdError(msg) async def run_async_CmdError(self_, msg): await asyncio.sleep(0) raise CmdError(msg) argspecs = ({'names': ('msg',), 'description': 'Error message'},) self.cmdmgr.register( make_cmdcls(name='error', run=run_sync_CmdError, argspecs=argspecs, provides=('sync',)) ) self.cmdmgr.register( make_cmdcls(name='error', run=run_async_CmdError, argspecs=argspecs, provides=('async',)) ) def run_sync_Exception(self_): 1 / 0 async def run_async_Exception(self_): await asyncio.sleep(0) 1 / 0 self.cmdmgr.register( make_cmdcls(name='raise', run=run_sync_Exception, argspecs=(), provides=('sync',)) ) self.cmdmgr.register( make_cmdcls(name='raise', run=run_async_Exception, argspecs=(), provides=('async',)) ) async def check(self, result, success, infos=(), errors=()): self.assertEqual(result, success) self.assertEqual(tuple(self.info_handler.args), infos) self.assertEqual(tuple(self.error_handler.args), errors)