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 |
| 5 // VMOptions=--assert_initializer |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 bool assertsEnabled = false; |
| 9 |
| 10 main() { |
| 11 assert((assertsEnabled = true)); |
| 12 runtimeAsserts(); /// none: ok |
| 13 |
| 14 // Passing const expressions. |
| 15 const c00 = const AssertArgument.constFirst(true, 0, 1); |
| 16 const c01 = const AssertArgument.constLast(true, 0, 1); |
| 17 const c02 = const AssertArgument.constMiddle(true, 0, 1); |
| 18 const c03 = const AssertArgument.constMulti(true, 0, 1); |
| 19 const c04 = const AssertArgument.constFirstSuper(true, 0, 1); |
| 20 const c05 = const AssertArgument.constLastSuper(true, 0, 1); |
| 21 const c06 = const AssertArgument.constMiddleSuper(true, 0, 1); |
| 22 const c07 = const AssertArgument.constMultiSuper(true, 0, 1); |
| 23 |
| 24 const c08 = const AssertCompare.constFirst(1, 2); |
| 25 const c09 = const AssertCompare.constLast(1, 2); |
| 26 const c10 = const AssertCompare.constMiddle(1, 2); |
| 27 const c11 = const AssertCompare.constMulti(1, 2); |
| 28 const c12 = const AssertCompare.constFirstSuper(1, 2); |
| 29 const c13 = const AssertCompare.constLastSuper(1, 2); |
| 30 const c14 = const AssertCompare.constMiddleSuper(1, 2); |
| 31 const c15 = const AssertCompare.constMultiSuper(1, 2); |
| 32 |
| 33 // Failing const expressions |
| 34 |
| 35 const c = const AssertArgument.constFirst(false, 0, 1); /// 01: compile-
time error |
| 36 const c = const AssertArgument.constLast(false, 0, 1); /// 02: compile-
time error |
| 37 const c = const AssertArgument.constMiddle(false, 0, 1); /// 03: compile-
time error |
| 38 const c = const AssertArgument.constMulti(false, 0, 1); /// 04: compile-
time error |
| 39 const c = const AssertArgument.constFirstSuper(false, 0, 1); /// 05: compile-
time error |
| 40 const c = const AssertArgument.constLastSuper(false, 0, 1); /// 06: compile-
time error |
| 41 const c = const AssertArgument.constMiddleSuper(false, 0, 1); /// 07: compile-
time error |
| 42 const c = const AssertArgument.constMultiSuper(false, 0, 1); /// 08: compile-
time error |
| 43 |
| 44 const c = const AssertArgument.constFirst("str", 0, 1); /// 11: compile-
time error |
| 45 const c = const AssertArgument.constLast("str", 0, 1); /// 12: compile-
time error |
| 46 const c = const AssertArgument.constMiddle("str", 0, 1); /// 13: compile-
time error |
| 47 const c = const AssertArgument.constMulti("str", 0, 1); /// 14: compile-
time error |
| 48 const c = const AssertArgument.constFirstSuper("str", 0, 1); /// 15: compile-
time error |
| 49 const c = const AssertArgument.constLastSuper("str", 0, 1); /// 16: compile-
time error |
| 50 const c = const AssertArgument.constMiddleSuper("str", 0, 1); /// 17: compile-
time error |
| 51 const c = const AssertArgument.constMultiSuper("str", 0, 1); /// 18: compile-
time error |
| 52 |
| 53 const c = const AssertCompare.constFirst(3, 2); /// 21: compile-
time error |
| 54 const c = const AssertCompare.constLast(3, 2); /// 22: compile-
time error |
| 55 const c = const AssertCompare.constMiddle(3, 2); /// 23: compile-
time error |
| 56 const c = const AssertCompare.constMulti(3, 2); /// 24: compile-
time error |
| 57 const c = const AssertCompare.constFirstSuper(3, 2); /// 25: compile-
time error |
| 58 const c = const AssertCompare.constLastSuper(3, 2); /// 26: compile-
time error |
| 59 const c = const AssertCompare.constMiddleSuper(3, 2); /// 27: compile-
time error |
| 60 const c = const AssertCompare.constMultiSuper(3, 2); /// 28: compile-
time error |
| 61 |
| 62 // Functions not allowed in asserts in const execution. |
| 63 const c = const AssertArgument.constFirst(kTrue, 0, 1); /// 31: compile-
time error |
| 64 const c = const AssertArgument.constLast(kTrue, 0, 1); /// 32: compile-
time error |
| 65 const c = const AssertArgument.constMiddle(kTrue, 0, 1); /// 33: compile-
time error |
| 66 const c = const AssertArgument.constMulti(kTrue, 0, 1); /// 34: compile-
time error |
| 67 const c = const AssertArgument.constFirstSuper(kTrue, 0, 1); /// 35: compile-
time error |
| 68 const c = const AssertArgument.constLastSuper(kTrue, 0, 1); /// 36: compile-
time error |
| 69 const c = const AssertArgument.constMiddleSuper(kTrue, 0, 1); /// 37: compile-
time error |
| 70 const c = const AssertArgument.constMultiSuper(kTrue, 0, 1); /// 38: compile-
time error |
| 71 |
| 72 const cTrue = const TrickCompare(true); |
| 73 // Value must be integer for potential-const expression to be actually const. |
| 74 const c = const AssertCompare.constFirst(cTrue, 2); /// 41: compile-
time error |
| 75 const c = const AssertCompare.constLast(cTrue, 2); /// 42: compile-
time error |
| 76 const c = const AssertCompare.constMiddle(cTrue, 2); /// 43: compile-
time error |
| 77 const c = const AssertCompare.constMulti(cTrue, 2); /// 44: compile-
time error |
| 78 const c = const AssertCompare.constFirstSuper(cTrue, 2); /// 45: compile-
time error |
| 79 const c = const AssertCompare.constLastSuper(cTrue, 2); /// 46: compile-
time error |
| 80 const c = const AssertCompare.constMiddleSuper(cTrue, 2); /// 47: compile-
time error |
| 81 const c = const AssertCompare.constMultiSuper(cTrue, 2); /// 48: compile-
time error |
| 82 } |
| 83 |
| 84 |
| 85 void runtimeAsserts() { |
| 86 |
| 87 testAssertArgumentCombinations(value, test, [testConst]) { |
| 88 test(() => new AssertArgument.first(value, 0, 1)); |
| 89 test(() => new AssertArgument.last(value, 0, 1)); |
| 90 test(() => new AssertArgument.middle(value, 0, 1)); |
| 91 test(() => new AssertArgument.multi(value, 0, 1)); |
| 92 test(() => new AssertArgument.firstSuper(value, 0, 1)); |
| 93 test(() => new AssertArgument.lastSuper(value, 0, 1)); |
| 94 test(() => new AssertArgument.middleSuper(value, 0, 1)); |
| 95 test(() => new AssertArgument.multiSuper(value, 0, 1)); |
| 96 testConst ??= test; |
| 97 testConst(() => new AssertArgument.constFirst(value, 0, 1)); |
| 98 testConst(() => new AssertArgument.constLast(value, 0, 1)); |
| 99 testConst(() => new AssertArgument.constMiddle(value, 0, 1)); |
| 100 testConst(() => new AssertArgument.constMulti(value, 0, 1)); |
| 101 testConst(() => new AssertArgument.constFirstSuper(value, 0, 1)); |
| 102 testConst(() => new AssertArgument.constLastSuper(value, 0, 1)); |
| 103 testConst(() => new AssertArgument.constMiddleSuper(value, 0, 1)); |
| 104 testConst(() => new AssertArgument.constMultiSuper(value, 0, 1)); |
| 105 } |
| 106 |
| 107 testAssertCompareCombinations(v1, v2, test, [testConst]) { |
| 108 test(() => new AssertCompare.first(v1, v2)); |
| 109 test(() => new AssertCompare.last(v1, v2)); |
| 110 test(() => new AssertCompare.middle(v1, v2)); |
| 111 test(() => new AssertCompare.multi(v1, v2)); |
| 112 test(() => new AssertCompare.firstSuper(v1, v2)); |
| 113 test(() => new AssertCompare.lastSuper(v1, v2)); |
| 114 test(() => new AssertCompare.middleSuper(v1, v2)); |
| 115 test(() => new AssertCompare.multiSuper(v1, v2)); |
| 116 testConst ??= test; |
| 117 testConst(() => new AssertCompare.constFirst(v1, v2)); |
| 118 testConst(() => new AssertCompare.constLast(v1, v2)); |
| 119 testConst(() => new AssertCompare.constMiddle(v1, v2)); |
| 120 testConst(() => new AssertCompare.constMulti(v1, v2)); |
| 121 testConst(() => new AssertCompare.constFirstSuper(v1, v2)); |
| 122 testConst(() => new AssertCompare.constLastSuper(v1, v2)); |
| 123 testConst(() => new AssertCompare.constMiddleSuper(v1, v2)); |
| 124 testConst(() => new AssertCompare.constMultiSuper(v1, v2)); |
| 125 } |
| 126 |
| 127 testAssertArgumentCombinations(true, pass); |
| 128 testAssertArgumentCombinations(kTrue, pass, failType); |
| 129 testAssertArgumentCombinations(false, failAssert); |
| 130 testAssertArgumentCombinations(kFalse, failAssert, failType); |
| 131 testAssertArgumentCombinations(42, failType); |
| 132 testAssertArgumentCombinations(null, failAssert); |
| 133 |
| 134 testAssertCompareCombinations(1, 2, pass); |
| 135 testAssertCompareCombinations(3, 2, failAssert); |
| 136 var TrickCompareInt = const TrickCompare(42); |
| 137 testAssertCompareCombinations(TrickCompareInt, 0, failType); |
| 138 var TrickCompareTrueFun = const TrickCompare(kTrue); |
| 139 testAssertCompareCombinations(TrickCompareTrueFun, 0, pass, failType); |
| 140 var TrickCompareFalseFun = const TrickCompare(kFalse); |
| 141 testAssertCompareCombinations(TrickCompareFalseFun, 0, failAssert, failType); |
| 142 } |
| 143 |
| 144 |
| 145 void pass(void action()) { |
| 146 action(); |
| 147 } |
| 148 |
| 149 void failAssert(void action()) { |
| 150 if (assertsEnabled) { |
| 151 Expect.throws(action, (e) => e is AssertionError && e is! TypeError); |
| 152 } else { |
| 153 action(); |
| 154 } |
| 155 } |
| 156 |
| 157 void failType(void action()) { |
| 158 if (assertsEnabled) { |
| 159 Expect.throws(action, (e) => e is TypeError); |
| 160 } else { |
| 161 action(); |
| 162 } |
| 163 } |
| 164 |
| 165 bool kTrue() => true; |
| 166 bool kFalse() => false; |
| 167 |
| 168 class AssertArgument { |
| 169 final y; |
| 170 final z; |
| 171 AssertArgument.first(x, y, z) : assert(x), y = y, z = z; |
| 172 AssertArgument.last(x, y, z) : y = y, z = z, assert(x); |
| 173 AssertArgument.middle(x, y, z) : y = y, assert(x), z = z; |
| 174 AssertArgument.multi(x, y, z) |
| 175 : assert(x), y = y, assert(x), z = z, assert(x); |
| 176 AssertArgument.firstSuper(x, y, z) : assert(x), y = y, z = z, super(); |
| 177 AssertArgument.lastSuper(x, y, z) : y = y, z = z, assert(x), super(); |
| 178 AssertArgument.middleSuper(x, y, z) : y = y, assert(x), z = z, super(); |
| 179 AssertArgument.multiSuper(x, y, z) |
| 180 : assert(x), y = y, assert(x), z = z, assert(x), super(); |
| 181 const AssertArgument.constFirst(x, y, z) : assert(x), y = y, z = z; |
| 182 const AssertArgument.constLast(x, y, z) : y = y, z = z, assert(x); |
| 183 const AssertArgument.constMiddle(x, y, z) : y = y, assert(x), z = z; |
| 184 const AssertArgument.constMulti(x, y, z) |
| 185 : assert(x), y = y, assert(x), z = z, assert(x); |
| 186 const AssertArgument.constFirstSuper(x, y, z) |
| 187 : assert(x), y = y, z = z, super(); |
| 188 const AssertArgument.constLastSuper(x, y, z) |
| 189 : y = y, z = z, assert(x), super(); |
| 190 const AssertArgument.constMiddleSuper(x, y, z) |
| 191 : y = y, assert(x), z = z, super(); |
| 192 const AssertArgument.constMultiSuper(x, y, z) |
| 193 : assert(x), y = y, assert(x), z = z, assert(x), super(); |
| 194 } |
| 195 |
| 196 class AssertCompare { |
| 197 final y; |
| 198 final z; |
| 199 AssertCompare.first(y, z) : assert(y < z), y = y, z = z; |
| 200 AssertCompare.last(y, z) : y = y, z = z, assert(y < z); |
| 201 AssertCompare.middle(y, z) : y = y, assert(y < z), z = z; |
| 202 AssertCompare.multi(y, z) |
| 203 : assert(y < z), y = y, assert(y < z), z = z, assert(y < z); |
| 204 AssertCompare.firstSuper(y, z) : assert(y < z), y = y, z = z, super(); |
| 205 AssertCompare.lastSuper(y, z) : y = y, z = z, assert(y < z), super(); |
| 206 AssertCompare.middleSuper(y, z) : y = y, assert(y < z), z = z, super(); |
| 207 AssertCompare.multiSuper(y, z) |
| 208 : assert(y < z), y = y, assert(y < z), z = z, assert(y < z), super(); |
| 209 const AssertCompare.constFirst(y, z) : assert(y < z), y = y, z = z; |
| 210 const AssertCompare.constLast(y, z) : y = y, z = z, assert(y < z); |
| 211 const AssertCompare.constMiddle(y, z) : y = y, assert(y < z), z = z; |
| 212 const AssertCompare.constMulti(y, z) |
| 213 : assert(y < z), y = y, assert(y < z), z = z, assert(y < z); |
| 214 const AssertCompare.constFirstSuper(y, z) |
| 215 : assert(y < z), y = y, z = z, super(); |
| 216 const AssertCompare.constLastSuper(y, z) |
| 217 : y = y, z = z, assert(y < z), super(); |
| 218 const AssertCompare.constMiddleSuper(y, z) |
| 219 : y = y, assert(y < z), z = z, super(); |
| 220 const AssertCompare.constMultiSuper(y, z) |
| 221 : assert(y < z), y = y, assert(y < z), z = z, assert(y < z), super(); |
| 222 } |
| 223 |
| 224 class TrickCompare { |
| 225 final result; |
| 226 const TrickCompare(this.result); |
| 227 operator<(other) => result; // Nyah-nyah! |
| 228 } |
OLD | NEW |