Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 // SharedOptions=--assert-message | |
| 6 | |
| 7 import "dart:async"; | |
| 8 | |
| 9 import "package:async_helper/async_helper.dart"; | |
| 10 import "package:expect/expect.dart"; | |
| 11 | |
| 12 main() { | |
| 13 // Only run with asserts enabled mode. | |
| 14 bool assertsEnabled = false; | |
| 15 assert(assertsEnabled = true); | |
| 16 if (!assertsEnabled) return; | |
| 17 | |
| 18 // Basics. | |
| 19 assert(true, ""); | |
| 20 assert(() => true, ""); | |
| 21 | |
| 22 int x = null; | |
| 23 // Successful asserts won't execute message. | |
|
regis
2016/12/13 23:26:51
Is this still true when await is involved?
hausner
2016/12/14 16:59:35
Yes. Added test case below in the async test cases
| |
| 24 assert(true, x + 42); | |
| 25 assert(true, throw "unreachable"); | |
| 26 | |
| 27 // Can use any value as message. | |
| 28 try { | |
| 29 assert(false, 42); | |
| 30 } on AssertionError catch (e) { | |
| 31 Expect.equals(42, e.message); | |
| 32 } | |
| 33 | |
| 34 try { | |
| 35 assert(false, ""); | |
| 36 } on AssertionError catch (e) { | |
| 37 Expect.equals("", e.message); | |
| 38 } | |
| 39 | |
| 40 try { | |
| 41 assert(false, null); | |
| 42 } on AssertionError catch (e) { | |
| 43 Expect.equals(null, e.message); | |
| 44 } | |
| 45 | |
| 46 // Test expression can throw. | |
| 47 try { | |
| 48 assert(throw "test", throw "message"); | |
| 49 } on String catch (e) { | |
| 50 Expect.equals("test", e); | |
| 51 } | |
| 52 | |
| 53 // Message expression can throw. | |
| 54 try { | |
| 55 assert(false, throw "message"); | |
| 56 } on String catch (e) { | |
| 57 Expect.equals("message", e); | |
| 58 } | |
| 59 | |
| 60 // Failing asserts evaluate message after test. | |
| 61 var list = []; | |
| 62 try { | |
| 63 assert((list..add(1)).isEmpty, (list..add(3)).length); | |
| 64 } on AssertionError catch (e) { | |
| 65 Expect.equals(2, e.message); | |
| 66 Expect.listEquals([1, 3], list); | |
| 67 } | |
| 68 | |
| 69 asyncStart(); | |
| 70 asyncTests().then((_) { asyncEnd(); }); | |
| 71 } | |
| 72 | |
| 73 Future asyncTests() async { | |
| 74 // You can await in both condition and message. | |
| 75 assert(true, await 0); | |
| 76 assert(await true, 1); | |
| 77 assert(await true, await 2); | |
|
regis
2016/12/13 23:26:51
How about
assert(await true, throw "unreachable");
hausner
2016/12/14 16:59:35
Done.
| |
| 78 | |
| 79 try { | |
| 80 assert(false, await 3); | |
| 81 } on AssertionError catch (e) { | |
| 82 Expect.equals(3, e.message); | |
| 83 } | |
| 84 | |
| 85 var falseFuture = new Future.value(false); | |
| 86 var numFuture = new Future.value(4); | |
| 87 | |
| 88 try { | |
| 89 assert(await falseFuture, await numFuture); | |
| 90 } on AssertionError catch (e) { | |
| 91 Expect.equals(4, e.message); | |
| 92 } | |
| 93 | |
| 94 try { | |
| 95 assert(await falseFuture, await new Future.error("error")); | |
| 96 } on String catch (e) { | |
| 97 Expect.equals("error", e); | |
| 98 } | |
| 99 } | |
| OLD | NEW |