Przeglądaj źródła

adding TestCalc for the IRC command; fixes, additions, cleanup to support etc

tags/v0.1^2
Ben Kurtovic 13 lat temu
rodzic
commit
e461592d71
4 zmienionych plików z 60 dodań i 6 usunięć
  1. +1
    -1
      bot/commands/calc.py
  2. +15
    -1
      tests/support.py
  3. +39
    -0
      tests/test_calc.py
  4. +5
    -4
      tests/test_test.py

+ 1
- 1
bot/commands/calc.py Wyświetl plik

@@ -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)


+ 15
- 1
tests/support.py Wyświetl plik

@@ -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]


+ 39
- 0
tests/test_calc.py Wyświetl plik

@@ -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)

+ 5
- 4
tests/test_test.py Wyświetl plik

@@ -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)

Ładowanie…
Anuluj
Zapisz