Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.addons.debug;
  23. import com.dmdirc.commandparser.CommandArguments;
  24. import com.dmdirc.commandparser.commands.context.CommandContext;
  25. import com.dmdirc.events.CommandErrorEvent;
  26. import com.dmdirc.interfaces.CommandController;
  27. import com.dmdirc.interfaces.EventBus;
  28. import com.dmdirc.interfaces.WindowModel;
  29. import com.google.common.collect.Sets;
  30. import org.hamcrest.Description;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.mockito.ArgumentMatcher;
  35. import org.mockito.Mock;
  36. import org.mockito.runners.MockitoJUnitRunner;
  37. import static org.mockito.Matchers.any;
  38. import static org.mockito.Matchers.argThat;
  39. import static org.mockito.Matchers.isA;
  40. import static org.mockito.Matchers.same;
  41. import static org.mockito.Mockito.never;
  42. import static org.mockito.Mockito.verify;
  43. import static org.mockito.Mockito.when;
  44. @RunWith(MockitoJUnitRunner.class)
  45. public class DebugTest {
  46. @Mock private CommandArguments arguments;
  47. @Mock private WindowModel container;
  48. @Mock private CommandController controller;
  49. @Mock private DebugCommand debugCommand;
  50. @Mock private CommandContext commandContext;
  51. @Mock private EventBus eventbus;
  52. private Debug debug;
  53. @Before
  54. public void setup() {
  55. when(controller.getCommandChar()).thenReturn('/');
  56. when(debugCommand.getName()).thenReturn("test");
  57. when(container.getEventBus()).thenReturn(eventbus);
  58. }
  59. /** Checks the debug command with no arguments shows usage. */
  60. @Test
  61. public void testNoArgs() {
  62. debug = new Debug(controller, Sets.<DebugCommand>newHashSet());
  63. when(arguments.isCommand()).thenReturn(true);
  64. when(arguments.getArguments()).thenReturn(new String[0]);
  65. debug.execute(container, arguments, null);
  66. verify(eventbus).publishAsync(isA(CommandErrorEvent.class));
  67. }
  68. /** Checks the debug command with an invalid subcommand shows an error. */
  69. @Test
  70. public void testInvalidArg() {
  71. debug = new Debug(controller, Sets.<DebugCommand>newHashSet());
  72. when(arguments.isCommand()).thenReturn(true);
  73. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  74. debug.execute(container, arguments, null);
  75. verify(eventbus).publishAsync(isA(CommandErrorEvent.class));
  76. }
  77. /** Checks the debug command executes a subcommand with no args. */
  78. @Test
  79. public void testCommandNoArgs() {
  80. debug = new Debug(controller, Sets.newHashSet(debugCommand));
  81. when(arguments.isCommand()).thenReturn(true);
  82. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  83. when(arguments.getArgumentsAsString(1)).thenReturn("");
  84. when(debugCommand.getName()).thenReturn("test");
  85. debug.execute(container, arguments, commandContext);
  86. verify(eventbus, never()).publishAsync(any());
  87. verify(debugCommand).execute(same(container), eqLine("/test"),
  88. same(commandContext));
  89. }
  90. /** Checks the debug command executes a subcommand with args. */
  91. @Test
  92. public void testCommandWithArgs() {
  93. debug = new Debug(controller, Sets.newHashSet(debugCommand));
  94. when(arguments.isCommand()).thenReturn(true);
  95. when(arguments.getArguments()).thenReturn(new String[]{"test", "1", "2", "3"});
  96. when(arguments.getArgumentsAsString(1)).thenReturn("1 2 3");
  97. when(debugCommand.getName()).thenReturn("test");
  98. debug.execute(container, arguments, commandContext);
  99. verify(eventbus, never()).publishAsync(any());
  100. verify(debugCommand).execute(same(container), eqLine("/test 1 2 3"),
  101. same(commandContext));
  102. }
  103. private static class CommandArgsMatcher extends ArgumentMatcher<CommandArguments> {
  104. private final String text;
  105. public CommandArgsMatcher(final String text) {
  106. this.text = text;
  107. }
  108. @Override
  109. public boolean matches(final Object item) {
  110. return text.equals(((CommandArguments) item).getLine());
  111. }
  112. @Override
  113. public void describeTo(final Description description) {
  114. description.appendText("eqLine(\"" + text + "\")");
  115. }
  116. }
  117. public static CommandArguments eqLine(final String line) {
  118. return argThat(new CommandArgsMatcher(line));
  119. }
  120. }