Selaa lähdekoodia

Add unit tests, remove unused file

Depends-On: Ib757d21e2619855308019c7bcec543c532851634
Change-Id: I7998deadce5889a16e704e347139af7efc30dbbe
Reviewed-on: http://gerrit.dmdirc.com/1794
Reviewed-by: Shane Mc Cormack <shane@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
tags/0.6.5
Chris Smith 13 vuotta sitten
vanhempi
commit
bb2c05c837

+ 0
- 84
src/com/dmdirc/addons/calc/DotOutputter.java Näytä tiedosto

@@ -1,84 +0,0 @@
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.addons.calc;
24
-
25
-import java.text.ParseException;
26
-
27
-/**
28
- * Outputs a string which can be read by the unix `dot` utility to show a
29
- * directed graph showing the tokens parsed by a {@link Parser}.
30
- *
31
- * @author chris
32
- */
33
-public class DotOutputter {
34
-
35
-    /** The parser which will be read. */
36
-    private final Parser parser;
37
-
38
-    /** Counter for assigning node IDs. */
39
-    private int nodes = 0;
40
-
41
-    /**
42
-     * Creates a new DotOutputter for the specified parser.
43
-     *
44
-     * @param parser The parser whose output should be shown
45
-     */
46
-    public DotOutputter(final Parser parser) {
47
-        this.parser = parser;
48
-    }
49
-
50
-    /**
51
-     * Outputs a string which may be read by the `dot` utility to create a
52
-     * directed graph of tokens.
53
-     *
54
-     * @return A string describing the parse tree from the specified parser
55
-     * @throws ParseException If the parser encounters an exception
56
-     */
57
-    public String output() throws ParseException {
58
-        return "digraph astoutput { " + output(0, parser.parse()) + " }";
59
-    }
60
-
61
-    /**
62
-     * Outputs the relevant text for the specified token, assigning it an ID
63
-     * specified by the node parameter.
64
-     *
65
-     * @param node The ID of the node to be used in the output
66
-     * @param token The token to be outputted
67
-     * @return A string corresponding to the dot representation of the
68
-     * specified node and its children
69
-     */
70
-    protected String output(final int node, final TreeToken token) {
71
-        String out = "node" + node + " [label=\"" + token.getToken().getType()
72
-                + "\\n" + token.getToken().getContent() + "\"];";
73
-
74
-        for (TreeToken child : token.getChildren()) {
75
-            int id = ++nodes;
76
-
77
-            out += output(id, child);
78
-            out += "node" + node + " -> node" + id + ";";
79
-        }
80
-
81
-        return out;
82
-    }
83
-
84
-}

+ 2
- 4
test/com/dmdirc/addons/calc/EvaluatorTest.java Näytä tiedosto

@@ -61,13 +61,11 @@ public class EvaluatorTest {
61 61
             {"2+2*3/4-1", "2.5"},
62 62
             {"1.0000(17.5+0.5)(1.000)", "18"},
63 63
             {"2^3", "8"},
64
+            {"+3", "3"},
65
+            {"20%5", "0"},
64 66
         };
65 67
 
66 68
         return Arrays.asList(data);
67 69
     }
68 70
 
69
-    public static junit.framework.Test suite() {
70
-        return new junit.framework.JUnit4TestAdapter(EvaluatorTest.class);
71
-    }
72
-
73 71
 }

+ 6
- 0
test/com/dmdirc/addons/calc/TokenTypeTest.java Näytä tiedosto

@@ -57,4 +57,10 @@ public class TokenTypeTest {
57 57
         assertFalse(TokenType.START.getFollowers().isEmpty());
58 58
     }
59 59
 
60
+    /** Checks that tokens which can't be evaluated throw an exception. */
61
+    @Test(expected=AbstractMethodError.class)
62
+    public void testNonEvaluatable() {
63
+        TokenType.BRACKET_CLOSE.evaluate(null);
64
+    }
65
+
60 66
 }

+ 126
- 0
test/com/dmdirc/addons/debug/DebugTest.java Näytä tiedosto

