From 198a0ca738d18980901b674fd89849fa5eba79f6 Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Sat, 9 Aug 2008 00:24:08 +0000 Subject: moved testing libraries into their own directory --- test/lib/__init__.py | 39 +++++ test/lib/data.py | 35 ++++ test/lib/mock.py | 467 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/lib/mocks.py | 98 +++++++++++ test/lib/notify.py | 17 ++ 5 files changed, 656 insertions(+) create mode 100644 test/lib/__init__.py create mode 100755 test/lib/data.py create mode 100644 test/lib/mock.py create mode 100644 test/lib/mocks.py create mode 100644 test/lib/notify.py (limited to 'test/lib') diff --git a/test/lib/__init__.py b/test/lib/__init__.py new file mode 100644 index 000000000..3a1300e12 --- /dev/null +++ b/test/lib/__init__.py @@ -0,0 +1,39 @@ +import sys +import os.path + +gajim_root = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../..') + +# look for modules in the CWD, then gajim/test/lib, then gajim/src, then everywhere else +sys.path.insert(1, gajim_root + '/src') +sys.path.insert(1, gajim_root + '/test/lib') + +# a temporary version of ~/.gajim for testing +configdir = gajim_root + '/test/tmp' + +# define _ for i18n +import __builtin__ +__builtin__._ = lambda x: x + +import os + +def setup_env(): + # wipe config directory + if os.path.isdir(configdir): + import shutil + shutil.rmtree(configdir) + + os.mkdir(configdir) + + import common.configpaths + common.configpaths.gajimpaths.init(configdir) + common.configpaths.gajimpaths.init_profile() + + # for some reason common.gajim needs to be imported before xmpppy? + from common import gajim + + gajim.DATA_DIR = gajim_root + '/data' + + import gtkgui_helpers + gtkgui_helpers.GLADE_DIR = gajim_root + '/data/glade' + +# vim: se ts=3: diff --git a/test/lib/data.py b/test/lib/data.py new file mode 100755 index 000000000..d7c19af5f --- /dev/null +++ b/test/lib/data.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +account1 = u'acc1' +account2 = u'Cool"chârßéµö' + +contacts = {} +contacts[account1] = { + u'default1@gajim.org': { + 'ask': None, 'groups': [], 'name': None, 'resources': {}, + 'subscription': u'both'}, + u'default2@gajim.org': { + 'ask': None, 'groups': [u'GroupA',], 'name': None, 'resources': {}, + 'subscription': u'both'}, + u'Cool"chârßéµö@gajim.org': { + 'ask': None, 'groups': [u' len(args) and not varargs: + raise MockInterfaceError("Original %s() takes at most %s arguments (%s given)" % + (name, len(args), numPosCallParams)) + + # Get the number of positional arguments that appear in the call, + # also check for duplicate parameters and unknown parameters + numPosSeen = _getNumPosSeenAndCheck(numPosCallParams, callKwParams, args, varkw) + + lenArgsNoDefaults = len(args) - len(defaults or []) + if numPosSeen < lenArgsNoDefaults: + raise MockInterfaceError("Original %s() takes at least %s arguments (%s given)" % (name, lenArgsNoDefaults, numPosSeen)) + + def mockGetAllCalls(self): + """ + Return a list of MockCall objects, + representing all the methods in the order they were called. + """ + return self.mockAllCalledMethods + getAllCalls = mockGetAllCalls # deprecated - kept for backward compatibility + + def mockGetNamedCalls(self, methodName): + """ + Return a list of MockCall objects, + representing all the calls to the named method in the order they were called. + """ + return self.mockCalledMethods.get(methodName, []) + getNamedCalls = mockGetNamedCalls # deprecated - kept for backward compatibility + + def mockCheckCall(self, index, name, *args, **kwargs): + '''test that the index-th call had the specified name and parameters''' + call = self.mockAllCalledMethods[index] + assert name == call.getName(), "%r != %r" % (name, call.getName()) + call.checkArgs(*args, **kwargs) + + +def _getNumPosSeenAndCheck(numPosCallParams, callKwParams, args, varkw): + """ + Positional arguments can appear as call parameters either named as + a named (keyword) parameter, or just as a value to be matched by + position. Count the positional arguments that are given by either + keyword or position, and check for duplicate specifications. + Also check for arguments specified by keyword that do not appear + in the method's parameter list. + """ + posSeen = {} + for arg in args[:numPosCallParams]: + posSeen[arg] = True + for kwp in callKwParams: + if posSeen.has_key(kwp): + raise MockInterfaceError("%s appears as both a positional and named parameter." % kwp) + if kwp in args: + posSeen[kwp] = True + elif not varkw: + raise MockInterfaceError("Original method does not have a parameter '%s'" % kwp) + return len(posSeen) + +class MockCall: + """ + MockCall records the name and parameters of a call to an instance + of a Mock class. Instances of MockCall are created by the Mock class, + but can be inspected later as part of the test. + """ + def __init__(self, name, params, kwparams ): + self.name = name + self.params = params + self.kwparams = kwparams + + def checkArgs(self, *args, **kwargs): + assert args == self.params, "%r != %r" % (args, self.params) + assert kwargs == self.kwparams, "%r != %r" % (kwargs, self.kwparams) + + def getParam( self, n ): + if isinstance(n, int): + return self.params[n] + elif isinstance(n, str): + return self.kwparams[n] + else: + raise IndexError, 'illegal index type for getParam' + + def getNumParams(self): + return len(self.params) + + def getNumKwParams(self): + return len(self.kwparams) + + def getName(self): + return self.name + + #pretty-print the method call + def __str__(self): + s = self.name + "(" + sep = '' + for p in self.params: + s = s + sep + repr(p) + sep = ', ' + items = self.kwparams.items() + items.sort() + for k,v in items: + s = s + sep + k + '=' + repr(v) + sep = ', ' + s = s + ')' + return s + def __repr__(self): + return self.__str__() + +class MockCallable: + """ + Intercepts the call and records it, then delegates to either the mock's + dictionary of mock return values that was passed in to the constructor, + or a handcrafted method of a Mock subclass. + """ + def __init__(self, name, mock, handcrafted=False): + self.name = name + self.mock = mock + self.handcrafted = handcrafted + + def __call__(self, *params, **kwparams): + self.mock._checkInterfaceCall(self.name, params, kwparams) + thisCall = self.recordCall(params,kwparams) + self.checkExpectations(thisCall, params, kwparams) + return self.makeCall(params, kwparams) + + def recordCall(self, params, kwparams): + """ + Record the MockCall in an ordered list of all calls, and an ordered + list of calls for that method name. + """ + thisCall = MockCall(self.name, params, kwparams) + calls = self.mock.mockCalledMethods.setdefault(self.name, []) + calls.append(thisCall) + self.mock.mockAllCalledMethods.append(thisCall) + return thisCall + + def makeCall(self, params, kwparams): + if self.handcrafted: + allPosParams = (self.mock,) + params + func = _findFunc(self.mock.__class__, self.name) + if not func: + raise NotImplementedError + return func(*allPosParams, **kwparams) + else: + returnVal = self.mock.mockReturnValues.get(self.name) + if isinstance(returnVal, ReturnValuesBase): + returnVal = returnVal.next() + return returnVal + + def checkExpectations(self, thisCall, params, kwparams): + if self.name in self.mock.mockExpectations: + callsMade = len(self.mock.mockCalledMethods[self.name]) + for (expectation, after, until) in self.mock.mockExpectations[self.name]: + if callsMade > after and (until==0 or callsMade < until): + assert expectation(self.mock, thisCall, len(self.mock.mockAllCalledMethods)-1), 'Expectation failed: '+str(thisCall) + + +def _findFunc(cl, name): + """ Depth first search for a method with a given name. """ + if cl.__dict__.has_key(name): + return cl.__dict__[name] + for base in cl.__bases__: + func = _findFunc(base, name) + if func: + return func + return None + + + +class ReturnValuesBase: + def next(self): + try: + return self.iter.next() + except StopIteration: + raise AssertionError("No more return values") + def __iter__(self): + return self + +class ReturnValues(ReturnValuesBase): + def __init__(self, *values): + self.iter = iter(values) + + +class ReturnIterator(ReturnValuesBase): + def __init__(self, iterator): + self.iter = iter(iterator) + + +def expectParams(*params, **keywords): + '''check that the callObj is called with specified params and keywords + ''' + def fn(mockObj, callObj, idx): + return callObj.params == params and callObj.kwparams == keywords + return fn + + +def expectAfter(*methods): + '''check that the function is only called after all the functions in 'methods' + ''' + def fn(mockObj, callObj, idx): + calledMethods = [method.getName() for method in mockObj.mockGetAllCalls()] + #skip last entry, since that is the current call + calledMethods = calledMethods[:-1] + for method in methods: + if method not in calledMethods: + return False + return True + return fn + +def expectException(exception, *args, **kwargs): + ''' raise an exception when the method is called + ''' + def fn(mockObj, callObj, idx): + raise exception(*args, **kwargs) + return fn + + +def expectParam(paramIdx, cond): + '''check that the callObj is called with parameter specified by paramIdx (a position index or keyword) + fulfills the condition specified by cond. + cond is a function that takes a single argument, the value to test. + ''' + def fn(mockObj, callObj, idx): + param = callObj.getParam(paramIdx) + return cond(param) + return fn + +def EQ(value): + def testFn(param): + return param == value + return testFn + +def NE(value): + def testFn(param): + return param != value + return testFn + +def GT(value): + def testFn(param): + return param > value + return testFn + +def LT(value): + def testFn(param): + return param < value + return testFn + +def GE(value): + def testFn(param): + return param >= value + return testFn + +def LE(value): + def testFn(param): + return param <= value + return testFn + +def AND(*condlist): + def testFn(param): + for cond in condlist: + if not cond(param): + return False + return True + return testFn + +def OR(*condlist): + def testFn(param): + for cond in condlist: + if cond(param): + return True + return False + return testFn + +def NOT(cond): + def testFn(param): + return not cond(param) + return testFn + +def MATCHES(regex, *args, **kwargs): + compiled_regex = re.compile(regex, *args, **kwargs) + def testFn(param): + return compiled_regex.match(param) != None + return testFn + +def SEQ(*sequence): + iterator = iter(sequence) + def testFn(param): + try: + cond = iterator.next() + except StopIteration: + raise AssertionError('SEQ exhausted') + return cond(param) + return testFn + +def IS(instance): + def testFn(param): + return param is instance + return testFn + +def ISINSTANCE(class_): + def testFn(param): + return isinstance(param, class_) + return testFn + +def ISSUBCLASS(class_): + def testFn(param): + return issubclass(param, class_) + return testFn + +def CONTAINS(val): + def testFn(param): + return val in param + return testFn + +def IN(container): + def testFn(param): + return param in container + return testFn + +def HASATTR(attr): + def testFn(param): + return hasattr(param, attr) + return testFn + +def HASMETHOD(method): + def testFn(param): + return hasattr(param, method) and callable(getattr(param, method)) + return testFn + +CALLABLE = callable + + + + +# vim: se ts=3: \ No newline at end of file diff --git a/test/lib/mocks.py b/test/lib/mocks.py new file mode 100644 index 000000000..5790dbde6 --- /dev/null +++ b/test/lib/mocks.py @@ -0,0 +1,98 @@ +# gajim-specific mock objects +from mock import Mock + +from common import gajim + +class MockConnection(Mock): + def __init__(self, account, *args): + Mock.__init__(self, *args) + self.name = account + self.connected = 2 + self.mood = {} + self.activity = {} + self.tune = {} + self.blocked_contacts = {} + self.blocked_groups = {} + gajim.interface.instances[account] = {'infos': {}, 'disco': {}, 'gc_config': {}, 'search': {}} + gajim.interface.minimized_controls[account] = {} + gajim.contacts.add_account(account) + gajim.groups[account] = {} + gajim.gc_connected[account] = {} + gajim.automatic_rooms[account] = {} + gajim.newly_added[account] = [] + gajim.to_be_removed[account] = [] + gajim.nicks[account] = gajim.config.get_per('accounts', account, 'name') + gajim.block_signed_in_notifications[account] = True + gajim.sleeper_state[account] = 0 + gajim.encrypted_chats[account] = [] + gajim.last_message_time[account] = {} + gajim.status_before_autoaway[account] = '' + gajim.transport_avatar[account] = {} + gajim.gajim_optional_features[account] = [] + gajim.caps_hash[account] = '' + + gajim.connections[account] = self + +class MockWindow(Mock): + def __init__(self, *args): + Mock.__init__(self, *args) + self.window = Mock() + +class MockChatControl(Mock): + def __init__(self, *args): + Mock.__init__(self, *args) + + self.parent_win = MockWindow({'get_active_control': self}) + self.session = None + + def set_session(self, sess): + self.session = sess + + def __nonzero__(self): + return True + + def __eq__(self, other): + return self is other + +class MockInterface(Mock): + def __init__(self, *args): + Mock.__init__(self, *args) + self.msg_win_mgr = Mock() + self.roster = Mock() + + self.remote_ctrl = None + self.instances = {} + self.minimized_controls = {} + self.status_sent_to_users = Mock() + + self.jabber_state_images = {'16': Mock(), '32': Mock(), 'opened': Mock(), 'closed': Mock()} + +class MockLogger(Mock): + def __init__(self): + Mock.__init__(self, {'write': None, 'get_transports_type': {}}) + +class MockContact(Mock): + def __nonzero__(self): + return True + +import random + +class MockSession(Mock): + def __init__(self, conn, jid, thread_id, type): + Mock.__init__(self) + + self.conn = conn + self.jid = jid + self.type = type + self.thread_id = thread_id + + if not self.thread_id: + self.thread_id = '%0x' % random.randint(0, 10000) + + def __repr__(self): + print '' % self.thread_id + + def __nonzero__(self): + return True + +# vim: se ts=3: diff --git a/test/lib/notify.py b/test/lib/notify.py new file mode 100644 index 000000000..f14100af3 --- /dev/null +++ b/test/lib/notify.py @@ -0,0 +1,17 @@ +# mock notify module + +notifications = [] + +def notify(event, jid, account, parameters, advanced_notif_num = None): + notifications.append((event, jid, account, parameters, advanced_notif_num)) + +def get_advanced_notification(event, account, contact): + return None + +def get_show_in_roster(event, account, contact, session = None): + return True + +def get_show_in_systray(event, account, contact, type_ = None): + return True + +# vim: se ts=3: \ No newline at end of file -- cgit v1.2.3