Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: tests/lib/unittest/unittest_test.dart

Issue 10692051: Some more changes that came out of writing an article on mocks. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« lib/unittest/mock.dart ('K') | « lib/unittest/operator_matchers.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // TODO(gram): 5 // TODO(gram):
6 // Unfortunately I can't seem to test anything that involves timeouts, e.g. 6 // Unfortunately I can't seem to test anything that involves timeouts, e.g.
7 // insufficient callbacks, because the timeout is controlled externally 7 // insufficient callbacks, because the timeout is controlled externally
8 // (test.dart?), and we would need to use a shorter timeout for the inner tests 8 // (test.dart?), and we would need to use a shorter timeout for the inner tests
9 // so the outer timeout doesn't fire. So I removed all such tests. 9 // so the outer timeout doesn't fire. So I removed all such tests.
10 // I'd like to revisit this at some point. 10 // I'd like to revisit this at some point.
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 if (++_testconfig.count < 10) { 135 if (++_testconfig.count < 10) {
136 _defer(_callback); 136 _defer(_callback);
137 } 137 }
138 }, 138 },
139 () => (_testconfig.count == 10)); 139 () => (_testconfig.count == 10));
140 _defer(_callback); 140 _defer(_callback);
141 }); 141 });
142 } else if (testName == 'mock test 1 (Mock)') { 142 } else if (testName == 'mock test 1 (Mock)') {
143 test(testName, () { 143 test(testName, () {
144 var m = new Mock(); 144 var m = new Mock();
145 print(m.length);
146 m.getLogs(callsTo('get length')).verify(happenedOnce);
145 147
146 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); 148 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B');
147 m.when(callsTo('foo', 1, 1)).thenReturn('C'); 149 m.when(callsTo('foo', 1, 1)).thenReturn('C');
148 m.when(callsTo('foo', 9, anything)).thenReturn('D'); 150 m.when(callsTo('foo', 9, anything)).thenReturn('D');
149 m.when(callsTo('bar', anything, anything)).thenReturn('E'); 151 m.when(callsTo('bar', anything, anything)).thenReturn('E');
150 m.when(callsTo('foobar')).thenReturn('F'); 152 m.when(callsTo('foobar')).thenReturn('F');
151 153
152 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}' 154 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}'
153 '${m.bar(1,1)}${m.foo(1,2)}'; 155 '${m.bar(1,1)}${m.foo(1,2)}';
154 getLogs(m, callsTo('foo', anything, anything)).verify(calledExactly(4)); 156 m.getLogs(callsTo('foo', anything, anything)).verify(happenedExactly(4)) ;
Siggi Cherem (dart-lang) 2012/06/29 23:50:50 80 col
155 getLogs(m, callsTo('foo', 1, anything)).verify(calledExactly(3)); 157 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3));
156 getLogs(m, callsTo('foo', 9, anything)).verify(calledOnce); 158 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce);
157 getLogs(m, callsTo('foo', anything, 2)).verify(calledExactly(2)); 159 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2));
158 getLogs(m, callsTo('foobar')).verify(neverCalled); 160 m.getLogs(callsTo('foobar')).verify(neverHappened);
159 getLogs(m, callsTo('foo', 10, anything)).verify(neverCalled); 161 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened);
162 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))).
163 verify(happenedExactly(2));
160 expect(s, 'ACDEB'); 164 expect(s, 'ACDEB');
165
161 }); 166 });
162 } else if (testName == 'mock test 2 (MockList)') { 167 } else if (testName == 'mock test 2 (MockList)') {
163 test(testName, () { 168 test(testName, () {
164 var l = new MockList(); 169 var l = new MockList();
165 l.when(callsTo('length')).thenReturn(1); 170 l.when(callsTo('length')).thenReturn(1);
166 l.when(callsTo('add', anything)).alwaysReturn(0); 171 l.when(callsTo('add', anything)).alwaysReturn(0);
167 l.add('foo'); 172 l.add('foo');
168 expect(l.length(), 1); 173 expect(l.length(), 1);
169 174
170 var m = new MockList(); 175 var m = new MockList();
171 m.when(callsTo('add', anything)).alwaysReturn(0); 176 m.when(callsTo('add', anything)).alwaysReturn(0);
172 177
173 m.add('foo'); 178 m.add('foo');
174 m.add('bar'); 179 m.add('bar');
175 180
176 getLogs(m, callsTo('add')).verify(calledExactly(2)); 181 m.getLogs(callsTo('add')).verify(happenedExactly(2));
177 getLogs(m, callsTo('add', 'foo')).verify(calledOnce); 182 m.getLogs(callsTo('add', 'foo')).verify(happenedOnce);
178 }); 183 });
179 } else if (testName == 'mock test 3 (Spy)') { 184 } else if (testName == 'mock test 3 (Spy)') {
180 test(testName, () { 185 test(testName, () {
181 var p = new FooSpy(); 186 var p = new FooSpy();
182 p.sum(1, 2, 3); 187 p.sum(1, 2, 3);
183 getLogs(p, callsTo('sum')).verify(calledOnce); 188 p.getLogs(callsTo('sum')).verify(happenedOnce);
184 p.sum(2, 2, 2); 189 p.sum(2, 2, 2);
185 getLogs(p, callsTo('sum')).verify(calledExactly(2)); 190 p.getLogs(callsTo('sum')).verify(happenedExactly(2));
186 getLogs(p, callsTo('sum')).verify(sometimeReturned(6)); 191 p.getLogs(callsTo('sum')).verify(sometimeReturned(6));
187 getLogs(p, callsTo('sum')).verify(alwaysReturned(6)); 192 p.getLogs(callsTo('sum')).verify(alwaysReturned(6));
188 getLogs(p, callsTo('sum')).verify(neverReturned(5)); 193 p.getLogs(callsTo('sum')).verify(neverReturned(5));
189 p.sum(2, 2, 1); 194 p.sum(2, 2, 1);
190 getLogs(p, callsTo('sum')).verify(sometimeReturned(5)); 195 p.getLogs(callsTo('sum')).verify(sometimeReturned(5));
191 }); 196 });
192 } else if (testName == 'mock test 4 (Excess calls)') { 197 } else if (testName == 'mock test 4 (Excess calls)') {
193 test(testName, () { 198 test(testName, () {
194 var m = new Mock(); 199 var m = new Mock();
195 m.when(callsTo('foo')).alwaysReturn(null); 200 m.when(callsTo('foo')).alwaysReturn(null);
196 m.foo(); 201 m.foo();
197 m.foo(); 202 m.foo();
198 getLogs(m, callsTo('foo')).verify(calledOnce); 203 m.getLogs(callsTo('foo')).verify(happenedOnce);
199 }); 204 });
200 } else if (testName == 'mock test 5 (No behavior)') { 205 } else if (testName == 'mock test 5 (No action)') {
201 test(testName, () { 206 test(testName, () {
202 var m = new Mock(); 207 var m = new Mock();
203 m.when(callsTo('foo')).thenReturn(null); 208 m.when(callsTo('foo')).thenReturn(null);
204 m.foo(); 209 m.foo();
205 m.foo(); 210 m.foo();
206 }); 211 });
207 } else if (testName == 'mock test 6 (No matching return)') { 212 } else if (testName == 'mock test 6 (No matching return)') {
208 test(testName, () { 213 test(testName, () {
209 var p = new FooSpy(); 214 var p = new FooSpy();
210 p.sum(1, 2, 3); 215 p.sum(1, 2, 3);
211 getLogs(p, callsTo('sum')).verify(sometimeReturned(0)); 216 p.getLogs(callsTo('sum')).verify(sometimeReturned(0));
217 });
218 } else if (testName == 'mock test 7 (No behavior)') {
Siggi Cherem (dart-lang) 2012/06/29 23:50:50 another reason to move mock + matchers into sub-li
219 test(testName, () {
220 var m = new Mock(throwIfNoBehavior:true);
221 m.when(callsTo('foo')).thenReturn(null);
222 m.foo();
223 m.bar();
212 }); 224 });
213 } 225 }
214 }); 226 });
215 } 227 }
216 228
217 void nextTest(int testNum) { 229 void nextTest(int testNum) {
218 SendPort sport = spawnFunction(runTest); 230 SendPort sport = spawnFunction(runTest);
219 sport.call(tests[testNum]).then((msg) { 231 sport.call(tests[testNum]).then((msg) {
220 actual.add(msg); 232 actual.add(msg);
221 if (actual.length == expected.length) { 233 if (actual.length == expected.length) {
(...skipping 15 matching lines...) Expand all
237 'setup test', 249 'setup test',
238 'teardown test', 250 'teardown test',
239 'setup and teardown test', 251 'setup and teardown test',
240 'correct callback test', 252 'correct callback test',
241 'excess callback test', 253 'excess callback test',
242 'completion test', 254 'completion test',
243 'mock test 1 (Mock)', 255 'mock test 1 (Mock)',
244 'mock test 2 (MockList)', 256 'mock test 2 (MockList)',
245 'mock test 3 (Spy)', 257 'mock test 3 (Spy)',
246 'mock test 4 (Excess calls)', 258 'mock test 4 (Excess calls)',
247 'mock test 5 (No behavior)', 259 'mock test 5 (No action)',
248 'mock test 6 (No matching return)' 260 'mock test 6 (No matching return)',
261 'mock test 7 (No behavior)'
249 ]; 262 ];
250 263
251 expected = [ 264 expected = [
252 buildStatusString(1, 0, 0, tests[0]), 265 buildStatusString(1, 0, 0, tests[0]),
253 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), 266 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'),
254 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), 267 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'),
255 buildStatusString(2, 0, 0, 'a a::a b b'), 268 buildStatusString(2, 0, 0, 'a a::a b b'),
256 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), 269 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'),
257 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), 270 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'),
258 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, 271 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0,
259 'setup', 'teardown'), 272 'setup', 'teardown'),
260 buildStatusString(1, 0, 0, tests[7], 1), 273 buildStatusString(1, 0, 0, tests[7], 1),
261 buildStatusString(0, 0, 1, tests[8], 1, 274 buildStatusString(0, 0, 1, tests[8], 1,
262 message: 'Callback called more times than expected (2 > 1)'), 275 message: 'Callback called more times than expected (2 > 1)'),
263 buildStatusString(1, 0, 0, tests[9], 10), 276 buildStatusString(1, 0, 0, tests[9], 10),
264 buildStatusString(1, 0, 0, tests[10]), 277 buildStatusString(1, 0, 0, tests[10]),
265 buildStatusString(1, 0, 0, tests[11]), 278 buildStatusString(1, 0, 0, tests[11]),
266 buildStatusString(1, 0, 0, tests[12]), 279 buildStatusString(1, 0, 0, tests[12]),
267 buildStatusString(0, 1, 0, tests[13], 280 buildStatusString(0, 1, 0, tests[13],
268 message: 'Expected foo() to be called 1 times but:' 281 message: 'Expected foo() to be called 1 times but:'
269 ' was called 2 times'), 282 ' was called 2 times'),
270 buildStatusString(0, 1, 0, tests[14], 283 buildStatusString(0, 1, 0, tests[14],
271 message: 'Caught Exception: No behavior specified for method foo'), 284 message: 'Caught Exception: No more actions for method foo'),
272 buildStatusString(0, 1, 0, tests[15], 285 buildStatusString(0, 1, 0, tests[15],
273 message: 'Expected sum() to sometimes return <0> but: never did') 286 message: 'Expected sum() to sometimes return <0> but: never did'),
287 buildStatusString(0, 1, 0, tests[16],
288 message: 'Caught Exception: No behavior specified for method bar'),
274 ]; 289 ];
275 290
276 actual = []; 291 actual = [];
277 292
278 nextTest(0); 293 nextTest(0);
279 } 294 }
280 295
OLDNEW
« lib/unittest/mock.dart ('K') | « lib/unittest/operator_matchers.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698