Procházet zdrojové kódy

Unit tests

Change-Id: I1c1b4dab1b62c2d14879aace25fbf92d91f9629f
Reviewed-on: http://gerrit.dmdirc.com/1796
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.6.5rc1
Chris Smith před 13 roky
rodič
revize
4b8bf0c056

+ 45
- 1
test/com/dmdirc/commandparser/CommandArgumentsTest.java Zobrazit soubor

@@ -22,10 +22,11 @@
22 22
 
23 23
 package com.dmdirc.commandparser;
24 24
 
25
+import java.util.Arrays;
26
+
25 27
 import com.dmdirc.config.IdentityManager;
26 28
 import com.dmdirc.config.InvalidIdentityFileException;
27 29
 
28
-import java.util.Arrays;
29 30
 import org.junit.BeforeClass;
30 31
 import org.junit.Test;
31 32
 import static org.junit.Assert.*;
@@ -37,6 +38,49 @@ public class CommandArgumentsTest {
37 38
         IdentityManager.load();
38 39
     }
39 40
 
41
+    /** Ensures the ctor which takes a collection builds the 'line' property. */
42
+    @Test
43
+    public void testCollectionCtorCreatesLine() {
44
+        final CommandArguments args = new CommandArguments(Arrays.asList(
45
+                "/command", "arg1", "arg2"));
46
+        assertEquals("/command arg1 arg2", args.getLine());
47
+    }
48
+
49
+    /** Ensures the ctor works with an empty collection. */
50
+    @Test
51
+    public void testCollectionCtorWithEmpty() {
52
+        final CommandArguments args = new CommandArguments(Arrays.asList(
53
+                new String[0]));
54
+        assertEquals("", args.getLine());
55
+    }
56
+
57
+    /** Ensures that getStrippedLine returns non-command lines as-is. */
58
+    @Test
59
+    public void testGetStrippedLineNormal() {
60
+        final CommandArguments args = new CommandArguments("blah blah");
61
+        assertEquals("blah blah", args.getStrippedLine());
62
+    }
63
+
64
+    /**
65
+     * Ensures that getStrippedLine returns command lines without the command
66
+     * char.
67
+     */
68
+    @Test
69
+    public void testGetStrippedLineCommand() {
70
+        final CommandArguments args = new CommandArguments("/blah blah");
71
+        assertEquals("blah blah", args.getStrippedLine());
72
+    }
73
+
74
+    /**
75
+     * Ensures that getStrippedLine returns silenced lines without the
76
+     * command or silence chars.
77
+     */
78
+    @Test
79
+    public void testGetStrippedLineSilenced() {
80
+        final CommandArguments args = new CommandArguments("/.blah blah");
81
+        assertEquals("blah blah", args.getStrippedLine());
82
+    }
83
+
40 84
     @Test
41 85
     public void testIsCommand() {
42 86
         assertTrue(new CommandArguments(CommandManager.getCommandChar() + "").isCommand());

+ 101
- 0
test/com/dmdirc/commandparser/PopupMenuItemTest.java Zobrazit soubor

@@ -0,0 +1,101 @@
1
+/*
2
+ * Copyright (c) 2006-2011 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 org.junit.BeforeClass;
28
+import org.junit.Test;
29
+import static org.junit.Assert.*;
30
+
31
+public class PopupMenuItemTest {
32
+
33
+    @BeforeClass
34
+    public static void setUpClass() throws Exception {
35
+        IdentityManager.load();
36
+    }
37
+
38
+    /**
39
+     * Tests a command which doesn't define an arity gets args substituted
40
+     * for a single target.
41
+     */
42
+    @Test
43
+    public void testUnaryCommandWithSingle() {
44
+        final PopupMenuItem item = new PopupMenuItem(null, 1, "foo %s");
45
+
46
+        assertEquals("/foo bar", item.getCommand(new Object[][]{{"bar"}}));
47
+    }
48
+
49
+    /**
50
+     * Tests a command which doesn't define an arity gets args substituted
51
+     * for multiple targets.
52
+     */
53
+    @Test
54
+    public void testUnaryCommandWithMultiple() {
55
+        final PopupMenuItem item = new PopupMenuItem(null, 1, "foo %s");
56
+
57
+        assertEquals("/foo bar\n/foo b\n/foo 2",
58
+                item.getCommand(new Object[][]{
59
+                    {"bar"}, {"b"}, {"2"},
60
+                }));
61
+    }
62
+
63
+    /**
64
+     * Tests a command which defines its arity as 1 gets args substituted
65
+     * for a single target.
66
+     */
67
+    @Test
68
+    public void testExplicitUnaryCommandWithSingle() {
69
+        final PopupMenuItem item = new PopupMenuItem(null, 1, "1:foo %s");
70
+
71
+        assertEquals("/foo bar", item.getCommand(new Object[][]{{"bar"}}));
72
+    }
73
+
74
+    /**
75
+     * Tests a command which defines its arity as 1 gets args substituted
76
+     * for multiple targets.
77
+     */
78
+    @Test
79
+    public void testExplicitUnaryCommandWithMultiple() {
80
+        final PopupMenuItem item = new PopupMenuItem(null, 1, "1:foo %s");
81
+
82
+        assertEquals("/foo bar\n/foo a\n/foo 1",
83
+                item.getCommand(new Object[][]{
84
+                    {"bar"}, {"a"}, {"1"},
85
+                }));
86
+    }
87
+
88
+    /**
89
+     * Tests a command which takes multiple arguments.
90
+     */
91
+    @Test
92
+    public void testMultipleCommand() {
93
+        final PopupMenuItem item = new PopupMenuItem(null, 2, "4:foo %s %s %s %s");
94
+
95
+        assertEquals("/foo 1 2 a b\n/foo bar baz",
96
+                item.getCommand(new Object[][]{
97
+                    {"1", "2"}, {"a", "b"}, {"bar", "baz"},
98
+                }).trim());
99
+    }
100
+
101
+}

