コード例 #1
0
 def setup(self):
     self.configuration = DeviceConfiguration.get(sample_configuration, is_file_path=False)
     self.configuration['commands'][3]['delay'] = 0
     self.terminal = MockTerminal()
     self.context_manager = ContextManager()
     base_ctx = DeviceContext('base')
     base_ctx.prompt = 'device#'
     self.context_manager.add(base_ctx)
     self.command_manager = CommandManager(self.configuration)
     self.run = partial(self.command_manager.run, self.terminal, self.context_manager)
コード例 #2
0
class TestCommandManager(object):
    def setup(self):
        self.configuration = DeviceConfiguration.get(sample_configuration, is_file_path=False)
        self.configuration['commands'][3]['delay'] = 0
        self.terminal = MockTerminal()
        self.context_manager = ContextManager()
        base_ctx = DeviceContext('base')
        base_ctx.prompt = 'device#'
        self.context_manager.add(base_ctx)
        self.command_manager = CommandManager(self.configuration)
        self.run = partial(self.command_manager.run, self.terminal, self.context_manager)

    def test_default_initialization(self):
        cm = CommandManager(dict())
        assert_equals(cm._invalid_command, CommandManager._default_invalid_command_message)
        assert_false(cm.callback)
        assert_is_instance(cm._callback_command, type(None))

    def test_run_empty(self):
        line = 'empty'
        self.run(line)
        expected = [self.context_manager.prompt]
        assert_equals(self.terminal.output, expected)

    def test_invalid_command(self):
        line = 'invalid command'
        self.run(line)
        expected = [self.command_manager._invalid_command.format(line),
                    self.context_manager.prompt]
        assert_equals(self.terminal.output, expected)

    def test_run_context_matching(self):
        line = 'command01'
        self.run(line)
        expected = [self.command_manager._invalid_command.format(line),
                    self.context_manager.prompt]
        assert_equals(self.terminal.output, expected)
        self.context_manager.add('ctx01')
        self.terminal.reset()
        self.run(line)
        expected = [self.context_manager.prompt]
        assert_equals(self.terminal.output, expected)

    def test_run_with_output(self):
        line = 'command02'
        self.context_manager.add('ctx02')
        self.run(line)
        expected = ['command02 output',
                    self.context_manager.prompt]
        assert_equals(self.terminal.output, expected)

    def test_run_callback_success(self):
        ctx = self.context_manager.new('success_ctx')
        ctx.prompt = 'success_prompt'
        self.context_manager.add(ctx)
        self.context_manager.add('fullctx')
        self.run('full')
        assert_equals(self.context_manager.current.name, 'fullctx')
        self.run('ok')
        assert_equals(self.context_manager.current.name, 'success_ctx')
        assert_equals(self.terminal.output[-3], 'type ok: ')
        assert_equals(self.terminal.output[-2], 'success')
        assert_equals(self.terminal.output[-1], 'success_prompt')

    def test_run_callback_fail(self):
        self.context_manager.add('fullctx')
        self.run('full')
        assert_equals(self.context_manager.current.name, 'fullctx')
        self.run('no')
        assert_equals(self.context_manager.current.name, 'fail_ctx')
        assert_equals(self.terminal.output[-3], 'type ok: ')
        assert_equals(self.terminal.output[-2], 'fail')
        assert_equals(self.terminal.output[-1], 'fail_prompt')
        self.terminal.reset()
        self.run('prompt-fail-no-action')
        self.run('no')
        assert_equals(self.terminal.output[-2], 'fail')

    def test_run_command_all_contexts(self):
        line = self.configuration['commands'][4]['name']
        invalid_cmd = self.command_manager._default_invalid_command_message.format(line)
        self.context_manager.add('any_ctx')
        self.run(line)
        command = self.command_manager._get(self.context_manager, line)
        assert_equals(command.name, line)
        assert_false(invalid_cmd in ''.join(self.terminal.output))