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 872 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
883 l = (l = /*info:INFERRED_TYPE_LITERAL*/[1]); | 883 l = (l = /*info:INFERRED_TYPE_LITERAL*/[1]); |
884 } | 884 } |
885 '''); | 885 '''); |
886 } | 886 } |
887 | 887 |
888 void test_downwardsInferenceAsyncAwait() { | 888 void test_downwardsInferenceAsyncAwait() { |
889 checkFile(''' | 889 checkFile(''' |
890 import 'dart:async'; | 890 import 'dart:async'; |
891 Future test() async { | 891 Future test() async { |
892 dynamic d; | 892 dynamic d; |
893 List<int> l0 = /*warning:DOWN_CAST_COMPOSITE should be pass*/await /*pass shou ld be info:INFERRED_TYPE_LITERAL*/[d]; | 893 List<int> l0 = await /*info:INFERRED_TYPE_LITERAL*/[/*info:DYNAMIC_CAST*/d]; |
894 List<int> l1 = await /*info:INFERRED_TYPE_ALLOCATION*/new Future.value(/*info: INFERRED_TYPE_LITERAL*/[/*info:DYNAMIC_CAST*/d]); | 894 List<int> l1 = await /*info:INFERRED_TYPE_ALLOCATION*/new Future.value(/*info: INFERRED_TYPE_LITERAL*/[/*info:DYNAMIC_CAST*/d]); |
895 } | 895 } |
896 '''); | 896 '''); |
897 } | 897 } |
898 | 898 |
899 void test_downwardsInferenceForEach() { | 899 void test_downwardsInferenceForEach() { |
900 checkFile(''' | 900 checkFile(''' |
901 import 'dart:async'; | 901 import 'dart:async'; |
902 Future main() async { | 902 Future main() async { |
903 for(int x in /*info:INFERRED_TYPE_LITERAL*/[1, 2, 3]) {} | 903 for(int x in /*info:INFERRED_TYPE_LITERAL*/[1, 2, 3]) {} |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1506 int get y => null; | 1506 int get y => null; |
1507 '''); | 1507 '''); |
1508 var x = mainUnit.types[0].fields[0]; | 1508 var x = mainUnit.types[0].fields[0]; |
1509 expect(x.type.toString(), 'int'); | 1509 expect(x.type.toString(), 'int'); |
1510 } | 1510 } |
1511 | 1511 |
1512 void test_futureThen() { | 1512 void test_futureThen() { |
1513 checkFile(''' | 1513 checkFile(''' |
1514 import 'dart:async'; | 1514 import 'dart:async'; |
1515 Future f; | 1515 Future f; |
1516 Future<int> t1 = f.then((_) => new Future<int>.value(42)); | 1516 Future<int> t1 = f.then((_) async => await new Future<int>.value(3)); |
1517 Future<int> t2 = f.then(/*info:INFERRED_TYPE_CLOSURE*/(_) async {return await ne w Future<int>.value(3);}); | |
1518 Future<int> t3 = f.then((_) async => 3); | |
1519 Future<int> t4 = f.then(/*info:INFERRED_TYPE_CLOSURE*/(_) async {return 3;}); | |
1520 Future<int> t5 = f.then((_) => new Future<int>.value(3)); | |
1521 Future<int> t6 = f.then((_) {return new Future<int>.value(3);}); | |
1522 Future<int> t7 = f.then((_) async => new Future<int>.value(3)); | |
1523 Future<int> t8 = f.then(/*info:INFERRED_TYPE_CLOSURE*/(_) async {return new Futu re<int>.value(3);}); | |
1517 '''); | 1524 '''); |
Leaf
2016/08/05 22:32:35
sweeeet.
| |
1518 } | 1525 } |
1519 | 1526 |
1527 void test_futureThen_conditional() { | |
1528 checkFile(''' | |
1529 import 'dart:async'; | |
1530 Future<bool> f; | |
1531 Future<int> t1 = f.then((x) async => x ? 2 : await new Future<int>.value(3)); | |
1532 Future<int> t2 = f.then(/*info:INFERRED_TYPE_CLOSURE*/(x) async {return await x ? 2 : new Future<int>.value(3);}); | |
1533 Future<int> t5 = f.then((x) => x ? 2 : new Future<int>.value(3)); | |
1534 Future<int> t6 = f.then((x) {return x ? 2 : new Future<int>.value(3);}); | |
1535 '''); | |
1536 } | |
1537 | |
1538 void test_futureUnion_asyncConditional() { | |
1539 checkFile(''' | |
1540 import 'dart:async'; | |
1541 | |
1542 Future<int> g1(bool x) async { return x ? 42 : /*info:INFERRED_TYPE_ALLOCATION*/ new Future.value(42); } | |
1543 Future<int> g2(bool x) async => x ? 42 : /*info:INFERRED_TYPE_ALLOCATION*/new Fu ture.value(42); | |
1544 | |
1545 Future<int> g3(bool x) async { | |
1546 var y = x ? 42 : /*info:INFERRED_TYPE_ALLOCATION*/new Future.value(42); | |
1547 return y; | |
1548 } | |
1549 '''); | |
1550 } | |
1551 | |
1552 void test_futureUnion_downwards() { | |
1553 checkFile(''' | |
1554 import 'dart:async'; | |
1555 Future f; | |
1556 // Instantiates Future<int> | |
1557 Future<int> t1 = f.then((_) => /*info:INFERRED_TYPE_ALLOCATION*/new Future.value (/*error:ARGUMENT_TYPE_NOT_ASSIGNABLE*/'hi')); | |
1558 | |
1559 // Instantiates List<int> | |
1560 Future<List<int>> t2 = f.then((_) => /*info:INFERRED_TYPE_LITERAL*/[3]); | |
1561 Future<List<int>> g2() async { return /*info:INFERRED_TYPE_LITERAL*/[3]; } | |
1562 Future<List<int>> g3() async { return /*info:INFERRED_TYPE_ALLOCATION*/new Futur e.value(/*info:INFERRED_TYPE_LITERAL*/[3]); } | |
1563 '''); | |
1564 } | |
1565 | |
1520 void test_genericMethods_basicDownwardInference() { | 1566 void test_genericMethods_basicDownwardInference() { |
1521 checkFile(r''' | 1567 checkFile(r''' |
1522 /*=T*/ f/*<S, T>*/(/*=S*/ s) => null; | 1568 /*=T*/ f/*<S, T>*/(/*=S*/ s) => null; |
1523 main() { | 1569 main() { |
1524 String x = f(42); | 1570 String x = f(42); |
1525 String y = (f)(42); | 1571 String y = (f)(42); |
1526 } | 1572 } |
1527 '''); | 1573 '''); |
1528 } | 1574 } |
1529 | 1575 |
(...skipping 2699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4229 } | 4275 } |
4230 | 4276 |
4231 /// Adds a file using [helper.addFile] and calls [helper.check]. | 4277 /// Adds a file using [helper.addFile] and calls [helper.check]. |
4232 /// | 4278 /// |
4233 /// Also returns the resolved compilation unit. | 4279 /// Also returns the resolved compilation unit. |
4234 @override | 4280 @override |
4235 CompilationUnitElement checkFile(String content) { | 4281 CompilationUnitElement checkFile(String content) { |
4236 return helper.checkFile(content).element; | 4282 return helper.checkFile(content).element; |
4237 } | 4283 } |
4238 } | 4284 } |
OLD | NEW |