|
@@ -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
|
+}
|