|
@@ -19,155 +19,218 @@
|
19
|
19
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20
|
20
|
* SOFTWARE.
|
21
|
21
|
*/
|
|
22
|
+
|
22
|
23
|
package com.dmdirc.commandparser.parsers;
|
23
|
24
|
|
24
|
|
-import com.dmdirc.ServerManager;
|
25
|
|
-import com.dmdirc.commandparser.CommandManager;
|
26
|
|
-import com.dmdirc.commandparser.commands.global.Echo;
|
27
|
|
-import com.dmdirc.config.ConfigBinder;
|
28
|
|
-import com.dmdirc.config.InvalidIdentityFileException;
|
|
25
|
+import com.dmdirc.Channel;
|
|
26
|
+import com.dmdirc.FrameContainer;
|
|
27
|
+import com.dmdirc.commandparser.CommandInfo;
|
|
28
|
+import com.dmdirc.commandparser.commands.Command;
|
29
|
29
|
import com.dmdirc.harness.TestCommandParser;
|
|
30
|
+import com.dmdirc.interfaces.CommandController;
|
|
31
|
+import com.dmdirc.interfaces.Connection;
|
30
|
32
|
import com.dmdirc.interfaces.config.AggregateConfigProvider;
|
31
|
|
-import com.dmdirc.ui.WindowManager;
|
32
|
33
|
|
33
|
34
|
import org.junit.Before;
|
34
|
35
|
import org.junit.Test;
|
|
36
|
+import org.junit.runner.RunWith;
|
35
|
37
|
import org.mockito.Mock;
|
36
|
|
-import org.mockito.MockitoAnnotations;
|
|
38
|
+import org.mockito.runners.MockitoJUnitRunner;
|
37
|
39
|
|
38
|
|
-import static org.junit.Assert.*;
|
39
|
|
-import static org.mockito.Mockito.*;
|
|
40
|
+import static org.junit.Assert.assertEquals;
|
|
41
|
+import static org.junit.Assert.assertFalse;
|
|
42
|
+import static org.junit.Assert.assertNull;
|
|
43
|
+import static org.junit.Assert.assertSame;
|
|
44
|
+import static org.junit.Assert.assertTrue;
|
|
45
|
+import static org.mockito.Mockito.when;
|
40
|
46
|
|
|
47
|
+@RunWith(MockitoJUnitRunner.class)
|
41
|
48
|
public class CommandParserTest {
|
42
|
49
|
|
43
|
|
- @Mock private ServerManager serverManager;
|
44
|
|
- @Mock private AggregateConfigProvider cm;
|
45
|
|
- @Mock private WindowManager windowManager;
|
46
|
|
- private CommandManager commands;
|
|
50
|
+ @Mock private AggregateConfigProvider configProvider;
|
|
51
|
+ @Mock private CommandController commandController;
|
|
52
|
+ @Mock private CommandInfo commandInfo;
|
|
53
|
+ @Mock private CommandInfo channelCommandInfo;
|
|
54
|
+ @Mock private Command command;
|
|
55
|
+ @Mock private Command channelCommand;
|
|
56
|
+ @Mock private FrameContainer container;
|
|
57
|
+ @Mock private Channel channel;
|
|
58
|
+ @Mock private Connection connection;
|
|
59
|
+ private TestCommandParser commandParser;
|
|
60
|
+ private TestCommandParser channelCommandParser;
|
47
|
61
|
|
48
|
62
|
@Before
|
49
|
|
- public void setup() throws InvalidIdentityFileException {
|
50
|
|
- MockitoAnnotations.initMocks(this);
|
51
|
|
- when(cm.getOptionChar("general", "silencechar")).thenReturn('.');
|
52
|
|
- when(cm.getOptionInt("general", "commandhistory")).thenReturn(10);
|
53
|
|
- when(cm.getOptionChar("general", "commandchar")).thenReturn('/');
|
54
|
|
- final ConfigBinder binder = new ConfigBinder(cm);
|
55
|
|
- when(cm.getBinder()).thenReturn(binder);
|
56
|
|
- commands = new CommandManager(serverManager);
|
57
|
|
- commands.initialise(cm);
|
58
|
|
- commands.registerCommand(new Echo(commands, windowManager), Echo.INFO);
|
|
63
|
+ public void setup() {
|
|
64
|
+ when(commandController.getCommandChar()).thenReturn('/');
|
|
65
|
+ when(commandController.getSilenceChar()).thenReturn('.');
|
|
66
|
+ when(commandController.isChannelCommand("channel")).thenReturn(true);
|
|
67
|
+
|
|
68
|
+ when(commandInfo.getName()).thenReturn("command");
|
|
69
|
+ when(channelCommandInfo.getName()).thenReturn("channel");
|
|
70
|
+
|
|
71
|
+ when(configProvider.getOptionInt("general", "commandhistory")).thenReturn(10);
|
|
72
|
+
|
|
73
|
+ when(container.getConnection()).thenReturn(connection);
|
|
74
|
+ when(connection.isValidChannelName("#channel1")).thenReturn(true);
|
|
75
|
+ when(connection.isValidChannelName("#channel2")).thenReturn(true);
|
|
76
|
+ when(connection.hasChannel("#channel1")).thenReturn(true);
|
|
77
|
+ when(connection.getChannel("#channel1")).thenReturn(channel);
|
|
78
|
+
|
|
79
|
+ commandParser = new TestCommandParser(configProvider, commandController);
|
|
80
|
+ commandParser.registerCommand(command, commandInfo);
|
|
81
|
+ commandParser.registerCommand(channelCommand, channelCommandInfo);
|
|
82
|
+
|
|
83
|
+ channelCommandParser = new TestCommandParser(configProvider, commandController);
|
|
84
|
+ channelCommandParser.registerCommand(channelCommand, channelCommandInfo);
|
|
85
|
+
|
|
86
|
+ when(channel.getCommandParser()).thenReturn(channelCommandParser);
|
59
|
87
|
}
|
60
|
88
|
|
61
|
89
|
@Test
|
62
|
|
- public void testBasicCommand() {
|
63
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
64
|
|
- tcp.parseCommand(null, "/echo this is a test");
|
65
|
|
-
|
66
|
|
- assertNull(tcp.nonCommandLine);
|
67
|
|
- assertNull(tcp.invalidCommand);
|
68
|
|
- assertNotNull(tcp.executedCommand);
|
69
|
|
- assertFalse(tcp.wasSilent);
|
70
|
|
- assertTrue(tcp.executedCommand instanceof Echo);
|
|
90
|
+ public void testParseCommandWithArguments() {
|
|
91
|
+ commandParser.parseCommand(null, "/command this is a test");
|
|
92
|
+
|
|
93
|
+ assertNull(commandParser.nonCommandLine);
|
|
94
|
+ assertNull(commandParser.invalidCommand);
|
|
95
|
+ assertFalse(commandParser.wasSilent);
|
|
96
|
+ assertSame(command, commandParser.executedCommand);
|
|
97
|
+ assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
|
71
|
98
|
}
|
72
|
99
|
|
73
|
100
|
@Test
|
74
|
|
- public void testBasicNoArgs() {
|
75
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
76
|
|
- tcp.parseCommand(null, "/echo");
|
77
|
|
-
|
78
|
|
- assertNull(tcp.nonCommandLine);
|
79
|
|
- assertNull(tcp.invalidCommand);
|
80
|
|
- assertNotNull(tcp.executedCommand);
|
81
|
|
- assertFalse(tcp.wasSilent);
|
82
|
|
- assertTrue(tcp.executedCommand instanceof Echo);
|
|
101
|
+ public void testParseCommandWithoutArguments() {
|
|
102
|
+ commandParser.parseCommand(null, "/command");
|
|
103
|
+
|
|
104
|
+ assertNull(commandParser.nonCommandLine);
|
|
105
|
+ assertNull(commandParser.invalidCommand);
|
|
106
|
+ assertFalse(commandParser.wasSilent);
|
|
107
|
+ assertSame(command, commandParser.executedCommand);
|
|
108
|
+ assertEquals("", commandParser.commandArgs.getArgumentsAsString());
|
83
|
109
|
}
|
84
|
110
|
|
85
|
111
|
@Test
|
86
|
|
- public void testSilentNoArgs() {
|
87
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
88
|
|
- tcp.parseCommand(null, "/.echo");
|
89
|
|
-
|
90
|
|
- assertNull(tcp.nonCommandLine);
|
91
|
|
- assertNull(tcp.invalidCommand);
|
92
|
|
- assertNotNull(tcp.executedCommand);
|
93
|
|
- assertTrue(tcp.wasSilent);
|
94
|
|
- assertTrue(tcp.executedCommand instanceof Echo);
|
|
112
|
+ public void testParseSilentCommandWithoutArguments() {
|
|
113
|
+ commandParser.parseCommand(null, "/.command");
|
|
114
|
+
|
|
115
|
+ assertNull(commandParser.nonCommandLine);
|
|
116
|
+ assertNull(commandParser.invalidCommand);
|
|
117
|
+ assertTrue(commandParser.wasSilent);
|
|
118
|
+ assertSame(command, commandParser.executedCommand);
|
|
119
|
+ assertEquals("", commandParser.commandArgs.getArgumentsAsString());
|
95
|
120
|
}
|
96
|
121
|
|
97
|
122
|
@Test
|
98
|
|
- public void testSilentCommand() {
|
99
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
100
|
|
- tcp.parseCommand(null, "/.echo this is a test");
|
101
|
|
-
|
102
|
|
- assertNull(tcp.nonCommandLine);
|
103
|
|
- assertNull(tcp.invalidCommand);
|
104
|
|
- assertNotNull(tcp.executedCommand);
|
105
|
|
- assertTrue(tcp.wasSilent);
|
106
|
|
- assertTrue(tcp.executedCommand instanceof Echo);
|
|
123
|
+ public void testParseSilentCommandWithArguments() {
|
|
124
|
+ commandParser.parseCommand(null, "/.command this is a test");
|
|
125
|
+
|
|
126
|
+ assertNull(commandParser.nonCommandLine);
|
|
127
|
+ assertNull(commandParser.invalidCommand);
|
|
128
|
+ assertTrue(commandParser.wasSilent);
|
|
129
|
+ assertSame(command, commandParser.executedCommand);
|
|
130
|
+ assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
|
107
|
131
|
}
|
108
|
132
|
|
109
|
133
|
@Test
|
110
|
|
- public void testNonExistantCommand() {
|
111
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
112
|
|
- tcp.parseCommand(null, "/foobar moo bar");
|
113
|
|
-
|
114
|
|
- assertNull(tcp.nonCommandLine);
|
115
|
|
- assertEquals("foobar", tcp.invalidCommand);
|
116
|
|
- assertNotNull(tcp.invalidCommand);
|
117
|
|
- assertNull(tcp.executedCommand);
|
118
|
|
- assertFalse(tcp.wasSilent);
|
|
134
|
+ public void testParseUnknownCommand() {
|
|
135
|
+ commandParser.parseCommand(null, "/foobar moo bar");
|
|
136
|
+
|
|
137
|
+ assertNull(commandParser.nonCommandLine);
|
|
138
|
+ assertEquals("foobar", commandParser.invalidCommand);
|
|
139
|
+ assertNull(commandParser.executedCommand);
|
|
140
|
+ assertFalse(commandParser.wasSilent);
|
119
|
141
|
}
|
120
|
142
|
|
121
|
143
|
@Test
|
122
|
|
- public void testEmptyCommand() {
|
123
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
124
|
|
- tcp.parseCommand(null, "/ moo bar");
|
125
|
|
-
|
126
|
|
- assertNull(tcp.nonCommandLine);
|
127
|
|
- assertEquals("", tcp.invalidCommand);
|
128
|
|
- assertNotNull(tcp.invalidCommand);
|
129
|
|
- assertNull(tcp.executedCommand);
|
130
|
|
- assertFalse(tcp.wasSilent);
|
|
144
|
+ public void testParseEmptyCommand() {
|
|
145
|
+ commandParser.parseCommand(null, "/ moo bar");
|
|
146
|
+
|
|
147
|
+ assertNull(commandParser.nonCommandLine);
|
|
148
|
+ assertEquals("", commandParser.invalidCommand);
|
|
149
|
+ assertNull(commandParser.executedCommand);
|
|
150
|
+ assertFalse(commandParser.wasSilent);
|
131
|
151
|
}
|
132
|
152
|
|
133
|
153
|
@Test
|
134
|
|
- public void testEmptySilentCommand() {
|
135
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
136
|
|
- tcp.parseCommand(null, "/. moo bar");
|
137
|
|
-
|
138
|
|
- assertNull(tcp.nonCommandLine);
|
139
|
|
- assertEquals("", tcp.invalidCommand);
|
140
|
|
- assertNotNull(tcp.invalidCommand);
|
141
|
|
- assertNull(tcp.executedCommand);
|
142
|
|
- assertFalse(tcp.wasSilent);
|
|
154
|
+ public void testParseEmptySilenceCommand() {
|
|
155
|
+ commandParser.parseCommand(null, "/. moo bar");
|
|
156
|
+
|
|
157
|
+ assertNull(commandParser.nonCommandLine);
|
|
158
|
+ assertEquals("", commandParser.invalidCommand);
|
|
159
|
+ assertNull(commandParser.executedCommand);
|
|
160
|
+ assertFalse(commandParser.wasSilent);
|
143
|
161
|
}
|
144
|
162
|
|
145
|
163
|
@Test
|
146
|
|
- public void testNonCommand() {
|
147
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
148
|
|
- tcp.parseCommand(null, "Foobar baz");
|
149
|
|
-
|
150
|
|
- assertNotNull(tcp.nonCommandLine);
|
151
|
|
- assertEquals("Foobar baz", tcp.nonCommandLine);
|
152
|
|
- assertNull(tcp.invalidCommand);
|
153
|
|
- assertNull(tcp.executedCommand);
|
154
|
|
- assertFalse(tcp.wasSilent);
|
|
164
|
+ public void testParseNonCommand() {
|
|
165
|
+ commandParser.parseCommand(null, "Foobar baz");
|
|
166
|
+
|
|
167
|
+ assertEquals("Foobar baz", commandParser.nonCommandLine);
|
|
168
|
+ assertNull(commandParser.invalidCommand);
|
|
169
|
+ assertNull(commandParser.executedCommand);
|
|
170
|
+ assertFalse(commandParser.wasSilent);
|
155
|
171
|
}
|
156
|
172
|
|
157
|
173
|
@Test
|
158
|
|
- public void testCommandHistory() {
|
159
|
|
- final TestCommandParser tcp = new TestCommandParser(cm, commands);
|
160
|
|
- tcp.parseCommand(null, "/echo this is a test");
|
|
174
|
+ public void testGetCommandTime() {
|
|
175
|
+ commandParser.parseCommand(null, "/command this is a test");
|
161
|
176
|
|
162
|
|
- final long time1 = tcp.getCommandTime("echo this is a test");
|
|
177
|
+ final long time1 = commandParser.getCommandTime("command this is a test");
|
163
|
178
|
assertTrue(time1 > 0);
|
164
|
179
|
|
165
|
|
- tcp.parseCommand( null, "/echo this is a test");
|
166
|
|
- final long time2 = tcp.getCommandTime("echo this is a test");
|
|
180
|
+ commandParser.parseCommand(null, "/command this is a test");
|
|
181
|
+ final long time2 = commandParser.getCommandTime("command this is a test");
|
167
|
182
|
assertTrue(time2 > 0);
|
168
|
183
|
assertTrue(time2 >= time1);
|
169
|
184
|
|
170
|
|
- assertEquals(0L, tcp.getCommandTime("echo"));
|
|
185
|
+ assertEquals(0L, commandParser.getCommandTime("command"));
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ @Test
|
|
189
|
+ public void testParseChannelCommandWithArguments() {
|
|
190
|
+ when(container.getConnection()).thenReturn(connection);
|
|
191
|
+ commandParser.parseCommand(container, "/channel #channel1 this is a test");
|
|
192
|
+
|
|
193
|
+ assertNull(channelCommandParser.nonCommandLine);
|
|
194
|
+ assertNull(channelCommandParser.invalidCommand);
|
|
195
|
+ assertFalse(channelCommandParser.wasSilent);
|
|
196
|
+ assertSame(channelCommand, channelCommandParser.executedCommand);
|
|
197
|
+ assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ @Test
|
|
201
|
+ public void testParseChannelCommandWithoutArguments() {
|
|
202
|
+ when(container.getConnection()).thenReturn(connection);
|
|
203
|
+ commandParser.parseCommand(container, "/channel #channel1");
|
|
204
|
+
|
|
205
|
+ assertNull(channelCommandParser.nonCommandLine);
|
|
206
|
+ assertNull(channelCommandParser.invalidCommand);
|
|
207
|
+ assertFalse(channelCommandParser.wasSilent);
|
|
208
|
+ assertSame(channelCommand, channelCommandParser.executedCommand);
|
|
209
|
+ assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ @Test
|
|
213
|
+ public void testParseSilencedChannelCommandWithArguments() {
|
|
214
|
+ when(container.getConnection()).thenReturn(connection);
|
|
215
|
+ commandParser.parseCommand(container, "/.channel #channel1 this is a test");
|
|
216
|
+
|
|
217
|
+ assertNull(channelCommandParser.nonCommandLine);
|
|
218
|
+ assertNull(channelCommandParser.invalidCommand);
|
|
219
|
+ assertTrue(channelCommandParser.wasSilent);
|
|
220
|
+ assertSame(channelCommand, channelCommandParser.executedCommand);
|
|
221
|
+ assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
|
|
222
|
+ }
|
|
223
|
+
|
|
224
|
+ @Test
|
|
225
|
+ public void testParseSilencedChannelCommandWithoutArguments() {
|
|
226
|
+ when(container.getConnection()).thenReturn(connection);
|
|
227
|
+ commandParser.parseCommand(container, "/.channel #channel1");
|
|
228
|
+
|
|
229
|
+ assertNull(channelCommandParser.nonCommandLine);
|
|
230
|
+ assertNull(channelCommandParser.invalidCommand);
|
|
231
|
+ assertTrue(channelCommandParser.wasSilent);
|
|
232
|
+ assertSame(channelCommand, channelCommandParser.executedCommand);
|
|
233
|
+ assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
|
171
|
234
|
}
|
172
|
235
|
|
173
|
236
|
}
|