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.

AliasCommandHandlerTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2006-2014 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.aliases;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.context.CommandContext;
  28. import com.dmdirc.commandparser.parsers.CommandParser;
  29. import com.dmdirc.interfaces.CommandController;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import org.junit.runner.RunWith;
  33. import org.mockito.Mock;
  34. import org.mockito.runners.MockitoJUnitRunner;
  35. import static org.mockito.Mockito.verify;
  36. import static org.mockito.Mockito.when;
  37. @RunWith(MockitoJUnitRunner.class)
  38. public class AliasCommandHandlerTest {
  39. @Mock private FrameContainer container;
  40. @Mock private CommandController commandController;
  41. @Mock private CommandParser commandParser;
  42. @Mock private CommandContext context;
  43. @Mock private CommandInfo commandInfo;
  44. @Before
  45. public void setup() {
  46. when(container.getCommandParser()).thenReturn(commandParser);
  47. when(commandController.getCommandChar()).thenReturn('#');
  48. when(commandController.getSilenceChar()).thenReturn('/');
  49. when(context.getSource()).thenReturn(container);
  50. when(context.getCommandInfo()).thenReturn(commandInfo);
  51. }
  52. @Test
  53. public void testBasicAlias() {
  54. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "test2");
  55. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  56. final CommandArguments arguments = new CommandArguments(commandController, "#test");
  57. handler.execute(container, arguments, context);
  58. verify(commandParser).parseCommand(container, "#test2");
  59. }
  60. @Test
  61. public void testSubstitutions() {
  62. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "test2 $1- $2 $1 $2- $2");
  63. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  64. final CommandArguments arguments
  65. = new CommandArguments(commandController, "#test agadoo do");
  66. handler.execute(container, arguments, context);
  67. verify(commandParser).parseCommand(container, "#test2 agadoo do do agadoo do do");
  68. }
  69. @Test
  70. public void testInsufficientArgsSingular() {
  71. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 1, "blah");
  72. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  73. final CommandArguments arguments = new CommandArguments(commandController, "#test");
  74. handler.execute(container, arguments, context);
  75. verify(container).addLine("commandError", "test requires at least 1 argument.");
  76. }
  77. @Test
  78. public void testInsufficientArgsPlural() {
  79. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 2, "blah");
  80. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  81. final CommandArguments arguments = new CommandArguments(commandController, "#test agadoo");
  82. handler.execute(container, arguments, context);
  83. verify(container).addLine("commandError", "test requires at least 2 arguments.");
  84. }
  85. @Test
  86. public void testCarriesForwardSilence() {
  87. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "blah");
  88. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  89. final CommandArguments arguments = new CommandArguments(commandController, "#/test agadoo");
  90. handler.execute(container, arguments, context);
  91. verify(commandParser).parseCommand(container, "#/blah");
  92. }
  93. }