Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AliasCommandHandlerTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 CommandInfo commandInfo;
  52. @Mock private EventBus eventbus;
  53. @Captor private ArgumentCaptor<CommandErrorEvent> errorEventCaptor;
  54. @Before
  55. public void setup() {
  56. when(container.getInputModel()).thenReturn(Optional.of(inputModel));
  57. when(inputModel.getCommandParser()).thenReturn(commandParser);
  58. when(commandController.getCommandChar()).thenReturn('#');
  59. when(commandController.getSilenceChar()).thenReturn('/');
  60. when(context.getSource()).thenReturn(container);
  61. when(context.getCommandInfo()).thenReturn(commandInfo);
  62. when(container.getEventBus()).thenReturn(eventbus);
  63. }
  64. @Test
  65. public void testBasicAlias() {
  66. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "test2");
  67. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  68. final CommandArguments arguments = new CommandArguments(commandController, "#test");
  69. handler.execute(container, arguments, context);
  70. verify(commandParser).parseCommand(container, "#test2");
  71. }
  72. @Test
  73. public void testSubstitutions() {
  74. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "test2 $1- $2 $1 $2- $2");
  75. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  76. final CommandArguments arguments
  77. = new CommandArguments(commandController, "#test agadoo do");
  78. handler.execute(container, arguments, context);
  79. verify(commandParser).parseCommand(container, "#test2 agadoo do do agadoo do do");
  80. }
  81. @Test
  82. public void testInsufficientArgsSingular() {
  83. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 1, "blah");
  84. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  85. final CommandArguments arguments = new CommandArguments(commandController, "#test");
  86. handler.execute(container, arguments, context);
  87. verify(eventbus).publishAsync(errorEventCaptor.capture());
  88. assertEquals("test requires at least 1 argument.",
  89. errorEventCaptor.getValue().getMessage());
  90. }
  91. @Test
  92. public void testInsufficientArgsPlural() {
  93. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 2, "blah");
  94. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  95. final CommandArguments arguments = new CommandArguments(commandController, "#test agadoo");
  96. handler.execute(container, arguments, context);
  97. verify(eventbus).publishAsync(errorEventCaptor.capture());
  98. assertEquals("test requires at least 2 arguments.",
  99. errorEventCaptor.getValue().getMessage());
  100. }
  101. @Test
  102. public void testCarriesForwardSilence() {
  103. final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "blah");
  104. final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
  105. final CommandArguments arguments = new CommandArguments(commandController, "#/test agadoo");
  106. handler.execute(container, arguments, context);
  107. verify(commandParser).parseCommand(container, "#/blah");
  108. }
  109. }