Browse Source

CommandParser unit test

git-svn-id: http://svn.dmdirc.com/trunk@3827 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
d1fe7ee2d0
1 changed files with 198 additions and 0 deletions
  1. 198
    0
      test/com/dmdirc/commandparser/parsers/CommandParserTest.java

+ 198
- 0
test/com/dmdirc/commandparser/parsers/CommandParserTest.java View File

@@ -0,0 +1,198 @@
1
+/*
2
+ * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.commandparser.parsers;
23
+
24
+import com.dmdirc.commandparser.CommandManager;
25
+import com.dmdirc.commandparser.commands.Command;
26
+import com.dmdirc.commandparser.commands.global.Echo;
27
+import com.dmdirc.ui.interfaces.InputWindow;
28
+
29
+import java.util.Arrays;
30
+
31
+import org.junit.BeforeClass;
32
+import org.junit.Test;
33
+import static org.junit.Assert.*;
34
+
35
+public class CommandParserTest {
36
+
37
+    @BeforeClass
38
+    public static void setUpClass() throws Exception {
39
+        CommandManager.initCommands();
40
+    }
41
+
42
+    @Test
43
+    public void testBasicCommand() {
44
+        final TestCommandParser tcp = new TestCommandParser();
45
+        tcp.parseCommand(null, "/echo this is a test");
46
+
47
+        assertNull(tcp.nonCommandLine);
48
+        assertNull(tcp.invalidCommand);
49
+        assertNotNull(tcp.executedCommand);
50
+        assertFalse(tcp.wasSilent);
51
+        assertTrue(Arrays.equals(tcp.commandArgs, new String[]{"this", "is", "a", "test"}));
52
+        assertTrue(tcp.executedCommand instanceof Echo);
53
+    }
54
+
55
+    @Test
56
+    public void testBasicNoArgs() {
57
+        final TestCommandParser tcp = new TestCommandParser();
58
+        tcp.parseCommand(null, "/echo");
59
+
60
+        assertNull(tcp.nonCommandLine);
61
+        assertNull(tcp.invalidCommand);
62
+        assertNotNull(tcp.executedCommand);
63
+        assertFalse(tcp.wasSilent);
64
+        assertTrue(Arrays.equals(tcp.commandArgs, new String[0]));
65
+        assertTrue(tcp.executedCommand instanceof Echo);
66
+    }
67
+
68
+    @Test
69
+    public void testSilentNoArgs() {
70
+        final TestCommandParser tcp = new TestCommandParser();
71
+        tcp.parseCommand(null, "/.echo");
72
+
73
+        assertNull(tcp.nonCommandLine);
74
+        assertNull(tcp.invalidCommand);
75
+        assertNotNull(tcp.executedCommand);
76
+        assertTrue(tcp.wasSilent);
77
+        assertTrue(Arrays.equals(tcp.commandArgs, new String[0]));
78
+        assertTrue(tcp.executedCommand instanceof Echo);
79
+    }
80
+
81
+    @Test
82
+    public void testSilentCommand() {
83
+        final TestCommandParser tcp = new TestCommandParser();
84
+        tcp.parseCommand(null, "/.echo this is a test");
85
+
86
+        assertNull(tcp.nonCommandLine);
87
+        assertNull(tcp.invalidCommand);
88
+        assertNotNull(tcp.executedCommand);
89
+        assertTrue(tcp.wasSilent);
90
+        assertTrue(Arrays.equals(tcp.commandArgs, new String[]{"this", "is", "a", "test"}));
91
+        assertTrue(tcp.executedCommand instanceof Echo);
92
+    }
93
+
94
+    @Test
95
+    public void testNonExistantCommand() {
96
+        final TestCommandParser tcp = new TestCommandParser();
97
+        tcp.parseCommand(null, "/foobar moo bar");
98
+
99
+        assertNull(tcp.nonCommandLine);
100
+        assertEquals("foobar", tcp.invalidCommand);
101
+        assertNotNull(tcp.invalidCommand);
102
+        assertNull(tcp.executedCommand);
103
+        assertFalse(tcp.wasSilent);
104
+    }
105
+
106
+    @Test
107
+    public void testEmptyCommand() {
108
+        final TestCommandParser tcp = new TestCommandParser();
109
+        tcp.parseCommand(null, "/ moo bar");
110
+
111
+        assertNull(tcp.nonCommandLine);
112
+        assertEquals("", tcp.invalidCommand);
113
+        assertNotNull(tcp.invalidCommand);
114
+        assertNull(tcp.executedCommand);
115
+        assertFalse(tcp.wasSilent);
116
+    }
117
+    
118
+    @Test
119
+    public void testEmptySilentCommand() {
120
+        final TestCommandParser tcp = new TestCommandParser();
121
+        tcp.parseCommand(null, "/. moo bar");
122
+
123
+        assertNull(tcp.nonCommandLine);
124
+        assertEquals("", tcp.invalidCommand);
125
+        assertNotNull(tcp.invalidCommand);
126
+        assertNull(tcp.executedCommand);
127
+        assertFalse(tcp.wasSilent);
128
+    }
129
+    
130
+    @Test
131
+    public void testNonCommand() {
132
+        final TestCommandParser tcp = new TestCommandParser();
133
+        tcp.parseCommand(null, "Foobar baz");
134
+
135
+        assertNotNull(tcp.nonCommandLine);
136
+        assertEquals("Foobar baz", tcp.nonCommandLine);
137
+        assertNull(tcp.invalidCommand);
138
+        assertNull(tcp.executedCommand);
139
+        assertFalse(tcp.wasSilent);
140
+    }
141
+    
142
+    @Test
143
+    public void testCommandHistory() {
144
+        final TestCommandParser tcp = new TestCommandParser();
145
+        tcp.parseCommand(null, "/echo this is a test");
146
+
147
+        final long time1 = tcp.getCommandTime("echo this is a test");
148
+        assertTrue(time1 > 0);
149
+        
150
+        tcp.parseCommand(null, "/echo this is a test");
151
+        final long time2 = tcp.getCommandTime("echo this is a test");
152
+        assertTrue(time2 > 0);
153
+        assertTrue(time2 >= time1);
154
+        
155
+        assertEquals(0l, tcp.getCommandTime("echo"));
156
+    }    
157
+
158
+    public static junit.framework.Test suite() {
159
+        return new junit.framework.JUnit4TestAdapter(CommandParserTest.class);
160
+    }
161
+
162
+    private class TestCommandParser extends CommandParser {
163
+
164
+        public String nonCommandLine;
165
+
166
+        public Command executedCommand;
167
+        public boolean wasSilent;
168
+        public String[] commandArgs;
169
+
170
+        public String invalidCommand;
171
+
172
+        @Override
173
+        protected void loadCommands() {
174
+            CommandManager.loadGlobalCommands(this);
175
+        }
176
+
177
+        @Override
178
+        protected void executeCommand(InputWindow origin, boolean isSilent,
179
+                Command command, String... args) {
180
+            executedCommand = command;
181
+            wasSilent = isSilent;
182
+            commandArgs = args;
183
+        }
184
+
185
+        @Override
186
+        protected void handleNonCommand(InputWindow origin, String line) {
187
+            nonCommandLine = line;
188
+        }
189
+
190
+        @Override
191
+        protected void handleInvalidCommand(InputWindow origin, String command,
192
+                                            String... args) {
193
+            invalidCommand = command;
194
+        }
195
+
196
+    }
197
+
198
+}

Loading…
Cancel
Save