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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 2006-2015 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.commandparser.CommandInfo;
  25. import com.dmdirc.commandparser.commands.Command;
  26. import com.dmdirc.harness.TestCommandParser;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.events.eventbus.EventBus;
  30. import com.dmdirc.interfaces.GroupChatManager;
  31. import com.dmdirc.interfaces.InputModel;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import com.dmdirc.config.provider.AggregateConfigProvider;
  34. import java.util.Optional;
  35. import org.junit.Before;
  36. import org.junit.Test;
  37. import org.junit.runner.RunWith;
  38. import org.mockito.Mock;
  39. import org.mockito.runners.MockitoJUnitRunner;
  40. import static org.junit.Assert.assertEquals;
  41. import static org.junit.Assert.assertFalse;
  42. import static org.junit.Assert.assertNull;
  43. import static org.junit.Assert.assertSame;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.mockito.Mockito.when;
  46. @RunWith(MockitoJUnitRunner.class)
  47. public class CommandParserTest {
  48. @Mock private AggregateConfigProvider configProvider;
  49. @Mock private CommandController commandController;
  50. @Mock private CommandInfo commandInfo;
  51. @Mock private CommandInfo channelCommandInfo;
  52. @Mock private Command command;
  53. @Mock private Command channelCommand;
  54. @Mock private WindowModel container;
  55. @Mock private InputModel inputModel;
  56. @Mock private Channel channel;
  57. @Mock private Connection connection;
  58. @Mock private GroupChatManager groupChatManager;
  59. @Mock private EventBus eventBus;
  60. private TestCommandParser commandParser;
  61. private TestCommandParser channelCommandParser;
  62. @Before
  63. public void setup() {
  64. when(commandController.getCommandChar()).thenReturn('/');
  65. when(commandController.getSilenceChar()).thenReturn('.');
  66. when(commandController.isChannelCommand("channel")).thenReturn(true);
  67. when(commandInfo.getName()).thenReturn("command");
  68. when(channelCommandInfo.getName()).thenReturn("channel");
  69. when(configProvider.getOptionInt("general", "commandhistory")).thenReturn(10);
  70. when(container.getConnection()).thenReturn(Optional.of(connection));
  71. when(connection.getGroupChatManager()).thenReturn(groupChatManager);
  72. when(groupChatManager.isValidChannelName("#channel1")).thenReturn(true);
  73. when(groupChatManager.getChannel("#channel1")).thenReturn(Optional.of(channel));
  74. commandParser = new TestCommandParser(configProvider, commandController, eventBus);
  75. commandParser.registerCommand(command, commandInfo);
  76. commandParser.registerCommand(channelCommand, channelCommandInfo);
  77. channelCommandParser = new TestCommandParser(configProvider, commandController, eventBus);
  78. channelCommandParser.registerCommand(channelCommand, channelCommandInfo);
  79. when(channel.getWindowModel()).thenReturn(channel);
  80. when(channel.getInputModel()).thenReturn(Optional.of(inputModel));
  81. when(inputModel.getCommandParser()).thenReturn(channelCommandParser);
  82. }
  83. @Test
  84. public void testParseCommandWithArguments() {
  85. commandParser.parseCommand(container, "/command this is a test");
  86. assertNull(commandParser.nonCommandLine);
  87. assertNull(commandParser.invalidCommand);
  88. assertFalse(commandParser.wasSilent);
  89. assertSame(command, commandParser.executedCommand);
  90. assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
  91. }
  92. @Test
  93. public void testParseCommandWithoutArguments() {
  94. commandParser.parseCommand(container, "/command");
  95. assertNull(commandParser.nonCommandLine);
  96. assertNull(commandParser.invalidCommand);
  97. assertFalse(commandParser.wasSilent);
  98. assertSame(command, commandParser.executedCommand);
  99. assertEquals("", commandParser.commandArgs.getArgumentsAsString());
  100. }
  101. @Test
  102. public void testParseSilentCommandWithoutArguments() {
  103. commandParser.parseCommand(container, "/.command");
  104. assertNull(commandParser.nonCommandLine);
  105. assertNull(commandParser.invalidCommand);
  106. assertTrue(commandParser.wasSilent);
  107. assertSame(command, commandParser.executedCommand);
  108. assertEquals("", commandParser.commandArgs.getArgumentsAsString());
  109. }
  110. @Test
  111. public void testParseSilentCommandWithArguments() {
  112. commandParser.parseCommand(container, "/.command this is a test");
  113. assertNull(commandParser.nonCommandLine);
  114. assertNull(commandParser.invalidCommand);
  115. assertTrue(commandParser.wasSilent);
  116. assertSame(command, commandParser.executedCommand);
  117. assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
  118. }
  119. @Test
  120. public void testParseUnknownCommand() {
  121. commandParser.parseCommand(container, "/foobar moo bar");
  122. assertNull(commandParser.nonCommandLine);
  123. assertEquals("foobar", commandParser.invalidCommand);
  124. assertNull(commandParser.executedCommand);
  125. assertFalse(commandParser.wasSilent);
  126. }
  127. @Test
  128. public void testParseEmptyCommand() {
  129. commandParser.parseCommand(container, "/ moo bar");
  130. assertNull(commandParser.nonCommandLine);
  131. assertEquals("", commandParser.invalidCommand);
  132. assertNull(commandParser.executedCommand);
  133. assertFalse(commandParser.wasSilent);
  134. }
  135. @Test
  136. public void testParseEmptySilenceCommand() {
  137. commandParser.parseCommand(container, "/. moo bar");
  138. assertNull(commandParser.nonCommandLine);
  139. assertEquals("", commandParser.invalidCommand);
  140. assertNull(commandParser.executedCommand);
  141. assertFalse(commandParser.wasSilent);
  142. }
  143. @Test
  144. public void testParseNonCommand() {
  145. commandParser.parseCommand(container, "Foobar baz");
  146. assertEquals("Foobar baz", commandParser.nonCommandLine);
  147. assertNull(commandParser.invalidCommand);
  148. assertNull(commandParser.executedCommand);
  149. assertFalse(commandParser.wasSilent);
  150. }
  151. @Test
  152. public void testGetCommandTime() {
  153. commandParser.parseCommand(container, "/command this is a test");
  154. final long time1 = commandParser.getCommandTime("command this is a test");
  155. assertTrue(time1 > 0);
  156. commandParser.parseCommand(container, "/command this is a test");
  157. final long time2 = commandParser.getCommandTime("command this is a test");
  158. assertTrue(time2 > 0);
  159. assertTrue(time2 >= time1);
  160. assertEquals(0L, commandParser.getCommandTime("command"));
  161. }
  162. @Test
  163. public void testParseChannelCommandWithArguments() {
  164. when(container.getConnection()).thenReturn(Optional.of(connection));
  165. commandParser.parseCommand(container, "/channel #channel1 this is a test");
  166. assertNull(channelCommandParser.nonCommandLine);
  167. assertNull(channelCommandParser.invalidCommand);
  168. assertFalse(channelCommandParser.wasSilent);
  169. assertSame(channelCommand, channelCommandParser.executedCommand);
  170. assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
  171. }
  172. @Test
  173. public void testParseChannelCommandWithoutArguments() {
  174. when(container.getConnection()).thenReturn(Optional.of(connection));
  175. commandParser.parseCommand(container, "/channel #channel1");
  176. assertNull(channelCommandParser.nonCommandLine);
  177. assertNull(channelCommandParser.invalidCommand);
  178. assertFalse(channelCommandParser.wasSilent);
  179. assertSame(channelCommand, channelCommandParser.executedCommand);
  180. assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
  181. }
  182. @Test
  183. public void testParseSilencedChannelCommandWithArguments() {
  184. when(container.getConnection()).thenReturn(Optional.of(connection));
  185. commandParser.parseCommand(container, "/.channel #channel1 this is a test");
  186. assertNull(channelCommandParser.nonCommandLine);
  187. assertNull(channelCommandParser.invalidCommand);
  188. assertTrue(channelCommandParser.wasSilent);
  189. assertSame(channelCommand, channelCommandParser.executedCommand);
  190. assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
  191. }
  192. @Test
  193. public void testParseSilencedChannelCommandWithoutArguments() {
  194. when(container.getConnection()).thenReturn(Optional.of(connection));
  195. commandParser.parseCommand(container, "/.channel #channel1");
  196. assertNull(channelCommandParser.nonCommandLine);
  197. assertNull(channelCommandParser.invalidCommand);
  198. assertTrue(channelCommandParser.wasSilent);
  199. assertSame(channelCommand, channelCommandParser.executedCommand);
  200. assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
  201. }
  202. @Test
  203. public void testParseUnregisterCommand() {
  204. commandParser.unregisterCommand(commandInfo);
  205. commandParser.parseCommand(container, "/command test 123");
  206. assertNull(commandParser.nonCommandLine);
  207. assertEquals("command", commandParser.invalidCommand);
  208. assertNull(commandParser.executedCommand);
  209. assertFalse(commandParser.wasSilent);
  210. }
  211. @Test
  212. public void testGetCommands() {
  213. assertEquals(2, commandParser.getCommands().size());
  214. assertTrue(commandParser.getCommands().containsKey("command"));
  215. assertTrue(commandParser.getCommands().containsKey("channel"));
  216. commandParser.unregisterCommand(commandInfo);
  217. assertEquals(1, commandParser.getCommands().size());
  218. assertFalse(commandParser.getCommands().containsKey("command"));
  219. assertTrue(commandParser.getCommands().containsKey("channel"));
  220. }
  221. }