1 |
commit: c8f668f617583998ed87b85f051846036631dbef |
2 |
Author: Michał Górny <mgorny <AT> gentoo <DOT> org> |
3 |
AuthorDate: Tue Aug 2 18:54:02 2011 +0000 |
4 |
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Aug 2 18:54:02 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/pms-test-suite.git;a=commit;h=c8f668f6 |
7 |
|
8 |
Store assertion results. |
9 |
|
10 |
--- |
11 |
pmstestsuite/library/case.py | 109 ++++++++++++++++++++++++++++++++++++++---- |
12 |
1 files changed, 100 insertions(+), 9 deletions(-) |
13 |
|
14 |
diff --git a/pmstestsuite/library/case.py b/pmstestsuite/library/case.py |
15 |
index 956a1a3..5295002 100644 |
16 |
--- a/pmstestsuite/library/case.py |
17 |
+++ b/pmstestsuite/library/case.py |
18 |
@@ -3,7 +3,7 @@ |
19 |
# Released under the terms of the 2-clause BSD license. |
20 |
|
21 |
import copy, random, re |
22 |
-from gentoopm.util import ABCObject |
23 |
+from gentoopm.util import ABCObject, BoolCompat |
24 |
|
25 |
from abc import ABCMeta, abstractmethod, abstractproperty |
26 |
|
27 |
@@ -53,6 +53,92 @@ def cleanup_test_case_name(classname): |
28 |
classname = classname[:-4] |
29 |
return pn_re.sub('\\1-\\2', classname).lower() |
30 |
|
31 |
+class AssertionResult(ABCObject, BoolCompat): |
32 |
+ def __init__(self, name): |
33 |
+ self._name = name |
34 |
+ |
35 |
+ @property |
36 |
+ def name(self): |
37 |
+ return self._name |
38 |
+ |
39 |
+ @abstractproperty |
40 |
+ def expected(self): |
41 |
+ pass |
42 |
+ |
43 |
+ @abstractproperty |
44 |
+ def actual(self): |
45 |
+ pass |
46 |
+ |
47 |
+ @abstractmethod |
48 |
+ def __bool__(self): |
49 |
+ pass |
50 |
+ |
51 |
+ def __str__(self): |
52 |
+ return '%s == %s' % (self.actual, self.expected) |
53 |
+ |
54 |
+class BoolAssertionResult(AssertionResult): |
55 |
+ def __init__(self, name, expect, cond): |
56 |
+ AssertionResult.__init__(self, name) |
57 |
+ self._expect = bool(expect) |
58 |
+ self._cond = bool(cond) |
59 |
+ |
60 |
+ @property |
61 |
+ def expected(self): |
62 |
+ return self._expect |
63 |
+ |
64 |
+ @property |
65 |
+ def actual(self): |
66 |
+ return self._cond |
67 |
+ |
68 |
+ def __bool__(self): |
69 |
+ return self._cond == self._expect |
70 |
+ |
71 |
+class ContainsAssertionResult(AssertionResult): |
72 |
+ def __init__(self, name, needle, container): |
73 |
+ AssertionResult.__init__(self, name) |
74 |
+ self._cont = container |
75 |
+ self._need = needle |
76 |
+ |
77 |
+ @property |
78 |
+ def expected(self): |
79 |
+ return 'contains %s' % repr(self._need) |
80 |
+ |
81 |
+ @property |
82 |
+ def actual(self): |
83 |
+ return repr(self._cont) |
84 |
+ |
85 |
+ def __bool__(self): |
86 |
+ return self._need in self._cont |
87 |
+ |
88 |
+class EqualAssertionResult(AssertionResult): |
89 |
+ def __init__(self, name, expect, value): |
90 |
+ AssertionResult.__init__(self, name) |
91 |
+ self._expect = expect |
92 |
+ self._value = value |
93 |
+ |
94 |
+ @property |
95 |
+ def expected(self): |
96 |
+ return repr(self._expect) |
97 |
+ |
98 |
+ @property |
99 |
+ def actual(self): |
100 |
+ return repr(self._value) |
101 |
+ |
102 |
+ def __bool__(self): |
103 |
+ return self._value == self._expect |
104 |
+ |
105 |
+class NotEqualAssertionResult(EqualAssertionResult): |
106 |
+ def __bool__(self): |
107 |
+ return self._value != self._expect |
108 |
+ |
109 |
+ @property |
110 |
+ def expected(self): |
111 |
+ return 'not %s' % repr(self._expect) |
112 |
+ |
113 |
+ def __str__(self): |
114 |
+ return '%s != %s' % (self.actual, |
115 |
+ repr(self._expect)) |
116 |
+ |
117 |
class TestCase(ABCObject): |
118 |
""" |
119 |
Base class for a test case. |
120 |
@@ -64,6 +150,9 @@ class TestCase(ABCObject): |
121 |
|
122 |
_finalized = False |
123 |
|
124 |
+ def __init__(self): |
125 |
+ self.assertions = [] |
126 |
+ |
127 |
def _finalize(self): |
128 |
""" |
129 |
Do any final modifications to test case data. Mark it finalized. |
130 |
@@ -102,6 +191,11 @@ class TestCase(ABCObject): |
131 |
""" |
132 |
pass |
133 |
|
134 |
+ def _append_assert(self, a): |
135 |
+ self.assertions.append(a) |
136 |
+ if not a: |
137 |
+ raise AssertionError(str(a)) |
138 |
+ |
139 |
def assertTrue(self, cond, msg): |
140 |
""" |
141 |
Assert that the condition evaluates to True. |
142 |
@@ -135,8 +229,7 @@ class TestCase(ABCObject): |
143 |
@param msg: assertion description |
144 |
@type msg: string |
145 |
""" |
146 |
- if bool(cond) != expect: |
147 |
- raise AssertionError(msg) |
148 |
+ self._append_assert(BoolAssertionResult(msg, expect, cond)) |
149 |
|
150 |
def assertContains(self, needle, container, msg = None): |
151 |
""" |
152 |
@@ -151,8 +244,7 @@ class TestCase(ABCObject): |
153 |
""" |
154 |
if msg is None: |
155 |
msg = '%s in %s' % (repr(needle), repr(container)) |
156 |
- if needle not in container: |
157 |
- raise AssertionError(msg) |
158 |
+ self._append_assert(ContainsAssertionResult(msg, needle, container)) |
159 |
|
160 |
def assertEqual(self, value, expect, msg): |
161 |
""" |
162 |
@@ -165,8 +257,7 @@ class TestCase(ABCObject): |
163 |
@param msg: assertion description |
164 |
@type msg: string |
165 |
""" |
166 |
- if value != expect: |
167 |
- raise AssertionError(msg) |
168 |
+ self._append_assert(EqualAssertionResult(msg, expect, value)) |
169 |
|
170 |
def assertNotEqual(self, value, unallowed, msg): |
171 |
""" |
172 |
@@ -179,8 +270,7 @@ class TestCase(ABCObject): |
173 |
@param msg: assertion description |
174 |
@type msg: string |
175 |
""" |
176 |
- if value == unallowed: |
177 |
- raise AssertionError(msg) |
178 |
+ self._append_assert(NotEqualAssertionResult(msg, unallowed, value)) |
179 |
|
180 |
@abstractmethod |
181 |
def check_result(self, pm): |
182 |
@@ -324,6 +414,7 @@ class EbuildTestCase(TestCase): |
183 |
@param eapi: the EAPI |
184 |
@type eapi: string |
185 |
""" |
186 |
+ TestCase.__init__(self) |
187 |
self.eapi = eapi |
188 |
|
189 |
for v in ('ebuild_vars', 'inherits', 'phase_funcs'): |