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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2011 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.global.Echo;
  26. import com.dmdirc.config.IdentityManager;
  27. import org.junit.BeforeClass;
  28. import org.junit.Test;
  29. import static org.junit.Assert.*;
  30. public class CommandParserTest {
  31. @BeforeClass
  32. public static void setUpClass() throws Exception {
  33. IdentityManager.load();
  34. CommandManager.getCommandManager().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(tcp.executedCommand instanceof Echo);
  45. }
  46. @Test
  47. public void testBasicNoArgs() {
  48. final TestCommandParser tcp = new TestCommandParser();
  49. tcp.parseCommand(null, "/echo");
  50. assertNull(tcp.nonCommandLine);
  51. assertNull(tcp.invalidCommand);
  52. assertNotNull(tcp.executedCommand);
  53. assertFalse(tcp.wasSilent);
  54. assertTrue(tcp.executedCommand instanceof Echo);
  55. }
  56. @Test
  57. public void testSilentNoArgs() {
  58. final TestCommandParser tcp = new TestCommandParser();
  59. tcp.parseCommand(null, "/.echo");
  60. assertNull(tcp.nonCommandLine);
  61. assertNull(tcp.invalidCommand);
  62. assertNotNull(tcp.executedCommand);
  63. assertTrue(tcp.wasSilent);
  64. assertTrue(tcp.executedCommand instanceof Echo);
  65. }
  66. @Test
  67. public void testSilentCommand() {
  68. final TestCommandParser tcp = new TestCommandParser();
  69. tcp.parseCommand(null, "/.echo this is a test");
  70. assertNull(tcp.nonCommandLine);
  71. assertNull(tcp.invalidCommand);
  72. assertNotNull(tcp.executedCommand);
  73. assertTrue(tcp.wasSilent);
  74. assertTrue(tcp.executedCommand instanceof Echo);
  75. }
  76. @Test
  77. public void testNonExistantCommand() {
  78. final TestCommandParser tcp = new TestCommandParser();
  79. tcp.parseCommand(null, "/foobar moo bar");
  80. assertNull(tcp.nonCommandLine);
  81. assertEquals("foobar", tcp.invalidCommand);
  82. assertNotNull(tcp.invalidCommand);
  83. assertNull(tcp.executedCommand);
  84. assertFalse(tcp.wasSilent);
  85. }
  86. @Test
  87. public void testEmptyCommand() {
  88. final TestCommandParser tcp = new TestCommandParser();
  89. tcp.parseCommand(null, "/ moo bar");
  90. assertNull(tcp.nonCommandLine);
  91. assertEquals("", tcp.invalidCommand);
  92. assertNotNull(tcp.invalidCommand);
  93. assertNull(tcp.executedCommand);
  94. assertFalse(tcp.wasSilent);
  95. }
  96. @Test
  97. public void testEmptySilentCommand() {
  98. final TestCommandParser tcp = new TestCommandParser();
  99. tcp.parseCommand(null, "/. moo bar");
  100. assertNull(tcp.nonCommandLine);
  101. assertEquals("", tcp.invalidCommand);
  102. assertNotNull(tcp.invalidCommand);
  103. assertNull(tcp.executedCommand);
  104. assertFalse(tcp.wasSilent);
  105. }
  106. @Test
  107. public void testNonCommand() {
  108. final TestCommandParser tcp = new TestCommandParser();
  109. tcp.parseCommand(null, "Foobar baz");
  110. assertNotNull(tcp.nonCommandLine);
  111. assertEquals("Foobar baz", tcp.nonCommandLine);
  112. assertNull(tcp.invalidCommand);
  113. assertNull(tcp.executedCommand);
  114. assertFalse(tcp.wasSilent);
  115. }
  116. @Test
  117. public void testCommandHistory() {
  118. final TestCommandParser tcp = new TestCommandParser();
  119. tcp.parseCommand(null, "/echo this is a test");
  120. final long time1 = tcp.getCommandTime("echo this is a test");
  121. assertTrue(time1 > 0);
  122. tcp.parseCommand( null, "/echo this is a test");
  123. final long time2 = tcp.getCommandTime("echo this is a test");
  124. assertTrue(time2 > 0);
  125. assertTrue(time2 >= time1);
  126. assertEquals(0L, tcp.getCommandTime("echo"));
  127. }
  128. }