OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // VMOptions=--enable_type_checks --enable_asserts | |
5 | |
6 // Dart test program testing assert statements. | |
7 | |
8 import "package:expect/expect.dart"; | |
9 | |
10 testTrue() { | |
11 int i = 0; | |
12 try { | |
13 assert(true); | |
14 } on AssertionError catch (error) { | |
15 i = 1; | |
16 } | |
17 return i; | |
18 } | |
19 | |
20 testFalse() { | |
21 int i = 0; | |
22 try { | |
23 assert(false); | |
24 } on AssertionError catch (error) { | |
25 i = 1; | |
26 } | |
27 return i; | |
28 } | |
29 | |
30 unknown(dynamic a) { | |
31 return a ? true : false; | |
32 } | |
33 | |
34 testClosureReturnsFalse() { | |
35 int i = 0; | |
36 try { | |
37 assert(() => false); | |
38 } on AssertionError catch (error) { | |
39 i = 1; | |
40 } | |
41 return i; | |
42 } | |
43 | |
44 testClosure(bool f()) { | |
45 int i = 0; | |
46 try { | |
47 assert(f); | |
48 } on AssertionError catch (error) { | |
49 i = 1; | |
50 } | |
51 return i; | |
52 } | |
53 | |
54 testBoolean(bool value) { | |
55 int i = 0; | |
56 try { | |
57 assert(value); | |
58 } on AssertionError catch (error) { | |
59 i = 1; | |
60 } | |
61 return i; | |
62 } | |
63 | |
64 testDynamic(dynamic value) { | |
65 int i = 0; | |
66 try { | |
67 assert(value); | |
68 } on AssertionError catch (error) { | |
69 i = 1; | |
70 } | |
71 return i; | |
72 } | |
73 | |
74 testMessage(value, message) { | |
75 try { | |
76 assert(value, message); | |
77 return null; | |
78 } on AssertionError catch (error) { | |
79 return error; | |
80 } | |
81 } | |
82 | |
83 main() { | |
84 Expect.equals(0, testTrue()); | |
85 Expect.equals(0, testBoolean(true)); | |
86 Expect.equals(0, testDynamic(unknown(true))); | |
87 Expect.equals(0, testClosure(() => true)); | |
88 Expect.equals(0, testDynamic(() => true)); | |
89 | |
90 Expect.equals(1, testFalse()); | |
91 Expect.equals(1, testBoolean(false)); | |
92 Expect.equals(1, testClosureReturnsFalse()); | |
93 Expect.equals(1, testDynamic(unknown(false))); | |
94 Expect.equals(1, testDynamic(() => false)); | |
95 | |
96 Expect.equals(1, testBoolean(null)); | |
97 Expect.equals(1, testDynamic(null)); | |
98 Expect.equals(1, testDynamic(42)); | |
99 Expect.equals(1, testDynamic(() => 42)); | |
100 Expect.equals(1, testDynamic(() => null)); | |
101 Expect.equals(1, testClosure(() => null)); | |
102 | |
103 Expect.equals(1234, testMessage(false, 1234).message); | |
104 Expect.equals('hi', testMessage(false, 'hi').message); | |
105 Expect.equals(1234, testMessage(() => false, 1234).message); | |
106 Expect.equals('hi', testMessage(() => false, 'hi').message); | |
107 | |
108 // These errors do not have the message because boolean conversion failed. | |
109 Expect.equals(null, testMessage(null, 1234).message); | |
110 Expect.equals(null, testMessage(null, 'hi').message); | |
111 Expect.equals(null, testMessage(() => null, 1234).message); | |
112 Expect.equals(null, testMessage(() => null, 'hi').message); | |
113 Expect.isTrue("${testMessage(42, 1234)}".contains('boolean expression')); | |
114 Expect.isTrue("${testMessage(() => 1, 1234)}".contains('boolean expression')); | |
115 } | |
OLD | NEW |