From 6ad5a7fae6f8907e2aaa81e1085abcab3c9bc9d6 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 15 Aug 2011 19:09:44 -0400 Subject: [PATCH] more work on tests; added a framework for command testing, now testing !test (ha) --- bot/commands/help.py | 2 +- tests/support.py | 77 ++++++++++++++++++++++++++++++++++++++++++++------ tests/test_blowfish.py | 2 +- tests/test_test.py | 28 ++++++++++++++++++ 4 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 tests/test_test.py diff --git a/bot/commands/help.py b/bot/commands/help.py index 5bb9e3d..b35d205 100644 --- a/bot/commands/help.py +++ b/bot/commands/help.py @@ -29,7 +29,7 @@ class Command(BaseCommand): # Create a dummy message to test which commands pick up the user's # input: - dummy = Data("PRIVMSG #fake-channel :Fake messsage!".split()) + dummy = Data(":foo!bar@example.com PRIVMSG #channel :msg!".split()) dummy.command = command.lower() dummy.is_command = True diff --git a/tests/support.py b/tests/support.py index ac65d9d..cd181d5 100644 --- a/tests/support.py +++ b/tests/support.py @@ -5,17 +5,78 @@ EarwigBot's Unit Test Support This module provides some support code for unit tests. -Importing this module will "fix" your path so that EarwigBot code from bot/ -can be imported normally. The run() function runs a given test case. +Importing this module will "fix" your path so that EarwigBot code from bot/ can +be imported normally. + +CommandTestCase is a subclass of unittest.TestCase that provides setUp() for +creating a fake connection and some other helpful methods. It uses +FakeConnection, a subclass of classes.Connection, but with an internal string +instead of a socket for data. """ from os import path +import re import sys -import unittest +from unittest import TestCase + +root_dir = path.split(path.dirname(path.abspath(__file__)))[0] +code_dir = path.join(root_dir, "bot") +sys.path.insert(0, code_dir) + +from classes import Connection, Data + +class CommandTestCase(TestCase): + re_sender = re.compile(":(.*?)!(.*?)@(.*?)\Z") + + def setUp(self, command): + self.connection = FakeConnection() + self.connection.connect() + self.command = command(self.connection) + + def get_single(self): + data = self.connection.get().split("\n") + line = data.pop(0) + for remaining in data: + self.connection.send(remaining) + return line + + def assertSent(self, msg): + line = self.get_single() + self.assertEqual(line, msg) + + def assertSentIn(self, msgs): + line = self.get_single() + self.assertIn(line, msgs) + + def maker(self, line, chan, msg=None): + data = Data(line) + data.nick, data.ident, data.host = self.re_sender.findall(line[0])[0] + if msg is not None: + data.msg = msg + data.chan = chan + data.parse_args() + return data + + def make_msg(self, command, *args): + line = ":Foo!bar@example.com PRIVMSG #channel :!{0}".format(command) + line = line.strip().split() + line.extend(args) + return self.maker(line, line[2], " ".join(line[3:])[1:]) + + def make_join(self): + line = ":Foo!bar@example.com JOIN :#channel".strip().split() + return self.maker(line, line[2][1:]) + +class FakeConnection(Connection): + def connect(self): + self._buffer = "" + + def close(self): + pass -root = path.join(path.split(path.dirname(path.abspath(__file__)))[0], "bot") -sys.path.insert(0, root) + def get(self, size=4096): + data, self._buffer = self._buffer, "" + return data -def run(case): - suite = unittest.TestLoader().loadTestsFromTestCase(case) - text_runner = unittest.TextTestRunner(verbosity=2).run(suite) + def send(self, msg): + self._buffer += msg + "\n" diff --git a/tests/test_blowfish.py b/tests/test_blowfish.py index 3b209de..9eb6184 100644 --- a/tests/test_blowfish.py +++ b/tests/test_blowfish.py @@ -74,4 +74,4 @@ class TestBlowfish(unittest.TestCase): self.assertRaisesRegexp(e, e3, d, "some_key", "abcdabcdabcdabcd") if __name__ == "__main__": - support.run(TestBlowfish) + unittest.main(verbosity=2) diff --git a/tests/test_test.py b/tests/test_test.py new file mode 100644 index 0000000..0ac0c37 --- /dev/null +++ b/tests/test_test.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- + +import unittest + +import support +from commands.test import Command + +class TestTest(support.CommandTestCase): + + def setUp(self): + super(TestTest, self).setUp(Command) + + def test_check(self): + self.assertFalse(self.command.check(self.make_msg("bloop"))) + self.assertFalse(self.command.check(self.make_join())) + + self.assertTrue(self.command.check(self.make_msg("test"))) + self.assertTrue(self.command.check(self.make_msg("TEST", "foo"))) + + def test_process(self): + self.command.process(self.make_msg("test")) + + msgs = ["PRIVMSG #channel :Hey \x02Foo\x0F!", + "PRIVMSG #channel :'sup \x02Foo\x0F?"] + self.assertSentIn(msgs) + +if __name__ == "__main__": + unittest.main(verbosity=2)