Selaa lähdekoodia

Strip prefixes from multi-line aliases.

When migrating make sure that existing multi-line aliases get
command chars (and any following silence chars) removed, rather
than just doing it from the first line.

Change-Id: I08b7081cd3f582901909001586048b209acbfa31
Fixes-Issue: CLIENT-512
Reviewed-on: http://gerrit.dmdirc.com/3645
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 10 vuotta sitten
vanhempi
commit
5f5889887a

+ 18
- 2
src/com/dmdirc/commandparser/aliases/AliasFactory.java Näytä tiedosto

@@ -25,8 +25,14 @@ package com.dmdirc.commandparser.aliases;
25 25
 import com.dmdirc.commandparser.CommandType;
26 26
 import com.dmdirc.interfaces.CommandController;
27 27
 
28
+import com.google.common.base.CharMatcher;
29
+import com.google.common.base.Joiner;
30
+import com.google.common.base.Splitter;
28 31
 import com.google.common.base.Strings;
29 32
 
33
+import java.util.ArrayList;
34
+import java.util.List;
35
+
30 36
 import javax.inject.Inject;
31 37
 import javax.inject.Singleton;
32 38
 
@@ -68,8 +74,18 @@ public class AliasFactory {
68 74
     }
69 75
 
70 76
     private String removeCommandChar(final String input) {
71
-        return !input.isEmpty() && input.charAt(0) == commandController.getCommandChar()
72
-                ? input.substring(1) : input;
77
+        // TODO: This could be moved into the command controller, or a utility class.
78
+        // CommandArguments does something similar.
79
+        final List<String> lines = Splitter.on(CharMatcher.anyOf("\r\n"))
80
+                .omitEmptyStrings()
81
+                .splitToList(input);
82
+        final List<String> trimmedLines = new ArrayList<>(lines.size());
83
+        for (String line : lines) {
84
+            trimmedLines.add(line.charAt(0) == commandController.getCommandChar()
85
+                    ? line.length() > 1 && line.charAt(1) == commandController.getSilenceChar()
86
+                    ? line.substring(2) : line.substring(1) : line);
87
+        }
88
+        return Joiner.on("\r\n").join(trimmedLines);
73 89
     }
74 90
 
75 91
 }

+ 103
- 0
test/com/dmdirc/commandparser/aliases/AliasFactoryTest.java Näytä tiedosto

@@ -0,0 +1,103 @@
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.interfaces.CommandController;
26
+
27
+import org.junit.Before;
28
+import org.junit.Test;
29
+import org.junit.runner.RunWith;
30
+import org.mockito.Mock;
31
+import org.mockito.runners.MockitoJUnitRunner;
32
+
33
+import static org.junit.Assert.assertEquals;
34
+import static org.mockito.Mockito.when;
35
+
36
+@RunWith(MockitoJUnitRunner.class)
37
+public class AliasFactoryTest {
38
+
39
+    private static final String COMMAND_CHAR = "!";
40
+    private static final String SILENCE_CHAR = "_";
41
+
42
+    @Mock
43
+    private CommandController commandController;
44
+    private AliasFactory factory;
45
+
46
+    @Before
47
+    public void setup() {
48
+        factory = new AliasFactory(commandController);
49
+
50
+        when(commandController.getCommandChar()).thenReturn(COMMAND_CHAR.charAt(0));
51
+        when(commandController.getSilenceChar()).thenReturn(SILENCE_CHAR.charAt(0));
52
+    }
53
+
54
+    @Test
55
+    public void testSimpleAlias() {
56
+        final Alias alias = factory.createAlias("name", 2, "response");
57
+        assertEquals("name", alias.getName());
58
+        assertEquals(2, alias.getMinArguments());
59
+        assertEquals("response", alias.getSubstitution());
60
+    }
61
+
62
+    @Test
63
+    public void testCommandChars() {
64
+        final Alias alias = factory.createAlias(COMMAND_CHAR + "name", 2,
65
+                COMMAND_CHAR + "response");
66
+        assertEquals("name", alias.getName());
67
+        assertEquals(2, alias.getMinArguments());
68
+        assertEquals("response", alias.getSubstitution());
69
+    }
70
+
71
+    @Test
72
+    public void testSilenceChars() {
73
+        final Alias alias = factory.createAlias(SILENCE_CHAR + "name", 2,
74
+                SILENCE_CHAR + "response");
75
+        assertEquals(SILENCE_CHAR + "name", alias.getName());
76
+        assertEquals(2, alias.getMinArguments());
77
+        assertEquals(SILENCE_CHAR + "response", alias.getSubstitution());
78
+    }
79
+
80
+    @Test
81
+    public void testCommandAndSilenceChars() {
82
+        final Alias alias = factory.createAlias(COMMAND_CHAR + SILENCE_CHAR + "name", 2,
83
+                COMMAND_CHAR + SILENCE_CHAR + "response");
84
+        assertEquals("name", alias.getName());
85
+        assertEquals(2, alias.getMinArguments());
86
+        assertEquals("response", alias.getSubstitution());
87
+    }
88
+
89
+    @Test
90
+    public void testMultilineResponse() {
91
+        final Alias alias = factory.createAlias("name", 2,
92
+                "response1\n"
93
+                + "response2\r\n"
94
+                + COMMAND_CHAR + "response3\r"
95
+                + COMMAND_CHAR + SILENCE_CHAR + "response4\r\n\r\n"
96
+                + SILENCE_CHAR + "response5\n\n\n");
97
+        assertEquals("name", alias.getName());
98
+        assertEquals(2, alias.getMinArguments());
99
+        assertEquals("response1\r\nresponse2\r\nresponse3\r\nresponse4\r\n"
100
+                + SILENCE_CHAR + "response5", alias.getSubstitution());
101
+    }
102
+
103
+}

Loading…
Peruuta
Tallenna