Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CommandParserTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.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.ui.interfaces.InputWindow;
  28. import java.util.Arrays;
  29. import org.junit.BeforeClass;
  30. import org.junit.Test;
  31. import static org.junit.Assert.*;
  32. public class CommandParserTest {
  33. @BeforeClass
  34. public static void setUpClass() throws Exception {
  35. CommandManager.initCommands();
  36. }
  37. @Test
  38. public void testBasicCommand() {
  39. final TestCommandParser tcp = new TestCommandParser();
  40. tcp.parseCommand(null, "/echo this is a test");
  41. assertNull(tcp.nonCommandLine);
  42. assertNull(tcp.invalidCommand);
  43. assertNotNull(tcp.executedCommand);
  44. assertFalse(tcp.wasSilent);
  45. assertTrue(Arrays.equals(tcp.commandArgs, new String[]{"this", "is", "a", "test"}));
  46. assertTrue(tcp.executedCommand instanceof Echo);
  47. }
  48. @Test
  49. public void testBasicNoArgs() {
  50. final TestCommandParser tcp = new TestCommandParser();
  51. tcp.parseCommand(null, "/echo");
  52. assertNull(tcp.nonCommandLine);
  53. assertNull(tcp.invalidCommand);
  54. assertNotNull(tcp.executedCommand);
  55. assertFalse(tcp.wasSilent);
  56. assertTrue(Arrays.equals(tcp.commandArgs, new String[0]));
  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(Arrays.equals(tcp.commandArgs, new String[0]));
  68. assertTrue(tcp.executedCommand instanceof Echo);
  69. }
  70. @Test
  71. public void testSilentCommand() {
  72. final TestCommandParser tcp = new TestCommandParser();
  73. tcp.parseCommand(null, "/.echo this is a test");
  74. assertNull(tcp.nonCommandLine);
  75. assertNull(tcp.invalidCommand);
  76. assertNotNull(tcp.executedCommand);
  77. assertTrue(tcp.wasSilent);
  78. assertTrue(Arrays.equals(tcp.commandArgs, new String[]{"this", "is", "a", "test"}));
  79. assertTrue(tcp.executedCommand instanceof Echo);
  80. }
  81. @Test
  82. public void testNonExistantCommand() {
  83. final TestCommandParser tcp = new TestCommandParser();
  84. tcp.parseCommand(null, "/foobar moo bar");
  85. assertNull(tcp.nonCommandLine);
  86. assertEquals("foobar", tcp.invalidCommand);
  87. assertNotNull(tcp.invalidCommand);
  88. assertNull(tcp.executedCommand);
  89. assertFalse(tcp.wasSilent);
  90. }
  91. @Test
  92. public void testEmptyCommand() {
  93. final TestCommandParser tcp = new TestCommandParser();
  94. tcp.parseCommand(null, "/ moo bar");
  95. assertNull(tcp.nonCommandLine);
  96. assertEquals("", tcp.invalidCommand);
  97. assertNotNull(tcp.invalidCommand);
  98. assertNull(tcp.executedCommand);
  99. assertFalse(tcp.wasSilent);
  100. }
  101. @Test
  102. public void testEmptySilentCommand() {
  103. final TestCommandParser tcp = new TestCommandParser();
  104. tcp.parseCommand(null, "/. moo bar");
  105. assertNull(tcp.nonCommandLine);
  106. assertEquals("", tcp.invalidCommand);
  107. assertNotNull(tcp.invalidCommand);
  108. assertNull(tcp.executedCommand);
  109. assertFalse(tcp.wasSilent);
  110. }
  111. @Test
  112. public void testNonCommand() {
  113. final TestCommandParser tcp = new TestCommandParser();
  114. tcp.parseCommand(null, "Foobar baz");
  115. assertNotNull(tcp.nonCommandLine);
  116. assertEquals("Foobar baz", tcp.nonCommandLine);
  117. assertNull(tcp.invalidCommand);
  118. assertNull(tcp.executedCommand);
  119. assertFalse(tcp.wasSilent);
  120. }
  121. @Test
  122. public void testCommandHistory() {
  123. final TestCommandParser tcp = new TestCommandParser();
  124. tcp.parseCommand(null, "/echo this is a test");
  125. final long time1 = tcp.getCommandTime("echo this is a test");
  126. assertTrue(time1 > 0);
  127. tcp.parseCommand(null, "/echo this is a test");
  128. final long time2 = tcp.getCommandTime("echo this is a test");
  129. assertTrue(time2 > 0);
  130. assertTrue(time2 >= time1);
  131. assertEquals(0l, tcp.getCommandTime("echo"));
  132. }
  133. public static junit.framework.Test suite() {
  134. return new junit.framework.JUnit4TestAdapter(CommandParserTest.class);
  135. }
  136. }