OLD | NEW |
---|---|
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 analyzer.test.generated.hint_code_test; | 5 library analyzer.test.generated.hint_code_test; |
6 | 6 |
7 import 'package:analyzer/src/generated/engine.dart'; | 7 import 'package:analyzer/src/generated/engine.dart'; |
8 import 'package:analyzer/src/generated/error.dart'; | 8 import 'package:analyzer/src/generated/error.dart'; |
9 import 'package:analyzer/src/generated/parser.dart'; | |
9 import 'package:analyzer/src/generated/source_io.dart'; | 10 import 'package:analyzer/src/generated/source_io.dart'; |
10 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
11 | 12 |
12 import '../reflective_tests.dart'; | 13 import '../reflective_tests.dart'; |
13 import '../utils.dart'; | 14 import '../utils.dart'; |
14 import 'analysis_context_factory.dart'; | 15 import 'analysis_context_factory.dart'; |
15 import 'resolver_test_case.dart'; | 16 import 'resolver_test_case.dart'; |
16 | 17 |
17 main() { | 18 main() { |
18 initializeTestEnvironment(); | 19 initializeTestEnvironment(); |
19 runReflectiveTests(HintCodeTest); | 20 runReflectiveTests(HintCodeTest); |
20 } | 21 } |
21 | 22 |
22 @reflectiveTest | 23 @reflectiveTest |
23 class HintCodeTest extends ResolverTestCase { | 24 class HintCodeTest extends ResolverTestCase { |
24 void fail_isInt() { | |
25 Source source = addSource("var v = 1 is int;"); | |
26 computeLibrarySourceErrors(source); | |
27 assertErrors(source, [HintCode.IS_INT]); | |
28 verify([source]); | |
29 } | |
30 | |
31 void fail_isNotInt() { | |
32 Source source = addSource("var v = 1 is! int;"); | |
33 computeLibrarySourceErrors(source); | |
34 assertErrors(source, [HintCode.IS_NOT_INT]); | |
35 verify([source]); | |
36 } | |
37 | |
38 void fail_overrideEqualsButNotHashCode() { | |
39 Source source = addSource(r''' | |
40 class A { | |
41 bool operator ==(x) {} | |
42 }'''); | |
43 computeLibrarySourceErrors(source); | |
44 assertErrors(source, [HintCode.OVERRIDE_EQUALS_BUT_NOT_HASH_CODE]); | |
45 verify([source]); | |
46 } | |
47 | |
48 void fail_unusedImport_as_equalPrefixes() { | |
49 // See todo at ImportsVerifier.prefixElementMap. | |
50 Source source = addSource(r''' | |
51 library L; | |
52 import 'lib1.dart' as one; | |
53 import 'lib2.dart' as one; | |
54 one.A a;'''); | |
55 Source source2 = addNamedSource( | |
56 "/lib1.dart", | |
57 r''' | |
58 library lib1; | |
59 class A {}'''); | |
60 Source source3 = addNamedSource( | |
61 "/lib2.dart", | |
62 r''' | |
63 library lib2; | |
64 class B {}'''); | |
65 computeLibrarySourceErrors(source); | |
66 assertErrors(source, [HintCode.UNUSED_IMPORT]); | |
67 assertNoErrors(source2); | |
68 assertNoErrors(source3); | |
69 verify([source, source2, source3]); | |
70 } | |
71 | |
72 @override | 25 @override |
73 void reset() { | 26 void reset() { |
74 analysisContext2 = AnalysisContextFactory.contextWithCoreAndPackages({ | 27 analysisContext2 = AnalysisContextFactory.contextWithCoreAndPackages({ |
75 'package:meta/meta.dart': r''' | 28 'package:meta/meta.dart': r''' |
76 library meta; | 29 library meta; |
77 | 30 |
78 const _Factory factory = const _Factory(); | 31 const _Factory factory = const _Factory(); |
79 const _Literal literal = const _Literal(); | 32 const _Literal literal = const _Literal(); |
80 const _MustCallSuper mustCallSuper = const _MustCallSuper(); | 33 const _MustCallSuper mustCallSuper = const _MustCallSuper(); |
81 const _Override override = const _Override(); | 34 const _Override override = const _Override(); |
(...skipping 1637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1719 void test_isDouble() { | 1672 void test_isDouble() { |
1720 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 1673 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
1721 options.dart2jsHint = true; | 1674 options.dart2jsHint = true; |
1722 resetWithOptions(options); | 1675 resetWithOptions(options); |
1723 Source source = addSource("var v = 1 is double;"); | 1676 Source source = addSource("var v = 1 is double;"); |
1724 computeLibrarySourceErrors(source); | 1677 computeLibrarySourceErrors(source); |
1725 assertErrors(source, [HintCode.IS_DOUBLE]); | 1678 assertErrors(source, [HintCode.IS_DOUBLE]); |
1726 verify([source]); | 1679 verify([source]); |
1727 } | 1680 } |
1728 | 1681 |
1682 @failingTest | |
1683 void test_isInt() { | |
1684 Source source = addSource("var v = 1 is int;"); | |
1685 computeLibrarySourceErrors(source); | |
1686 assertErrors(source, [HintCode.IS_INT]); | |
1687 verify([source]); | |
1688 } | |
1689 | |
1729 void test_isNotDouble() { | 1690 void test_isNotDouble() { |
1730 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 1691 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
1731 options.dart2jsHint = true; | 1692 options.dart2jsHint = true; |
1732 resetWithOptions(options); | 1693 resetWithOptions(options); |
1733 Source source = addSource("var v = 1 is! double;"); | 1694 Source source = addSource("var v = 1 is! double;"); |
1734 computeLibrarySourceErrors(source); | 1695 computeLibrarySourceErrors(source); |
1735 assertErrors(source, [HintCode.IS_NOT_DOUBLE]); | 1696 assertErrors(source, [HintCode.IS_NOT_DOUBLE]); |
1736 verify([source]); | 1697 verify([source]); |
1737 } | 1698 } |
1738 | 1699 |
1700 @failingTest | |
1701 void test_isNotInt() { | |
1702 Source source = addSource("var v = 1 is! int;"); | |
1703 computeLibrarySourceErrors(source); | |
1704 assertErrors(source, [HintCode.IS_NOT_INT]); | |
1705 verify([source]); | |
1706 } | |
1707 | |
1739 void test_js_lib_OK() { | 1708 void test_js_lib_OK() { |
1740 Source source = addSource(r''' | 1709 Source source = addSource(r''' |
1741 @JS() | 1710 @JS() |
1742 library foo; | 1711 library foo; |
1743 | 1712 |
1744 import 'package:js/js.dart'; | 1713 import 'package:js/js.dart'; |
1745 | 1714 |
1746 @JS() | 1715 @JS() |
1747 class A { } | 1716 class A { } |
1748 '''); | 1717 '''); |
1749 computeLibrarySourceErrors(source); | 1718 computeLibrarySourceErrors(source); |
1750 assertNoErrors(source); | 1719 assertNoErrors(source); |
1751 verify([source]); | 1720 verify([source]); |
1752 } | 1721 } |
1753 | 1722 |
1754 void test_missing_js_lib_on_class_decl() { | 1723 void test_missingJsLibAnnotation_class() { |
1755 Source source = addSource(r''' | 1724 Source source = addSource(r''' |
1756 library foo; | 1725 library foo; |
1757 | 1726 |
1758 import 'package:js/js.dart'; | 1727 import 'package:js/js.dart'; |
1759 | 1728 |
1760 @JS() | 1729 @JS() |
1761 class A { } | 1730 class A { } |
1762 '''); | 1731 '''); |
1763 computeLibrarySourceErrors(source); | 1732 computeLibrarySourceErrors(source); |
1764 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); | 1733 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); |
1765 verify([source]); | 1734 verify([source]); |
1766 } | 1735 } |
1767 | 1736 |
1768 void test_missing_js_lib_on_function() { | 1737 void test_missingJsLibAnnotation_externalField() { |
1738 // https://github.com/dart-lang/sdk/issues/26987 | |
1739 Source source = addSource(r''' | |
1740 import 'package:js/js.dart'; | |
1741 | |
1742 @JS() | |
1743 external dynamic exports; | |
1744 '''); | |
1745 computeLibrarySourceErrors(source); | |
1746 assertErrors(source, [ParserErrorCode.EXTERNAL_FIELD]); | |
1747 verify([source]); | |
1748 } | |
1749 | |
1750 void test_missingJsLibAnnotation_function() { | |
1769 Source source = addSource(r''' | 1751 Source source = addSource(r''' |
1770 library foo; | 1752 library foo; |
1771 | 1753 |
1772 import 'package:js/js.dart'; | 1754 import 'package:js/js.dart'; |
1773 | 1755 |
1774 @JS('acxZIndex') | 1756 @JS('acxZIndex') |
1775 set _currentZIndex(int value) { } | 1757 set _currentZIndex(int value) { } |
1776 '''); | 1758 '''); |
1777 computeLibrarySourceErrors(source); | 1759 computeLibrarySourceErrors(source); |
1778 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); | 1760 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); |
1779 verify([source]); | 1761 verify([source]); |
1780 } | 1762 } |
1781 | 1763 |
1782 void test_missing_js_lib_on_member() { | 1764 void test_missingJsLibAnnotation_method() { |
1783 Source source = addSource(r''' | 1765 Source source = addSource(r''' |
1784 library foo; | 1766 library foo; |
1785 | 1767 |
1786 import 'package:js/js.dart'; | 1768 import 'package:js/js.dart'; |
1787 | 1769 |
1788 class A { | 1770 class A { |
1789 @JS() | 1771 @JS() |
1790 void a() { } | 1772 void a() { } |
1791 } | 1773 } |
1792 '''); | 1774 '''); |
1793 computeLibrarySourceErrors(source); | 1775 computeLibrarySourceErrors(source); |
1794 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); | 1776 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); |
1795 verify([source]); | 1777 verify([source]); |
1796 } | 1778 } |
1797 | 1779 |
1780 @failingTest | |
1781 void test_missingJsLibAnnotation_variable() { | |
Brian Wilkerson
2016/07/29 17:09:06
Should this test pass, or is the test invalid?
pquitslund
2016/07/29 18:05:25
Hmmmm. It seems to me like it *should* pass.
Brian Wilkerson
2016/07/29 18:56:22
Ok, it passes now.
| |
1782 Source source = addSource(r''' | |
1783 import 'package:js/js.dart'; | |
1784 | |
1785 @JS() | |
1786 dynamic variable; | |
1787 '''); | |
1788 computeLibrarySourceErrors(source); | |
1789 assertErrors(source, [HintCode.MISSING_JS_LIB_ANNOTATION]); | |
1790 verify([source]); | |
1791 } | |
1792 | |
1798 void test_missingReturn_async() { | 1793 void test_missingReturn_async() { |
1799 Source source = addSource(''' | 1794 Source source = addSource(''' |
1800 import 'dart:async'; | 1795 import 'dart:async'; |
1801 Future<int> f() async {} | 1796 Future<int> f() async {} |
1802 '''); | 1797 '''); |
1803 computeLibrarySourceErrors(source); | 1798 computeLibrarySourceErrors(source); |
1804 assertErrors(source, [HintCode.MISSING_RETURN]); | 1799 assertErrors(source, [HintCode.MISSING_RETURN]); |
1805 verify([source]); | 1800 verify([source]); |
1806 } | 1801 } |
1807 | 1802 |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2040 Source source = addSource(r''' | 2035 Source source = addSource(r''' |
2041 m(x) { | 2036 m(x) { |
2042 while (x?.a) {} | 2037 while (x?.a) {} |
2043 } | 2038 } |
2044 '''); | 2039 '''); |
2045 computeLibrarySourceErrors(source); | 2040 computeLibrarySourceErrors(source); |
2046 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); | 2041 assertErrors(source, [HintCode.NULL_AWARE_IN_CONDITION]); |
2047 verify([source]); | 2042 verify([source]); |
2048 } | 2043 } |
2049 | 2044 |
2045 @failingTest | |
2046 void test_overrideEqualsButNotHashCode() { | |
2047 Source source = addSource(r''' | |
2048 class A { | |
2049 bool operator ==(x) {} | |
2050 }'''); | |
2051 computeLibrarySourceErrors(source); | |
2052 assertErrors(source, [HintCode.OVERRIDE_EQUALS_BUT_NOT_HASH_CODE]); | |
2053 verify([source]); | |
2054 } | |
2055 | |
2050 void test_overrideOnNonOverridingField_invalid() { | 2056 void test_overrideOnNonOverridingField_invalid() { |
2051 Source source = addSource(r''' | 2057 Source source = addSource(r''' |
2052 library dart.core; | 2058 library dart.core; |
2053 const override = null; | 2059 const override = null; |
2054 class A { | 2060 class A { |
2055 } | 2061 } |
2056 class B extends A { | 2062 class B extends A { |
2057 @override | 2063 @override |
2058 final int m = 1; | 2064 final int m = 1; |
2059 }'''); | 2065 }'''); |
(...skipping 1444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3504 "/lib1.dart", | 3510 "/lib1.dart", |
3505 r''' | 3511 r''' |
3506 library lib1; | 3512 library lib1; |
3507 class A {}'''); | 3513 class A {}'''); |
3508 computeLibrarySourceErrors(source); | 3514 computeLibrarySourceErrors(source); |
3509 assertErrors(source, [HintCode.UNUSED_IMPORT]); | 3515 assertErrors(source, [HintCode.UNUSED_IMPORT]); |
3510 assertNoErrors(source2); | 3516 assertNoErrors(source2); |
3511 verify([source, source2]); | 3517 verify([source, source2]); |
3512 } | 3518 } |
3513 | 3519 |
3520 @failingTest | |
3521 void test_unusedImport_as_equalPrefixes() { | |
3522 // See todo at ImportsVerifier.prefixElementMap. | |
3523 Source source = addSource(r''' | |
3524 library L; | |
3525 import 'lib1.dart' as one; | |
3526 import 'lib2.dart' as one; | |
3527 one.A a;'''); | |
3528 Source source2 = addNamedSource( | |
3529 "/lib1.dart", | |
3530 r''' | |
3531 library lib1; | |
3532 class A {}'''); | |
3533 Source source3 = addNamedSource( | |
3534 "/lib2.dart", | |
3535 r''' | |
3536 library lib2; | |
3537 class B {}'''); | |
3538 computeLibrarySourceErrors(source); | |
3539 assertErrors(source, [HintCode.UNUSED_IMPORT]); | |
3540 assertNoErrors(source2); | |
3541 assertNoErrors(source3); | |
3542 verify([source, source2, source3]); | |
3543 } | |
3544 | |
3514 void test_unusedImport_hide() { | 3545 void test_unusedImport_hide() { |
3515 Source source = addSource(r''' | 3546 Source source = addSource(r''' |
3516 library L; | 3547 library L; |
3517 import 'lib1.dart'; | 3548 import 'lib1.dart'; |
3518 import 'lib1.dart' hide A; | 3549 import 'lib1.dart' hide A; |
3519 A a;'''); | 3550 A a;'''); |
3520 Source source2 = addNamedSource( | 3551 Source source2 = addNamedSource( |
3521 "/lib1.dart", | 3552 "/lib1.dart", |
3522 r''' | 3553 r''' |
3523 library lib1; | 3554 library lib1; |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3894 n() { | 3925 n() { |
3895 var a = m(), b = m(); | 3926 var a = m(), b = m(); |
3896 } | 3927 } |
3897 }'''); | 3928 }'''); |
3898 computeLibrarySourceErrors(source); | 3929 computeLibrarySourceErrors(source); |
3899 assertErrors( | 3930 assertErrors( |
3900 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]); | 3931 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]); |
3901 verify([source]); | 3932 verify([source]); |
3902 } | 3933 } |
3903 } | 3934 } |
OLD | NEW |