| @@ -12,7 +12,7 @@ class Command(BaseCommand): | |||||
| def process(self, data): | def process(self, data): | ||||
| if not data.args: | 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 | return | ||||
| query = ' '.join(data.args) | query = ' '.join(data.args) | ||||
| @@ -36,7 +36,7 @@ class CommandTestCase(TestCase): | |||||
| def get_single(self): | def get_single(self): | ||||
| data = self.connection.get().split("\n") | data = self.connection.get().split("\n") | ||||
| line = data.pop(0) | line = data.pop(0) | ||||
| for remaining in data: | |||||
| for remaining in data[1:]: | |||||
| self.connection.send(remaining) | self.connection.send(remaining) | ||||
| return line | return line | ||||
| @@ -48,6 +48,20 @@ class CommandTestCase(TestCase): | |||||
| line = self.get_single() | line = self.get_single() | ||||
| self.assertIn(line, msgs) | 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): | def maker(self, line, chan, msg=None): | ||||
| data = Data(line) | data = Data(line) | ||||
| data.nick, data.ident, data.host = self.re_sender.findall(line[0])[0] | data.nick, data.ident, data.host = self.re_sender.findall(line[0])[0] | ||||
| @@ -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) | |||||
| @@ -18,11 +18,12 @@ class TestTest(support.CommandTestCase): | |||||
| self.assertTrue(self.command.check(self.make_msg("TEST", "foo"))) | self.assertTrue(self.command.check(self.make_msg("TEST", "foo"))) | ||||
| def test_process(self): | 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__": | if __name__ == "__main__": | ||||
| unittest.main(verbosity=2) | unittest.main(verbosity=2) | ||||