@Test(dataProvider = "onCommandLongTests") public void executeOnCommandLongTest( String cmdMessage, OnCommandLong command, String[][] expectedArgs) throws Exception { // Build the CommandEvent CommandEvent event = new CommandEvent( command, new MessageEvent(bot, null, null, cmdMessage), null, null, cmdMessage, "?testCommand", hook.getArgs(cmdMessage)); // Execute and verify values String returned = hook.executeOnCommandLong(event); assertEquals(returned, "Success", "onCommandLong doesn't return expected value"); log.debug("Current command: " + command.getName()); logMultiArray(expectedArgs, "Expected args"); logMultiArray(command.getArgs(), "Given args"); assertTrue( Arrays.deepEquals(command.getArgs(), expectedArgs), "Command test " + command.getName() + " args don't equal"); }
@Test public void onPrivateMessageTest() throws Exception { // Generate a simple message event final String origMessage = "hello " + args4; final PrivateMessageEvent pmEvent = new PrivateMessageEvent(bot, user, origMessage); // This will notify us that execute actually ran. Yes, its ugly, but boolean is final final StringBuilder executed = new StringBuilder("false"); // Test hook that makes sure all the information passed into execute is good CoreQuackbotHook tempHook = new CoreQuackbotHook() { @Override protected void execute(Event event, Channel chan, User user, String message) throws Exception { assertEquals(event, pmEvent, "Event passed to execute doesn't match given"); assertEquals(chan, null, "Channel does not match given"); assertEquals(user, CoreQuackbotHookTest.this.user, "User does not match given"); assertEquals(message, origMessage, "Message does not match given"); executed.setLength(0); executed.append("true"); } }; tempHook.onPrivateMessage(pmEvent); }
/** Instead of only testing onCommandLong, test feeding into onMessage */ @Test(dataProvider = "onCommandLongTests") public void executeTest(String cmdMessage, OnCommandLong command, String[][] expectedArgs) throws Exception { // Remove the prefix cmdMessage = cmdMessage.substring(1); // Build the messageEvent final StringBuilder response = new StringBuilder(); MessageEvent messageEvent = new MessageEvent(bot, channel, null, cmdMessage) { @Override public void respond(String commandResponse) { if (commandResponse != null) response.append(commandResponse); } }; // Add our test listener controller.getHookManager().addHook(command); // Feed into onMessage log.trace("Sending message " + cmdMessage); hook.execute(messageEvent, channel, user, cmdMessage); // Verify the results assertEquals( response.toString(), "Success", "onCommandLong in test " + command.getName() + " doesn't return expected value"); assertTrue( Arrays.deepEquals(command.getArgs(), expectedArgs), "Command test " + command.getName() + " args don't equal"); // Remove it to cleanup controller.getHookManager().removeHook(command); }
@Test public void getArgsTest() { String[] args = hook.getArgs("?someCommand " + args4); assertEquals(args.length, 5, "Too many or too few args given from getArgs"); assertEquals(args[0], "hello0", "Args 0 doesn't match given"); assertEquals(args[1], "hello1", "Args 1 doesn't match given"); assertEquals(args[2], "hello2", "Args 2 doesn't match given"); assertEquals(args[3], "hello3", "Args 3 doesn't match given"); assertEquals(args[4], "hello4", "Args 4 doesn't match given"); }