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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2013 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.TestMain;
  24. import com.dmdirc.Main;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.commandparser.commands.global.Echo;
  27. import com.dmdirc.config.ConfigBinder;
  28. import com.dmdirc.config.ConfigManager;
  29. import com.dmdirc.config.InvalidIdentityFileException;
  30. import com.dmdirc.harness.TestCommandParser;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import static org.junit.Assert.*;
  34. import static org.mockito.Mockito.*;
  35. public class CommandParserTest {
  36. private ConfigManager cm;
  37. private CommandManager commands;
  38. @Before
  39. public void setup() throws InvalidIdentityFileException {
  40. cm = mock(ConfigManager.class);
  41. when(cm.getOptionChar("general", "silencechar")).thenReturn('.');
  42. when(cm.getOptionInt("general", "commandhistory")).thenReturn(10);
  43. when(cm.getOptionChar("general", "commandchar")).thenReturn('/');
  44. final ConfigBinder binder = new ConfigBinder(cm);
  45. when(cm.getBinder()).thenReturn(binder);
  46. commands = new CommandManager(cm, TestMain.getTestMain());
  47. commands.registerCommand(new Echo(commands), Echo.INFO);
  48. }
  49. @Test
  50. public void testBasicCommand() {
  51. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  52. tcp.parseCommand(null, "/echo this is a test");
  53. assertNull(tcp.nonCommandLine);
  54. assertNull(tcp.invalidCommand);
  55. assertNotNull(tcp.executedCommand);
  56. assertFalse(tcp.wasSilent);
  57. assertTrue(tcp.executedCommand instanceof Echo);
  58. }
  59. @Test
  60. public void testBasicNoArgs() {
  61. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  62. tcp.parseCommand(null, "/echo");
  63. assertNull(tcp.nonCommandLine);
  64. assertNull(tcp.invalidCommand);
  65. assertNotNull(tcp.executedCommand);
  66. assertFalse(tcp.wasSilent);
  67. assertTrue(tcp.executedCommand instanceof Echo);
  68. }
  69. @Test
  70. public void testSilentNoArgs() {
  71. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  72. tcp.parseCommand(null, "/.echo");
  73. assertNull(tcp.nonCommandLine);
  74. assertNull(tcp.invalidCommand);
  75. assertNotNull(tcp.executedCommand);
  76. assertTrue(tcp.wasSilent);
  77. assertTrue(tcp.executedCommand instanceof Echo);
  78. }
  79. @Test
  80. public void testSilentCommand() {
  81. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  82. tcp.parseCommand(null, "/.echo this is a test");
  83. assertNull(tcp.nonCommandLine);
  84. assertNull(tcp.invalidCommand);
  85. assertNotNull(tcp.executedCommand);
  86. assertTrue(tcp.wasSilent);
  87. assertTrue(tcp.executedCommand instanceof Echo);
  88. }
  89. @Test
  90. public void testNonExistantCommand() {
  91. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  92. tcp.parseCommand(null, "/foobar moo bar");
  93. assertNull(tcp.nonCommandLine);
  94. assertEquals("foobar", tcp.invalidCommand);
  95. assertNotNull(tcp.invalidCommand);
  96. assertNull(tcp.executedCommand);
  97. assertFalse(tcp.wasSilent);
  98. }
  99. @Test
  100. public void testEmptyCommand() {
  101. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  102. tcp.parseCommand(null, "/ moo bar");
  103. assertNull(tcp.nonCommandLine);
  104. assertEquals("", tcp.invalidCommand);
  105. assertNotNull(tcp.invalidCommand);
  106. assertNull(tcp.executedCommand);
  107. assertFalse(tcp.wasSilent);
  108. }
  109. @Test
  110. public void testEmptySilentCommand() {
  111. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  112. tcp.parseCommand(null, "/. moo bar");
  113. assertNull(tcp.nonCommandLine);
  114. assertEquals("", tcp.invalidCommand);
  115. assertNotNull(tcp.invalidCommand);
  116. assertNull(tcp.executedCommand);
  117. assertFalse(tcp.wasSilent);
  118. }
  119. @Test
  120. public void testNonCommand() {
  121. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  122. tcp.parseCommand(null, "Foobar baz");
  123. assertNotNull(tcp.nonCommandLine);
  124. assertEquals("Foobar baz", tcp.nonCommandLine);
  125. assertNull(tcp.invalidCommand);
  126. assertNull(tcp.executedCommand);
  127. assertFalse(tcp.wasSilent);
  128. }
  129. @Test
  130. public void testCommandHistory() {
  131. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  132. tcp.parseCommand(null, "/echo this is a test");
  133. final long time1 = tcp.getCommandTime("echo this is a test");
  134. assertTrue(time1 > 0);
  135. tcp.parseCommand( null, "/echo this is a test");
  136. final long time2 = tcp.getCommandTime("echo this is a test");
  137. assertTrue(time2 > 0);
  138. assertTrue(time2 >= time1);
  139. assertEquals(0L, tcp.getCommandTime("echo"));
  140. }
  141. }