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 // TODO(jmesserly): this file needs to be refactored, it's a port from | 5 // TODO(jmesserly): this file needs to be refactored, it's a port from |
6 // package:dev_compiler's tests | 6 // package:dev_compiler's tests |
7 /// Tests for type inference. | 7 /// Tests for type inference. |
8 library analyzer.test.src.task.strong.inferred_type_test; | 8 library analyzer.test.src.task.strong.inferred_type_test; |
9 | 9 |
10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 expect(g.type.toString(), '() → Future<int>'); | 54 expect(g.type.toString(), '() → Future<int>'); |
55 } | 55 } |
56 | 56 |
57 void test_asyncClosureReturnType_future() { | 57 void test_asyncClosureReturnType_future() { |
58 var mainUnit = checkFile('var f = () async => 0;'); | 58 var mainUnit = checkFile('var f = () async => 0;'); |
59 var f = mainUnit.topLevelVariables[0]; | 59 var f = mainUnit.topLevelVariables[0]; |
60 expect(f.name, 'f'); | 60 expect(f.name, 'f'); |
61 expect(f.type.toString(), '() → Future<int>'); | 61 expect(f.type.toString(), '() → Future<int>'); |
62 } | 62 } |
63 | 63 |
| 64 void test_asyncClosureReturnType_futureOr() { |
| 65 var mainUnit = checkFile(''' |
| 66 import 'dart:async'; |
| 67 FutureOr<int> futureOrInt = null; |
| 68 var f = () => futureOrInt; |
| 69 var g = () async => futureOrInt; |
| 70 '''); |
| 71 var futureOrInt = mainUnit.topLevelVariables[0]; |
| 72 expect(futureOrInt.name, 'futureOrInt'); |
| 73 expect(futureOrInt.type.toString(), 'FutureOr<int>'); |
| 74 var f = mainUnit.topLevelVariables[1]; |
| 75 expect(f.name, 'f'); |
| 76 expect(f.type.toString(), '() → FutureOr<int>'); |
| 77 var g = mainUnit.topLevelVariables[2]; |
| 78 expect(g.name, 'g'); |
| 79 expect(g.type.toString(), '() → Future<int>'); |
| 80 } |
| 81 |
64 void test_blockBodiedLambdas_async_allReturnsAreFutures() { | 82 void test_blockBodiedLambdas_async_allReturnsAreFutures() { |
65 if (!mayCheckTypesOfLocals) { | 83 if (!mayCheckTypesOfLocals) { |
66 return; | 84 return; |
67 } | 85 } |
68 var mainUnit = checkFile(r''' | 86 var mainUnit = checkFile(r''' |
69 import 'dart:async'; | 87 import 'dart:async'; |
70 import 'dart:math' show Random; | 88 import 'dart:math' show Random; |
71 main() { | 89 main() { |
72 var f = /*info:INFERRED_TYPE_CLOSURE*/() async { | 90 var f = /*info:INFERRED_TYPE_CLOSURE*/() async { |
73 if (new Random().nextBool()) { | 91 if (new Random().nextBool()) { |
(...skipping 1584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1658 var mainUnit = checkFile(''' | 1676 var mainUnit = checkFile(''' |
1659 class C { | 1677 class C { |
1660 final x = y; | 1678 final x = y; |
1661 } | 1679 } |
1662 int get y => null; | 1680 int get y => null; |
1663 '''); | 1681 '''); |
1664 var x = mainUnit.types[0].fields[0]; | 1682 var x = mainUnit.types[0].fields[0]; |
1665 expect(x.type.toString(), 'int'); | 1683 expect(x.type.toString(), 'int'); |
1666 } | 1684 } |
1667 | 1685 |
| 1686 void test_futureOr_subtyping() { |
| 1687 checkFile(r''' |
| 1688 import 'dart:async'; |
| 1689 void add(int x) {} |
| 1690 add2(int y) {} |
| 1691 main() { |
| 1692 Future<int> f; |
| 1693 var a = f.then(add); |
| 1694 var b = f.then(add2); |
| 1695 } |
| 1696 '''); |
| 1697 } |
| 1698 |
1668 void test_futureThen() { | 1699 void test_futureThen() { |
1669 String build({String declared, String downwards, String upwards}) => ''' | 1700 String build({String declared, String downwards, String upwards}) => ''' |
1670 import 'dart:async'; | 1701 import 'dart:async'; |
1671 class MyFuture<T> implements Future<T> { | 1702 class MyFuture<T> implements Future<T> { |
1672 MyFuture() {} | 1703 MyFuture() {} |
1673 MyFuture.value(T x) {} | 1704 MyFuture.value(T x) {} |
1674 dynamic noSuchMethod(invocation); | 1705 dynamic noSuchMethod(invocation); |
1675 MyFuture<S> then<S>(FutureOr<S> f(T x), {Function onError}) => null; | 1706 MyFuture<S> then<S>(FutureOr<S> f(T x), {Function onError}) => null; |
1676 } | 1707 } |
1677 | 1708 |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1848 main() { | 1879 main() { |
1849 Future<int> base; | 1880 Future<int> base; |
1850 var f = base.then(/*info:INFERRED_TYPE_CLOSURE,info:INFERRED_TYPE_CLOSURE*/(x)
{ return x == 0; }); | 1881 var f = base.then(/*info:INFERRED_TYPE_CLOSURE,info:INFERRED_TYPE_CLOSURE*/(x)
{ return x == 0; }); |
1851 var g = base.then(/*info:INFERRED_TYPE_CLOSURE*/(x) => x == 0); | 1882 var g = base.then(/*info:INFERRED_TYPE_CLOSURE*/(x) => x == 0); |
1852 Future<bool> b = f; | 1883 Future<bool> b = f; |
1853 b = g; | 1884 b = g; |
1854 } | 1885 } |
1855 '''); | 1886 '''); |
1856 } | 1887 } |
1857 | 1888 |
1858 void test_futureOr_subtyping() { | |
1859 checkFile(r''' | |
1860 import 'dart:async'; | |
1861 void add(int x) {} | |
1862 add2(int y) {} | |
1863 main() { | |
1864 Future<int> f; | |
1865 var a = f.then(add); | |
1866 var b = f.then(add2); | |
1867 } | |
1868 '''); | |
1869 } | |
1870 | |
1871 void test_futureUnion_asyncConditional() { | 1889 void test_futureUnion_asyncConditional() { |
1872 String build({String declared, String downwards, String upwards}) => ''' | 1890 String build({String declared, String downwards, String upwards}) => ''' |
1873 import 'dart:async'; | 1891 import 'dart:async'; |
1874 class MyFuture<T> implements Future<T> { | 1892 class MyFuture<T> implements Future<T> { |
1875 MyFuture() {} | 1893 MyFuture() {} |
1876 MyFuture.value(x) {} | 1894 MyFuture.value(x) {} |
1877 dynamic noSuchMethod(invocation); | 1895 dynamic noSuchMethod(invocation); |
1878 MyFuture<S> then<S>(FutureOr<S> f(T x), {Function onError}) => null; | 1896 MyFuture<S> then<S>(FutureOr<S> f(T x), {Function onError}) => null; |
1879 } | 1897 } |
1880 | 1898 |
(...skipping 3499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5380 } | 5398 } |
5381 | 5399 |
5382 void setUp() { | 5400 void setUp() { |
5383 helper.doSetUp(); | 5401 helper.doSetUp(); |
5384 } | 5402 } |
5385 | 5403 |
5386 void tearDown() { | 5404 void tearDown() { |
5387 helper.doTearDown(); | 5405 helper.doTearDown(); |
5388 } | 5406 } |
5389 } | 5407 } |
OLD | NEW |