OLD | NEW |
(Empty) | |
| 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=--enable_async |
| 6 |
| 7 import 'package:expect/expect.dart'; |
| 8 |
| 9 import 'dart:async'; |
| 10 |
| 11 topLevelFunction() async { } |
| 12 |
| 13 Future<int> topLevelWithParameter(int a) async { |
| 14 return 7 + a; |
| 15 } |
| 16 |
| 17 int topLevelWithParameterWrongType(int a) async { |
| 18 return 7 + a; |
| 19 } |
| 20 |
| 21 var what = 'async getter'; |
| 22 Future<String> get topLevelGetter async { |
| 23 return 'I want to be an ${what}'; |
| 24 } |
| 25 |
| 26 class A { |
| 27 static int staticVar = 1; |
| 28 |
| 29 static staticMethod(int param) async => staticVar + param; |
| 30 static get staticGetter async => staticVar + 3; |
| 31 |
| 32 int _x; |
| 33 A(this._x); |
| 34 |
| 35 A.fail() async; /// constructor2: compile-time error |
| 36 factory A.create() async {return null; } /// constructor3: compile-time error |
| 37 |
| 38 int someMethod(int param1, int param2, int param3) async => _x + param2; |
| 39 int get getter async { return 5 + _x; } |
| 40 operator+(A other) async { |
| 41 return new A(_x + other._x); |
| 42 } |
| 43 |
| 44 get value => _x; |
| 45 } |
| 46 |
| 47 class B { |
| 48 final _y; |
| 49 const B._internal(this._y); |
| 50 const factory B.createConst(int y) async = A._internal; /// constructor4: com
pile-time error |
| 51 |
| 52 B(); |
| 53 |
| 54 set dontDoThat(value) async {} /// setter1: compile-time error |
| 55 } |
| 56 |
| 57 |
| 58 main() { |
| 59 var asyncReturn; |
| 60 |
| 61 asyncReturn = topLevelFunction(); |
| 62 Expect.isTrue(asyncReturn is Future); |
| 63 |
| 64 int a1 = topLevelWithParameter(2); /// type-mismatch1: static type warning, d
ynamic type error |
| 65 int a2 = topLevelWithParameterWrongType(2); /// type-mismatch2: static type w
arning, dynamic type error |
| 66 asyncReturn = topLevelWithParameter(4); |
| 67 Expect.isTrue(asyncReturn is Future); |
| 68 asyncReturn.then((int result) => Expect.equals(result, 11)); |
| 69 |
| 70 asyncReturn = topLevelGetter; |
| 71 Expect.isTrue(asyncReturn is Future); |
| 72 asyncReturn.then((String result) => |
| 73 Expect.stringEquals(result, 'I want to be an async getter')); |
| 74 |
| 75 asyncReturn = A.staticMethod(2); |
| 76 Expect.isTrue(asyncReturn is Future); |
| 77 asyncReturn.then((int result) => Expect.equals(result, 3)); |
| 78 |
| 79 asyncReturn = A.staticGetter; |
| 80 Expect.isTrue(asyncReturn is Future); |
| 81 asyncReturn.then((int result) => Expect.equals(result, 4)); |
| 82 |
| 83 A a = new A(13); |
| 84 |
| 85 asyncReturn = a.someMethod(1,2,3); /// type-mismatch3: static type warning, d
ynamic type error |
| 86 Expect.isTrue(asyncReturn is Future); /// type-mismatch3: continued |
| 87 asyncReturn.then((int result) => Expect.equals(result, 15)); /// type-mismatc
h3: continued |
| 88 |
| 89 asyncReturn = a.getter; /// type-mismatch4: static type warning, dynamic type
error |
| 90 Expect.isTrue(asyncReturn is Future); /// type-mismatch4: continued |
| 91 asyncReturn.then((int result) => Expect.equals(result, 18)); /// type-mismatc
h4: continued |
| 92 |
| 93 var b = new A(9); |
| 94 asyncReturn = a + b; |
| 95 Expect.isTrue(asyncReturn is Future); |
| 96 asyncReturn.then((A result) => Expect.equals(result.value, 22)); |
| 97 |
| 98 var foo = 17; |
| 99 bar(int p1, p2) async { |
| 100 var z = 8; |
| 101 return p2 + z + foo; |
| 102 } |
| 103 asyncReturn = bar(1,2); |
| 104 Expect.isTrue(asyncReturn is Future); |
| 105 asyncReturn.then((int result) => Expect.equals(result, 27)); |
| 106 |
| 107 var moreNesting = (int shadowP1, String p2, num p3) { |
| 108 var z = 3; |
| 109 aa(int shadowP1) async { |
| 110 return foo + z + p3 + shadowP1; |
| 111 } |
| 112 return aa(6); |
| 113 }; |
| 114 asyncReturn = moreNesting(1, "ignore", 2); |
| 115 Expect.isTrue(asyncReturn is Future); |
| 116 asyncReturn.then((int result) => Expect.equals(result, 28)); |
| 117 |
| 118 var b1 = const B.createConst(4); /// constructor4: compile-time error |
| 119 var b2 = new B(); |
| 120 b2.dontDoThat = 4; /// setter1: compile-time error |
| 121 |
| 122 var checkAsync = (var someFunc) { |
| 123 var toTest = someFunc(); |
| 124 Expect.isTrue(toTest is Future); |
| 125 toTest.then((int result) => Expect.equals(result, 4)); |
| 126 }; |
| 127 checkAsync(() async => 4); |
| 128 |
| 129 } |
OLD | NEW |