summaryrefslogtreecommitdiff
path: root/unit_tests/mock.py
blob: 98e7d436f3b454be3e2eba779f69a9317e8eda08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import imp
import sys
from nose.config import Config
from nose import proxy
from nose.plugins.manager import NoPlugins
from nose.util import odict


def mod(name):
    m = imp.new_module(name)
    sys.modules[name] = m
    return m
    
class ResultProxyFactory:
    def __call__(self, result, test):
        return ResultProxy(result, test)


class ResultProxy(proxy.ResultProxy):
    called = []
    def __init__(self, result, test):
        self.result = result
        self.test = test
    def afterTest(self, test):
        self.assertMyTest(test)
        self.called.append(('afterTest', test))
    def beforeTest(self, test):
        self.assertMyTest(test)
        self.called.append(('beforeTest', test))
    def startTest(self, test):
        print "proxy startTest"
        self.assertMyTest(test)
        self.called.append(('startTest', test))
    def stopTest(self, test):
        print "proxy stopTest"
        self.assertMyTest(test)
        self.called.append(('stopTest', test))
    def addDeprecated(self, test, err):
        print "proxy addDeprecated"
        self.assertMyTest(test)
        self.called.append(('addDeprecated', test, err))
    def addError(self, test, err):
        print "proxy addError"
        self.assertMyTest(test)
        self.called.append(('addError', test, err))
    def addFailure(self, test, err):
        print "proxy addFailure"
        self.assertMyTest(test)
        self.called.append(('addFailure', test, err))
    def addSkip(self, test, err):
        print "proxy addSkip"
        self.assertMyTest(test)
        self.called.append(('addSkip', test, err))
    def addSuccess(self, test):
        self.assertMyTest(test)
        self.called.append(('addSuccess', test))
    

class RecordingPluginManager(object):

    def __init__(self):
        self.reset()

    def __getattr__(self, call):
        return RecordingPluginProxy(self, call)

    def null_call(self, call, *arg, **kw):
        return getattr(self._nullPluginManager, call)(*arg, **kw)

    def reset(self):
        self._nullPluginManager = NoPlugins()
        self.called = odict()

    def calls(self):
        return self.called.keys()


class RecordingPluginProxy(object):

    def __init__(self, manager, call):
        self.man = manager
        self.call = call

    def __call__(self, *arg, **kw):
        self.man.called.setdefault(self.call, []).append((arg, kw))
        return self.man.null_call(self.call, *arg, **kw)


class Bucket(object):
    def __init__(self, **kw):
        self.__dict__['d'] = {}
        self.__dict__['d'].update(kw)
        
    def __getattr__(self, attr):
        if not self.__dict__.has_key('d'):
            return None
        return self.__dict__['d'].get(attr)

    def __setattr__(self, attr, val):        
        self.d[attr] = val


class MockOptParser(object):
    def __init__(self):
        self.opts = []
    def add_option(self, *args, **kw):
        self.opts.append((args, kw))