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

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