| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library async_await_test; | 5 library async_await_test; |
| 6 | 6 |
| 7 import "package:unittest/unittest.dart"; | 7 import "package:unittest/unittest.dart"; |
| 8 import "dart:async"; | 8 import "dart:async"; |
| 9 | 9 |
| 10 main() { | 10 main() { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 }); | 156 }); |
| 157 | 157 |
| 158 test("async flattens futures, error", () { | 158 test("async flattens futures, error", () { |
| 159 f() async { | 159 f() async { |
| 160 return new Future.error("err"); // Not awaited. | 160 return new Future.error("err"); // Not awaited. |
| 161 }; | 161 }; |
| 162 return throwsErr(f()); | 162 return throwsErr(f()); |
| 163 }); | 163 }); |
| 164 | 164 |
| 165 test("await for", () { | 165 test("await for", () { |
| 166 f(s) async { | 166 f(Stream<int> s) async { |
| 167 int i = 0; | 167 int i = 0; |
| 168 await for (int v in s) { | 168 await for (int v in s) { |
| 169 i += v; | 169 i += v; |
| 170 } | 170 } |
| 171 return i; | 171 return i; |
| 172 } | 172 } |
| 173 return f(mkStream()).then((v) { | 173 return f(mkStream()).then((v) { |
| 174 expect(v, equals(45)); // 0 + 1 + ... + 9 | 174 expect(v, equals(45)); // 0 + 1 + ... + 9 |
| 175 }); | 175 }); |
| 176 }); | 176 }); |
| 177 | 177 |
| 178 test("await for w/ await", () { | 178 test("await for w/ await", () { |
| 179 f(s) async { | 179 f(Stream<int> s) async { |
| 180 int i = 0; | 180 int i = 0; |
| 181 await for (int v in s) { | 181 await for (int v in s) { |
| 182 i += await new Future.value(v); | 182 i += await new Future.value(v); |
| 183 } | 183 } |
| 184 return i; | 184 return i; |
| 185 } | 185 } |
| 186 return f(mkStream()).then((v) { | 186 return f(mkStream()).then((v) { |
| 187 expect(v, equals(45)); // 0 + 1 + ... + 9 | 187 expect(v, equals(45)); // 0 + 1 + ... + 9 |
| 188 }); | 188 }); |
| 189 }); | 189 }); |
| 190 | 190 |
| 191 test("await for empty", () { | 191 test("await for empty", () { |
| 192 f(s) async { | 192 f(Stream<int> s) async { |
| 193 int v = 0; | 193 int v = 0; |
| 194 await for (int i in s) { | 194 await for (int i in s) { |
| 195 v += i; | 195 v += i; |
| 196 } | 196 } |
| 197 return v; | 197 return v; |
| 198 } | 198 } |
| 199 var s = (new StreamController()..close()).stream; | 199 var s = (new StreamController<int>()..close()).stream; |
| 200 return f(s).then((v) { | 200 return f(s).then((v) { |
| 201 expect(v, equals(0)); | 201 expect(v, equals(0)); |
| 202 }); | 202 }); |
| 203 }); | 203 }); |
| 204 | 204 |
| 205 if (checkedMode) { | 205 if (checkedMode) { |
| 206 test("await for w/ await, asseert", () { | 206 test("await for w/ await, asseert", () { |
| 207 f(s) async { | 207 f(Stream<int> s) async { |
| 208 int i = 0; | 208 int i = 0; |
| 209 await for (int v in s) { | 209 await for (int v in s) { |
| 210 i += await new Future.microtask(() => v); | 210 i += await new Future.microtask(() => v); |
| 211 assert(v < 8); | 211 assert(v < 8); |
| 212 } | 212 } |
| 213 return i; | 213 return i; |
| 214 } | 214 } |
| 215 return f(mkStream()).then((v) { | 215 return f(mkStream()).then((v) { |
| 216 fail("assert didn't throw"); | 216 fail("assert didn't throw"); |
| 217 }, onError: (e, s) { | 217 }, onError: (e, s) { |
| (...skipping 1571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1789 id(v) { | 1789 id(v) { |
| 1790 try { | 1790 try { |
| 1791 if (v != null) throw v; | 1791 if (v != null) throw v; |
| 1792 } catch (e) { | 1792 } catch (e) { |
| 1793 return e; | 1793 return e; |
| 1794 } | 1794 } |
| 1795 return null; | 1795 return null; |
| 1796 } | 1796 } |
| 1797 | 1797 |
| 1798 // Create a stream for testing "async for-in". | 1798 // Create a stream for testing "async for-in". |
| 1799 Stream mkStream() { | 1799 Stream<int> mkStream() { |
| 1800 var c; | 1800 StreamController<int> c; |
| 1801 int i = 0; | 1801 int i = 0; |
| 1802 next() { | 1802 next() { |
| 1803 c.add(i++); | 1803 c.add(i++); |
| 1804 if (i == 10) { | 1804 if (i == 10) { |
| 1805 c.close(); | 1805 c.close(); |
| 1806 } else { | 1806 } else { |
| 1807 scheduleMicrotask(next); | 1807 scheduleMicrotask(next); |
| 1808 } | 1808 } |
| 1809 } | 1809 } |
| 1810 c = new StreamController(onListen: () { | 1810 c = new StreamController(onListen: () { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1956 } | 1956 } |
| 1957 return await inner(f); | 1957 return await inner(f); |
| 1958 } | 1958 } |
| 1959 | 1959 |
| 1960 /** | 1960 /** |
| 1961 * A non-standard implementation of Future with a value. | 1961 * A non-standard implementation of Future with a value. |
| 1962 */ | 1962 */ |
| 1963 class FakeValueFuture implements Future { | 1963 class FakeValueFuture implements Future { |
| 1964 final _value; | 1964 final _value; |
| 1965 FakeValueFuture(this._value); | 1965 FakeValueFuture(this._value); |
| 1966 Future then(callback(value), {Function onError}) { | 1966 Future/*<S>*/ then/*<S>*/(/*=S*/ callback(value), {Function onError}) { |
| 1967 return new Future.microtask(() => callback(_value)); | 1967 return new Future<dynamic /*=S*/>.microtask(() => callback(_value)); |
| 1968 } | 1968 } |
| 1969 Future whenComplete(callback()) { | 1969 Future whenComplete(callback()) { |
| 1970 return new Future.microtask(() { callback(); }); | 1970 return new Future.microtask(() { callback(); }); |
| 1971 } | 1971 } |
| 1972 Future catchError(Function onError, {bool test(error)}) => this; | 1972 Future catchError(Function onError, {bool test(error)}) => this; |
| 1973 Stream asStream() => (new StreamController()..add(_value)..close()).stream; | 1973 Stream asStream() => (new StreamController()..add(_value)..close()).stream; |
| 1974 Future timeout(Duration duration, {onTimeout()}) => this; | 1974 Future timeout(Duration duration, {onTimeout()}) => this; |
| 1975 } | 1975 } |
| 1976 | 1976 |
| 1977 typedef BinaryFunction(a, b); | 1977 typedef BinaryFunction(a, b); |
| 1978 | 1978 |
| 1979 /** | 1979 /** |
| 1980 * A non-standard implementation of Future with an error. | 1980 * A non-standard implementation of Future with an error. |
| 1981 */ | 1981 */ |
| 1982 class FakeErrorFuture implements Future { | 1982 class FakeErrorFuture implements Future { |
| 1983 final _error; | 1983 final _error; |
| 1984 FakeErrorFuture(this._error); | 1984 FakeErrorFuture(this._error); |
| 1985 Future then(callback(value), {Function onError}) { | 1985 Future/*<S>*/ then/*<S>*/(/*=S*/ callback(value), {Function onError}) { |
| 1986 if (onError != null) { | 1986 if (onError != null) { |
| 1987 if (onError is BinaryFunction) { | 1987 if (onError is BinaryFunction) { |
| 1988 return new Future.microtask(() => onError(_error, null)); | 1988 return new Future/*<S>*/.microtask(() => onError(_error, null)); |
| 1989 } | 1989 } |
| 1990 return new Future.microtask(() => onError(_error)); | 1990 return new Future/*<S>*/.microtask(() => onError(_error)); |
| 1991 } | 1991 } |
| 1992 return this; | 1992 return new Future/*<S>*/.error(_error); |
| 1993 } | 1993 } |
| 1994 Future whenComplete(callback()) { | 1994 Future whenComplete(callback()) { |
| 1995 return new Future.microtask(() { callback(); }).then((_) => this); | 1995 return new Future.microtask(() { callback(); }).then((_) => this); |
| 1996 } | 1996 } |
| 1997 Future catchError(Function onError, {bool test(error)}) { | 1997 Future catchError(Function onError, {bool test(error)}) { |
| 1998 return new Future.microtask(() { | 1998 return new Future.microtask(() { |
| 1999 if (test != null && !test(_error)) return this; | 1999 if (test != null && !test(_error)) return this; |
| 2000 if (onError is BinaryFunction) { | 2000 if (onError is BinaryFunction) { |
| 2001 return onError(_error, null); | 2001 return onError(_error, null); |
| 2002 } | 2002 } |
| 2003 return onError(_error); | 2003 return onError(_error); |
| 2004 }); | 2004 }); |
| 2005 } | 2005 } |
| 2006 Stream asStream() => | 2006 Stream asStream() => |
| 2007 (new StreamController()..addError(_error)..close()).stream; | 2007 (new StreamController()..addError(_error)..close()).stream; |
| 2008 Future timeout(Duration duration, {onTimeout()}) => this; | 2008 Future timeout(Duration duration, {onTimeout()}) => this; |
| 2009 } | 2009 } |
| OLD | NEW |