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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2015 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.commandparser.CommandArguments;
  24. import com.dmdirc.commandparser.CommandInfo;
  25. import com.dmdirc.commandparser.CommandType;
  26. import com.dmdirc.commandparser.commands.context.CommandContext;
  27. import com.dmdirc.commandparser.parsers.CommandParser;
  28. import com.dmdirc.events.CommandErrorEvent;
  29. import com.dmdirc.interfaces.CommandController;
  30. import com.dmdirc.interfaces.EventBus;
  31. import com.dmdirc.interfaces.InputModel;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import java.util.Optional;
  34. import org.junit.Before;
  35. import org.junit.Test;
  36. import org.junit.runner.RunWith;
  37. import org.mockito.ArgumentCaptor;
  38. import org.mockito.Captor;
  39. import org.mockito.Mock;
  40. import org.mockito.runners.MockitoJUnitRunner;
  41. import static org.junit.Assert.assertEquals;
  42. import static org.mockito.Mockito.verify;
  43. import static org.mockito.Mockito.when;
  44. @RunWith(MockitoJUnitRunner.class)
  45. public class AliasCommandHandlerTest {
  46. @Mock private WindowModel container;
  47. @Mock private InputModel inputModel;
  48. @Mock private CommandController commandController;
  49. @Mock private CommandParser commandParser;
  50. @Mock private CommandContext context;
  51. @Mock private EventBus eventbus;
  52. @Captor private ArgumentCaptor<CommandErrorEvent> errorEventCaptor;
  53. @Before
  54. public void setup() {
  55. when(container.getInputModel()).thenReturn(Optional.of(inputModel));
  56. when(inputModel.getCommandParser()).thenReturn(commandParser);
  57. when(commandController.getCommandChar()).thenReturn('#');
  58. when(commandController.getSilenceChar()).thenReturn('/');
  59. when(container.getEventBus()).thenReturn(eventbus);
  60. }
  61. @Test
  62. public void testBasicAlias() {
  63. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "test2");
  64. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  65. final CommandArguments arguments = new CommandArguments(commandController, "#test");
  66. handler.execute(container, arguments, context);
  67. verify(commandParser).parseCommand(container, "#test2");
  68. }
  69. @Test
  70. public void testSubstitutions() {
  71. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "test2 $1- $2 $1 $2- $2");
  72. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  73. final CommandArguments arguments
  74. = new CommandArguments(commandController, "#test agadoo do");
  75. handler.execute(container, arguments, context);
  76. verify(commandParser).parseCommand(container, "#test2 agadoo do do agadoo do do");
  77. }
  78. @Test
  79. public void testInsufficientArgsSingular() {
  80. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 1, "blah");
  81. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  82. final CommandArguments arguments = new CommandArguments(commandController, "#test");
  83. handler.execute(container, arguments, context);
  84. verify(eventbus).publishAsync(errorEventCaptor.capture());
  85. assertEquals("test requires at least 1 argument.",
  86. errorEventCaptor.getValue().getMessage());
  87. }
  88. @Test
  89. public void testInsufficientArgsPlural() {
  90. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 2, "blah");
  91. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  92. final CommandArguments arguments = new CommandArguments(commandController, "#test agadoo");
  93. handler.execute(container, arguments, context);
  94. verify(eventbus).publishAsync(errorEventCaptor.capture());
  95. assertEquals("test requires at least 2 arguments.",
  96. errorEventCaptor.getValue().getMessage());
  97. }
  98. @Test
  99. public void testCarriesForwardSilence() {
  100. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "blah");
  101. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  102. final CommandArguments arguments = new CommandArguments(commandController, "#/test agadoo");
  103. handler.execute(container, arguments, context);
  104. verify(commandParser).parseCommand(container, "#/blah");
  105. }
  106. }