diff --git a/bot/commands/calc.py b/bot/commands/calc.py index 0fc0fbd..9fcb63a 100644 --- a/bot/commands/calc.py +++ b/bot/commands/calc.py @@ -12,7 +12,7 @@ class Command(BaseCommand): def process(self, data): if not data.args: - self.connection.reply(data, "What do you want me to calculate?") + self.connection.reply(data, "what do you want me to calculate?") return query = ' '.join(data.args) diff --git a/tests/support.py b/tests/support.py index cd181d5..a2dadb5 100644 --- a/tests/support.py +++ b/tests/support.py @@ -36,7 +36,7 @@ class CommandTestCase(TestCase): def get_single(self): data = self.connection.get().split("\n") line = data.pop(0) - for remaining in data: + for remaining in data[1:]: self.connection.send(remaining) return line @@ -48,6 +48,20 @@ class CommandTestCase(TestCase): line = self.get_single() self.assertIn(line, msgs) + def assertSaid(self, msg): + self.assertSent("PRIVMSG #channel :{0}".format(msg)) + + def assertSaidIn(self, msgs): + msgs = ["PRIVMSG #channel :{0}".format(msg) for msg in msgs] + self.assertSentIn(msgs) + + def assertReply(self, msg): + self.assertSaid("\x02Foo\x0F: {0}".format(msg)) + + def assertReplyIn(self, msgs): + msgs = ["\x02Foo\x0F: {0}".format(msg) for msg in msgs] + self.assertSaidIn(msgs) + def maker(self, line, chan, msg=None): data = Data(line) data.nick, data.ident, data.host = self.re_sender.findall(line[0])[0] diff --git a/tests/test_calc.py b/tests/test_calc.py new file mode 100644 index 0000000..981fda3 --- /dev/null +++ b/tests/test_calc.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- + +import unittest + +import support +from commands.calc import Command + +class TestCalc(support.CommandTestCase): + + def setUp(self): + super(TestCalc, 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("calc"))) + self.assertTrue(self.command.check(self.make_msg("CALC", "foo"))) + + def test_ignore_empty(self): + self.command.process(self.make_msg("calc")) + self.assertReply("what do you want me to calculate?") + + def test_maths(self): + tests = [ + ("2 + 2", "2 + 2 = 4"), + ("13 * 5", "13 * 5 = 65"), + ("80 / 42", "80 / 42 = 40/21 (approx. 1.9047619047619047)"), + ("2/0", "2/0 = undef"), + ("π", "π = 3.141592653589793238"), + ] + + for test in tests: + q = test[0].strip().split() + self.command.process(self.make_msg("calc", *q)) + self.assertReply(test[1]) + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/tests/test_test.py b/tests/test_test.py index 0ac0c37..540e1eb 100644 --- a/tests/test_test.py +++ b/tests/test_test.py @@ -18,11 +18,12 @@ class TestTest(support.CommandTestCase): self.assertTrue(self.command.check(self.make_msg("TEST", "foo"))) def test_process(self): - self.command.process(self.make_msg("test")) + def _test(): + self.command.process(self.make_msg("test")) + self.assertSaidIn(["Hey \x02Foo\x0F!", "'sup \x02Foo\x0F?"]) - msgs = ["PRIVMSG #channel :Hey \x02Foo\x0F!", - "PRIVMSG #channel :'sup \x02Foo\x0F?"] - self.assertSentIn(msgs) + for i in xrange(64): + _test() if __name__ == "__main__": unittest.main(verbosity=2)