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. | |
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((_) { | |
71 asyncEnd(); | |
72 }); | |
73 } | |
74 | |
75 Future asyncTests() async { | |
76 // You can await in both condition and message. | |
77 assert(true, await 0); | |
78 assert(await true, 1); | |
79 assert(await true, await 2); | |
80 | |
81 // Successful asserts won't await/evaluate message. | |
82 void unreachable() => throw "unreachable"; | |
83 assert(await true, await unreachable()); | |
84 | |
85 try { | |
86 assert(false, await 3); | |
87 } on AssertionError catch (e) { | |
88 Expect.equals(3, e.message); | |
89 } | |
90 | |
91 var falseFuture = new Future.value(false); | |
92 var numFuture = new Future.value(4); | |
93 | |
94 try { | |
95 assert(await falseFuture, await numFuture); | |
96 } on AssertionError catch (e) { | |
97 Expect.equals(4, e.message); | |
98 } | |
99 | |
100 try { | |
101 assert(await falseFuture, await new Future.error("error")); | |
102 } on String catch (e) { | |
103 Expect.equals("error", e); | |
104 } | |
105 } | |
OLD | NEW |