3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-27 05:03:07 +01:00
PyLink/tests/test_proto_common.py

91 lines
2.6 KiB
Python
Raw Normal View History

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
2015-07-05 22:44:48 +02:00
from log import log
import main
import classes
from collections import defaultdict
import unittest
2015-07-06 04:19:49 +02:00
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"],
}
}
2015-07-06 04:19:49 +02:00
class FakeIRC(main.Irc):
def connect(self):
self.messages = []
2015-07-06 04:19:49 +02:00
self.socket = None
def run(self, data):
"""Queues a message to the fake IRC server."""
2015-07-05 22:44:48 +02:00
log.debug('-> ' + data)
self.proto.handle_events(self, data)
def send(self, data):
self.messages.append(data)
2015-07-05 22:44:48 +02:00
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()