OLD | NEW |
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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 test(testName, () { | 132 test(testName, () { |
133 var _callback; | 133 var _callback; |
134 _callback = expectAsyncUntil0(() { | 134 _callback = expectAsyncUntil0(() { |
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.startsWith('mock test 1 ')) { |
143 test(testName, () { | 143 test(testName, () { |
144 var m = new Mock(); | 144 var m = new Mock(); |
145 print(m.length); | 145 print(m.length); |
146 m.getLogs(callsTo('get length')).verify(happenedOnce); | 146 m.getLogs(callsTo('get length')).verify(happenedOnce); |
147 | 147 |
148 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); | 148 m.when(callsTo('foo', 1, 2)).thenReturn('A').thenReturn('B'); |
149 m.when(callsTo('foo', 1, 1)).thenReturn('C'); | 149 m.when(callsTo('foo', 1, 1)).thenReturn('C'); |
150 m.when(callsTo('foo', 9, anything)).thenReturn('D'); | 150 m.when(callsTo('foo', 9, anything)).thenReturn('D'); |
151 m.when(callsTo('bar', anything, anything)).thenReturn('E'); | 151 m.when(callsTo('bar', anything, anything)).thenReturn('E'); |
152 m.when(callsTo('foobar')).thenReturn('F'); | 152 m.when(callsTo('foobar')).thenReturn('F'); |
153 | 153 |
154 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)}' |
155 '${m.bar(1,1)}${m.foo(1,2)}'; | 155 '${m.bar(1,1)}${m.foo(1,2)}'; |
156 m.getLogs(callsTo('foo', anything, anything)). | 156 m.getLogs(callsTo('foo', anything, anything)). |
157 verify(happenedExactly(4)); | 157 verify(happenedExactly(4)); |
158 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3)); | 158 m.getLogs(callsTo('foo', 1, anything)).verify(happenedExactly(3)); |
159 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce); | 159 m.getLogs(callsTo('foo', 9, anything)).verify(happenedOnce); |
160 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2)); | 160 m.getLogs(callsTo('foo', anything, 2)).verify(happenedExactly(2)); |
161 m.getLogs(callsTo('foobar')).verify(neverHappened); | 161 m.getLogs(callsTo('foobar')).verify(neverHappened); |
162 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened); | 162 m.getLogs(callsTo('foo', 10, anything)).verify(neverHappened); |
163 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))). | 163 m.getLogs(callsTo('foo'), returning(anyOf('A', 'C'))). |
164 verify(happenedExactly(2)); | 164 verify(happenedExactly(2)); |
165 expect(s, 'ACDEB'); | 165 expect(s, 'ACDEB'); |
166 }); | 166 }); |
167 } else if (testName == 'mock test 2 (MockList)') { | 167 } else if (testName.startsWith('mock test 2 ')) { |
168 test(testName, () { | 168 test(testName, () { |
169 var l = new MockList(); | 169 var l = new MockList(); |
170 l.when(callsTo('length')).thenReturn(1); | 170 l.when(callsTo('get length')).thenReturn(1); |
171 l.when(callsTo('add', anything)).alwaysReturn(0); | 171 l.when(callsTo('add', anything)).alwaysReturn(0); |
172 l.add('foo'); | 172 l.add('foo'); |
173 expect(l.length(), 1); | 173 expect(l.length, 1); |
174 | 174 |
175 var m = new MockList(); | 175 var m = new MockList(); |
176 m.when(callsTo('add', anything)).alwaysReturn(0); | 176 m.when(callsTo('add', anything)).alwaysReturn(0); |
177 | 177 |
178 m.add('foo'); | 178 m.add('foo'); |
179 m.add('bar'); | 179 m.add('bar'); |
180 | 180 |
181 m.getLogs(callsTo('add')).verify(happenedExactly(2)); | 181 m.getLogs(callsTo('add')).verify(happenedExactly(2)); |
182 m.getLogs(callsTo('add', 'foo')).verify(happenedOnce); | 182 m.getLogs(callsTo('add', 'foo')).verify(happenedOnce); |
183 }); | 183 }); |
184 } else if (testName == 'mock test 3 (Spy)') { | 184 } else if (testName.startsWith('mock test 3 ')) { |
185 test(testName, () { | 185 test(testName, () { |
186 var p = new FooSpy(); | 186 var p = new FooSpy(); |
187 p.sum(1, 2, 3); | 187 p.sum(1, 2, 3); |
188 p.getLogs(callsTo('sum')).verify(happenedOnce); | 188 p.getLogs(callsTo('sum')).verify(happenedOnce); |
189 p.sum(2, 2, 2); | 189 p.sum(2, 2, 2); |
190 p.getLogs(callsTo('sum')).verify(happenedExactly(2)); | 190 p.getLogs(callsTo('sum')).verify(happenedExactly(2)); |
191 p.getLogs(callsTo('sum')).verify(sometimeReturned(6)); | 191 p.getLogs(callsTo('sum')).verify(sometimeReturned(6)); |
192 p.getLogs(callsTo('sum')).verify(alwaysReturned(6)); | 192 p.getLogs(callsTo('sum')).verify(alwaysReturned(6)); |
193 p.getLogs(callsTo('sum')).verify(neverReturned(5)); | 193 p.getLogs(callsTo('sum')).verify(neverReturned(5)); |
194 p.sum(2, 2, 1); | 194 p.sum(2, 2, 1); |
195 p.getLogs(callsTo('sum')).verify(sometimeReturned(5)); | 195 p.getLogs(callsTo('sum')).verify(sometimeReturned(5)); |
196 }); | 196 }); |
197 } else if (testName == 'mock test 4 (Excess calls)') { | 197 } else if (testName.startsWith('mock test 4 ')) { |
198 test(testName, () { | 198 test(testName, () { |
199 var m = new Mock(); | 199 var m = new Mock(); |
200 m.when(callsTo('foo')).alwaysReturn(null); | 200 m.when(callsTo('foo')).alwaysReturn(null); |
201 m.foo(); | 201 m.foo(); |
202 m.foo(); | 202 m.foo(); |
203 m.getLogs(callsTo('foo')).verify(happenedOnce); | 203 m.getLogs(callsTo('foo')).verify(happenedOnce); |
204 }); | 204 }); |
205 } else if (testName == 'mock test 5 (No action)') { | 205 } else if (testName.startsWith('mock test 5 ')) { |
206 test(testName, () { | 206 test(testName, () { |
207 var m = new Mock(); | 207 var m = new Mock(); |
208 m.when(callsTo('foo')).thenReturn(null); | 208 m.when(callsTo('foo')).thenReturn(null); |
209 m.foo(); | 209 m.foo(); |
210 m.foo(); | 210 m.foo(); |
211 }); | 211 }); |
212 } else if (testName == 'mock test 6 (No matching return)') { | 212 } else if (testName.startsWith('mock test 6 ')) { |
213 test(testName, () { | 213 test(testName, () { |
214 var p = new FooSpy(); | 214 var p = new FooSpy(); |
215 p.sum(1, 2, 3); | 215 p.sum(1, 2, 3); |
216 p.getLogs(callsTo('sum')).verify(sometimeReturned(0)); | 216 p.getLogs(callsTo('sum')).verify(sometimeReturned(0)); |
217 }); | 217 }); |
218 } else if (testName == 'mock test 7 (No behavior)') { | 218 } else if (testName.startsWith('mock test 7 ')) { |
219 test(testName, () { | 219 test(testName, () { |
220 var m = new Mock.custom(throwIfNoBehavior:true); | 220 var m = new Mock.custom(throwIfNoBehavior:true); |
221 m.when(callsTo('foo')).thenReturn(null); | 221 m.when(callsTo('foo')).thenReturn(null); |
222 m.foo(); | 222 m.foo(); |
223 m.bar(); | 223 m.bar(); |
224 }); | 224 }); |
225 } else if (testName == 'mock test 8 (Shared log)') { | 225 } else if (testName.startsWith('mock test 8 ')) { |
226 test(testName, () { | 226 test(testName, () { |
227 var log = new LogEntryList(); | 227 var log = new LogEntryList(); |
228 var m1 = new Mock.custom(name:'m1', log:log); | 228 var m1 = new Mock.custom(name:'m1', log:log); |
229 var m2 = new Mock.custom(name:'m2', log:log); | 229 var m2 = new Mock.custom(name:'m2', log:log); |
230 m1.foo(); | 230 m1.foo(); |
231 m2.foo(); | 231 m2.foo(); |
232 m1.bar(); | 232 m1.bar(); |
233 m2.bar(); | 233 m2.bar(); |
234 expect(log.logs.length, 4); | 234 expect(log.logs.length, 4); |
235 log.getMatches(null, callsTo('foo')).verify(happenedExactly(2)); | 235 log.getMatches(anything, callsTo('foo')).verify(happenedExactly(2)); |
236 log.getMatches('m1', callsTo('foo')).verify(happenedOnce); | 236 log.getMatches('m1', callsTo('foo')).verify(happenedOnce); |
237 log.getMatches('m1', callsTo('bar')).verify(happenedOnce); | 237 log.getMatches('m1', callsTo('bar')).verify(happenedOnce); |
238 m2.getLogs(callsTo('foo')).verify(happenedOnce); | 238 m2.getLogs(callsTo('foo')).verify(happenedOnce); |
239 m2.getLogs(callsTo('bar')).verify(happenedOnce); | 239 m2.getLogs(callsTo('bar')).verify(happenedOnce); |
240 }); | 240 }); |
| 241 } else if (testName.startsWith('mock test 9 ')) { |
| 242 test(testName, () { |
| 243 var m = new Mock(); |
| 244 m.when(callsTo(null, 1)).alwaysReturn(2); |
| 245 m.when(callsTo(null, 2)).alwaysReturn(4); |
| 246 expect(m.foo(1), 2); |
| 247 expect(m.foo(2), 4); |
| 248 expect(m.bar(1), 2); |
| 249 expect(m.bar(2), 4); |
| 250 m.getLogs(callsTo()).verify(happenedExactly(4)); |
| 251 m.getLogs(callsTo(null, 1)).verify(happenedExactly(2)); |
| 252 m.getLogs(callsTo(null, 2)).verify(happenedExactly(2)); |
| 253 m.getLogs(null, returning(1)).verify(neverHappened); |
| 254 m.getLogs(null, returning(2)).verify(happenedExactly(2)); |
| 255 m.getLogs(null, returning(4)).verify(happenedExactly(2)); |
| 256 }); |
| 257 } else if (testName.startsWith('mock test 10 ')) { |
| 258 test(testName, () { |
| 259 var m = new Mock(); |
| 260 m.when(callsTo(matches('^[A-Z]'))). |
| 261 alwaysThrow('Method names must start with lower case'); |
| 262 m.test(); |
| 263 }); |
| 264 } else if (testName.startsWith('mock test 11 ')) { |
| 265 test(testName, () { |
| 266 var m = new Mock(); |
| 267 m.when(callsTo(matches('^[A-Z]'))). |
| 268 alwaysThrow('Method names must start with lower case'); |
| 269 m.Test(); |
| 270 }); |
241 } | 271 } |
242 }); | 272 }); |
243 } | 273 } |
244 | 274 |
245 void nextTest(int testNum) { | 275 void nextTest(int testNum) { |
246 SendPort sport = spawnFunction(runTest); | 276 SendPort sport = spawnFunction(runTest); |
247 sport.call(tests[testNum]).then((msg) { | 277 sport.call(tests[testNum]).then((msg) { |
248 actual.add(msg); | 278 actual.add(msg); |
249 if (actual.length == expected.length) { | 279 if (actual.length == expected.length) { |
250 for (var i = 0; i < tests.length; i++) { | 280 for (var i = 0; i < tests.length; i++) { |
(...skipping 17 matching lines...) Expand all Loading... |
268 'correct callback test', | 298 'correct callback test', |
269 'excess callback test', | 299 'excess callback test', |
270 'completion test', | 300 'completion test', |
271 'mock test 1 (Mock)', | 301 'mock test 1 (Mock)', |
272 'mock test 2 (MockList)', | 302 'mock test 2 (MockList)', |
273 'mock test 3 (Spy)', | 303 'mock test 3 (Spy)', |
274 'mock test 4 (Excess calls)', | 304 'mock test 4 (Excess calls)', |
275 'mock test 5 (No action)', | 305 'mock test 5 (No action)', |
276 'mock test 6 (No matching return)', | 306 'mock test 6 (No matching return)', |
277 'mock test 7 (No behavior)', | 307 'mock test 7 (No behavior)', |
278 'mock test 8 (Shared log)' | 308 'mock test 8 (Shared log)', |
| 309 'mock test 9 (Null CallMatcher)', |
| 310 'mock test 10 (RegExp CallMatcher Good)', |
| 311 'mock test 11 (RegExp CallMatcher Bad)' |
279 ]; | 312 ]; |
280 | 313 |
281 expected = [ | 314 expected = [ |
282 buildStatusString(1, 0, 0, tests[0]), | 315 buildStatusString(1, 0, 0, tests[0]), |
283 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), | 316 buildStatusString(0, 1, 0, tests[1], message: 'Expected: <5> but: was <4>'), |
284 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), | 317 buildStatusString(0, 1, 0, tests[2], message: 'Caught Exception: fail'), |
285 buildStatusString(2, 0, 0, 'a a::a b b'), | 318 buildStatusString(2, 0, 0, 'a a::a b b'), |
286 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), | 319 buildStatusString(1, 0, 0, 'a ${tests[4]}', 0, 'setup'), |
287 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), | 320 buildStatusString(1, 0, 0, 'a ${tests[5]}', 0, '', 'teardown'), |
288 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, | 321 buildStatusString(1, 0, 0, 'a ${tests[6]}', 0, |
289 'setup', 'teardown'), | 322 'setup', 'teardown'), |
290 buildStatusString(1, 0, 0, tests[7], 1), | 323 buildStatusString(1, 0, 0, tests[7], 1), |
291 buildStatusString(0, 0, 1, tests[8], 1, | 324 buildStatusString(0, 0, 1, tests[8], 1, |
292 message: 'Callback called more times than expected (2 > 1)'), | 325 message: 'Callback called more times than expected (2 > 1)'), |
293 buildStatusString(1, 0, 0, tests[9], 10), | 326 buildStatusString(1, 0, 0, tests[9], 10), |
294 buildStatusString(1, 0, 0, tests[10]), | 327 buildStatusString(1, 0, 0, tests[10]), |
295 buildStatusString(1, 0, 0, tests[11]), | 328 buildStatusString(1, 0, 0, tests[11]), |
296 buildStatusString(1, 0, 0, tests[12]), | 329 buildStatusString(1, 0, 0, tests[12]), |
297 buildStatusString(0, 1, 0, tests[13], | 330 buildStatusString(0, 1, 0, tests[13], |
298 message: 'Expected foo() to be called 1 times but:' | 331 message: "Expected <null>.'foo'() to be called 1 times but:" |
299 ' was called 2 times'), | 332 " was called 2 times"), |
300 buildStatusString(0, 1, 0, tests[14], | 333 buildStatusString(0, 1, 0, tests[14], |
301 message: 'Caught Exception: No more actions for method foo'), | 334 message: 'Caught Exception: No more actions for method foo'), |
302 buildStatusString(0, 1, 0, tests[15], | 335 buildStatusString(0, 1, 0, tests[15], message: |
303 message: 'Expected sum() to sometimes return <0> but: never did'), | 336 "Expected <null>.'sum'() to sometimes return <0> but: never did"), |
304 buildStatusString(0, 1, 0, tests[16], | 337 buildStatusString(0, 1, 0, tests[16], |
305 message: 'Caught Exception: No behavior specified for method bar'), | 338 message: 'Caught Exception: No behavior specified for method bar'), |
306 buildStatusString(1, 0, 0, tests[17]) | 339 buildStatusString(1, 0, 0, tests[17]), |
| 340 buildStatusString(1, 0, 0, tests[18]), |
| 341 buildStatusString(1, 0, 0, tests[19]), |
| 342 buildStatusString(0, 1, 0, tests[20], |
| 343 message:'Caught Method names must start with lower case') |
307 ]; | 344 ]; |
308 | 345 |
309 actual = []; | 346 actual = []; |
310 | 347 |
311 nextTest(0); | 348 nextTest(0); |
312 } | 349 } |
313 | 350 |
OLD | NEW |