Beispiel #1
0
def test_raises_exception_if_commands_with_same_name_are_registered():
    class Cmd(Command):
        name = 'cmd'

    with pytest.raises(AttributeError) as exc_info:
        terminal.Terminal(client=Mock(), commands=[Cmd(), Cmd()])

    assert str(exc_info.value) == "Some command names are duplicated: cmd"
Beispiel #2
0
def test_do_not_execute_command_on_parse_error():
    class Cmd(Command):
        name = 'ham'

        def parse_args(self, *args):
            raise ArgParseError("spam")

    cmd = Cmd()
    cmd.execute = Mock()
    term = terminal.Terminal(client=Mock(), commands=[cmd])
    term.stdout = Mock()

    assert term.onecmd(':ham') is False
    assert cmd.execute.called is False
    term.stdout.write.assert_called_once_with('spam\n')
Beispiel #3
0
def test_calls_command():
    class Cmd(Command):
        name = 'ham'

        def parse_args(self, *args):
            return {'first': args[1], 'second': args[2]}

    cmd = Cmd()
    cmd.execute = Mock()
    cmd.execute.return_value = 'return value'
    term = terminal.Terminal(client=Mock(), commands=[cmd])
    term.stdout = Mock()

    assert term.onecmd(':ham spam egg') is False
    cmd.execute.assert_called_once_with(
        term.client, first='spam', second='egg')
    term.stdout.write.assert_called_once_with('return value')
Beispiel #4
0
def test_parse_empty_line():
    term = terminal.Terminal(client=Mock())
    assert term.parse_line('') == (None, [])
Beispiel #5
0
def test_handles_unknown_commands():
    term = terminal.Terminal(client=Mock())
    term.stdout = Mock()

    assert term.onecmd(':ham') is False
    term.stdout.write.assert_called_once_with("unknown command 'ham'\n")
Beispiel #6
0
def test_empty_command_names_returns_none():
    term = terminal.Terminal(client=Mock())
    assert term.parse_line(': foo') == (None, [])
Beispiel #7
0
def test_parse_line_returns_extracts_command_name():
    term = terminal.Terminal(client=Mock())
    assert term.parse_line(':ham spam') == ('ham', [':ham', 'spam'])