You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandParserTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.commandparser.parsers;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.FrameContainer;
  26. import com.dmdirc.commandparser.CommandInfo;
  27. import com.dmdirc.commandparser.commands.Command;
  28. import com.dmdirc.harness.TestCommandParser;
  29. import com.dmdirc.interfaces.CommandController;
  30. import com.dmdirc.interfaces.Connection;
  31. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  32. import java.util.Optional;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import org.junit.runner.RunWith;
  36. import org.mockito.Mock;
  37. import org.mockito.runners.MockitoJUnitRunner;
  38. import static org.junit.Assert.assertEquals;
  39. import static org.junit.Assert.assertFalse;
  40. import static org.junit.Assert.assertNull;
  41. import static org.junit.Assert.assertSame;
  42. import static org.junit.Assert.assertTrue;
  43. import static org.mockito.Mockito.when;
  44. @RunWith(MockitoJUnitRunner.class)
  45. public class CommandParserTest {
  46. @Mock private AggregateConfigProvider configProvider;
  47. @Mock private CommandController commandController;
  48. @Mock private CommandInfo commandInfo;
  49. @Mock private CommandInfo channelCommandInfo;
  50. @Mock private Command command;
  51. @Mock private Command channelCommand;
  52. @Mock private FrameContainer container;
  53. @Mock private Channel channel;
  54. @Mock private Connection connection;
  55. @Mock private DMDircMBassador eventBus;
  56. private TestCommandParser commandParser;
  57. private TestCommandParser channelCommandParser;
  58. @Before
  59. public void setup() {
  60. when(commandController.getCommandChar()).thenReturn('/');
  61. when(commandController.getSilenceChar()).thenReturn('.');
  62. when(commandController.isChannelCommand("channel")).thenReturn(true);
  63. when(commandInfo.getName()).thenReturn("command");
  64. when(channelCommandInfo.getName()).thenReturn("channel");
  65. when(configProvider.getOptionInt("general", "commandhistory")).thenReturn(10);
  66. when(container.getConnection()).thenReturn(Optional.of(connection));
  67. when(connection.isValidChannelName("#channel1")).thenReturn(true);
  68. when(connection.isValidChannelName("#channel2")).thenReturn(true);
  69. when(connection.getChannel("#channel1")).thenReturn(Optional.of(channel));
  70. commandParser = new TestCommandParser(configProvider, commandController, eventBus);
  71. commandParser.registerCommand(command, commandInfo);
  72. commandParser.registerCommand(channelCommand, channelCommandInfo);
  73. channelCommandParser = new TestCommandParser(configProvider, commandController, eventBus);
  74. channelCommandParser.registerCommand(channelCommand, channelCommandInfo);
  75. when(channel.getWindowModel()).thenReturn(channel);
  76. when(channel.getCommandParser()).thenReturn(channelCommandParser);
  77. }
  78. @Test
  79. public void testParseCommandWithArguments() {
  80. commandParser.parseCommand(container, "/command this is a test");
  81. assertNull(commandParser.nonCommandLine);
  82. assertNull(commandParser.invalidCommand);
  83. assertFalse(commandParser.wasSilent);
  84. assertSame(command, commandParser.executedCommand);
  85. assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
  86. }
  87. @Test
  88. public void testParseCommandWithoutArguments() {
  89. commandParser.parseCommand(container, "/command");
  90. assertNull(commandParser.nonCommandLine);
  91. assertNull(commandParser.invalidCommand);
  92. assertFalse(commandParser.wasSilent);
  93. assertSame(command, commandParser.executedCommand);
  94. assertEquals("", commandParser.commandArgs.getArgumentsAsString());
  95. }
  96. @Test
  97. public void testParseSilentCommandWithoutArguments() {
  98. commandParser.parseCommand(container, "/.command");
  99. assertNull(commandParser.nonCommandLine);
  100. assertNull(commandParser.invalidCommand);
  101. assertTrue(commandParser.wasSilent);
  102. assertSame(command, commandParser.executedCommand);
  103. assertEquals("", commandParser.commandArgs.getArgumentsAsString());
  104. }
  105. @Test
  106. public void testParseSilentCommandWithArguments() {
  107. commandParser.parseCommand(container, "/.command this is a test");
  108. assertNull(commandParser.nonCommandLine);
  109. assertNull(commandParser.invalidCommand);
  110. assertTrue(commandParser.wasSilent);
  111. assertSame(command, commandParser.executedCommand);
  112. assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
  113. }
  114. @Test
  115. public void testParseUnknownCommand() {
  116. commandParser.parseCommand(container, "/foobar moo bar");
  117. assertNull(commandParser.nonCommandLine);
  118. assertEquals("foobar", commandParser.invalidCommand);
  119. assertNull(commandParser.executedCommand);
  120. assertFalse(commandParser.wasSilent);
  121. }
  122. @Test
  123. public void testParseEmptyCommand() {
  124. commandParser.parseCommand(container, "/ moo bar");
  125. assertNull(commandParser.nonCommandLine);
  126. assertEquals("", commandParser.invalidCommand);
  127. assertNull(commandParser.executedCommand);
  128. assertFalse(commandParser.wasSilent);
  129. }
  130. @Test
  131. public void testParseEmptySilenceCommand() {
  132. commandParser.parseCommand(container, "/. moo bar");
  133. assertNull(commandParser.nonCommandLine);
  134. assertEquals("", commandParser.invalidCommand);
  135. assertNull(commandParser.executedCommand);
  136. assertFalse(commandParser.wasSilent);
  137. }
  138. @Test
  139. public void testParseNonCommand() {
  140. commandParser.parseCommand(container, "Foobar baz");
  141. assertEquals("Foobar baz", commandParser.nonCommandLine);
  142. assertNull(commandParser.invalidCommand);
  143. assertNull(commandParser.executedCommand);
  144. assertFalse(commandParser.wasSilent);
  145. }
  146. @Test
  147. public void testGetCommandTime() {
  148. commandParser.parseCommand(container, "/command this is a test");
  149. final long time1 = commandParser.getCommandTime("command this is a test");
  150. assertTrue(time1 > 0);
  151. commandParser.parseCommand(container, "/command this is a test");
  152. final long time2 = commandParser.getCommandTime("command this is a test");
  153. assertTrue(time2 > 0);
  154. assertTrue(time2 >= time1);
  155. assertEquals(0L, commandParser.getCommandTime("command"));
  156. }
  157. @Test
  158. public void testParseChannelCommandWithArguments() {
  159. when(container.getConnection()).thenReturn(Optional.of(connection));
  160. commandParser.parseCommand(container, "/channel #channel1 this is a test");
  161. assertNull(channelCommandParser.nonCommandLine);
  162. assertNull(channelCommandParser.invalidCommand);
  163. assertFalse(channelCommandParser.wasSilent);
  164. assertSame(channelCommand, channelCommandParser.executedCommand);
  165. assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
  166. }
  167. @Test
  168. public void testParseChannelCommandWithoutArguments() {
  169. when(container.getConnection()).thenReturn(Optional.of(connection));
  170. commandParser.parseCommand(container, "/channel #channel1");
  171. assertNull(channelCommandParser.nonCommandLine);
  172. assertNull(channelCommandParser.invalidCommand);
  173. assertFalse(channelCommandParser.wasSilent);
  174. assertSame(channelCommand, channelCommandParser.executedCommand);
  175. assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
  176. }
  177. @Test
  178. public void testParseSilencedChannelCommandWithArguments() {
  179. when(container.getConnection()).thenReturn(Optional.of(connection));
  180. commandParser.parseCommand(container, "/.channel #channel1 this is a test");
  181. assertNull(channelCommandParser.nonCommandLine);
  182. assertNull(channelCommandParser.invalidCommand);
  183. assertTrue(channelCommandParser.wasSilent);
  184. assertSame(channelCommand, channelCommandParser.executedCommand);
  185. assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
  186. }
  187. @Test
  188. public void testParseSilencedChannelCommandWithoutArguments() {
  189. when(container.getConnection()).thenReturn(Optional.of(connection));
  190. commandParser.parseCommand(container, "/.channel #channel1");
  191. assertNull(channelCommandParser.nonCommandLine);
  192. assertNull(channelCommandParser.invalidCommand);
  193. assertTrue(channelCommandParser.wasSilent);
  194. assertSame(channelCommand, channelCommandParser.executedCommand);
  195. assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
  196. }
  197. @Test
  198. public void testParseUnregisterCommand() {
  199. commandParser.unregisterCommand(commandInfo);
  200. commandParser.parseCommand(container, "/command test 123");
  201. assertNull(commandParser.nonCommandLine);
  202. assertEquals("command", commandParser.invalidCommand);
  203. assertNull(commandParser.executedCommand);
  204. assertFalse(commandParser.wasSilent);
  205. }
  206. @Test
  207. public void testGetCommands() {
  208. assertEquals(2, commandParser.getCommands().size());
  209. assertTrue(commandParser.getCommands().containsKey("command"));
  210. assertTrue(commandParser.getCommands().containsKey("channel"));
  211. commandParser.unregisterCommand(commandInfo);
  212. assertEquals(1, commandParser.getCommands().size());
  213. assertFalse(commandParser.getCommands().containsKey("command"));
  214. assertTrue(commandParser.getCommands().containsKey("channel"));
  215. }
  216. }