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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2006-2010 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.harness.TestCommandParser;
  24. import com.dmdirc.commandparser.CommandManager;
  25. import com.dmdirc.commandparser.commands.Command;
  26. import com.dmdirc.commandparser.commands.global.Echo;
  27. import com.dmdirc.config.IdentityManager;
  28. import com.dmdirc.ui.interfaces.InputWindow;
  29. import java.util.Arrays;
  30. import org.junit.BeforeClass;
  31. import org.junit.Test;
  32. import static org.junit.Assert.*;
  33. public class CommandParserTest {
  34. @BeforeClass
  35. public static void setUpClass() throws Exception {
  36. IdentityManager.load();
  37. CommandManager.initCommands();
  38. }
  39. @Test
  40. public void testBasicCommand() {
  41. final TestCommandParser tcp = new TestCommandParser();
  42. tcp.parseCommand(null, "/echo this is a test");
  43. assertNull(tcp.nonCommandLine);
  44. assertNull(tcp.invalidCommand);
  45. assertNotNull(tcp.executedCommand);
  46. assertFalse(tcp.wasSilent);
  47. assertTrue(tcp.executedCommand instanceof Echo);
  48. }
  49. @Test
  50. public void testBasicNoArgs() {
  51. final TestCommandParser tcp = new TestCommandParser();
  52. tcp.parseCommand(null, "/echo");
  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 testSilentNoArgs() {
  61. final TestCommandParser tcp = new TestCommandParser();
  62. tcp.parseCommand(null, "/.echo");
  63. assertNull(tcp.nonCommandLine);
  64. assertNull(tcp.invalidCommand);
  65. assertNotNull(tcp.executedCommand);
  66. assertTrue(tcp.wasSilent);
  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(tcp.executedCommand instanceof Echo);
  78. }
  79. @Test
  80. public void testNonExistantCommand() {
  81. final TestCommandParser tcp = new TestCommandParser();
  82. tcp.parseCommand(null, "/foobar moo bar");
  83. assertNull(tcp.nonCommandLine);
  84. assertEquals("foobar", tcp.invalidCommand);
  85. assertNotNull(tcp.invalidCommand);
  86. assertNull(tcp.executedCommand);
  87. assertFalse(tcp.wasSilent);
  88. }
  89. @Test
  90. public void testEmptyCommand() {
  91. final TestCommandParser tcp = new TestCommandParser();
  92. tcp.parseCommand(null, "/ moo bar");
  93. assertNull(tcp.nonCommandLine);
  94. assertEquals("", tcp.invalidCommand);
  95. assertNotNull(tcp.invalidCommand);
  96. assertNull(tcp.executedCommand);
  97. assertFalse(tcp.wasSilent);
  98. }
  99. @Test
  100. public void testEmptySilentCommand() {
  101. final TestCommandParser tcp = new TestCommandParser();
  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 testNonCommand() {
  111. final TestCommandParser tcp = new TestCommandParser();
  112. tcp.parseCommand(null, "Foobar baz");
  113. assertNotNull(tcp.nonCommandLine);
  114. assertEquals("Foobar baz", tcp.nonCommandLine);
  115. assertNull(tcp.invalidCommand);
  116. assertNull(tcp.executedCommand);
  117. assertFalse(tcp.wasSilent);
  118. }
  119. @Test
  120. public void testCommandHistory() {
  121. final TestCommandParser tcp = new TestCommandParser();
  122. tcp.parseCommand(null, "/echo this is a test");
  123. final long time1 = tcp.getCommandTime("echo this is a test");
  124. assertTrue(time1 > 0);
  125. tcp.parseCommand(null, "/echo this is a test");
  126. final long time2 = tcp.getCommandTime("echo this is a test");
  127. assertTrue(time2 > 0);
  128. assertTrue(time2 >= time1);
  129. assertEquals(0l, tcp.getCommandTime("echo"));
  130. }
  131. }