testtools: Fix included testtools, for systems that don't have it.
[nivanova/samba-autobuild/.git] / lib / testtools / testtools / testresult / doubles.py
1 # Copyright (c) 2009 Jonathan M. Lange. See LICENSE for details.
2
3 """Doubles of test result objects, useful for testing unittest code."""
4
5 __all__ = [
6     'Python26TestResult',
7     'Python27TestResult',
8     'ExtendedTestResult',
9     ]
10
11
12 class LoggingBase(object):
13     """Basic support for logging of results."""
14
15     def __init__(self):
16         self._events = []
17         self.shouldStop = False
18
19
20 class Python26TestResult(LoggingBase):
21     """A precisely python 2.6 like test result, that logs."""
22
23     def addError(self, test, err):
24         self._events.append(('addError', test, err))
25
26     def addFailure(self, test, err):
27         self._events.append(('addFailure', test, err))
28
29     def addSuccess(self, test):
30         self._events.append(('addSuccess', test))
31
32     def startTest(self, test):
33         self._events.append(('startTest', test))
34
35     def stop(self):
36         self.shouldStop = True
37
38     def stopTest(self, test):
39         self._events.append(('stopTest', test))
40
41
42 class Python27TestResult(Python26TestResult):
43     """A precisely python 2.7 like test result, that logs."""
44
45     def addExpectedFailure(self, test, err):
46         self._events.append(('addExpectedFailure', test, err))
47
48     def addSkip(self, test, reason):
49         self._events.append(('addSkip', test, reason))
50
51     def addUnexpectedSuccess(self, test):
52         self._events.append(('addUnexpectedSuccess', test))
53
54     def startTestRun(self):
55         self._events.append(('startTestRun',))
56
57     def stopTestRun(self):
58         self._events.append(('stopTestRun',))
59
60
61 class ExtendedTestResult(Python27TestResult):
62     """A test result like the proposed extended unittest result API."""
63
64     def addError(self, test, err=None, details=None):
65         self._events.append(('addError', test, err or details))
66
67     def addFailure(self, test, err=None, details=None):
68         self._events.append(('addFailure', test, err or details))
69
70     def addExpectedFailure(self, test, err=None, details=None):
71         self._events.append(('addExpectedFailure', test, err or details))
72
73     def addSkip(self, test, reason=None, details=None):
74         self._events.append(('addSkip', test, reason or details))
75
76     def addSuccess(self, test, details=None):
77         if details:
78             self._events.append(('addSuccess', test, details))
79         else:
80             self._events.append(('addSuccess', test))
81
82     def addUnexpectedSuccess(self, test, details=None):
83         if details is not None:
84             self._events.append(('addUnexpectedSuccess', test, details))
85         else:
86             self._events.append(('addUnexpectedSuccess', test))
87
88     def progress(self, offset, whence):
89         self._events.append(('progress', offset, whence))
90
91     def tags(self, new_tags, gone_tags):
92         self._events.append(('tags', new_tags, gone_tags))
93
94     def time(self, time):
95         self._events.append(('time', time))