選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CommandParserTest.java 11KB

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