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.

ConditionalExecuteCommandTest.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.conditional_execute;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.commandparser.parsers.CommandParser;
  27. import com.dmdirc.interfaces.CommandController;
  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 org.mockito.Matchers.anyString;
  34. import static org.mockito.Matchers.contains;
  35. import static org.mockito.Matchers.eq;
  36. import static org.mockito.Matchers.same;
  37. import static org.mockito.Mockito.never;
  38. import static org.mockito.Mockito.verify;
  39. import static org.mockito.Mockito.when;
  40. @RunWith(MockitoJUnitRunner.class)
  41. public class ConditionalExecuteCommandTest {
  42. @Mock private CommandController commandController;
  43. @Mock private CommandParser commandParser;
  44. @Mock private FrameContainer container;
  45. @Mock private CommandContext context;
  46. private ConditionalExecuteCommand command;
  47. @Before
  48. public void setup() {
  49. when(commandController.getCommandChar()).thenReturn('/');
  50. when(commandController.getSilenceChar()).thenReturn('/');
  51. when(container.getCommandParser()).thenReturn(commandParser);
  52. when(container.isWritable()).thenReturn(true);
  53. command = new ConditionalExecuteCommand(commandController);
  54. }
  55. /** Tests that {@code --namespace} with no args shows an error. */
  56. @Test
  57. public void testWithNoNamespace() {
  58. execute("/command --namespace");
  59. verifyErrorOutput("must specify a namespace");
  60. }
  61. /** Tests that passing a command without a namespace shows an error. */
  62. @Test
  63. public void testExecutingWithNoNamespace() {
  64. execute("/command /echo test");
  65. verifyErrorOutput("must specify a namespace");
  66. }
  67. /** Tests that passing a command with a previously unused namespace executes it. */
  68. @Test
  69. public void testExecutingWithNewNamespace() {
  70. execute("/command --namespace foo /echo test");
  71. verifyCommandExecuted("/echo test");
  72. }
  73. /** Tests that passing a command with an inhibited namespace does not execute it. */
  74. @Test
  75. public void testExecutingWithInhibitedNamespace() {
  76. execute("/command --namespace foo --inhibit");
  77. execute("/command --namespace foo /echo test");
  78. verifyNoCommandExecuted();
  79. }
  80. /** Tests that passing a command with a forced namespace executes it. */
  81. @Test
  82. public void testExecutingWithForcedNamespace() {
  83. execute("/command --namespace foo --force");
  84. execute("/command --namespace foo /echo test");
  85. verifyCommandExecuted("/echo test");
  86. }
  87. /** Tests that passing a command with an inhibited then forced namespace executes it. */
  88. @Test
  89. public void testExecutingWithInhibitedThenForcedNamespace() {
  90. execute("/command --namespace foo --inhibit");
  91. execute("/command --namespace foo --force");
  92. execute("/command --namespace foo /echo test");
  93. verifyCommandExecuted("/echo test");
  94. }
  95. /** Tests that passing a command with an inhibited then allowed namespace executes it. */
  96. @Test
  97. public void testExecutingWithInhibitedThenAllowedNamespace() {
  98. execute("/command --namespace foo --inhibit");
  99. execute("/command --namespace foo --allow");
  100. execute("/command --namespace foo /echo test");
  101. verifyCommandExecuted("/echo test");
  102. }
  103. /** Tests that passing a command with an inhibited then removed namespace executes it. */
  104. @Test
  105. public void testExecutingWithInhibitedThenRemovedNamespace() {
  106. execute("/command --namespace foo --inhibit");
  107. execute("/command --namespace foo --remove");
  108. execute("/command --namespace foo /echo test");
  109. verifyCommandExecuted("/echo test");
  110. }
  111. /** Tests that passing a command with a forced then inhibited namespace does not execute it. */
  112. @Test
  113. public void testExecutingWithForcedThenInhibitedNamespace() {
  114. execute("/command --namespace foo --force");
  115. execute("/command --namespace foo --inhibit");
  116. execute("/command --namespace foo /echo test");
  117. verifyNoCommandExecuted();
  118. }
  119. /**
  120. * Tests that passing a command with a new namespace and {@link --inverse} does not execute it.
  121. */
  122. @Test
  123. public void testInverseExecutingWithNewNamespace() {
  124. execute("/command --namespace foo --inverse /echo test");
  125. verifyNoCommandExecuted();
  126. }
  127. /**
  128. * Tests that passing a command with an inhibited namespace and {@link --inverse} does not
  129. * execute it. Inverse doesn't apply to inhibited namespaces.
  130. */
  131. @Test
  132. public void testInverseExecutingWithInhibitedNamespace() {
  133. execute("/command --namespace foo --inhibit");
  134. execute("/command --namespace foo --inverse /echo test");
  135. verifyNoCommandExecuted();
  136. }
  137. private void execute(String line) {
  138. command.execute(container, new CommandArguments(commandController, line), context);
  139. }
  140. private void verifyCommandExecuted(String command) {
  141. verify(commandParser).parseCommand(same(container), eq(command));
  142. }
  143. private void verifyNoCommandExecuted() {
  144. verify(commandParser, never()).parseCommand(same(container), anyString());
  145. }
  146. private void verifyErrorOutput(String substring) {
  147. verify(container).addLine(eq("commandError"), contains(substring));
  148. }
  149. }