OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 | |
7 @AssumeDynamic() @NoInline() | |
8 confuse(x) => x; | |
9 | |
10 | |
11 testFalse(name, fault) { | |
12 try { | |
13 fault(); | |
14 } catch (e) { | |
15 Expect.isTrue(e is AssertionError, '$name: is AssertionError'); | |
16 Expect.isTrue('$e'.contains('Mumble'), '$name: <<$e>> contains "Mumble"'); | |
17 return; | |
18 } | |
19 Expect.fail('Expected assert to throw'); | |
20 } | |
21 | |
22 test1() { | |
23 testFalse('constant false', () { assert(false, 'Mumble'); }); | |
24 } | |
25 | |
26 test2() { | |
27 testFalse('constant function', () { assert(() => false, 'Mumble'); }); | |
28 } | |
29 | |
30 test3() { | |
31 testFalse('variable false', () { assert(confuse(false), 'Mumble'); }); | |
32 } | |
33 | |
34 test4() { | |
35 testFalse('variable function', | |
36 () { assert(confuse(() => false), 'Mumble'); }); | |
37 } | |
38 | |
39 testTypeErrors() { | |
40 check(name, fault) { | |
41 try { | |
42 fault(); | |
43 } catch (e) { | |
44 Expect.isTrue(e is TypeError, | |
45 'name: <<$e>> (${e.runtimeType}) is TypeError'); | |
46 return; | |
47 } | |
48 Expect.fail('Expected assert to throw'); | |
49 } | |
50 | |
51 check('constant type error', () { assert(null, 'Mumble'); }); | |
52 check('variable type error', () { assert(confuse(null), 'Mumble'); }); | |
53 check('function type error', () { assert(confuse(() => null), 'Mumble'); }); | |
54 } | |
55 | |
56 testMessageEffect1() { | |
57 var v = 1; | |
58 // Message is not evaluated on succeeding assert. | |
59 assert(confuse(true), '${v = 123}'); | |
60 Expect.equals(1, v); | |
61 } | |
62 | |
63 testMessageEffect2() { | |
64 var v = 1; | |
65 try { | |
66 // Message is evaluated to produce AssertionError argument on failing | |
67 // assert. | |
68 assert(confuse(false), '${v = 123}'); | |
69 } catch (e) { | |
70 Expect.equals(123, v); | |
71 Expect.isTrue('$e'.contains('123'), '<<$e>> contains "123"'); | |
72 return; | |
73 } | |
74 Expect.fail('Expected assert to throw'); | |
75 } | |
76 | |
77 testMessageEffect3() { | |
78 var v = 1; | |
79 try { | |
80 // Message is evaluated to produce AssertionError argument on failing | |
81 // assert. | |
82 assert(confuse(() => ++v > 100), '${++v}'); | |
83 } catch (e) { | |
84 Expect.equals(3, v); | |
85 Expect.isTrue('$e'.contains('3'), '<<$e>> contains "3"'); | |
86 return; | |
87 } | |
88 Expect.fail('Expected assert to throw'); | |
89 } | |
90 | |
91 bool get checkedMode { | |
92 bool b = false; | |
93 assert((b = true)); | |
94 return b; | |
95 } | |
96 | |
97 main() { | |
98 if (!checkedMode) return; | |
99 | |
100 test1(); | |
101 test2(); | |
102 test3(); | |
103 test4(); | |
104 testTypeErrors(); | |
105 testMessageEffect1(); | |
106 testMessageEffect2(); | |
107 testMessageEffect3(); | |
108 } | |
OLD | NEW |