ソースを参照

Initial work on moving to CommandArguments

tags/0.6.3m1rc1
Chris Smith 15年前
コミット
c33fd01752

+ 127
- 10
src/com/dmdirc/commandparser/CommandArguments.java ファイルの表示

@@ -22,9 +22,17 @@
22 22
 
23 23
 package com.dmdirc.commandparser;
24 24
 
25
+import java.util.Arrays;
26
+import java.util.regex.Matcher;
27
+import java.util.regex.Pattern;
28
+
25 29
 /**
26
- * Represents a command and its arguments.
30
+ * Represents a command and its arguments. In this class, input is split into
31
+ * 'words' which are separated by any number of whitespace characters;
32
+ * 'arguments' are the same but exclude the first word, which will normally be
33
+ * the command name.
27 34
  *
35
+ * @since 0.6.3
28 36
  * @author chris
29 37
  */
30 38
 public class CommandArguments {
@@ -32,31 +40,140 @@ public class CommandArguments {
32 40
     /** The raw line that was input. */
33 41
     private final String line;
34 42
 
35
-    private String[] arguments;
43
+    /** The line split into whitespace-delimited words. */
44
+    private String[] words;
36 45
 
46
+    /**
47
+     * Creates a new command arguments parser for the specified line.
48
+     *
49
+     * @param line The line to be parsed
50
+     */
37 51
     public CommandArguments(final String line) {
38 52
         this.line = line;
39 53
     }
40
-    
41
-    public synchronized String[] getArguments() {
42
-        if (arguments == null) {
43
-            parse();
44
-        }
54
+
55
+    /**
56
+     * Retrieves the raw line that was input, including any command character(s)
57
+     * and names.
58
+     *
59
+     * @return The raw line entered
60
+     */
61
+    public String getLine() {
62
+        return line;
63
+    }
64
+
65
+    /**
66
+     * Retrieves the raw line that was input, including the command name but
67
+     * stripped of any command characters.
68
+     *
69
+     * @return The raw line entered, without command chars
70
+     */
71
+    public String getStrippedLine() {
72
+        final int offset = isCommand() ? isSilent() ? 2 : 1 : 0;
73
+
74
+        return line.substring(offset);
75
+    }
76
+
77
+    /**
78
+     * Retrieves the input split into distinct, whitespace-separated words. The
79
+     * first item in the array will be the command name complete with any
80
+     * command characters.
81
+     *
82
+     * @return An array of 'words' that make up the input
83
+     */
84
+    public String[] getWords() {
85
+        parse();
86
+        
87
+        return words;
88
+    }
89
+
90
+    /**
91
+     * Retrieves the arguments to the command split into disticnt,
92
+     * whitespace-separated words.
93
+     *
94
+     * @return An array of 'words' that make up the command's arguments
95
+     */
96
+    public String[] getArguments() {
97
+        parse();
98
+
99
+        return Arrays.copyOfRange(words, 1, words.length);
100
+    }
101
+
102
+    /**
103
+     * Retrieves all the arguments to the command (i.e., not including the
104
+     * command name) with their original whitespace separation preserved.
105
+     *
106
+     * @return A String representation of the command arguments
107
+     */
108
+    public String getArgumentsAsString() {
109
+        parse();
45 110
         
46
-        return arguments;
111
+        return getWordsAsString(1);
47 112
     }
48 113
 
49
-    protected void parse() {
50
-        arguments = line.split("\\s+");
114
+    /**
115
+     * Retrieves the specified words with their original whitespace separation
116
+     * preserved.
117
+     *
118
+     * @param start The index of the first word to include (starting at 0)
119
+     * @return A String representation of the requested words
120
+     */
121
+    public String getWordsAsString(final int start) {
122
+        return getWordsAsString(start, words.length);
123
+    }
124
+
125
+    /**
126
+     * Retrieves the specified words with their original whitespace separation
127
+     * preserved.
128
+     *
129
+     * @param start The index of the first word to include (starting at 0)
130
+     * @param end The index of the last word to include
131
+     * @return A String representation of the requested words
132
+     */
133
+    public String getWordsAsString(final int start, final int end) {
134
+        final Pattern pattern = Pattern.compile("(\\S+\\s*){" + (start) + "}"
135
+                + "((\\S+\\s*){" + (end - start) + "}).*?");
136
+        final Matcher matcher = pattern.matcher(line);
137
+
138
+        return matcher.matches() ? matcher.group(2) : "";
51 139
     }
52 140
 
141
+    /**
142
+     * Parses the input into a set of words, if it has not been done before.
143
+     */
144
+    protected synchronized void parse() {
145
+        if (words == null) {
146
+            words = line.split("\\s+");
147
+        }
148
+    }
149
+
150
+    /**
151
+     * Determines if the input was a command or not.
152
+     *
153
+     * @return True if the input was a command, false otherwise
154
+     */
53 155
     public boolean isCommand() {
54 156
         return !line.isEmpty() && line.charAt(0) == CommandManager.getCommandChar();
55 157
     }
56 158
 
159
+    /**
160
+     * Determines if the input was a silenced command or not.
161
+     *
162
+     * @return True if the input was a silenced command, false otherwise
163
+     */
57 164
     public boolean isSilent() {
58 165
         return isCommand() && line.length() >= 2 &&
59 166
                 line.charAt(1) == CommandManager.getSilenceChar();
60 167
     }
61 168
 
169
+    /**
170
+     * Retrieves the name of the command that was used.
171
+     *
172
+     * @return The command name used
173
+     */
174
+    public String getCommandName() {
175
+        final int offset = isCommand() ? isSilent() ? 2 : 1 : 0;
176
+        return getWords()[0].substring(offset);
177
+    }
178
+
62 179
 }

+ 4
- 1
src/com/dmdirc/commandparser/commands/ChannelCommand.java ファイルの表示

@@ -24,6 +24,7 @@ package com.dmdirc.commandparser.commands;
24 24
 
25 25
 import com.dmdirc.Channel;
26 26
 import com.dmdirc.Server;
27
+import com.dmdirc.commandparser.CommandArguments;
27 28
 import com.dmdirc.commandparser.CommandInfo;
28 29
 import com.dmdirc.commandparser.CommandType;
29 30
 import com.dmdirc.ui.interfaces.InputWindow;
@@ -36,14 +37,16 @@ public abstract class ChannelCommand extends Command implements CommandInfo {
36 37
     
37 38
     /**
38 39
      * Executes this command.
40
+     *
39 41
      * @param origin The window in which the command was typed
40 42
      * @param server The server instance that this command is being executed on
41 43
      * @param channel The channel instance that this command is being executed on
42 44
      * @param isSilent Whether this command is silenced or not
43 45
      * @param args Arguments passed to this command
46
+     * @since 0.6.3
44 47
      */
45 48
     public abstract void execute(InputWindow origin, Server server, Channel channel,
46
-            boolean isSilent, String... args);
49
+            boolean isSilent, CommandArguments args);
47 50
 
48 51
     /** {@inheritDoc} */
49 52
     @Override

+ 3
- 1
src/com/dmdirc/commandparser/commands/ChatCommand.java ファイルの表示

@@ -24,6 +24,7 @@ package com.dmdirc.commandparser.commands;
24 24
 
25 25
 import com.dmdirc.MessageTarget;
26 26
 import com.dmdirc.Server;
27
+import com.dmdirc.commandparser.CommandArguments;
27 28
 import com.dmdirc.commandparser.CommandInfo;
28 29
 import com.dmdirc.commandparser.CommandType;
29 30
 import com.dmdirc.ui.interfaces.InputWindow;
@@ -44,9 +45,10 @@ public abstract class ChatCommand extends Command implements CommandInfo {
44 45
      * @param target The target of this command
45 46
      * @param isSilent Whether this command is silenced or not
46 47
      * @param args Arguments passed to this command
48
+     * @since 0.6.3
47 49
      */
48 50
     public abstract void execute(InputWindow origin, Server server, MessageTarget target,
49
-            boolean isSilent, String... args);
51
+            boolean isSilent, CommandArguments args);
50 52
 
51 53
     /** {@inheritDoc} */
52 54
     @Override

+ 4
- 0
src/com/dmdirc/commandparser/commands/Command.java ファイルの表示

@@ -44,7 +44,9 @@ public abstract class Command {
44 44
      * @param offset The index to start at
45 45
      * @param args The arguments to implode
46 46
      * @return A string containing each argument seperated by a space
47
+     * @deprecated Should be no need for this now
47 48
      */
49
+    @Deprecated
48 50
     protected static final String implodeArgs(final int offset, final String... args) {
49 51
         String res = "";
50 52
         for (int i = offset; i < args.length; i++) {
@@ -61,7 +63,9 @@ public abstract class Command {
61 63
      * Implodes the given list of arguments.
62 64
      * @param args The arguments to implode
63 65
      * @return A string containing each argument seperated by a space
66
+     * @deprecated Should be no need for this now
64 67
      */
68
+    @Deprecated
65 69
     protected static final String implodeArgs(final String... args) {
66 70
         return implodeArgs(0, args);
67 71
     } 

+ 3
- 1
src/com/dmdirc/commandparser/commands/ExternalCommand.java ファイルの表示

@@ -22,6 +22,7 @@
22 22
 package com.dmdirc.commandparser.commands;
23 23
 
24 24
 import com.dmdirc.Server;
25
+import com.dmdirc.commandparser.CommandArguments;
25 26
 import com.dmdirc.ui.interfaces.InputWindow;
26 27
 
27 28
 /**
@@ -40,8 +41,9 @@ public interface ExternalCommand {
40 41
      * @param channel The name of the channel the command is being executed for
41 42
      * @param isSilent Whether this command is silenced or not
42 43
      * @param args Arguments passed to this command
44
+     * @since 0.6.3
43 45
      */
44 46
     void execute(InputWindow origin, Server server, String channel,
45
-            boolean isSilent, String... args);    
47
+            boolean isSilent, CommandArguments args);
46 48
 
47 49
 }

+ 5
- 1
src/com/dmdirc/commandparser/commands/GlobalCommand.java ファイルの表示

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.commandparser.commands;
24 24
 
25
+import com.dmdirc.commandparser.CommandArguments;
25 26
 import com.dmdirc.commandparser.CommandInfo;
26 27
 import com.dmdirc.commandparser.CommandType;
27 28
 import com.dmdirc.ui.interfaces.InputWindow;
@@ -29,6 +30,7 @@ import com.dmdirc.ui.interfaces.InputWindow;
29 30
 /**
30 31
  * Represents a generic global command. Global commands are associated with
31 32
  * no servers.
33
+ *
32 34
  * @author chris
33 35
  */
34 36
 public abstract class GlobalCommand extends Command implements CommandInfo {
@@ -36,11 +38,13 @@ public abstract class GlobalCommand extends Command implements CommandInfo {
36 38
     /**
37 39
      * Executes this command. Note that for global commands, origin may be
38 40
      * null.
41
+     *
39 42
      * @param origin The window in which the command was typed
40 43
      * @param isSilent Whether this command is silenced or not
41 44
      * @param args Arguments passed to this command
45
+     * @since 0.6.3
42 46
      */
43
-    public abstract void execute(InputWindow origin, boolean isSilent, String ... args);
47
+    public abstract void execute(InputWindow origin, boolean isSilent, CommandArguments args);
44 48
 
45 49
     /** {@inheritDoc} */
46 50
     @Override

+ 5
- 1
src/com/dmdirc/commandparser/commands/QueryCommand.java ファイルの表示

@@ -24,26 +24,30 @@ package com.dmdirc.commandparser.commands;
24 24
 
25 25
 import com.dmdirc.Query;
26 26
 import com.dmdirc.Server;
27
+import com.dmdirc.commandparser.CommandArguments;
27 28
 import com.dmdirc.commandparser.CommandInfo;
28 29
 import com.dmdirc.commandparser.CommandType;
29 30
 import com.dmdirc.ui.interfaces.InputWindow;
30 31
 
31 32
 /**
32 33
  * Represents a command which can be performed only in the context of a query.
34
+ *
33 35
  * @author chris
34 36
  */
35 37
 public abstract class QueryCommand extends Command implements CommandInfo {
36 38
         
37 39
     /**
38 40
      * Executes this command.
41
+     *
39 42
      * @param origin The window in which the command was typed
40 43
      * @param server The server instance that this command is being executed on
41 44
      * @param query The query object that the commadparser is associated with
42 45
      * @param isSilent Whether this command is silenced or not
43 46
      * @param args Arguments passed to this command
47
+     * @since 0.6.3
44 48
      */
45 49
     public abstract void execute(InputWindow origin, Server server, Query query,
46
-            boolean isSilent, String... args);
50
+            boolean isSilent, CommandArguments args);
47 51
 
48 52
     /** {@inheritDoc} */
49 53
     @Override

+ 5
- 1
src/com/dmdirc/commandparser/commands/ServerCommand.java ファイルの表示

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.commandparser.commands;
24 24
 
25 25
 import com.dmdirc.Server;
26
+import com.dmdirc.commandparser.CommandArguments;
26 27
 import com.dmdirc.commandparser.CommandInfo;
27 28
 import com.dmdirc.commandparser.CommandType;
28 29
 import com.dmdirc.ui.interfaces.InputWindow;
@@ -30,19 +31,22 @@ import com.dmdirc.ui.interfaces.InputWindow;
30 31
 /**
31 32
  * Represents a generic server command. Server commands are associated with
32 33
  * a server instance.
34
+ *
33 35
  * @author chris
34 36
  */
35 37
 public abstract class ServerCommand extends Command implements CommandInfo {
36 38
     
37 39
     /**
38 40
      * Executes this command.
41
+     *
39 42
      * @param origin The window in which the command was typed
40 43
      * @param server The server instance that this command is being executed on
41 44
      * @param isSilent Whether this command is silenced or not
42 45
      * @param args Arguments passed to this command
46
+     * @since 0.6.3
43 47
      */
44 48
     public abstract void execute(InputWindow origin, Server server,
45
-            boolean isSilent, String ... args);
49
+            boolean isSilent, CommandArguments args);
46 50
 
47 51
     /** {@inheritDoc} */
48 52
     @Override

+ 2
- 1
src/com/dmdirc/commandparser/parsers/ChannelCommandParser.java ファイルの表示

@@ -24,6 +24,7 @@ package com.dmdirc.commandparser.parsers;
24 24
 
25 25
 import com.dmdirc.Channel;
26 26
 import com.dmdirc.Server;
27
+import com.dmdirc.commandparser.CommandArguments;
27 28
 import com.dmdirc.commandparser.CommandManager;
28 29
 import com.dmdirc.commandparser.CommandType;
29 30
 import com.dmdirc.commandparser.commands.ChannelCommand;
@@ -78,7 +79,7 @@ public final class ChannelCommandParser extends CommandParser {
78 79
     /** {@inheritDoc} */
79 80
     @Override
80 81
     protected void executeCommand(final InputWindow origin,
81
-            final boolean isSilent, final Command command, final String... args) {
82
+            final boolean isSilent, final Command command, final CommandArguments args) {
82 83
         if (command instanceof ChannelCommand) {
83 84
             ((ChannelCommand) command).execute(origin, server, channel, isSilent, args);
84 85
         } else if (command instanceof ChatCommand) {

+ 26
- 48
src/com/dmdirc/commandparser/parsers/CommandParser.java ファイルの表示

@@ -25,6 +25,7 @@ package com.dmdirc.commandparser.parsers;
25 25
 import com.dmdirc.Server;
26 26
 import com.dmdirc.actions.ActionManager;
27 27
 import com.dmdirc.actions.CoreActionType;
28
+import com.dmdirc.commandparser.CommandArguments;
28 29
 import com.dmdirc.commandparser.CommandInfo;
29 30
 import com.dmdirc.commandparser.CommandManager;
30 31
 import com.dmdirc.commandparser.CommandType;
@@ -108,67 +109,42 @@ public abstract class CommandParser implements Serializable {
108 109
      */
109 110
     public final void parseCommand(final InputWindow origin,
110 111
             final String line, final boolean parseChannel) {
111
-        if (line.isEmpty()) {
112
-            return;
113
-        }
114
-
115
-        if (line.charAt(0) == CommandManager.getCommandChar()) {
116
-            int offset = 1;
117
-            boolean silent = false;
118
-
119
-            if (line.length() > offset && line.charAt(offset) == CommandManager.getSilenceChar()) {
120
-                silent = true;
121
-                offset++;
122
-            }
112
+        final CommandArguments args = new CommandArguments(line);
123 113
 
124
-            final String[] args = line.split(" ");
125
-            final String command = args[0].substring(offset);
126
-            String[] comargs;
114
+        if (args.isCommand()) {
115
+            final boolean silent = args.isSilent();
116
+            final String command = args.getCommandName();
117
+            final String[] cargs = args.getArguments();
127 118
 
128
-            if (args.length >= 2 && parseChannel && origin != null
119
+            if (args.getArguments().length > 0 && parseChannel && origin != null
129 120
                     && origin.getContainer() != null
130 121
                     && origin.getContainer().getServer() != null
131
-                    && origin.getContainer().getServer().isValidChannelName(args[1])
122
+                    && origin.getContainer().getServer().isValidChannelName(cargs[0])
132 123
                     && CommandManager.isChannelCommand(command)) {
133 124
                 final Server server = origin.getContainer().getServer();
134 125
 
135
-                if (server.hasChannel(args[1])) {
136
-
137
-                    final StringBuilder newLine = new StringBuilder();
138
-                    for (int i = 0; i < args.length; i++) {
139
-                        if (i == 1) { continue; }
140
-                        newLine.append(" ").append(args[i]);
141
-                    }
142
-
143
-                    server.getChannel(args[1]).getFrame().getCommandParser()
144
-                            .parseCommand(origin, newLine.substring(1), false);
145
-
126
+                if (server.hasChannel(cargs[0])) {
127
+                    server.getChannel(cargs[0]).getFrame().getCommandParser()
128
+                            .parseCommand(origin, args.getWordsAsString(2), false);
146 129
                     return;
147 130
                 } else {
148 131
                     final Command actCommand = CommandManager.getCommand(
149 132
                             CommandType.TYPE_CHANNEL, command);
150 133
 
151 134
                     if (actCommand instanceof ExternalCommand) {
152
-                        comargs = new String[args.length - 2];
153
-                        System.arraycopy(args, 2, comargs, 0, args.length - 2);
154
-
155
-                        ((ExternalCommand) actCommand).execute(origin, server,
156
-                                args[1], silent, comargs);
135
+                        ((ExternalCommand) actCommand).execute(
136
+                                origin, server, cargs[0], silent,
137
+                                new CommandArguments(args.getWordsAsString(2)));
157 138
                         return;
158 139
                     }
159 140
                 }
160 141
             }
161 142
 
162
-            comargs = new String[args.length - 1];
163
-            System.arraycopy(args, 1, comargs, 0, args.length - 1);
164
-
165
-            final String signature = command;
166
-
167
-            if (commands.containsKey(signature.toLowerCase())) {
168
-                addHistory(line.substring(offset));
169
-                executeCommand(origin, silent, commands.get(signature.toLowerCase()), comargs);
143
+            if (commands.containsKey(command.toLowerCase())) {
144
+                addHistory(args.getStrippedLine());
145
+                executeCommand(origin, silent, commands.get(command.toLowerCase()), args);
170 146
            } else {
171
-                handleInvalidCommand(origin, command, comargs);
147
+                handleInvalidCommand(origin, args);
172 148
             }
173 149
         } else {
174 150
             handleNonCommand(origin, line);
@@ -237,30 +213,32 @@ public abstract class CommandParser implements Serializable {
237 213
      * @param isSilent Whether the command is being silenced or not
238 214
      * @param command The command to be executed
239 215
      * @param args The arguments to the command
216
+     * @since 0.6.3
240 217
      */
241 218
     protected abstract void executeCommand(final InputWindow origin,
242
-            final boolean isSilent, final Command command, final String... args);
219
+            final boolean isSilent, final Command command, final CommandArguments args);
243 220
 
244 221
     /**
245 222
      * Called when the user attempted to issue a command (i.e., used the command
246 223
      * character) that wasn't found. It could be that the command has a different
247 224
      * arity, or that it plain doesn't exist.
225
+     *
248 226
      * @param origin The window in which the command was typed
249
-     * @param command The command the user tried to execute
250 227
      * @param args The arguments passed to the command
228
+     * @since 0.6.3
251 229
      */
252 230
     protected void handleInvalidCommand(final InputWindow origin,
253
-            final String command, final String... args) {
231
+            final CommandArguments args) {
254 232
         if (origin == null) {
255 233
             ActionManager.processEvent(CoreActionType.UNKNOWN_COMMAND, null,
256
-                    null, command, args);
234
+                    null, args.getCommandName(), args.getArguments());
257 235
         } else {
258 236
             final StringBuffer buff = new StringBuffer("unknownCommand");
259 237
 
260 238
             ActionManager.processEvent(CoreActionType.UNKNOWN_COMMAND, buff,
261
-                    origin.getContainer(), command, args);
239
+                    origin.getContainer(), args.getCommandName(), args.getArguments());
262 240
 
263
-            origin.addLine(buff, command);
241
+            origin.addLine(buff, args.getCommandName());
264 242
         }
265 243
     }
266 244
 

+ 2
- 1
src/com/dmdirc/commandparser/parsers/GlobalCommandParser.java ファイルの表示

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.commandparser.parsers;
24 24
 
25
+import com.dmdirc.commandparser.CommandArguments;
25 26
 import com.dmdirc.commandparser.CommandManager;
26 27
 import com.dmdirc.commandparser.CommandType;
27 28
 import com.dmdirc.commandparser.commands.Command;
@@ -76,7 +77,7 @@ public final class GlobalCommandParser extends CommandParser {
76 77
     /** {@inheritDoc} */
77 78
     @Override
78 79
     protected void executeCommand(final InputWindow origin,
79
-            final boolean isSilent, final Command command, final String... args) {
80
+            final boolean isSilent, final Command command, final CommandArguments args) {
80 81
         ((GlobalCommand) command).execute(origin, isSilent, args);
81 82
     }
82 83
     

+ 2
- 1
src/com/dmdirc/commandparser/parsers/QueryCommandParser.java ファイルの表示

@@ -24,6 +24,7 @@ package com.dmdirc.commandparser.parsers;
24 24
 
25 25
 import com.dmdirc.Query;
26 26
 import com.dmdirc.Server;
27
+import com.dmdirc.commandparser.CommandArguments;
27 28
 import com.dmdirc.commandparser.CommandManager;
28 29
 import com.dmdirc.commandparser.CommandType;
29 30
 import com.dmdirc.commandparser.commands.ChatCommand;
@@ -78,7 +79,7 @@ public final class QueryCommandParser extends CommandParser {
78 79
     /** {@inheritDoc} */
79 80
     @Override
80 81
     protected void executeCommand(final InputWindow origin,
81
-            final boolean isSilent, final Command command, final String... args) {
82
+            final boolean isSilent, final Command command, final CommandArguments args) {
82 83
         if (command instanceof QueryCommand) {
83 84
             ((QueryCommand) command).execute(origin, server, query, isSilent, args);
84 85
         } else if (command instanceof ChatCommand) {

+ 2
- 1
src/com/dmdirc/commandparser/parsers/ServerCommandParser.java ファイルの表示

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.commandparser.parsers;
24 24
 
25 25
 import com.dmdirc.Server;
26
+import com.dmdirc.commandparser.CommandArguments;
26 27
 import com.dmdirc.commandparser.CommandManager;
27 28
 import com.dmdirc.commandparser.CommandType;
28 29
 import com.dmdirc.commandparser.commands.Command;
@@ -67,7 +68,7 @@ public final class ServerCommandParser extends CommandParser {
67 68
     /** {@inheritDoc} */
68 69
     @Override
69 70
     protected void executeCommand(final InputWindow origin,
70
-            final boolean isSilent, final Command command, final String... args) {
71
+            final boolean isSilent, final Command command, final CommandArguments args) {
71 72
         if (command instanceof ServerCommand) {
72 73
             ((ServerCommand) command).execute(origin, server, isSilent, args);
73 74
         } else {

+ 98
- 0
test/com/dmdirc/commandparser/CommandArgumentsTest.java ファイルの表示

@@ -0,0 +1,98 @@
1
+/*
2
+ * Copyright (c) 2006-2009 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
+
23
+package com.dmdirc.commandparser;
24
+
25
+import com.dmdirc.config.IdentityManager;
26
+
27
+import java.util.Arrays;
28
+import org.junit.BeforeClass;
29
+import org.junit.Test;
30
+import static org.junit.Assert.*;
31
+
32
+public class CommandArgumentsTest {
33
+
34
+    @BeforeClass
35
+    public static void beforeClass() {
36
+        IdentityManager.load();
37
+    }
38
+
39
+    @Test
40
+    public void testIsCommand() {
41
+        assertTrue(new CommandArguments(CommandManager.getCommandChar() + "").isCommand());
42
+        assertTrue(new CommandArguments(CommandManager.getCommandChar() + "foo bar").isCommand());
43
+        assertFalse(new CommandArguments(" " + CommandManager.getCommandChar()).isCommand());
44
+        assertFalse(new CommandArguments("").isCommand());
45
+        assertFalse(new CommandArguments("foo").isCommand());
46
+    }
47
+
48
+    @Test
49
+    public void testIsSilent() {
50
+        final char c = CommandManager.getCommandChar();
51
+        final char s = CommandManager.getSilenceChar();
52
+
53
+        assertTrue(new CommandArguments(c + "" + s).isSilent());
54
+        assertFalse(new CommandArguments("f" + s).isSilent());
55
+        assertTrue(new CommandArguments(c + "" + s + "foo").isSilent());
56
+        assertFalse(new CommandArguments("").isSilent());
57
+        assertFalse(new CommandArguments("foo").isSilent());
58
+    }
59
+
60
+    @Test
61
+    public void testGetLine() {
62
+        assertEquals("foo", new CommandArguments("foo").getLine());
63
+        assertEquals("foo  bar", new CommandArguments("foo  bar").getLine());
64
+        assertEquals("", new CommandArguments("").getLine());
65
+    }
66
+
67
+    @Test
68
+    public void testGetWords() {
69
+        final CommandArguments args = new CommandArguments("a\tb    c d e");
70
+
71
+        assertEquals(5, args.getWords().length);
72
+        assertEquals("a", args.getWords()[0]);
73
+        assertEquals("b", args.getWords()[1]);
74
+        assertEquals("c", args.getWords()[2]);
75
+        assertEquals("d", args.getWords()[3]);
76
+        assertEquals("e", args.getWords()[4]);
77
+    }
78
+
79
+    @Test
80
+    public void testGetArguments() {
81
+        final CommandArguments args = new CommandArguments("a\tb    c d e");
82
+
83
+        assertEquals(4, args.getArguments().length);
84
+        assertEquals("b", args.getArguments()[0]);
85
+        assertEquals("c", args.getArguments()[1]);
86
+        assertEquals("d", args.getArguments()[2]);
87
+        assertEquals("e", args.getArguments()[3]);
88
+    }
89
+
90
+    @Test
91
+    public void testGetArgumentsAsString() {
92
+        assertEquals("b\tc  d", new CommandArguments("a b\tc  d").getArgumentsAsString());
93
+        assertEquals("", new CommandArguments("a").getArgumentsAsString());
94
+        assertEquals("", new CommandArguments("a\t  \t   \t").getArgumentsAsString());
95
+        assertEquals("b", new CommandArguments("a\t  \t   \tb").getArgumentsAsString());
96
+    }
97
+
98
+}

読み込み中…
キャンセル
保存