123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.commandparser.parsers;
-
- import com.dmdirc.Channel;
- import com.dmdirc.FrameContainer;
- import com.dmdirc.commandparser.CommandInfo;
- import com.dmdirc.commandparser.commands.Command;
- import com.dmdirc.harness.TestCommandParser;
- import com.dmdirc.interfaces.CommandController;
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.interfaces.config.AggregateConfigProvider;
-
- import com.dmdirc.DMDircMBassador;
-
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.mockito.Mock;
- import org.mockito.runners.MockitoJUnitRunner;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertNull;
- import static org.junit.Assert.assertSame;
- import static org.junit.Assert.assertTrue;
- import static org.mockito.Mockito.when;
-
- @RunWith(MockitoJUnitRunner.class)
- public class CommandParserTest {
-
- @Mock private AggregateConfigProvider configProvider;
- @Mock private CommandController commandController;
- @Mock private CommandInfo commandInfo;
- @Mock private CommandInfo channelCommandInfo;
- @Mock private Command command;
- @Mock private Command channelCommand;
- @Mock private FrameContainer container;
- @Mock private Channel channel;
- @Mock private Connection connection;
- @Mock private DMDircMBassador eventBus;
- private TestCommandParser commandParser;
- private TestCommandParser channelCommandParser;
-
- @Before
- public void setup() {
- when(commandController.getCommandChar()).thenReturn('/');
- when(commandController.getSilenceChar()).thenReturn('.');
- when(commandController.isChannelCommand("channel")).thenReturn(true);
-
- when(commandInfo.getName()).thenReturn("command");
- when(channelCommandInfo.getName()).thenReturn("channel");
-
- when(configProvider.getOptionInt("general", "commandhistory")).thenReturn(10);
-
- when(container.getConnection()).thenReturn(connection);
- when(connection.isValidChannelName("#channel1")).thenReturn(true);
- when(connection.isValidChannelName("#channel2")).thenReturn(true);
- when(connection.hasChannel("#channel1")).thenReturn(true);
- when(connection.getChannel("#channel1")).thenReturn(channel);
-
- commandParser = new TestCommandParser(configProvider, commandController, eventBus);
- commandParser.registerCommand(command, commandInfo);
- commandParser.registerCommand(channelCommand, channelCommandInfo);
-
- channelCommandParser = new TestCommandParser(configProvider, commandController, eventBus);
- channelCommandParser.registerCommand(channelCommand, channelCommandInfo);
-
- when(channel.getCommandParser()).thenReturn(channelCommandParser);
- }
-
- @Test
- public void testParseCommandWithArguments() {
- commandParser.parseCommand(container, "/command this is a test");
-
- assertNull(commandParser.nonCommandLine);
- assertNull(commandParser.invalidCommand);
- assertFalse(commandParser.wasSilent);
- assertSame(command, commandParser.executedCommand);
- assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
- }
-
- @Test
- public void testParseCommandWithoutArguments() {
- commandParser.parseCommand(container, "/command");
-
- assertNull(commandParser.nonCommandLine);
- assertNull(commandParser.invalidCommand);
- assertFalse(commandParser.wasSilent);
- assertSame(command, commandParser.executedCommand);
- assertEquals("", commandParser.commandArgs.getArgumentsAsString());
- }
-
- @Test
- public void testParseSilentCommandWithoutArguments() {
- commandParser.parseCommand(container, "/.command");
-
- assertNull(commandParser.nonCommandLine);
- assertNull(commandParser.invalidCommand);
- assertTrue(commandParser.wasSilent);
- assertSame(command, commandParser.executedCommand);
- assertEquals("", commandParser.commandArgs.getArgumentsAsString());
- }
-
- @Test
- public void testParseSilentCommandWithArguments() {
- commandParser.parseCommand(container, "/.command this is a test");
-
- assertNull(commandParser.nonCommandLine);
- assertNull(commandParser.invalidCommand);
- assertTrue(commandParser.wasSilent);
- assertSame(command, commandParser.executedCommand);
- assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
- }
-
- @Test
- public void testParseUnknownCommand() {
- commandParser.parseCommand(container, "/foobar moo bar");
-
- assertNull(commandParser.nonCommandLine);
- assertEquals("foobar", commandParser.invalidCommand);
- assertNull(commandParser.executedCommand);
- assertFalse(commandParser.wasSilent);
- }
-
- @Test
- public void testParseEmptyCommand() {
- commandParser.parseCommand(container, "/ moo bar");
-
- assertNull(commandParser.nonCommandLine);
- assertEquals("", commandParser.invalidCommand);
- assertNull(commandParser.executedCommand);
- assertFalse(commandParser.wasSilent);
- }
-
- @Test
- public void testParseEmptySilenceCommand() {
- commandParser.parseCommand(container, "/. moo bar");
-
- assertNull(commandParser.nonCommandLine);
- assertEquals("", commandParser.invalidCommand);
- assertNull(commandParser.executedCommand);
- assertFalse(commandParser.wasSilent);
- }
-
- @Test
- public void testParseNonCommand() {
- commandParser.parseCommand(container, "Foobar baz");
-
- assertEquals("Foobar baz", commandParser.nonCommandLine);
- assertNull(commandParser.invalidCommand);
- assertNull(commandParser.executedCommand);
- assertFalse(commandParser.wasSilent);
- }
-
- @Test
- public void testGetCommandTime() {
- commandParser.parseCommand(container, "/command this is a test");
-
- final long time1 = commandParser.getCommandTime("command this is a test");
- assertTrue(time1 > 0);
-
- commandParser.parseCommand(container, "/command this is a test");
- final long time2 = commandParser.getCommandTime("command this is a test");
- assertTrue(time2 > 0);
- assertTrue(time2 >= time1);
-
- assertEquals(0L, commandParser.getCommandTime("command"));
- }
-
- @Test
- public void testParseChannelCommandWithArguments() {
- when(container.getConnection()).thenReturn(connection);
- commandParser.parseCommand(container, "/channel #channel1 this is a test");
-
- assertNull(channelCommandParser.nonCommandLine);
- assertNull(channelCommandParser.invalidCommand);
- assertFalse(channelCommandParser.wasSilent);
- assertSame(channelCommand, channelCommandParser.executedCommand);
- assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
- }
-
- @Test
- public void testParseChannelCommandWithoutArguments() {
- when(container.getConnection()).thenReturn(connection);
- commandParser.parseCommand(container, "/channel #channel1");
-
- assertNull(channelCommandParser.nonCommandLine);
- assertNull(channelCommandParser.invalidCommand);
- assertFalse(channelCommandParser.wasSilent);
- assertSame(channelCommand, channelCommandParser.executedCommand);
- assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
- }
-
- @Test
- public void testParseSilencedChannelCommandWithArguments() {
- when(container.getConnection()).thenReturn(connection);
- commandParser.parseCommand(container, "/.channel #channel1 this is a test");
-
- assertNull(channelCommandParser.nonCommandLine);
- assertNull(channelCommandParser.invalidCommand);
- assertTrue(channelCommandParser.wasSilent);
- assertSame(channelCommand, channelCommandParser.executedCommand);
- assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
- }
-
- @Test
- public void testParseSilencedChannelCommandWithoutArguments() {
- when(container.getConnection()).thenReturn(connection);
- commandParser.parseCommand(container, "/.channel #channel1");
-
- assertNull(channelCommandParser.nonCommandLine);
- assertNull(channelCommandParser.invalidCommand);
- assertTrue(channelCommandParser.wasSilent);
- assertSame(channelCommand, channelCommandParser.executedCommand);
- assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
- }
-
- @Test
- public void testParseUnregisterCommand() {
- commandParser.unregisterCommand(commandInfo);
- commandParser.parseCommand(container, "/command test 123");
-
- assertNull(commandParser.nonCommandLine);
- assertEquals("command", commandParser.invalidCommand);
- assertNull(commandParser.executedCommand);
- assertFalse(commandParser.wasSilent);
- }
-
- @Test
- public void testGetCommands() {
- assertEquals(2, commandParser.getCommands().size());
- assertTrue(commandParser.getCommands().containsKey("command"));
- assertTrue(commandParser.getCommands().containsKey("channel"));
-
- commandParser.unregisterCommand(commandInfo);
- assertEquals(1, commandParser.getCommands().size());
- assertFalse(commandParser.getCommands().containsKey("command"));
- assertTrue(commandParser.getCommands().containsKey("channel"));
- }
-
- }
|