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