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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.commandparser.CommandManager;
  24. import com.dmdirc.commandparser.commands.Command;
  25. import com.dmdirc.commandparser.commands.global.Echo;
  26. import com.dmdirc.ui.interfaces.InputWindow;
  27. import java.util.Arrays;
  28. import org.junit.BeforeClass;
  29. import org.junit.Test;
  30. import static org.junit.Assert.*;
  31. public class CommandParserTest {
  32. @BeforeClass
  33. public static void setUpClass() throws Exception {
  34. CommandManager.initCommands();
  35. }
  36. @Test
  37. public void testBasicCommand() {
  38. final TestCommandParser tcp = new TestCommandParser();
  39. tcp.parseCommand(null, "/echo this is a test");
  40. assertNull(tcp.nonCommandLine);
  41. assertNull(tcp.invalidCommand);
  42. assertNotNull(tcp.executedCommand);
  43. assertFalse(tcp.wasSilent);
  44. assertTrue(Arrays.equals(tcp.commandArgs, new String[]{"this", "is", "a", "test"}));
  45. assertTrue(tcp.executedCommand instanceof Echo);
  46. }
  47. @Test
  48. public void testBasicNoArgs() {
  49. final TestCommandParser tcp = new TestCommandParser();
  50. tcp.parseCommand(null, "/echo");
  51. assertNull(tcp.nonCommandLine);
  52. assertNull(tcp.invalidCommand);
  53. assertNotNull(tcp.executedCommand);
  54. assertFalse(tcp.wasSilent);
  55. assertTrue(Arrays.equals(tcp.commandArgs, new String[0]));
  56. assertTrue(tcp.executedCommand instanceof Echo);
  57. }
  58. @Test
  59. public void testSilentNoArgs() {
  60. final TestCommandParser tcp = new TestCommandParser();
  61. tcp.parseCommand(null, "/.echo");
  62. assertNull(tcp.nonCommandLine);
  63. assertNull(tcp.invalidCommand);
  64. assertNotNull(tcp.executedCommand);
  65. assertTrue(tcp.wasSilent);
  66. assertTrue(Arrays.equals(tcp.commandArgs, new String[0]));
  67. assertTrue(tcp.executedCommand instanceof Echo);
  68. }
  69. @Test
  70. public void testSilentCommand() {
  71. final TestCommandParser tcp = new TestCommandParser();
  72. tcp.parseCommand(null, "/.echo this is a test");
  73. assertNull(tcp.nonCommandLine);
  74. assertNull(tcp.invalidCommand);
  75. assertNotNull(tcp.executedCommand);
  76. assertTrue(tcp.wasSilent);
  77. assertTrue(Arrays.equals(tcp.commandArgs, new String[]{"this", "is", "a", "test"}));
  78. assertTrue(tcp.executedCommand instanceof Echo);
  79. }
  80. @Test
  81. public void testNonExistantCommand() {
  82. final TestCommandParser tcp = new TestCommandParser();
  83. tcp.parseCommand(null, "/foobar moo bar");
  84. assertNull(tcp.nonCommandLine);
  85. assertEquals("foobar", tcp.invalidCommand);
  86. assertNotNull(tcp.invalidCommand);
  87. assertNull(tcp.executedCommand);
  88. assertFalse(tcp.wasSilent);
  89. }
  90. @Test
  91. public void testEmptyCommand() {
  92. final TestCommandParser tcp = new TestCommandParser();
  93. tcp.parseCommand(null, "/ moo bar");
  94. assertNull(tcp.nonCommandLine);
  95. assertEquals("", tcp.invalidCommand);
  96. assertNotNull(tcp.invalidCommand);
  97. assertNull(tcp.executedCommand);
  98. assertFalse(tcp.wasSilent);
  99. }
  100. @Test
  101. public void testEmptySilentCommand() {
  102. final TestCommandParser tcp = new TestCommandParser();
  103. tcp.parseCommand(null, "/. moo bar");
  104. assertNull(tcp.nonCommandLine);
  105. assertEquals("", tcp.invalidCommand);
  106. assertNotNull(tcp.invalidCommand);
  107. assertNull(tcp.executedCommand);
  108. assertFalse(tcp.wasSilent);
  109. }
  110. @Test
  111. public void testNonCommand() {
  112. final TestCommandParser tcp = new TestCommandParser();
  113. tcp.parseCommand(null, "Foobar baz");
  114. assertNotNull(tcp.nonCommandLine);
  115. assertEquals("Foobar baz", tcp.nonCommandLine);
  116. assertNull(tcp.invalidCommand);
  117. assertNull(tcp.executedCommand);
  118. assertFalse(tcp.wasSilent);
  119. }
  120. @Test
  121. public void testCommandHistory() {
  122. final TestCommandParser tcp = new TestCommandParser();
  123. tcp.parseCommand(null, "/echo this is a test");
  124. final long time1 = tcp.getCommandTime("echo this is a test");
  125. assertTrue(time1 > 0);
  126. tcp.parseCommand(null, "/echo this is a test");
  127. final long time2 = tcp.getCommandTime("echo this is a test");
  128. assertTrue(time2 > 0);
  129. assertTrue(time2 >= time1);
  130. assertEquals(0l, tcp.getCommandTime("echo"));
  131. }
  132. public static junit.framework.Test suite() {
  133. return new junit.framework.JUnit4TestAdapter(CommandParserTest.class);
  134. }
  135. private class TestCommandParser extends CommandParser {
  136. public String nonCommandLine;
  137. public Command executedCommand;
  138. public boolean wasSilent;
  139. public String[] commandArgs;
  140. public String invalidCommand;
  141. @Override
  142. protected void loadCommands() {
  143. CommandManager.loadGlobalCommands(this);
  144. }
  145. @Override
  146. protected void executeCommand(InputWindow origin, boolean isSilent,
  147. Command command, String... args) {
  148. executedCommand = command;
  149. wasSilent = isSilent;
  150. commandArgs = args;
  151. }
  152. @Override
  153. protected void handleNonCommand(InputWindow origin, String line) {
  154. nonCommandLine = line;
  155. }
  156. @Override
  157. protected void handleInvalidCommand(InputWindow origin, String command,
  158. String... args) {
  159. invalidCommand = command;
  160. }
  161. }
  162. }