Browse Source

Add Command implementation for aliases.

Change-Id: Id9e68a94dc0b42b3fc4450154e2e2e71807a88c1
Reviewed-on: http://gerrit.dmdirc.com/3516
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 10 years ago
parent
commit
da525f87b6

+ 99
- 0
src/com/dmdirc/commandparser/aliases/AliasCommandHandler.java View File

@@ -0,0 +1,99 @@
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.commandparser.aliases;
24
+
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.commandparser.CommandArguments;
27
+import com.dmdirc.commandparser.commands.Command;
28
+import com.dmdirc.commandparser.commands.context.CommandContext;
29
+import com.dmdirc.interfaces.CommandController;
30
+
31
+/**
32
+ * {@link Command} implementation that handles an alias.
33
+ */
34
+public class AliasCommandHandler extends Command {
35
+
36
+    private final Alias alias;
37
+
38
+    public AliasCommandHandler(final CommandController controller, final Alias alias) {
39
+        super(controller);
40
+        this.alias = alias;
41
+    }
42
+
43
+    @Override
44
+    public void execute(final FrameContainer origin, final CommandArguments args,
45
+            final CommandContext context) {
46
+        if (args.getArguments().length >= alias.getMinArguments()) {
47
+            origin.getCommandParser().parseCommand(origin, getSubstituteCommand(args));
48
+        } else {
49
+            sendLine(origin, args.isSilent(), FORMAT_ERROR, alias.getName() + " requires at least "
50
+                    + alias.getMinArguments() + " argument"
51
+                    + (alias.getMinArguments() == 1 ? "" : "s") + ".");
52
+        }
53
+    }
54
+
55
+    /**
56
+     * Gets the command that should be executed, with the appropriate substitutions made.
57
+     *
58
+     * <p>
59
+     * The returned command will have arguments substituted (replacing "$1-", "$2", etc), and will
60
+     * be silenced if the given args are silent.
61
+     *
62
+     * @param args The arguments entered by the user.
63
+     *
64
+     * @return The substituted command to execute.
65
+     */
66
+    private String getSubstituteCommand(final CommandArguments args) {
67
+        final StringBuilder builder = new StringBuilder(alias.getSubstitution());
68
+
69
+        final String[] arguments = args.getArguments();
70
+        for (int i = 0; i < arguments.length; i++) {
71
+            replaceAll(builder, "$" + (i + 1) + "-", args.getArgumentsAsString(i));
72
+            replaceAll(builder, "$" + (i + 1), arguments[i]);
73
+        }
74
+
75
+        if (args.isSilent()) {
76
+            builder.insert(0, getController().getSilenceChar());
77
+        }
78
+        builder.insert(0, getController().getCommandChar());
79
+
80
+        return builder.toString();
81
+    }
82
+
83
+    /**
84
+     * Replaces all instances of the specified substring in the builder.
85
+     *
86
+     * @param builder     The builder to modify.
87
+     * @param substr      The substring to replace.
88
+     * @param replacement The string to use as a replacement.
89
+     */
90
+    private static void replaceAll(final StringBuilder builder,
91
+            final String substr, final String replacement) {
92
+        int index = builder.indexOf(substr);
93
+        while (index > -1) {
94
+            builder.replace(index, index + substr.length(), replacement);
95
+            index = builder.indexOf(substr);
96
+        }
97
+    }
98
+
99
+}

+ 106
- 0
test/com/dmdirc/commandparser/aliases/AliasCommandHandlerTest.java View File

@@ -0,0 +1,106 @@
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.commandparser.aliases;
24
+
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.commandparser.CommandArguments;
27
+import com.dmdirc.commandparser.CommandInfo;
28
+import com.dmdirc.commandparser.CommandType;
29
+import com.dmdirc.commandparser.commands.context.CommandContext;
30
+import com.dmdirc.commandparser.parsers.CommandParser;
31
+import com.dmdirc.interfaces.CommandController;
32
+
33
+import org.junit.Before;
34
+import org.junit.Test;
35
+import org.junit.runner.RunWith;
36
+import org.mockito.Mock;
37
+import org.mockito.runners.MockitoJUnitRunner;
38
+
39
+import static org.mockito.Mockito.verify;
40
+import static org.mockito.Mockito.when;
41
+
42
+@RunWith(MockitoJUnitRunner.class)
43
+public class AliasCommandHandlerTest {
44
+
45
+    @Mock private FrameContainer container;
46
+    @Mock private CommandController commandController;
47
+    @Mock private CommandParser commandParser;
48
+    @Mock private CommandContext context;
49
+    @Mock private CommandInfo commandInfo;
50
+
51
+    @Before
52
+    public void setup() {
53
+        when(container.getCommandParser()).thenReturn(commandParser);
54
+        when(commandController.getCommandChar()).thenReturn('#');
55
+        when(commandController.getSilenceChar()).thenReturn('/');
56
+        when(context.getSource()).thenReturn(container);
57
+        when(context.getCommandInfo()).thenReturn(commandInfo);
58
+    }
59
+
60
+    @Test
61
+    public void testBasicAlias() {
62
+        final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "test2");
63
+        final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
64
+        final CommandArguments arguments = new CommandArguments(commandController, "#test");
65
+        handler.execute(container, arguments, context);
66
+        verify(commandParser).parseCommand(container, "#test2");
67
+    }
68
+
69
+    @Test
70
+    public void testSubstitutions() {
71
+        final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "test2 $1- $2 $1 $2- $2");
72
+        final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
73
+        final CommandArguments arguments
74
+                = new CommandArguments(commandController, "#test agadoo do");
75
+        handler.execute(container, arguments, context);
76
+        verify(commandParser).parseCommand(container, "#test2 agadoo do do agadoo do do");
77
+    }
78
+
79
+    @Test
80
+    public void testInsufficientArgsSingular() {
81
+        final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 1, "blah");
82
+        final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
83
+        final CommandArguments arguments = new CommandArguments(commandController, "#test");
84
+        handler.execute(container, arguments, context);
85
+        verify(container).addLine("commandError", "test requires at least 1 argument.");
86
+    }
87
+
88
+    @Test
89
+    public void testInsufficientArgsPlural() {
90
+        final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 2, "blah");
91
+        final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
92
+        final CommandArguments arguments = new CommandArguments(commandController, "#test agadoo");
93
+        handler.execute(container, arguments, context);
94
+        verify(container).addLine("commandError", "test requires at least 2 arguments.");
95
+    }
96
+
97
+    @Test
98
+    public void testCarriesForwardSilence() {
99
+        final Alias alias = new Alias(CommandType.TYPE_CHAT, "test", 0, "blah");
100
+        final AliasCommandHandler handler = new AliasCommandHandler(commandController, alias);
101
+        final CommandArguments arguments = new CommandArguments(commandController, "#/test agadoo");
102
+        handler.execute(container, arguments, context);
103
+        verify(commandParser).parseCommand(container, "#/blah");
104
+    }
105
+
106
+}

Loading…
Cancel
Save