Skip to content
This repository was archived by the owner on Jan 19, 2018. It is now read-only.

Commit 69f2a03

Browse files
webpigeonSuborbitalPigeon
authored andcommitted
added unit tests for command processor
1 parent 83a5134 commit 69f2a03

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Copyright © 2012 Bruce Cowan <[email protected]>
3+
*
4+
* This file is part of uc_PircBotX.
5+
*
6+
* uc_PircBotX is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* uc_PircBotX is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with uc_PircBotX. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package uk.co.unitycoders.pircbotx.commandprocessor;
20+
21+
import java.util.Arrays;
22+
23+
import org.junit.Assert;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
/**
28+
* @author webpigeon
29+
*
30+
*/
31+
public class CommandProcessorTest
32+
{
33+
private CommandProcessor processor;
34+
35+
/**
36+
* @throws java.lang.Exception
37+
*/
38+
@Before
39+
public void setUp() throws Exception
40+
{
41+
processor = new CommandProcessor('!');
42+
43+
}
44+
45+
/**
46+
* Check that if the module list is empty, the getModules method returns an
47+
* empty array.
48+
*/
49+
@Test
50+
public void testEmptyModules()
51+
{
52+
String[] expected = new String[0];
53+
String[] result = processor.getModules();
54+
Assert.assertArrayEquals(expected, result);
55+
}
56+
57+
/**
58+
* Check that if an invalid module is passed to the getCommands list, that
59+
* an empty array is returned.
60+
*/
61+
@Test
62+
public void testInvalidModuleCommands()
63+
{
64+
String fakeModuleName = "fakemodule";
65+
String[] expected = new String[0];
66+
String[] result = processor.getCommands(fakeModuleName);
67+
Assert.assertArrayEquals(expected, result);
68+
}
69+
70+
/**
71+
* Check that if a module is registered then all module commands show up
72+
*/
73+
@Test
74+
public void testModuleExists()
75+
{
76+
String name = "fake";
77+
Object module = new FakeModule();
78+
processor.register(name, module);
79+
80+
String[] expected = { "fake" };
81+
String[] result = processor.getModules();
82+
Assert.assertArrayEquals(expected, result);
83+
}
84+
85+
/**
86+
* Check that if a module is registered then all module commands show up
87+
*/
88+
@Test
89+
public void testCommandsExists()
90+
{
91+
String name = "fake";
92+
Object module = new FakeModule();
93+
processor.register(name, module);
94+
95+
String[] expected = { "default", "goodbye", "bye", "hello" };
96+
String[] result = processor.getCommands(name);
97+
98+
// sort arrays to prevent ordering error
99+
Arrays.sort(result);
100+
Arrays.sort(expected);
101+
102+
Assert.assertArrayEquals(expected, result);
103+
}
104+
105+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright © 2012 Bruce Cowan <[email protected]>
3+
*
4+
* This file is part of uc_PircBotX.
5+
*
6+
* uc_PircBotX is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* uc_PircBotX is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with uc_PircBotX. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package uk.co.unitycoders.pircbotx.commandprocessor;
20+
21+
import org.pircbotx.PircBotX;
22+
import org.pircbotx.hooks.events.MessageEvent;
23+
24+
/**
25+
* Fake module used for unit testing.
26+
*
27+
* @author webpigeon
28+
*
29+
*/
30+
public class FakeModule
31+
{
32+
33+
/**
34+
* Default command
35+
*
36+
* @param ex
37+
*/
38+
@Command
39+
public void doStuff(MessageEvent<PircBotX> ex)
40+
{
41+
}
42+
43+
/**
44+
* Single command
45+
*
46+
* @param ex
47+
*/
48+
@Command("hello")
49+
public void doStuffOnce(MessageEvent<PircBotX> ex)
50+
{
51+
}
52+
53+
/**
54+
* Double command
55+
*
56+
* @param ex
57+
*/
58+
@Command({ "goodbye", "bye" })
59+
public void doStuffTwice(MessageEvent<PircBotX> ex)
60+
{
61+
}
62+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright © 2012 Bruce Cowan <[email protected]>
3+
*
4+
* This file is part of uc_PircBotX.
5+
*
6+
* uc_PircBotX is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* uc_PircBotX is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with uc_PircBotX. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package uk.co.unitycoders.pircbotx.commandprocessor;
20+
21+
import org.pircbotx.PircBotX;
22+
import org.pircbotx.hooks.events.MessageEvent;
23+
24+
/**
25+
* @author webpigeon
26+
*
27+
*/
28+
public class TriggerModule
29+
{
30+
int called = 0;
31+
32+
@Command
33+
public void onMessage(MessageEvent<PircBotX> pircbox)
34+
{
35+
called++;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)