OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class Expect { | 5 class Expect { |
6 | 6 |
7 /** | 7 /** |
8 * Checks whether the expected and actual values are equal (using [:==:]). | 8 * Checks whether the expected and actual values are equal (using [:==:]). |
9 */ | 9 */ |
10 static void equals(var expected, var actual, [String reason = null]) { | 10 static void equals(var expected, var actual, [String reason = null]) { |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 * Expect.throws(myThrowingFunction, | 225 * Expect.throws(myThrowingFunction, |
226 * (e) { return e is MyException}) | 226 * (e) { return e is MyException}) |
227 * :] | 227 * :] |
228 */ | 228 */ |
229 static void throws(void f(), | 229 static void throws(void f(), |
230 [_CheckExceptionFn check = null, | 230 [_CheckExceptionFn check = null, |
231 String reason = null]) { | 231 String reason = null]) { |
232 try { | 232 try { |
233 f(); | 233 f(); |
234 } catch (var e) { | 234 } catch (var e) { |
235 if (check != null) { | 235 if (check !== null) { |
236 Expect.isTrue(check(e)); | 236 Expect.isTrue(check(e)); |
237 } | 237 } |
238 return; | 238 return; |
239 } | 239 } |
240 String msg = _getMessage(reason); | 240 String msg = _getMessage(reason); |
241 _fail('Expect.throws($msg) fails'); | 241 _fail('Expect.throws($msg) fails'); |
242 } | 242 } |
243 | 243 |
244 static String _getMessage(String reason) | 244 static String _getMessage(String reason) |
245 => (reason === null) ? "" : ", '$reason'"; | 245 => (reason === null) ? "" : ", '$reason'"; |
246 | 246 |
247 static void _fail(String message) { | 247 static void _fail(String message) { |
248 throw new ExpectException(message); | 248 throw new ExpectException(message); |
249 } | 249 } |
250 } | 250 } |
251 | 251 |
252 typedef bool _CheckExceptionFn(exception); | 252 typedef bool _CheckExceptionFn(exception); |
253 | 253 |
254 class ExpectException implements Exception { | 254 class ExpectException implements Exception { |
255 ExpectException(this.message); | 255 ExpectException(this.message); |
256 String toString() => message; | 256 String toString() => message; |
257 String message; | 257 String message; |
258 } | 258 } |
OLD | NEW |