PageRenderTime 26ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/unit_tests/mock.py

https://bitbucket.org/jpellerin/nose/
Python | 107 lines | 85 code | 22 blank | 0 comment | 2 complexity | f9adf5af7e0bd8bd661e4e4fef3ad480 MD5 | raw file
  1import imp
  2import sys
  3from nose.config import Config
  4from nose import proxy
  5from nose.plugins.manager import NoPlugins
  6from nose.util import odict
  7
  8
  9def mod(name):
 10    m = imp.new_module(name)
 11    sys.modules[name] = m
 12    return m
 13    
 14class ResultProxyFactory:
 15    def __call__(self, result, test):
 16        return ResultProxy(result, test)
 17
 18
 19class ResultProxy(proxy.ResultProxy):
 20    called = []
 21    def __init__(self, result, test):
 22        self.result = result
 23        self.test = test
 24    def afterTest(self, test):
 25        self.assertMyTest(test)
 26        self.called.append(('afterTest', test))
 27    def beforeTest(self, test):
 28        self.assertMyTest(test)
 29        self.called.append(('beforeTest', test))
 30    def startTest(self, test):
 31        print "proxy startTest"
 32        self.assertMyTest(test)
 33        self.called.append(('startTest', test))
 34    def stopTest(self, test):
 35        print "proxy stopTest"
 36        self.assertMyTest(test)
 37        self.called.append(('stopTest', test))
 38    def addDeprecated(self, test, err):
 39        print "proxy addDeprecated"
 40        self.assertMyTest(test)
 41        self.called.append(('addDeprecated', test, err))
 42    def addError(self, test, err):
 43        print "proxy addError"
 44        self.assertMyTest(test)
 45        self.called.append(('addError', test, err))
 46    def addFailure(self, test, err):
 47        print "proxy addFailure"
 48        self.assertMyTest(test)
 49        self.called.append(('addFailure', test, err))
 50    def addSkip(self, test, err):
 51        print "proxy addSkip"
 52        self.assertMyTest(test)
 53        self.called.append(('addSkip', test, err))
 54    def addSuccess(self, test):
 55        self.assertMyTest(test)
 56        self.called.append(('addSuccess', test))
 57    
 58
 59class RecordingPluginManager(object):
 60
 61    def __init__(self):
 62        self.reset()
 63
 64    def __getattr__(self, call):
 65        return RecordingPluginProxy(self, call)
 66
 67    def null_call(self, call, *arg, **kw):
 68        return getattr(self._nullPluginManager, call)(*arg, **kw)
 69
 70    def reset(self):
 71        self._nullPluginManager = NoPlugins()
 72        self.called = odict()
 73
 74    def calls(self):
 75        return self.called.keys()
 76
 77
 78class RecordingPluginProxy(object):
 79
 80    def __init__(self, manager, call):
 81        self.man = manager
 82        self.call = call
 83
 84    def __call__(self, *arg, **kw):
 85        self.man.called.setdefault(self.call, []).append((arg, kw))
 86        return self.man.null_call(self.call, *arg, **kw)
 87
 88
 89class Bucket(object):
 90    def __init__(self, **kw):
 91        self.__dict__['d'] = {}
 92        self.__dict__['d'].update(kw)
 93        
 94    def __getattr__(self, attr):
 95        if not self.__dict__.has_key('d'):
 96            return None
 97        return self.__dict__['d'].get(attr)
 98
 99    def __setattr__(self, attr, val):        
100        self.d[attr] = val
101
102
103class MockOptParser(object):
104    def __init__(self):
105        self.opts = []
106    def add_option(self, *args, **kw):
107        self.opts.append((args, kw))