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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2006-2014 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.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.interfaces.CommandController;
  27. import com.google.common.collect.Sets;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.mockito.Mock;
  32. import org.mockito.runners.MockitoJUnitRunner;
  33. import static com.dmdirc.harness.CommandArgsMatcher.eqLine;
  34. import static org.mockito.Matchers.anyObject;
  35. import static org.mockito.Matchers.anyString;
  36. import static org.mockito.Matchers.eq;
  37. import static org.mockito.Matchers.same;
  38. import static org.mockito.Mockito.never;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.when;
  41. @RunWith(MockitoJUnitRunner.class)
  42. public class DebugTest {
  43. @Mock private CommandArguments arguments;
  44. @Mock private FrameContainer container;
  45. @Mock private DebugPlugin plugin;
  46. @Mock private CommandController controller;
  47. @Mock private DebugCommand debugCommand;
  48. @Mock private CommandContext commandContext;
  49. private Debug debug;
  50. @Before
  51. public void setup() {
  52. when(controller.getCommandChar()).thenReturn('/');
  53. when(debugCommand.getName()).thenReturn("test");
  54. }
  55. /** Checks the debug command with no arguments shows usage. */
  56. @Test
  57. public void testNoArgs() {
  58. debug = new Debug(controller, Sets.<DebugCommand>newHashSet());
  59. when(arguments.isCommand()).thenReturn(true);
  60. when(arguments.getArguments()).thenReturn(new String[0]);
  61. debug.execute(container, arguments, null);
  62. verify(container).addLine(eq("commandUsage"), anyString(),
  63. eq("debug"), anyObject());
  64. }
  65. /** Checks the debug command with an invalid subcommand shows an error. */
  66. @Test
  67. public void testInvalidArg() {
  68. debug = new Debug(controller, Sets.<DebugCommand>newHashSet());
  69. when(arguments.isCommand()).thenReturn(true);
  70. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  71. debug.execute(container, arguments, null);
  72. verify(container).addLine(eq("commandError"), anyString());
  73. }
  74. /** Checks the debug command executes a subcommand with no args. */
  75. @Test
  76. public void testCommandNoArgs() {
  77. debug = new Debug(controller, Sets.newHashSet(debugCommand));
  78. when(arguments.isCommand()).thenReturn(true);
  79. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  80. when(arguments.getArgumentsAsString(1)).thenReturn("");
  81. when(debugCommand.getName()).thenReturn("test");
  82. debug.execute(container, arguments, commandContext);
  83. verify(container, never()).addLine(anyString(), anyString());
  84. verify(debugCommand).execute(same(container), eqLine("/test"), same(commandContext));
  85. }
  86. /** Checks the debug command executes a subcommand with args. */
  87. @Test
  88. public void testCommandWithArgs() {
  89. debug = new Debug(controller, Sets.newHashSet(debugCommand));
  90. when(arguments.isCommand()).thenReturn(true);
  91. when(arguments.getArguments()).thenReturn(new String[]{"test", "1", "2", "3"});
  92. when(arguments.getArgumentsAsString(1)).thenReturn("1 2 3");
  93. when(debugCommand.getName()).thenReturn("test");
  94. debug.execute(container, arguments, commandContext);
  95. verify(container, never()).addLine(anyString(), anyString());
  96. verify(debugCommand).execute(same(container), eqLine("/test 1 2 3"), same(commandContext));
  97. }
  98. }