| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:test/src/backend/state.dart'; | 7 import 'package:test/src/backend/state.dart'; |
| 8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 | 9 |
| 10 import '../utils.dart'; | 10 import '../utils.dart'; |
| 11 | 11 |
| 12 void main() { | 12 void main() { |
| 13 group("supports a function with this many arguments:", () { | 13 group("supports a function with this many arguments:", () { |
| 14 test("0", () async { | 14 test("0", () async { |
| 15 var callbackRun = false; | 15 var callbackRun = false; |
| 16 var liveTest = await runTestBody(() { | 16 var liveTest = await runTestBody(() { |
| 17 expectAsync(() { | 17 expectAsync0(() { |
| 18 callbackRun = true; | 18 callbackRun = true; |
| 19 })(); | 19 })(); |
| 20 }); | 20 }); |
| 21 | 21 |
| 22 expectTestPassed(liveTest); | 22 expectTestPassed(liveTest); |
| 23 expect(callbackRun, isTrue); | 23 expect(callbackRun, isTrue); |
| 24 }); | 24 }); |
| 25 | 25 |
| 26 test("1", () async { | 26 test("1", () async { |
| 27 var callbackRun = false; | 27 var callbackRun = false; |
| 28 var liveTest = await runTestBody(() { | 28 var liveTest = await runTestBody(() { |
| 29 expectAsync((arg) { | 29 expectAsync1((arg) { |
| 30 expect(arg, equals(1)); | 30 expect(arg, equals(1)); |
| 31 callbackRun = true; | 31 callbackRun = true; |
| 32 })(1); | 32 })(1); |
| 33 }); | 33 }); |
| 34 | 34 |
| 35 expectTestPassed(liveTest); | 35 expectTestPassed(liveTest); |
| 36 expect(callbackRun, isTrue); | 36 expect(callbackRun, isTrue); |
| 37 }); | 37 }); |
| 38 | 38 |
| 39 test("2", () async { | 39 test("2", () async { |
| 40 var callbackRun = false; | 40 var callbackRun = false; |
| 41 var liveTest = await runTestBody(() { | 41 var liveTest = await runTestBody(() { |
| 42 expectAsync((arg1, arg2) { | 42 expectAsync2((arg1, arg2) { |
| 43 expect(arg1, equals(1)); | 43 expect(arg1, equals(1)); |
| 44 expect(arg2, equals(2)); | 44 expect(arg2, equals(2)); |
| 45 callbackRun = true; | 45 callbackRun = true; |
| 46 })(1, 2); | 46 })(1, 2); |
| 47 }); | 47 }); |
| 48 | 48 |
| 49 expectTestPassed(liveTest); | 49 expectTestPassed(liveTest); |
| 50 expect(callbackRun, isTrue); | 50 expect(callbackRun, isTrue); |
| 51 }); | 51 }); |
| 52 | 52 |
| 53 test("3", () async { | 53 test("3", () async { |
| 54 var callbackRun = false; | 54 var callbackRun = false; |
| 55 var liveTest = await runTestBody(() { | 55 var liveTest = await runTestBody(() { |
| 56 expectAsync((arg1, arg2, arg3) { | 56 expectAsync3((arg1, arg2, arg3) { |
| 57 expect(arg1, equals(1)); | 57 expect(arg1, equals(1)); |
| 58 expect(arg2, equals(2)); | 58 expect(arg2, equals(2)); |
| 59 expect(arg3, equals(3)); | 59 expect(arg3, equals(3)); |
| 60 callbackRun = true; | 60 callbackRun = true; |
| 61 })(1, 2, 3); | 61 })(1, 2, 3); |
| 62 }); | 62 }); |
| 63 | 63 |
| 64 expectTestPassed(liveTest); | 64 expectTestPassed(liveTest); |
| 65 expect(callbackRun, isTrue); | 65 expect(callbackRun, isTrue); |
| 66 }); | 66 }); |
| 67 | 67 |
| 68 test("4", () async { | 68 test("4", () async { |
| 69 var callbackRun = false; | 69 var callbackRun = false; |
| 70 var liveTest = await runTestBody(() { | 70 var liveTest = await runTestBody(() { |
| 71 expectAsync((arg1, arg2, arg3, arg4) { | 71 expectAsync4((arg1, arg2, arg3, arg4) { |
| 72 expect(arg1, equals(1)); | 72 expect(arg1, equals(1)); |
| 73 expect(arg2, equals(2)); | 73 expect(arg2, equals(2)); |
| 74 expect(arg3, equals(3)); | 74 expect(arg3, equals(3)); |
| 75 expect(arg4, equals(4)); | 75 expect(arg4, equals(4)); |
| 76 callbackRun = true; | 76 callbackRun = true; |
| 77 })(1, 2, 3, 4); | 77 })(1, 2, 3, 4); |
| 78 }); | 78 }); |
| 79 | 79 |
| 80 expectTestPassed(liveTest); | 80 expectTestPassed(liveTest); |
| 81 expect(callbackRun, isTrue); | 81 expect(callbackRun, isTrue); |
| 82 }); | 82 }); |
| 83 | 83 |
| 84 test("5", () async { | 84 test("5", () async { |
| 85 var callbackRun = false; | 85 var callbackRun = false; |
| 86 var liveTest = await runTestBody(() { | 86 var liveTest = await runTestBody(() { |
| 87 expectAsync((arg1, arg2, arg3, arg4, arg5) { | 87 expectAsync5((arg1, arg2, arg3, arg4, arg5) { |
| 88 expect(arg1, equals(1)); | 88 expect(arg1, equals(1)); |
| 89 expect(arg2, equals(2)); | 89 expect(arg2, equals(2)); |
| 90 expect(arg3, equals(3)); | 90 expect(arg3, equals(3)); |
| 91 expect(arg4, equals(4)); | 91 expect(arg4, equals(4)); |
| 92 expect(arg5, equals(5)); | 92 expect(arg5, equals(5)); |
| 93 callbackRun = true; | 93 callbackRun = true; |
| 94 })(1, 2, 3, 4, 5); | 94 })(1, 2, 3, 4, 5); |
| 95 }); | 95 }); |
| 96 | 96 |
| 97 expectTestPassed(liveTest); | 97 expectTestPassed(liveTest); |
| 98 expect(callbackRun, isTrue); | 98 expect(callbackRun, isTrue); |
| 99 }); | 99 }); |
| 100 | 100 |
| 101 test("6", () async { | 101 test("6", () async { |
| 102 var callbackRun = false; | 102 var callbackRun = false; |
| 103 var liveTest = await runTestBody(() { | 103 var liveTest = await runTestBody(() { |
| 104 expectAsync((arg1, arg2, arg3, arg4, arg5, arg6) { | 104 expectAsync6((arg1, arg2, arg3, arg4, arg5, arg6) { |
| 105 expect(arg1, equals(1)); | 105 expect(arg1, equals(1)); |
| 106 expect(arg2, equals(2)); | 106 expect(arg2, equals(2)); |
| 107 expect(arg3, equals(3)); | 107 expect(arg3, equals(3)); |
| 108 expect(arg4, equals(4)); | 108 expect(arg4, equals(4)); |
| 109 expect(arg5, equals(5)); | 109 expect(arg5, equals(5)); |
| 110 expect(arg6, equals(6)); | 110 expect(arg6, equals(6)); |
| 111 callbackRun = true; | 111 callbackRun = true; |
| 112 })(1, 2, 3, 4, 5, 6); | 112 })(1, 2, 3, 4, 5, 6); |
| 113 }); | 113 }); |
| 114 | 114 |
| 115 expectTestPassed(liveTest); | 115 expectTestPassed(liveTest); |
| 116 expect(callbackRun, isTrue); | 116 expect(callbackRun, isTrue); |
| 117 }); | 117 }); |
| 118 }); | 118 }); |
| 119 | 119 |
| 120 group("with optional arguments", () { | 120 group("with optional arguments", () { |
| 121 test("allows them to be passed", () async { | 121 test("allows them to be passed", () async { |
| 122 var callbackRun = false; | 122 var callbackRun = false; |
| 123 var liveTest = await runTestBody(() { | 123 var liveTest = await runTestBody(() { |
| 124 expectAsync(([arg = 1]) { | 124 expectAsync1(([arg = 1]) { |
| 125 expect(arg, equals(2)); | 125 expect(arg, equals(2)); |
| 126 callbackRun = true; | 126 callbackRun = true; |
| 127 })(2); | 127 })(2); |
| 128 }); | 128 }); |
| 129 | 129 |
| 130 expectTestPassed(liveTest); | 130 expectTestPassed(liveTest); |
| 131 expect(callbackRun, isTrue); | 131 expect(callbackRun, isTrue); |
| 132 }); | 132 }); |
| 133 | 133 |
| 134 test("allows them not to be passed", () async { | 134 test("allows them not to be passed", () async { |
| 135 var callbackRun = false; | 135 var callbackRun = false; |
| 136 var liveTest = await runTestBody(() { | 136 var liveTest = await runTestBody(() { |
| 137 expectAsync(([arg = 1]) { | 137 expectAsync1(([arg = 1]) { |
| 138 expect(arg, equals(1)); | 138 expect(arg, equals(1)); |
| 139 callbackRun = true; | 139 callbackRun = true; |
| 140 })(); | 140 })(); |
| 141 }); | 141 }); |
| 142 | 142 |
| 143 expectTestPassed(liveTest); | 143 expectTestPassed(liveTest); |
| 144 expect(callbackRun, isTrue); | 144 expect(callbackRun, isTrue); |
| 145 }); | 145 }); |
| 146 }); | 146 }); |
| 147 | 147 |
| 148 test("doesn't support a function with 7 arguments", () { | 148 test("doesn't support a function with 7 arguments", () { |
| 149 expect(() => expectAsync((_1, _2, _3, _4, _5, _6, _7) {}), | 149 expect(() => expectAsync((_1, _2, _3, _4, _5, _6, _7) {}), |
| 150 throwsArgumentError); | 150 throwsArgumentError); |
| 151 }); | 151 }); |
| 152 | 152 |
| 153 group("by default", () { | 153 group("by default", () { |
| 154 test("won't allow the test to complete until it's called", () { | 154 test("won't allow the test to complete until it's called", () { |
| 155 return expectTestBlocks( | 155 return expectTestBlocks( |
| 156 () => expectAsync(() {}), | 156 () => expectAsync0(() {}), |
| 157 (callback) => callback()); | 157 (callback) => callback()); |
| 158 }); | 158 }); |
| 159 | 159 |
| 160 test("may only be called once", () async { | 160 test("may only be called once", () async { |
| 161 var liveTest = await runTestBody(() { | 161 var liveTest = await runTestBody(() { |
| 162 var callback = expectAsync(() {}); | 162 var callback = expectAsync0(() {}); |
| 163 callback(); | 163 callback(); |
| 164 callback(); | 164 callback(); |
| 165 }); | 165 }); |
| 166 | 166 |
| 167 expectTestFailed(liveTest, | 167 expectTestFailed(liveTest, |
| 168 "Callback called more times than expected (1)."); | 168 "Callback called more times than expected (1)."); |
| 169 }); | 169 }); |
| 170 }); | 170 }); |
| 171 | 171 |
| 172 group("with count", () { | 172 group("with count", () { |
| 173 test("won't allow the test to complete until it's called at least that " | 173 test("won't allow the test to complete until it's called at least that " |
| 174 "many times", () async { | 174 "many times", () async { |
| 175 var liveTest; | 175 var liveTest; |
| 176 var future; | 176 var future; |
| 177 liveTest = createTest(() { | 177 liveTest = createTest(() { |
| 178 var callback = expectAsync(() {}, count: 3); | 178 var callback = expectAsync0(() {}, count: 3); |
| 179 | 179 |
| 180 future = new Future.sync(() async { | 180 future = new Future.sync(() async { |
| 181 await pumpEventQueue(); | 181 await pumpEventQueue(); |
| 182 expect(liveTest.state.status, equals(Status.running)); | 182 expect(liveTest.state.status, equals(Status.running)); |
| 183 callback(); | 183 callback(); |
| 184 | 184 |
| 185 await pumpEventQueue(); | 185 await pumpEventQueue(); |
| 186 expect(liveTest.state.status, equals(Status.running)); | 186 expect(liveTest.state.status, equals(Status.running)); |
| 187 callback(); | 187 callback(); |
| 188 | 188 |
| 189 await pumpEventQueue(); | 189 await pumpEventQueue(); |
| 190 expect(liveTest.state.status, equals(Status.running)); | 190 expect(liveTest.state.status, equals(Status.running)); |
| 191 callback(); | 191 callback(); |
| 192 }); | 192 }); |
| 193 }); | 193 }); |
| 194 | 194 |
| 195 await liveTest.run(); | 195 await liveTest.run(); |
| 196 expectTestPassed(liveTest); | 196 expectTestPassed(liveTest); |
| 197 // Ensure that the outer test doesn't complete until the inner future | 197 // Ensure that the outer test doesn't complete until the inner future |
| 198 // completes. | 198 // completes. |
| 199 await future; | 199 await future; |
| 200 }); | 200 }); |
| 201 | 201 |
| 202 test("will throw an error if it's called more than that many times", () asyn
c { | 202 test("will throw an error if it's called more than that many times", () asyn
c { |
| 203 var liveTest = await runTestBody(() { | 203 var liveTest = await runTestBody(() { |
| 204 var callback = expectAsync(() {}, count: 3); | 204 var callback = expectAsync0(() {}, count: 3); |
| 205 callback(); | 205 callback(); |
| 206 callback(); | 206 callback(); |
| 207 callback(); | 207 callback(); |
| 208 callback(); | 208 callback(); |
| 209 }); | 209 }); |
| 210 | 210 |
| 211 expectTestFailed( | 211 expectTestFailed( |
| 212 liveTest, "Callback called more times than expected (3)."); | 212 liveTest, "Callback called more times than expected (3)."); |
| 213 }); | 213 }); |
| 214 | 214 |
| 215 group("0,", () { | 215 group("0,", () { |
| 216 test("won't block the test's completion", () { | 216 test("won't block the test's completion", () { |
| 217 expectAsync(() {}, count: 0); | 217 expectAsync0(() {}, count: 0); |
| 218 }); | 218 }); |
| 219 | 219 |
| 220 test("will throw an error if it's ever called", () async { | 220 test("will throw an error if it's ever called", () async { |
| 221 var liveTest = await runTestBody(() { | 221 var liveTest = await runTestBody(() { |
| 222 expectAsync(() {}, count: 0)(); | 222 expectAsync0(() {}, count: 0)(); |
| 223 }); | 223 }); |
| 224 | 224 |
| 225 expectTestFailed( | 225 expectTestFailed( |
| 226 liveTest, "Callback called more times than expected (0)."); | 226 liveTest, "Callback called more times than expected (0)."); |
| 227 }); | 227 }); |
| 228 }); | 228 }); |
| 229 }); | 229 }); |
| 230 | 230 |
| 231 group("with max", () { | 231 group("with max", () { |
| 232 test("will allow the callback to be called that many times", () { | 232 test("will allow the callback to be called that many times", () { |
| 233 var callback = expectAsync(() {}, max: 3); | 233 var callback = expectAsync0(() {}, max: 3); |
| 234 callback(); | 234 callback(); |
| 235 callback(); | 235 callback(); |
| 236 callback(); | 236 callback(); |
| 237 }); | 237 }); |
| 238 | 238 |
| 239 test("will allow the callback to be called fewer than that many times", () { | 239 test("will allow the callback to be called fewer than that many times", () { |
| 240 var callback = expectAsync(() {}, max: 3); | 240 var callback = expectAsync0(() {}, max: 3); |
| 241 callback(); | 241 callback(); |
| 242 }); | 242 }); |
| 243 | 243 |
| 244 test("will throw an error if it's called more than that many times", | 244 test("will throw an error if it's called more than that many times", |
| 245 () async { | 245 () async { |
| 246 var liveTest = await runTestBody(() { | 246 var liveTest = await runTestBody(() { |
| 247 var callback = expectAsync(() {}, max: 3); | 247 var callback = expectAsync0(() {}, max: 3); |
| 248 callback(); | 248 callback(); |
| 249 callback(); | 249 callback(); |
| 250 callback(); | 250 callback(); |
| 251 callback(); | 251 callback(); |
| 252 }); | 252 }); |
| 253 | 253 |
| 254 expectTestFailed( | 254 expectTestFailed( |
| 255 liveTest, "Callback called more times than expected (3)."); | 255 liveTest, "Callback called more times than expected (3)."); |
| 256 }); | 256 }); |
| 257 | 257 |
| 258 test("-1, will allow the callback to be called any number of times", () { | 258 test("-1, will allow the callback to be called any number of times", () { |
| 259 var callback = expectAsync(() {}, max: -1); | 259 var callback = expectAsync0(() {}, max: -1); |
| 260 for (var i = 0; i < 20; i++) { | 260 for (var i = 0; i < 20; i++) { |
| 261 callback(); | 261 callback(); |
| 262 } | 262 } |
| 263 }); | 263 }); |
| 264 }); | 264 }); |
| 265 | 265 |
| 266 test("will throw an error if max is less than count", () { | 266 test("will throw an error if max is less than count", () { |
| 267 expect(() => expectAsync(() {}, max: 1, count: 2), | 267 expect(() => expectAsync0(() {}, max: 1, count: 2), |
| 268 throwsArgumentError); | 268 throwsArgumentError); |
| 269 }); | 269 }); |
| 270 | 270 |
| 271 group("expectAsyncUntil()", () { | 271 group("expectAsyncUntil()", () { |
| 272 test("won't allow the test to complete until isDone returns true", | 272 test("won't allow the test to complete until isDone returns true", |
| 273 () async { | 273 () async { |
| 274 var liveTest; | 274 var liveTest; |
| 275 var future; | 275 var future; |
| 276 liveTest = createTest(() { | 276 liveTest = createTest(() { |
| 277 var done = false; | 277 var done = false; |
| 278 var callback = expectAsyncUntil(() {}, () => done); | 278 var callback = expectAsyncUntil0(() {}, () => done); |
| 279 | 279 |
| 280 future = new Future.sync(() async { | 280 future = new Future.sync(() async { |
| 281 await pumpEventQueue(); | 281 await pumpEventQueue(); |
| 282 expect(liveTest.state.status, equals(Status.running)); | 282 expect(liveTest.state.status, equals(Status.running)); |
| 283 callback(); | 283 callback(); |
| 284 await pumpEventQueue(); | 284 await pumpEventQueue(); |
| 285 expect(liveTest.state.status, equals(Status.running)); | 285 expect(liveTest.state.status, equals(Status.running)); |
| 286 done = true; | 286 done = true; |
| 287 callback(); | 287 callback(); |
| 288 }); | 288 }); |
| 289 }); | 289 }); |
| 290 | 290 |
| 291 await liveTest.run(); | 291 await liveTest.run(); |
| 292 expectTestPassed(liveTest); | 292 expectTestPassed(liveTest); |
| 293 // Ensure that the outer test doesn't complete until the inner future | 293 // Ensure that the outer test doesn't complete until the inner future |
| 294 // completes. | 294 // completes. |
| 295 await future; | 295 await future; |
| 296 }); | 296 }); |
| 297 | 297 |
| 298 test("doesn't call isDone until after the callback is called", () { | 298 test("doesn't call isDone until after the callback is called", () { |
| 299 var callbackRun = false; | 299 var callbackRun = false; |
| 300 expectAsyncUntil(() => callbackRun = true, () { | 300 expectAsyncUntil0(() => callbackRun = true, () { |
| 301 expect(callbackRun, isTrue); | 301 expect(callbackRun, isTrue); |
| 302 return true; | 302 return true; |
| 303 })(); | 303 })(); |
| 304 }); | 304 }); |
| 305 }); | 305 }); |
| 306 | 306 |
| 307 group("with errors", () { | 307 group("with errors", () { |
| 308 test("reports them to the current test", () async { | 308 test("reports them to the current test", () async { |
| 309 var liveTest = await runTestBody(() { | 309 var liveTest = await runTestBody(() { |
| 310 expectAsync(() => throw new TestFailure('oh no'))(); | 310 expectAsync0(() => throw new TestFailure('oh no'))(); |
| 311 }); | 311 }); |
| 312 | 312 |
| 313 expectTestFailed(liveTest, 'oh no'); | 313 expectTestFailed(liveTest, 'oh no'); |
| 314 }); | 314 }); |
| 315 | 315 |
| 316 test("swallows them and returns null", () async { | 316 test("swallows them and returns null", () async { |
| 317 var returnValue; | 317 var returnValue; |
| 318 var caughtError = false; | 318 var caughtError = false; |
| 319 var liveTest = await runTestBody(() { | 319 var liveTest = await runTestBody(() { |
| 320 try { | 320 try { |
| 321 returnValue = expectAsync(() => throw new TestFailure('oh no'))(); | 321 returnValue = expectAsync0(() => throw new TestFailure('oh no'))(); |
| 322 } on TestFailure catch (_) { | 322 } on TestFailure catch (_) { |
| 323 caughtError = true; | 323 caughtError = true; |
| 324 } | 324 } |
| 325 }); | 325 }); |
| 326 | 326 |
| 327 expectTestFailed(liveTest, 'oh no'); | 327 expectTestFailed(liveTest, 'oh no'); |
| 328 expect(returnValue, isNull); | 328 expect(returnValue, isNull); |
| 329 expect(caughtError, isFalse); | 329 expect(caughtError, isFalse); |
| 330 }); | 330 }); |
| 331 }); | 331 }); |
| 332 } | 332 } |
| OLD | NEW |