From 8f75c5363c64f14a02de8c21e44d9ac59151179e Mon Sep 17 00:00:00 2001 From: James Lu Date: Tue, 7 Jul 2015 18:07:20 -0700 Subject: [PATCH] Move all the FakeIRC stuff to classes.py, update FakeIRC with the ability to get hook data --- classes.py | 75 +++++++++++++++++++++++++++++++ tests/test_fakeirc.py | 31 +++++++++++++ tests/test_proto_common.py | 90 -------------------------------------- 3 files changed, 106 insertions(+), 90 deletions(-) create mode 100644 tests/test_fakeirc.py delete mode 100644 tests/test_proto_common.py diff --git a/classes.py b/classes.py index 2aabfae..4de13ef 100644 --- a/classes.py +++ b/classes.py @@ -1,4 +1,7 @@ from collections import defaultdict + +from log import log +import main import time class IrcUser(): @@ -55,3 +58,75 @@ class IrcChannel(): class ProtocolError(Exception): pass + +global testconf +testconf = {'server': + { + 'netname': 'fakeirc', + 'ip': '0.0.0.0', + 'port': 7000, + 'recvpass': "abcd", + 'sendpass': "abcd", + 'protocol': "null", + 'hostname': "pylink.unittest", + 'sid': "9PY", + 'channels': ["#pylink"], + } + } + +class FakeIRC(main.Irc): + def connect(self): + self.messages = [] + self.hookargs = [] + self.hookmsgs = [] + self.socket = None + + def run(self, data): + """Queues a message to the fake IRC server.""" + log.debug('<- ' + data) + self.proto.handle_events(self, data) + + def send(self, data): + self.messages.append(data) + log.debug('-> ' + data) + + def takeMsgs(self): + """Returns a list of messages sent by the protocol module since + the last takeMsgs() call, so we can track what has been sent.""" + msgs = self.messages + self.messages = [] + return msgs + + def takeCommands(self, msgs): + """Returns a list of commands parsed from the output of takeMsgs().""" + sidprefix = ':' + self.sid + commands = [] + for m in msgs: + args = m.split() + if m.startswith(sidprefix): + commands.append(args[1]) + else: + commands.append(args[0]) + return commands + + def takeHooks(self): + """Returns a list of hook arguments sent by the protocol module since + the last takeHooks() call.""" + hookmsgs = self.hookmsgs + self.hookmsgs = [] + return hookmsgs + + @staticmethod + def dummyhook(irc, source, command, parsed_args): + """Dummy function to bind to hooks.""" + irc.hookmsgs.append(parsed_args) + +class FakeProto(): + """Dummy protocol module for testing purposes.""" + @staticmethod + def handle_events(irc, data): + pass + + @staticmethod + def connect(irc): + pass diff --git a/tests/test_fakeirc.py b/tests/test_fakeirc.py new file mode 100644 index 0000000..4a640a4 --- /dev/null +++ b/tests/test_fakeirc.py @@ -0,0 +1,31 @@ +import sys +import os +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from log import log + +import classes +import unittest + +# Yes, we're going to even test the testing classes. Testception? I think so. +class TestFakeIRC(unittest.TestCase): + def setUp(self): + self.irc = classes.FakeIRC(classes.FakeProto(), classes.testconf) + + def testFakeIRC(self): + self.irc.run('this should do nothing') + self.irc.send('ADD this message') + self.irc.send(':add THIS message too') + msgs = self.irc.takeMsgs() + self.assertEqual(['ADD this message', ':add THIS message too'], + msgs) + # takeMsgs() clears cached messages queue, so the next call should + # return an empty list. + msgs = self.irc.takeMsgs() + self.assertEqual([], msgs) + + def testFakeIRCtakeCommands(self): + msgs = ['ADD this message', ':9PY THIS message too'] + self.assertEqual(['ADD', 'THIS'], self.irc.takeCommands(msgs)) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_proto_common.py b/tests/test_proto_common.py deleted file mode 100644 index 6dde294..0000000 --- a/tests/test_proto_common.py +++ /dev/null @@ -1,90 +0,0 @@ -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from log import log - -import main -import classes -from collections import defaultdict -import unittest - -global testconf -testconf = {'server': - {'netname': 'fakeirc', - 'ip': '0.0.0.0', - 'port': 7000, - 'recvpass': "abcd", - 'sendpass': "abcd", - 'protocol': "null", - 'hostname': "pylink.unittest", - 'sid': "9PY", - 'channels': ["#pylink"], - } - } - -class FakeIRC(main.Irc): - def connect(self): - self.messages = [] - self.socket = None - - def run(self, data): - """Queues a message to the fake IRC server.""" - log.debug('<- ' + data) - self.proto.handle_events(self, data) - - def send(self, data): - self.messages.append(data) - log.debug('-> ' + data) - - def takeMsgs(self): - """Returns a list of messages sent by the protocol module since - the last takeMsgs() call, so we can track what has been sent.""" - msgs = self.messages - self.messages = [] - return msgs - - def takeCommands(self, msgs): - """Returns a list of commands parsed from the output of takeMsgs().""" - sidprefix = ':' + self.sid - commands = [] - for m in msgs: - args = m.split() - if m.startswith(sidprefix): - commands.append(args[1]) - else: - commands.append(args[0]) - return commands - -class FakeProto(): - """Dummy protocol module for testing purposes.""" - @staticmethod - def handle_events(irc, data): - pass - - @staticmethod - def connect(irc): - pass - -# Yes, we're going to even test the testing classes. Testception? I think so. -class Test_TestProtoCommon(unittest.TestCase): - def setUp(self): - self.irc = FakeIRC(FakeProto(), testconf) - - def testFakeIRC(self): - self.irc.run('this should do nothing') - self.irc.send('ADD this message') - self.irc.send(':add THIS message too') - msgs = self.irc.takeMsgs() - self.assertEqual(['ADD this message', ':add THIS message too'], - msgs) - # takeMsgs() clears cached messages queue, so the next call should - # return an empty list. - msgs = self.irc.takeMsgs() - self.assertEqual([], msgs) - - def testFakeIRCtakeCommands(self): - msgs = ['ADD this message', ':9PY THIS message too'] - self.assertEqual(['ADD', 'THIS'], self.irc.takeCommands(msgs)) - -if __name__ == '__main__': - unittest.main()