@@ -0,0 +1,126 @@
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.addons.debug;
24
+
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.commandparser.CommandArguments;
27
+import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.context.CommandContext;
29
+import com.dmdirc.config.IdentityManager;
30
+import com.dmdirc.config.InvalidIdentityFileException;
31
+import static com.dmdirc.harness.CommandArgsMatcher.eqLine;
32
+
33
+import org.junit.BeforeClass;
34
+import org.junit.Test;
35
+import static org.mockito.Mockito.*;
36
+
37
+public class DebugTest {
38
+    
39
+    @BeforeClass
40
+    public static void setUp() throws InvalidIdentityFileException {
41
+        // Command has a dependency on CommandManager (to get the command char)
42
+        // And CommandManager has a dependency on IdentityManager for the same
43
+        IdentityManager.load();
44
+        CommandManager.initCommands();
45
+    }
46
+
47
+    /** Checks the debug command with no arguments shows usage. */
48
+    @Test
49
+    public void testNoArgs() {
50
+        final CommandArguments arguments = mock(CommandArguments.class);
51
+        final FrameContainer<?> container = mock(FrameContainer.class);
52
+        
53
+        when(arguments.isCommand()).thenReturn(true);
54
+        when(arguments.getArguments()).thenReturn(new String[0]);
55
+        
56
+        final Debug debug = new Debug(null);
57
+        
58
+        debug.execute(container, arguments, null);
59
+        verify(container).addLine(eq("commandUsage"), anyObject(),
60
+                eq("debug"), anyObject());
61
+    }
62
+    
63
+    /** Checks the debug command with an invalid subcommand shows an error. */
64
+    @Test
65
+    public void testInvalidArg() {
66
+        final DebugPlugin plugin = mock(DebugPlugin.class);
67
+        final CommandArguments arguments = mock(CommandArguments.class);
68
+        final FrameContainer<?> container = mock(FrameContainer.class);
69
+        
70
+        when(plugin.getCommand("test")).thenReturn(null);
71
+        when(arguments.isCommand()).thenReturn(true);
72
+        when(arguments.getArguments()).thenReturn(new String[]{"test"});
73
+        
74
+        final Debug debug = new Debug(plugin);
75
+        
76
+        debug.execute(container, arguments, null);
77
+        verify(container).addLine(eq("commandError"), anyObject());
78
+    }
79
+    
80
+    /** Checks the debug command executes a subcommand with no args. */
81
+    @Test
82
+    public void testCommandNoArgs() {
83
+        final DebugPlugin plugin = mock(DebugPlugin.class);
84
+        final CommandArguments arguments = mock(CommandArguments.class);
85
+        final FrameContainer<?> container = mock(FrameContainer.class);
86
+        final DebugCommand command = mock(DebugCommand.class);
87
+        final CommandContext context = mock(CommandContext.class);
88
+        
89
+        when(plugin.getCommand("test")).thenReturn(command);
90
+        when(arguments.isCommand()).thenReturn(true);
91
+        when(arguments.getArguments()).thenReturn(new String[]{"test"});
92
+        when(arguments.getArgumentsAsString(1)).thenReturn("");
93
+        when(command.getName()).thenReturn("test");
94
+        
95
+        final Debug debug = new Debug(plugin);
96
+        
97
+        debug.execute(container, arguments, context);
98
+        
99
+        verify(container, never()).addLine(anyString(), anyObject());
100
+        verify(command).execute(same(container), eqLine("/test"), same(context));
101
+    }
102
+    
103
+    /** Checks the debug command executes a subcommand with args. */
104
+    @Test
105
+    public void testCommandWithArgs() {
106
+        final DebugPlugin plugin = mock(DebugPlugin.class);
107
+        final CommandArguments arguments = mock(CommandArguments.class);
108
+        final FrameContainer<?> container = mock(FrameContainer.class);
109
+        final DebugCommand command = mock(DebugCommand.class);
110
+        final CommandContext context = mock(CommandContext.class);
111
+        
112
+        when(plugin.getCommand("test")).thenReturn(command);
113
+        when(arguments.isCommand()).thenReturn(true);
114
+        when(arguments.getArguments()).thenReturn(new String[]{"test", "1", "2", "3"});
115
+        when(arguments.getArgumentsAsString(1)).thenReturn("1 2 3");
116
+        when(command.getName()).thenReturn("test");
117
+        
118
+        final Debug debug = new Debug(plugin);
119
+        
120
+        debug.execute(container, arguments, context);
121
+        
122
+        verify(container, never()).addLine(anyString(), anyObject());
123
+        verify(command).execute(same(container), eqLine("/test 1 2 3"), same(context));
124
+    }
125
+
126
+}

Loading…
Peruuta
Tallenna