|
| 1 | +// Copyright (c) 2014, 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=---optimization-counter-threshold=10 |
| 6 | + |
| 7 | +import 'package:expect/expect.dart'; |
| 8 | +import 'package:async_helper/async_helper.dart'; |
| 9 | + |
| 10 | +import 'dart:async'; |
| 11 | + |
| 12 | +int globalVariable = 1; |
| 13 | +int topLevelFoo(int param) => 1; |
| 14 | +int get topLevelGetter => globalVariable; |
| 15 | +void set topLevelSetter(val) { |
| 16 | + globalVariable = val; |
| 17 | +} |
| 18 | + |
| 19 | +class C { |
| 20 | + static int staticField = 1; |
| 21 | + static int get staticGetter => staticField; |
| 22 | + static void set staticSetter(val) { |
| 23 | + staticField = val; |
| 24 | + } |
| 25 | + |
| 26 | + static int staticFoo(int param) => param; |
| 27 | + |
| 28 | + int field = 1; |
| 29 | + int get getter => field; |
| 30 | + void set setter(val) { |
| 31 | + field = val; |
| 32 | + } |
| 33 | + |
| 34 | + int foo(int param) => param; |
| 35 | +} |
| 36 | + |
| 37 | +dummy() => 1; |
| 38 | + |
| 39 | +staticMembers() async { |
| 40 | + var a = C.staticField + await dummy(); |
| 41 | + Expect.equals(a, 2); |
| 42 | + var f = (C.staticField = 1) + await dummy(); |
| 43 | + Expect.equals(f, 2); |
| 44 | + var b = C.staticGetter + await dummy(); |
| 45 | + Expect.equals(b, 2); |
| 46 | + var c = (C.staticSetter = 1) + await dummy(); |
| 47 | + Expect.equals(c, 2); |
| 48 | + var d = C.staticFoo(2) + await dummy(); |
| 49 | + Expect.equals(d, 3); |
| 50 | + var e = C.staticField + |
| 51 | + C.staticGetter + |
| 52 | + (C.staticSetter = 1) + |
| 53 | + C.staticFoo(1) + |
| 54 | + await dummy(); |
| 55 | + Expect.equals(e, 5); |
| 56 | +} |
| 57 | + |
| 58 | +topLevelMembers() async { |
| 59 | + var a = globalVariable + await dummy(); |
| 60 | + Expect.equals(a, 2); |
| 61 | + var b = topLevelGetter + await dummy(); |
| 62 | + Expect.equals(b, 2); |
| 63 | + var c = (topLevelSetter = 1) + await dummy(); |
| 64 | + Expect.equals(c, 2); |
| 65 | + var d = topLevelFoo(1) + await dummy(); |
| 66 | + Expect.equals(d, 2); |
| 67 | + var e = globalVariable + |
| 68 | + topLevelGetter + |
| 69 | + (topLevelSetter = 1) + |
| 70 | + topLevelFoo(1) + |
| 71 | + await dummy(); |
| 72 | + Expect.equals(e, 5); |
| 73 | +} |
| 74 | + |
| 75 | +instanceMembers() async { |
| 76 | + var inst = new C(); |
| 77 | + var a = inst.field + await dummy(); |
| 78 | + Expect.equals(a, 2); |
| 79 | + var b = inst.getter + await dummy(); |
| 80 | + Expect.equals(b, 2); |
| 81 | + var c = (inst.setter = 1) + await dummy(); |
| 82 | + Expect.equals(c, 2); |
| 83 | + var d = inst.foo(1) + await dummy(); |
| 84 | + Expect.equals(d, 2); |
| 85 | + var e = inst.field + |
| 86 | + inst.getter + |
| 87 | + (inst.setter = 1) + |
| 88 | + inst.foo(1) + |
| 89 | + await dummy(); |
| 90 | + Expect.equals(e, 5); |
| 91 | +} |
| 92 | + |
| 93 | +others() async { |
| 94 | + var a = "${globalVariable} ${await dummy()} " + await "someString"; |
| 95 | + Expect.equals(a, "1 1 someString"); |
| 96 | + var c = new C(); |
| 97 | + var d = c.field + await dummy(); |
| 98 | + var cnt = 2; |
| 99 | + var b = [1, 2, 3]; |
| 100 | + b[cnt] = await dummy(); |
| 101 | + Expect.equals(b[cnt], 1); |
| 102 | + var e = b[0] + await dummy(); |
| 103 | + Expect.equals(e, 2); |
| 104 | +} |
| 105 | + |
| 106 | +conditionals() async { |
| 107 | + var a = false; |
| 108 | + var b = true; |
| 109 | + var c = (a || b) || await dummy(); |
| 110 | + Expect.isTrue(c); |
| 111 | + var d = (a || b) ? a : await dummy(); |
| 112 | + Expect.isFalse(d); |
| 113 | + var e = (a is int) ? await dummy() : 2; |
| 114 | + Expect.equals(e, 2); |
| 115 | + try { |
| 116 | + var f = (a is int) ? await dummy() : 2; |
| 117 | + } catch (e) {} |
| 118 | +} |
| 119 | + |
| 120 | +asserts() async { |
| 121 | + for (final FutureOr<T> Function<T>(T) func in <dynamic>[id, future]) { |
| 122 | + assert(await func(true)); |
| 123 | + assert(id(true) as bool, await func("message")); |
| 124 | + assert(await func(true), await (func("message"))); |
| 125 | + bool success = true; |
| 126 | + try { |
| 127 | + assert(await func(false), await (func("message"))); |
| 128 | + if (assertStatementsEnabled) Expect.fail("Didn't throw"); |
| 129 | + } on AssertionError catch (e) { |
| 130 | + Expect.equals("message", e.message); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +controlFlow() async { |
| 136 | + for (final FutureOr<T> Function<T>(T) func in <dynamic>[id, future]) { |
| 137 | + // For. |
| 138 | + var c = 0; |
| 139 | + for (var i = await (func(0)); await func(i < 5); await func(i++)) { |
| 140 | + c++; |
| 141 | + } |
| 142 | + Expect.equals(5, c); |
| 143 | + // While. |
| 144 | + c = 0; |
| 145 | + while (await func(c < 5)) c++; |
| 146 | + Expect.equals(5, c); |
| 147 | + // Do-while. |
| 148 | + c = 0; |
| 149 | + do { |
| 150 | + c++; |
| 151 | + } while (await func(c < 5)); |
| 152 | + Expect.equals(5, c); |
| 153 | + // If. |
| 154 | + if (await func(c == 5)) { |
| 155 | + Expect.equals(5, c); |
| 156 | + } else { |
| 157 | + Expect.fail("unreachable"); |
| 158 | + } |
| 159 | + // Throw. |
| 160 | + try { |
| 161 | + throw await func("string"); |
| 162 | + } on String { |
| 163 | + // OK. |
| 164 | + } |
| 165 | + |
| 166 | + try { |
| 167 | + await (throw "string"); |
| 168 | + } on String { |
| 169 | + // OK. |
| 170 | + } |
| 171 | + // Try/catch/finally |
| 172 | + try { |
| 173 | + try { |
| 174 | + throw "string"; |
| 175 | + } catch (e) { |
| 176 | + Expect.equals("string", e); |
| 177 | + Expect.equals(0, await func(0)); |
| 178 | + rethrow; |
| 179 | + } finally { |
| 180 | + Expect.equals(0, await func(0)); |
| 181 | + } |
| 182 | + } catch (e) { |
| 183 | + Expect.equals(0, await func(0)); |
| 184 | + Expect.equals("string", e); |
| 185 | + } finally { |
| 186 | + Expect.equals(0, await func(0)); |
| 187 | + } |
| 188 | + // Switch |
| 189 | + switch (await func(2)) { |
| 190 | + case 2: |
| 191 | + break; |
| 192 | + default: |
| 193 | + Expect.fail("unreachable"); |
| 194 | + } |
| 195 | + // Return. |
| 196 | + Expect.equals( |
| 197 | + 42, |
| 198 | + await () async { |
| 199 | + return await func(42); |
| 200 | + }()); |
| 201 | + Expect.equals( |
| 202 | + 42, |
| 203 | + await () async { |
| 204 | + return func(42); |
| 205 | + }()); |
| 206 | + // Yield. |
| 207 | + Stream<int> testStream1() async* { |
| 208 | + yield await func(42); |
| 209 | + } |
| 210 | + |
| 211 | + Expect.listEquals([42], await testStream1().toList()); |
| 212 | + // Yield* |
| 213 | + Stream<int> testStream2() async* { |
| 214 | + yield* await func(intStream()); |
| 215 | + } |
| 216 | + |
| 217 | + Expect.listEquals([42], await testStream2().toList()); |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +FutureOr<T> future<T>(T value) async => value; |
| 222 | +FutureOr<T> id<T>(T value) => value; |
| 223 | + |
| 224 | +Stream<int> intStream() async* { |
| 225 | + yield 42; |
| 226 | +} |
| 227 | + |
| 228 | +main() { |
| 229 | + asyncStart(); |
| 230 | + for (int i = 0; i < 11; i++) { |
| 231 | + asyncTest(staticMembers); |
| 232 | + asyncTest(topLevelMembers); |
| 233 | + asyncTest(instanceMembers); |
| 234 | + asyncTest(conditionals); |
| 235 | + asyncTest(others); |
| 236 | + asyncTest(asserts); |
| 237 | + asyncTest(controlFlow); |
| 238 | + } |
| 239 | + asyncEnd(); |
| 240 | +} |
0 commit comments