public async Task RendersHelpForAllCommandInCommandContainerTest()
        {
            string expectedOutput = @":information_source: __**Here are all supported commands:**__ :information_source:

**!help**   - Prints usage for a command
**!ping**   - Execute this to get a super special response
";

            ICommandContainer container = new CommandContainer();

            container.AddCommandHandler <PingCommand>();
            container.AddCommandHandler <HelpCommand>();

            string respone = null;

            Mock <IMessage>        socketMessageMock  = new Mock <IMessage>();
            Mock <IMessageChannel> messageChannelMock = new Mock <IMessageChannel>();

            messageChannelMock
            .Setup(c => c.SendMessageAsync(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <Embed>(), It.IsAny <RequestOptions>()))
            .Callback <string, bool, Embed, RequestOptions>((msg, tts, embed, reqOptions) => respone = msg)
            .Returns(() => Task.FromResult <IUserMessage>(default(SocketUserMessage)));
            socketMessageMock
            .Setup(x => x.Channel)
            .Returns(messageChannelMock.Object);

            HelpCommand helpCommand = new HelpCommand(container);

            //Act
            await helpCommand.Execute(socketMessageMock.Object);

            //Assert
            messageChannelMock.Verify(c => c.SendMessageAsync(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <Embed>(), It.IsAny <RequestOptions>()), Times.Once());
            Assert.Equal(expectedOutput, respone);
        }
        [InlineData(typeof(String), false)]                     //Here everything is missing
        public void OnlyAllowValidTypesToBeAddedToCommandContainerTest(Type typeToAdd, bool isValidType)
        {
            CommandContainer container = new CommandContainer();

            if (isValidType)
            {
                container.AddCommandHandler(typeToAdd);
                Assert.Single(container);   //Verify command was added
            }
            else
            {
                Assert.Throws <Exception>(() => container.AddCommandHandler(typeToAdd));
                Assert.Empty(container);    //Verify type was not added
            }
        }
Exemplo n.º 3
0
        private static ICommandContainer GetPingCommandContainer()
        {
            CommandContainer container = new CommandContainer();

            container.AddCommandHandler <PingCommand>();
            return(container);
        }
        public async Task ShowErrorIfHelpForUnknownCommandIsRequested()
        {
            string expectedOutput = "\"foo\" is an unknown command. Type !help to list all available commands!";

            ICommandContainer container = new CommandContainer();

            container.AddCommandHandler <PingCommand>();

            string respone = null;

            //TODO Write a helper method to create this mock, because we use the same code in a lot of tests to create this basic mock
            Mock <IMessage>        socketMessageMock  = new Mock <IMessage>();
            Mock <IMessageChannel> messageChannelMock = new Mock <IMessageChannel>();

            messageChannelMock
            .Setup(c => c.SendMessageAsync(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <Embed>(), It.IsAny <RequestOptions>()))
            .Callback <string, bool, Embed, RequestOptions>((msg, tts, embed, reqOptions) => respone = msg)
            .Returns(() => Task.FromResult <IUserMessage>(default(SocketUserMessage)));
            socketMessageMock
            .Setup(x => x.Channel)
            .Returns(messageChannelMock.Object);

            HelpCommand helpCommand = new HelpCommand(container);

            helpCommand.Command = "foo";

            //Act
            await helpCommand.Execute(socketMessageMock.Object);

            //Assert
            messageChannelMock.Verify(c => c.SendMessageAsync(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <Embed>(), It.IsAny <RequestOptions>()), Times.Once());
            Assert.Equal(expectedOutput, respone);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Override this method to define a custom <see cref="ICommandContainer"/>
        /// </summary>
        /// <returns></returns>
        protected virtual ICommandContainer BuildCommandContainer()
        {
            CommandContainer container = new CommandContainer();

            List <Type> discovered = DiscoverCommands().ToList();

            _logger.Info("Discovered {0} command(s)", discovered.Count);

            foreach (Type type in discovered)
            {
                container.AddCommandHandler(type);
            }

            return(container);
        }
        public void GetCommandTypeForMessageInputTest(IEnumerable <Type> commands, string msgCommand, Type expectedCommandHandler)
        {
            CommandContainer container = new CommandContainer();

            foreach (Type command in commands)
            {
                container.AddCommandHandler(command);
            }

            if (container.TryGetCommandType(msgCommand, out Type commandHandler))
            {
                Assert.Equal(expectedCommandHandler, commandHandler);
            }
            else
            {
                Assert.Null(expectedCommandHandler);
            }
        }