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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.interfaces.EventBus;
  30. import com.dmdirc.interfaces.GroupChatManager;
  31. import com.dmdirc.interfaces.InputModel;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import com.dmdirc.interfaces.config.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.isValidChannelName("#channel2")).thenReturn(true);
  74. when(groupChatManager.getChannel("#channel1")).thenReturn(Optional.of(channel));
  75. commandParser = new TestCommandParser(configProvider, commandController, eventBus);
  76. commandParser.registerCommand(command, commandInfo);
  77. commandParser.registerCommand(channelCommand, channelCommandInfo);
  78. channelCommandParser = new TestCommandParser(configProvider, commandController, eventBus);
  79. channelCommandParser.registerCommand(channelCommand, channelCommandInfo);
  80. when(channel.getWindowModel()).thenReturn(channel);
  81. when(channel.getInputModel()).thenReturn(Optional.of(inputModel));
  82. when(inputModel.getCommandParser()).thenReturn(channelCommandParser);
  83. }
  84. @Test
  85. public void testParseCommandWithArguments() {
  86. commandParser.parseCommand(container, "/command this is a test");
  87. assertNull(commandParser.nonCommandLine);
  88. assertNull(commandParser.invalidCommand);
  89. assertFalse(commandParser.wasSilent);
  90. assertSame(command, commandParser.executedCommand);
  91. assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
  92. }
  93. @Test
  94. public void testParseCommandWithoutArguments() {
  95. commandParser.parseCommand(container, "/command");
  96. assertNull(commandParser.nonCommandLine);
  97. assertNull(commandParser.invalidCommand);
  98. assertFalse(commandParser.wasSilent);
  99. assertSame(command, commandParser.executedCommand);
  100. assertEquals("", commandParser.commandArgs.getArgumentsAsString());
  101. }
  102. @Test
  103. public void testParseSilentCommandWithoutArguments() {
  104. commandParser.parseCommand(container, "/.command");
  105. assertNull(commandParser.nonCommandLine);
  106. assertNull(commandParser.invalidCommand);
  107. assertTrue(commandParser.wasSilent);
  108. assertSame(command, commandParser.executedCommand);
  109. assertEquals("", commandParser.commandArgs.getArgumentsAsString());
  110. }
  111. @Test
  112. public void testParseSilentCommandWithArguments() {
  113. commandParser.parseCommand(container, "/.command this is a test");
  114. assertNull(commandParser.nonCommandLine);
  115. assertNull(commandParser.invalidCommand);
  116. assertTrue(commandParser.wasSilent);
  117. assertSame(command, commandParser.executedCommand);
  118. assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
  119. }
  120. @Test
  121. public void testParseUnknownCommand() {
  122. commandParser.parseCommand(container, "/foobar moo bar");
  123. assertNull(commandParser.nonCommandLine);
  124. assertEquals("foobar", commandParser.invalidCommand);
  125. assertNull(commandParser.executedCommand);
  126. assertFalse(commandParser.wasSilent);
  127. }
  128. @Test
  129. public void testParseEmptyCommand() {
  130. commandParser.parseCommand(container, "/ moo bar");
  131. assertNull(commandParser.nonCommandLine);
  132. assertEquals("", commandParser.invalidCommand);
  133. assertNull(commandParser.executedCommand);
  134. assertFalse(commandParser.wasSilent);
  135. }
  136. @Test
  137. public void testParseEmptySilenceCommand() {
  138. commandParser.parseCommand(container, "/. moo bar");
  139. assertNull(commandParser.nonCommandLine);
  140. assertEquals("", commandParser.invalidCommand);
  141. assertNull(commandParser.executedCommand);
  142. assertFalse(commandParser.wasSilent);
  143. }
  144. @Test
  145. public void testParseNonCommand() {
  146. commandParser.parseCommand(container, "Foobar baz");
  147. assertEquals("Foobar baz", commandParser.nonCommandLine);
  148. assertNull(commandParser.invalidCommand);
  149. assertNull(commandParser.executedCommand);
  150. assertFalse(commandParser.wasSilent);
  151. }
  152. @Test
  153. public void testGetCommandTime() {
  154. commandParser.parseCommand(container, "/command this is a test");
  155. final long time1 = commandParser.getCommandTime("command this is a test");
  156. assertTrue(time1 > 0);
  157. commandParser.parseCommand(container, "/command this is a test");
  158. final long time2 = commandParser.getCommandTime("command this is a test");
  159. assertTrue(time2 > 0);
  160. assertTrue(time2 >= time1);
  161. assertEquals(0L, commandParser.getCommandTime("command"));
  162. }
  163. @Test
  164. public void testParseChannelCommandWithArguments() {
  165. when(container.getConnection()).thenReturn(Optional.of(connection));
  166. commandParser.parseCommand(container, "/channel #channel1 this is a test");
  167. assertNull(channelCommandParser.nonCommandLine);
  168. assertNull(channelCommandParser.invalidCommand);
  169. assertFalse(channelCommandParser.wasSilent);
  170. assertSame(channelCommand, channelCommandParser.executedCommand);
  171. assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
  172. }
  173. @Test
  174. public void testParseChannelCommandWithoutArguments() {
  175. when(container.getConnection()).thenReturn(Optional.of(connection));
  176. commandParser.parseCommand(container, "/channel #channel1");
  177. assertNull(channelCommandParser.nonCommandLine);
  178. assertNull(channelCommandParser.invalidCommand);
  179. assertFalse(channelCommandParser.wasSilent);
  180. assertSame(channelCommand, channelCommandParser.executedCommand);
  181. assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
  182. }
  183. @Test
  184. public void testParseSilencedChannelCommandWithArguments() {
  185. when(container.getConnection()).thenReturn(Optional.of(connection));
  186. commandParser.parseCommand(container, "/.channel #channel1 this is a test");
  187. assertNull(channelCommandParser.nonCommandLine);
  188. assertNull(channelCommandParser.invalidCommand);
  189. assertTrue(channelCommandParser.wasSilent);
  190. assertSame(channelCommand, channelCommandParser.executedCommand);
  191. assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
  192. }
  193. @Test
  194. public void testParseSilencedChannelCommandWithoutArguments() {
  195. when(container.getConnection()).thenReturn(Optional.of(connection));
  196. commandParser.parseCommand(container, "/.channel #channel1");
  197. assertNull(channelCommandParser.nonCommandLine);
  198. assertNull(channelCommandParser.invalidCommand);
  199. assertTrue(channelCommandParser.wasSilent);
  200. assertSame(channelCommand, channelCommandParser.executedCommand);
  201. assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
  202. }
  203. @Test
  204. public void testParseUnregisterCommand() {
  205. commandParser.unregisterCommand(commandInfo);
  206. commandParser.parseCommand(container, "/command test 123");
  207. assertNull(commandParser.nonCommandLine);
  208. assertEquals("command", commandParser.invalidCommand);
  209. assertNull(commandParser.executedCommand);
  210. assertFalse(commandParser.wasSilent);
  211. }
  212. @Test
  213. public void testGetCommands() {
  214. assertEquals(2, commandParser.getCommands().size());
  215. assertTrue(commandParser.getCommands().containsKey("command"));
  216. assertTrue(commandParser.getCommands().containsKey("channel"));
  217. commandParser.unregisterCommand(commandInfo);
  218. assertEquals(1, commandParser.getCommands().size());
  219. assertFalse(commandParser.getCommands().containsKey("command"));
  220. assertTrue(commandParser.getCommands().containsKey("channel"));
  221. }
  222. }