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

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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 var m = new Mock(); 144 var m = new Mock();
145 145
146 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); 146 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B');
147 m.when(callsTo('foo', 1, 1)).thenReturn('C'); 147 m.when(callsTo('foo', 1, 1)).thenReturn('C');
148 m.when(callsTo('foo', 9, anything)).thenReturn('D'); 148 m.when(callsTo('foo', 9, anything)).thenReturn('D');
149 m.when(callsTo('bar', anything, anything)).thenReturn('E'); 149 m.when(callsTo('bar', anything, anything)).thenReturn('E');
150 m.when(callsTo('foobar')).thenReturn('F'); 150 m.when(callsTo('foobar')).thenReturn('F');
151 151
152 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}' 152 var s = '${m.foo(1,2)}${m.foo(1,1)}${m.foo(9,10)}'
153 '${m.bar(1,1)}${m.foo(1,2)}'; 153 '${m.bar(1,1)}${m.foo(1,2)}';
154 getLogs(m, callsTo('foo', anything, anything)).verify(calledExactly(4)); 154 m.getLogs(callsTo('foo', anything, anything)).verify(happenedExactly(4)) ;
Jennifer Messerly 2012/06/29 23:24:09 nit: 80 col
155 getLogs(m, callsTo('foo', 1, anything)).verify(calledExactly(3)); 155 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3));
156 getLogs(m, callsTo('foo', 9, anything)).verify(calledOnce); 156 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce);
157 getLogs(m, callsTo('foo', anything, 2)).verify(calledExactly(2)); 157 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2));
158 getLogs(m, callsTo('foobar')).verify(neverCalled); 158 m.getLogs(callsTo('foobar')).verify(neverHappened);
159 getLogs(m, callsTo('foo', 10, anything)).verify(neverCalled); 159 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened);
160 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))).
161 verify(happenedExactly(2));
160 expect(s, 'ACDEB'); 162 expect(s, 'ACDEB');
161 }); 163 });
162 } else if (testName == 'mock test 2 (MockList)') { 164 } else if (testName == 'mock test 2 (MockList)') {
163 test(testName, () { 165 test(testName, () {
164 var l = new MockList(); 166 var l = new MockList();
165 l.when(callsTo('length')).thenReturn(1); 167 l.when(callsTo('length')).thenReturn(1);
166 l.when(callsTo('add', anything)).alwaysReturn(0); 168 l.when(callsTo('add', anything)).alwaysReturn(0);
167 l.add('foo'); 169 l.add('foo');
168 expect(l.length(), 1); 170 expect(l.length(), 1);
169 171
170 var m = new MockList(); 172 var m = new MockList();
171 m.when(callsTo('add', anything)).alwaysReturn(0); 173 m.when(callsTo('add', anything)).alwaysReturn(0);
172 174
173 m.add('foo'); 175 m.add('foo');
174 m.add('bar'); 176 m.add('bar');
175 177
176 getLogs(m, callsTo('add')).verify(calledExactly(2)); 178 m.getLogs(callsTo('add')).verify(happenedExactly(2));
177 getLogs(m, callsTo('add', 'foo')).verify(calledOnce); 179 m.getLogs(callsTo('add', 'foo')).verify(happenedOnce);
178 }); 180 });
179 } else if (testName == 'mock test 3 (Spy)') { 181 } else if (testName == 'mock test 3 (Spy)') {
180 test(testName, () { 182 test(testName, () {
181 var p = new FooSpy(); 183 var p = new FooSpy();
182 p.sum(1, 2, 3); 184 p.sum(1, 2, 3);
183 getLogs(p, callsTo('sum')).verify(calledOnce); 185 p.getLogs(callsTo('sum')).verify(happenedOnce);
184 p.sum(2, 2, 2); 186 p.sum(2, 2, 2);
185 getLogs(p, callsTo('sum')).verify(calledExactly(2)); 187 p.getLogs(callsTo('sum')).verify(happenedExactly(2));
186 getLogs(p, callsTo('sum')).verify(sometimeReturned(6)); 188 p.getLogs(callsTo('sum')).verify(sometimeReturned(6));
187 getLogs(p, callsTo('sum')).verify(alwaysReturned(6)); 189 p.getLogs(callsTo('sum')).verify(alwaysReturned(6));
188 getLogs(p, callsTo('sum')).verify(neverReturned(5)); 190 p.getLogs(callsTo('sum')).verify(neverReturned(5));
189 p.sum(2, 2, 1); 191 p.sum(2, 2, 1);
190 getLogs(p, callsTo('sum')).verify(sometimeReturned(5)); 192 p.getLogs(callsTo('sum')).verify(sometimeReturned(5));
191 }); 193 });
192 } else if (testName == 'mock test 4 (Excess calls)') { 194 } else if (testName == 'mock test 4 (Excess calls)') {
193 test(testName, () { 195 test(testName, () {
194 var m = new Mock(); 196 var m = new Mock();
195 m.when(callsTo('foo')).alwaysReturn(null); 197 m.when(callsTo('foo')).alwaysReturn(null);
196 m.foo(); 198 m.foo();
197 m.foo(); 199 m.foo();
198 getLogs(m, callsTo('foo')).verify(calledOnce); 200 m.getLogs(callsTo('foo')).verify(happenedOnce);
199 }); 201 });
200 } else if (testName == 'mock test 5 (No behavior)') { 202 } else if (testName == 'mock test 5 (No action)') {
201 test(testName, () { 203 test(testName, () {
202 var m = new Mock(); 204 var m = new Mock();
203 m.when(callsTo('foo')).thenReturn(null); 205 m.when(callsTo('foo')).thenReturn(null);
204 m.foo(); 206 m.foo();
205 m.foo(); 207 m.foo();
206 }); 208 });
207 } else if (testName == 'mock test 6 (No matching return)') { 209 } else if (testName == 'mock test 6 (No matching return)') {
208 test(testName, () { 210 test(testName, () {
209 var p = new FooSpy(); 211 var p = new FooSpy();
210 p.sum(1, 2, 3); 212 p.sum(1, 2, 3);
211 getLogs(p, callsTo('sum')).verify(sometimeReturned(0)); 213 p.getLogs(callsTo('sum')).verify(sometimeReturned(0));
214 });
215 } else if (testName == 'mock test 7 (No behavior)') {
216 test(testName, () {
217 var m = new Mock(throwIfNoBehavior:true);
218 m.when(callsTo('foo')).thenReturn(null);
219 m.foo();
220 m.bar();
212 }); 221 });
213 } 222 }
214 }); 223 });
215 } 224 }
216 225
217 void nextTest(int testNum) { 226 void nextTest(int testNum) {
218 SendPort sport = spawnFunction(runTest); 227 SendPort sport = spawnFunction(runTest);
219 sport.call(tests[testNum]).then((msg) { 228 sport.call(tests[testNum]).then((msg) {
220 actual.add(msg); 229 actual.add(msg);
221 if (actual.length == expected.length) { 230 if (actual.length == expected.length) {
(...skipping 15 matching lines...) Expand all
237 'setup test', 246 'setup test',
238 'teardown test', 247 'teardown test',
239 'setup and teardown test', 248 'setup and teardown test',
240 'correct callback test', 249 'correct callback test',
241 'excess callback test', 250 'excess callback test',
242 'completion test', 251 'completion test',
243 'mock test 1 (Mock)', 252 'mock test 1 (Mock)',
244 'mock test 2 (MockList)', 253 'mock test 2 (MockList)',
245 'mock test 3 (Spy)', 254 'mock test 3 (Spy)',
246 'mock test 4 (Excess calls)', 255 'mock test 4 (Excess calls)',
247 'mock test 5 (No behavior)', 256 'mock test 5 (No action)',
248 'mock test 6 (No matching return)' 257 'mock test 6 (No matching return)',
258 'mock test 7 (No behavior)'
249 ]; 259 ];
250 260
251 expected = [ 261 expected = [
252 buildStatusString(1, 0, 0, tests[0]), 262 buildStatusString(1, 0, 0, tests[0]),
253 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), 263 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'),
254 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), 264 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'),
255 buildStatusString(2, 0, 0, 'a a::a b b'), 265 buildStatusString(2, 0, 0, 'a a::a b b'),
256 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), 266 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'),
257 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), 267 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'),
258 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, 268 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0,
259 'setup', 'teardown'), 269 'setup', 'teardown'),
260 buildStatusString(1, 0, 0, tests[7], 1), 270 buildStatusString(1, 0, 0, tests[7], 1),
261 buildStatusString(0, 0, 1, tests[8], 1, 271 buildStatusString(0, 0, 1, tests[8], 1,
262 message: 'Callback called more times than expected (2 > 1)'), 272 message: 'Callback called more times than expected (2 > 1)'),
263 buildStatusString(1, 0, 0, tests[9], 10), 273 buildStatusString(1, 0, 0, tests[9], 10),
264 buildStatusString(1, 0, 0, tests[10]), 274 buildStatusString(1, 0, 0, tests[10]),
265 buildStatusString(1, 0, 0, tests[11]), 275 buildStatusString(1, 0, 0, tests[11]),
266 buildStatusString(1, 0, 0, tests[12]), 276 buildStatusString(1, 0, 0, tests[12]),
267 buildStatusString(0, 1, 0, tests[13], 277 buildStatusString(0, 1, 0, tests[13],
268 message: 'Expected foo() to be called 1 times but:' 278 message: 'Expected foo() to be called 1 times but:'
269 ' was called 2 times'), 279 ' was called 2 times'),
270 buildStatusString(0, 1, 0, tests[14], 280 buildStatusString(0, 1, 0, tests[14],
271 message: 'Caught Exception: No behavior specified for method foo'), 281 message: 'Caught Exception: No more actions for method foo'),
272 buildStatusString(0, 1, 0, tests[15], 282 buildStatusString(0, 1, 0, tests[15],
273 message: 'Expected sum() to sometimes return <0> but: never did') 283 message: 'Expected sum() to sometimes return <0> but: never did'),
284 buildStatusString(0, 1, 0, tests[16],
285 message: 'Caught Exception: No behavior specified for method bar'),
274 ]; 286 ];
275 287
276 actual = []; 288 actual = [];
277 289
278 nextTest(0); 290 nextTest(0);
279 } 291 }
280 292
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