mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 01:09:22 +01:00
Move all the FakeIRC stuff to classes.py, update FakeIRC with the ability to get hook data
This commit is contained in:
parent
a177a58ae7
commit
8f75c5363c
75
classes.py
75
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
|
||||
|
31
tests/test_fakeirc.py
Normal file
31
tests/test_fakeirc.py
Normal file
@ -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()
|
@ -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()
|
Loading…
Reference in New Issue
Block a user