+ 43
- 3
test/com/dmdirc/commandparser/commands/channel/BanTest.java Zobrazit soubor

@@ -22,11 +22,14 @@
22 22
 
23 23
 package com.dmdirc.commandparser.commands.channel;
24 24
 
25
+import com.dmdirc.parser.interfaces.ClientInfo;
25 26
 import com.dmdirc.Channel;
26 27
 import com.dmdirc.FrameContainer;
27 28
 import com.dmdirc.config.IdentityManager;
28 29
 import com.dmdirc.commandparser.CommandArguments;
29 30
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
31
+import com.dmdirc.parser.interfaces.ChannelClientInfo;
32
+import com.dmdirc.parser.interfaces.ChannelInfo;
30 33
 
31 34
 import org.junit.BeforeClass;
32 35
 import org.junit.Test;
@@ -38,16 +41,53 @@ public class BanTest {
38 41
     public static void setUpClass() throws Exception {
39 42
         IdentityManager.load();
40 43
     }
41
-    
44
+
42 45
     private final Ban command = new Ban();
43 46
 
44 47
     @Test
45 48
     public void testUsage() {
46 49
         final FrameContainer<?> tiw = mock(FrameContainer.class);
47 50
         final Channel channel = mock(Channel.class);
48
-        command.execute(tiw, new CommandArguments("/ban"), 
51
+        command.execute(tiw, new CommandArguments("/ban"),
49 52
                 new ChannelCommandContext(null, command, channel));
50
-        
53
+
51 54
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
52 55
     }
56
+
57
+    /** Tests that the ban command uses the correct hostname if given a user. */
58
+    @Test
59
+    public void testKnownUser() {
60
+        final FrameContainer<?> container = mock(FrameContainer.class);
61
+        final ChannelInfo channelInfo = mock(ChannelInfo.class);
62
+        final ChannelClientInfo ccInfo = mock(ChannelClientInfo.class);
63
+        final ClientInfo clientInfo = mock(ClientInfo.class);
64
+        final Channel channel = mock(Channel.class);
65
+
66
+        when(channel.getChannelInfo()).thenReturn(channelInfo);
67
+        when(channelInfo.getChannelClient("user")).thenReturn(ccInfo);
68
+        when(ccInfo.getClient()).thenReturn(clientInfo);
69
+        when(clientInfo.getHostname()).thenReturn("my.host.name");
70
+
71
+        command.execute(container, new CommandArguments("/ban user"),
72
+                new ChannelCommandContext(null, command, channel));
73
+
74
+        verify(channelInfo).alterMode(true, 'b', "*!*@my.host.name");
75
+        verify(channelInfo).flushModes();
76
+    }
77
+
78
+    /** Tests that the ban command works if given a mask not a username. */
79
+    @Test
80
+    public void testHostmask() {
81
+        final FrameContainer<?> container = mock(FrameContainer.class);
82
+        final ChannelInfo channelInfo = mock(ChannelInfo.class);
83
+        final Channel channel = mock(Channel.class);
84
+
85
+        when(channel.getChannelInfo()).thenReturn(channelInfo);
86
+
87
+        command.execute(container, new CommandArguments("/ban *!*@my.host.name"),
88
+                new ChannelCommandContext(null, command, channel));
89
+
90
+        verify(channelInfo).alterMode(true, 'b', "*!*@my.host.name");
91
+        verify(channelInfo).flushModes();
92
+    }
53 93
 }

Načítá se…
Zrušit
Uložit