xref: /openbsd-src/gnu/llvm/lldb/third_party/Python/module/unittest2/unittest2/test/support.py (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1import sys
2import warnings
3
4import unittest2
5
6
7def resultFactory(*_):
8    return unittest2.TestResult()
9
10
11class OldTestResult(object):
12    """An object honouring TestResult before startTestRun/stopTestRun."""
13
14    def __init__(self, *_):
15        self.failures = []
16        self.errors = []
17        self.testsRun = 0
18        self.shouldStop = False
19
20    def startTest(self, test):
21        pass
22
23    def stopTest(self, test):
24        pass
25
26    def addError(self, test, err):
27        self.errors.append((test, err))
28
29    def addFailure(self, test, err):
30        self.failures.append((test, err))
31
32    def addSuccess(self, test):
33        pass
34
35    def wasSuccessful(self):
36        return True
37
38    def printErrors(self):
39        pass
40
41
42class LoggingResult(unittest2.TestResult):
43
44    def __init__(self, log):
45        self._events = log
46        super(LoggingResult, self).__init__()
47
48    def startTest(self, test):
49        self._events.append('startTest')
50        super(LoggingResult, self).startTest(test)
51
52    def startTestRun(self):
53        self._events.append('startTestRun')
54        super(LoggingResult, self).startTestRun()
55
56    def stopTest(self, test):
57        self._events.append('stopTest')
58        super(LoggingResult, self).stopTest(test)
59
60    def stopTestRun(self):
61        self._events.append('stopTestRun')
62        super(LoggingResult, self).stopTestRun()
63
64    def addFailure(self, *args):
65        self._events.append('addFailure')
66        super(LoggingResult, self).addFailure(*args)
67
68    def addSuccess(self, *args):
69        self._events.append('addSuccess')
70        super(LoggingResult, self).addSuccess(*args)
71
72    def addError(self, *args):
73        self._events.append('addError')
74        super(LoggingResult, self).addError(*args)
75
76    def addSkip(self, *args):
77        self._events.append('addSkip')
78        super(LoggingResult, self).addSkip(*args)
79
80    def addExpectedFailure(self, *args):
81        self._events.append('addExpectedFailure')
82        super(LoggingResult, self).addExpectedFailure(*args)
83
84    def addUnexpectedSuccess(self, *args):
85        self._events.append('addUnexpectedSuccess')
86        super(LoggingResult, self).addUnexpectedSuccess(*args)
87
88
89class EqualityMixin(object):
90    """Used as a mixin for TestCase"""
91
92    # Check for a valid __eq__ implementation
93    def test_eq(self):
94        for obj_1, obj_2 in self.eq_pairs:
95            self.assertEqual(obj_1, obj_2)
96            self.assertEqual(obj_2, obj_1)
97
98    # Check for a valid __ne__ implementation
99    def test_ne(self):
100        for obj_1, obj_2 in self.ne_pairs:
101            self.assertNotEqual(obj_1, obj_2)
102            self.assertNotEqual(obj_2, obj_1)
103
104
105class HashingMixin(object):
106    """Used as a mixin for TestCase"""
107
108    # Check for a valid __hash__ implementation
109    def test_hash(self):
110        for obj_1, obj_2 in self.eq_pairs:
111            try:
112                if not hash(obj_1) == hash(obj_2):
113                    self.fail("%r and %r do not hash equal" % (obj_1, obj_2))
114            except KeyboardInterrupt:
115                raise
116            except Exception as e:
117                self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e))
118
119        for obj_1, obj_2 in self.ne_pairs:
120            try:
121                if hash(obj_1) == hash(obj_2):
122                    self.fail("%s and %s hash equal, but shouldn't" %
123                              (obj_1, obj_2))
124            except KeyboardInterrupt:
125                raise
126            except Exception as e:
127                self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
128
129
130# copied from Python 2.6
131try:
132    from warnings import catch_warnings
133except ImportError:
134    class catch_warnings(object):
135
136        def __init__(self, record=False, module=None):
137            self._record = record
138            self._module = sys.modules['warnings']
139            self._entered = False
140
141        def __repr__(self):
142            args = []
143            if self._record:
144                args.append("record=True")
145            name = type(self).__name__
146            return "%s(%s)" % (name, ", ".join(args))
147
148        def __enter__(self):
149            if self._entered:
150                raise RuntimeError("Cannot enter %r twice" % self)
151            self._entered = True
152            self._filters = self._module.filters
153            self._module.filters = self._filters[:]
154            self._showwarning = self._module.showwarning
155            if self._record:
156                log = []
157
158                def showwarning(*args, **kwargs):
159                    log.append(WarningMessage(*args, **kwargs))
160                self._module.showwarning = showwarning
161                return log
162            else:
163                return None
164
165        def __exit__(self, *exc_info):
166            if not self._entered:
167                raise RuntimeError(
168                    "Cannot exit %r without entering first" %
169                    self)
170            self._module.filters = self._filters
171            self._module.showwarning = self._showwarning
172
173    class WarningMessage(object):
174        _WARNING_DETAILS = (
175            "message",
176            "category",
177            "filename",
178            "lineno",
179            "file",
180            "line")
181
182        def __init__(self, message, category, filename, lineno, file=None,
183                     line=None):
184            local_values = locals()
185            for attr in self._WARNING_DETAILS:
186                setattr(self, attr, local_values[attr])
187            self._category_name = None
188            if category.__name__:
189                self._category_name = category.__name__
190