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.

DebugTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.debug;
  18. import com.dmdirc.commandparser.CommandArguments;
  19. import com.dmdirc.commandparser.commands.context.CommandContext;
  20. import com.dmdirc.events.CommandErrorEvent;
  21. import com.dmdirc.interfaces.CommandController;
  22. import com.dmdirc.events.eventbus.EventBus;
  23. import com.dmdirc.interfaces.WindowModel;
  24. import com.google.common.collect.Sets;
  25. import org.hamcrest.Description;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.mockito.ArgumentMatcher;
  30. import org.mockito.Mock;
  31. import org.mockito.runners.MockitoJUnitRunner;
  32. import static org.mockito.Matchers.any;
  33. import static org.mockito.Matchers.argThat;
  34. import static org.mockito.Matchers.isA;
  35. import static org.mockito.Matchers.same;
  36. import static org.mockito.Mockito.never;
  37. import static org.mockito.Mockito.verify;
  38. import static org.mockito.Mockito.when;
  39. @RunWith(MockitoJUnitRunner.class)
  40. public class DebugTest {
  41. @Mock private CommandArguments arguments;
  42. @Mock private WindowModel container;
  43. @Mock private CommandController controller;
  44. @Mock private DebugCommand debugCommand;
  45. @Mock private CommandContext commandContext;
  46. @Mock private EventBus eventbus;
  47. private Debug debug;
  48. @Before
  49. public void setup() {
  50. when(controller.getCommandChar()).thenReturn('/');
  51. when(debugCommand.getName()).thenReturn("test");
  52. when(container.getEventBus()).thenReturn(eventbus);
  53. }
  54. /** Checks the debug command with no arguments shows usage. */
  55. @Test
  56. public void testNoArgs() {
  57. debug = new Debug(controller, Sets.<DebugCommand>newHashSet());
  58. when(arguments.isCommand()).thenReturn(true);
  59. when(arguments.getArguments()).thenReturn(new String[0]);
  60. debug.execute(container, arguments, null);
  61. verify(eventbus).publishAsync(isA(CommandErrorEvent.class));
  62. }
  63. /** Checks the debug command with an invalid subcommand shows an error. */
  64. @Test
  65. public void testInvalidArg() {
  66. debug = new Debug(controller, Sets.<DebugCommand>newHashSet());
  67. when(arguments.isCommand()).thenReturn(true);
  68. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  69. debug.execute(container, arguments, null);
  70. verify(eventbus).publishAsync(isA(CommandErrorEvent.class));
  71. }
  72. /** Checks the debug command executes a subcommand with no args. */
  73. @Test
  74. public void testCommandNoArgs() {
  75. debug = new Debug(controller, Sets.newHashSet(debugCommand));
  76. when(arguments.isCommand()).thenReturn(true);
  77. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  78. when(arguments.getArgumentsAsString(1)).thenReturn("");
  79. when(debugCommand.getName()).thenReturn("test");
  80. debug.execute(container, arguments, commandContext);
  81. verify(eventbus, never()).publishAsync(any());
  82. verify(debugCommand).execute(same(container), eqLine("/test"),
  83. same(commandContext));
  84. }
  85. /** Checks the debug command executes a subcommand with args. */
  86. @Test
  87. public void testCommandWithArgs() {
  88. debug = new Debug(controller, Sets.newHashSet(debugCommand));
  89. when(arguments.isCommand()).thenReturn(true);
  90. when(arguments.getArguments()).thenReturn(new String[]{"test", "1", "2", "3"});
  91. when(arguments.getArgumentsAsString(1)).thenReturn("1 2 3");
  92. when(debugCommand.getName()).thenReturn("test");
  93. debug.execute(container, arguments, commandContext);
  94. verify(eventbus, never()).publishAsync(any());
  95. verify(debugCommand).execute(same(container), eqLine("/test 1 2 3"),
  96. same(commandContext));
  97. }
  98. private static class CommandArgsMatcher extends ArgumentMatcher<CommandArguments> {
  99. private final String text;
  100. public CommandArgsMatcher(final String text) {
  101. this.text = text;
  102. }
  103. @Override
  104. public boolean matches(final Object item) {
  105. return text.equals(((CommandArguments) item).getLine());
  106. }
  107. @Override
  108. public void describeTo(final Description description) {
  109. description.appendText("eqLine(\"" + text + "\")");
  110. }
  111. }
  112. public static CommandArguments eqLine(final String line) {
  113. return argThat(new CommandArgsMatcher(line));
  114. }
  115. }