Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AliasCommandHandlerTest.java 5.2KB

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