def test_execute_impermissible_command():
    subject = CommandDispatcher()
    subject.register(literal("foo").requires(lambda _: False))
    
    try:
        subject.execute("foo", {})
    except CommandSyntaxException as error:
        assert error.get_type() == BuiltInExceptions.dispatcher_unknown_command()
        assert error.get_cursor() == 0
def test_execute_empty_command():
    subject = CommandDispatcher()
    subject.register(literal(""))
        
    try:
        subject.execute("", {})
    except CommandSyntaxException as error:
        assert error.get_type() == BuiltInExceptions.dispatcher_unknown_command()
        assert error.get_cursor() == 0
    def execute_preparsed(self, parse):

        if parse.get_reader().can_read():
            if len(parse.get_exceptions()) == 1:
                raise list(parse.get_exceptions().values())[0]
            elif not parse.get_context().get_range():
                raise BuiltInExceptions.dispatcher_unknown_command(
                ).create_with_context(parse.get_reader())
            else:
                raise BuiltInExceptions.dispatcher_unknown_argument(
                ).create_with_context(parse.get_reader())

        result = 0
        successful_forks = 0
        forked = False
        found_command = False
        command = parse.get_reader().get_string()
        original = parse.get_context().build(command)
        contexts = [original]
        _next = None

        while contexts is not None:
            for context in contexts:
                child = context.get_child()
                if child is not None:
                    forked |= context.is_forked()
                    if child.has_nodes():
                        found_command = True
                        modifier = context.get_redirect_modifier()
                        if modifier is None:
                            if _next is None:
                                _next = []
                            _next.append(child.copy_for(context.get_source()))
                        else:
                            try:
                                results = modifier(context)
                                if results:
                                    if _next is None:
                                        _next = []
                                    for source in results:
                                        _next.append(child.copy_for(source))
                            except CommandSyntaxException as e:
                                self.consumer.on_command_complete(
                                    context, False, 0)
                                if not forked:
                                    raise e
                elif context.get_command() is not None:
                    found_command = True
                    try:
                        value = context.get_command()(context)
                        result += value if value else 1
                        self.consumer.on_command_complete(context, True, value)
                        successful_forks += 1
                    except CommandSyntaxException as e:
                        self.consumer.on_command_complete(context, False, 0)
                        if not forked:
                            raise e

            contexts = _next
            _next = None

        if not found_command:
            self.consumer.on_command_complete(original, False, 0)
            raise BuiltInExceptions.dispatcher_unknown_command(
            ).create_with_context(parse.get_reader())

        return successful_forks if forked else result