Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 engine.resolver_test; | 5 library engine.resolver_test; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'package:analyzer/src/generated/java_core.dart'; | 8 import 'package:analyzer/src/generated/java_core.dart'; |
| 9 import 'package:analyzer/src/generated/java_engine.dart'; | 9 import 'package:analyzer/src/generated/java_engine.dart'; |
| 10 import 'package:analyzer/src/generated/java_engine_io.dart'; | 10 import 'package:analyzer/src/generated/java_engine_io.dart'; |
| (...skipping 3811 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3822 } catch (exception, stackTrace) { | 3822 } catch (exception, stackTrace) { |
| 3823 print('exception at $stackTrace'); | 3823 print('exception at $stackTrace'); |
| 3824 } | 3824 } |
| 3825 } | 3825 } |
| 3826 print(x) {}'''); | 3826 print(x) {}'''); |
| 3827 resolve(source); | 3827 resolve(source); |
| 3828 assertErrors(source); | 3828 assertErrors(source); |
| 3829 verify([source]); | 3829 verify([source]); |
| 3830 } | 3830 } |
| 3831 | 3831 |
| 3832 void test_unusedField_notUsed_noReference() { | |
| 3833 enableUnusedElement = true; | |
| 3834 Source source = addSource(r''' | |
| 3835 class A { | |
| 3836 int _f; | |
| 3837 } | |
| 3838 '''); | |
| 3839 resolve(source); | |
| 3840 assertErrors(source, [HintCode.UNUSED_FIELD]); | |
| 3841 verify([source]); | |
| 3842 } | |
| 3843 | |
| 3844 void test_unusedField_notUsed_simpleAssignment() { | |
| 3845 enableUnusedElement = true; | |
| 3846 Source source = addSource(r''' | |
| 3847 class A { | |
| 3848 int _f; | |
| 3849 m() { | |
| 3850 _f = 1; | |
| 3851 } | |
| 3852 } | |
| 3853 main(A a) { | |
| 3854 a._f = 2; | |
| 3855 } | |
| 3856 '''); | |
| 3857 resolve(source); | |
| 3858 assertErrors(source, [HintCode.UNUSED_FIELD]); | |
| 3859 verify([source]); | |
| 3860 } | |
| 3861 | |
| 3862 void test_unusedField_notUsed_compoundAssign() { | |
| 3863 enableUnusedElement = true; | |
| 3864 Source source = addSource(r''' | |
| 3865 class A { | |
| 3866 int _f; | |
| 3867 main() { | |
| 3868 _f += 2; | |
| 3869 } | |
| 3870 }'''); | |
| 3871 resolve(source); | |
| 3872 assertErrors(source, [HintCode.UNUSED_FIELD]); | |
| 3873 verify([source]); | |
| 3874 } | |
| 3875 | |
| 3876 void test_unusedField_notUsed_postfixExpr() { | |
| 3877 enableUnusedElement = true; | |
| 3878 Source source = addSource(r''' | |
| 3879 class A { | |
| 3880 int _f = 0; | |
| 3881 main() { | |
| 3882 _f++; | |
| 3883 } | |
| 3884 }'''); | |
| 3885 resolve(source); | |
| 3886 assertErrors(source, [HintCode.UNUSED_FIELD]); | |
| 3887 verify([source]); | |
| 3888 } | |
| 3889 | |
| 3890 void test_unusedField_notUsed_prefixExpr() { | |
| 3891 enableUnusedElement = true; | |
| 3892 Source source = addSource(r''' | |
| 3893 class A { | |
| 3894 int _f = 0; | |
| 3895 main() { | |
| 3896 ++_f; | |
| 3897 } | |
| 3898 }'''); | |
| 3899 resolve(source); | |
| 3900 assertErrors(source, [HintCode.UNUSED_FIELD]); | |
| 3901 verify([source]); | |
| 3902 } | |
| 3903 | |
| 3904 void test_unusedField_isUsed_argument() { | |
| 3905 enableUnusedElement = true; | |
| 3906 Source source = addSource(r''' | |
| 3907 class A { | |
| 3908 int _f = 0; | |
| 3909 main() { | |
| 3910 print(++_f); | |
| 3911 } | |
| 3912 } | |
| 3913 print(x) {}'''); | |
| 3914 resolve(source); | |
| 3915 assertErrors(source); | |
| 3916 verify([source]); | |
| 3917 } | |
| 3918 | |
| 3919 void test_unusedField_isUsed_reference_implicitThis() { | |
| 3920 enableUnusedElement = true; | |
| 3921 Source source = addSource(r''' | |
| 3922 class A { | |
| 3923 int _f; | |
| 3924 main() { | |
| 3925 print(_f); | |
| 3926 } | |
| 3927 } | |
| 3928 print(x) {}'''); | |
| 3929 resolve(source); | |
| 3930 assertErrors(source); | |
| 3931 verify([source]); | |
| 3932 } | |
| 3933 | |
| 3934 void test_unusedField_isUsed_reference_implicitThis_subclass() { | |
| 3935 enableUnusedElement = true; | |
| 3936 Source source = addSource(r''' | |
| 3937 class A { | |
| 3938 int _f; | |
| 3939 main() { | |
| 3940 print(_f); | |
| 3941 } | |
| 3942 } | |
| 3943 class B extends A { | |
| 3944 int _f; | |
| 3945 } | |
| 3946 print(x) {}'''); | |
| 3947 resolve(source); | |
| 3948 assertErrors(source); | |
| 3949 verify([source]); | |
| 3950 } | |
| 3951 | |
| 3952 void test_unusedField_isUsed_reference_qualified_staticElement() { | |
| 3953 enableUnusedElement = true; | |
| 3954 Source source = addSource(r''' | |
| 3955 class A { | |
| 3956 int _f; | |
| 3957 } | |
| 3958 main() { | |
| 3959 A a = new A(); | |
| 3960 print(a._f); | |
| 3961 } | |
| 3962 print(x) {}'''); | |
| 3963 resolve(source); | |
| 3964 assertErrors(source); | |
| 3965 verify([source]); | |
| 3966 } | |
| 3967 | |
| 3968 void test_unusedField_isUsed_reference_qualified_propagatedElement() { | |
| 3969 enableUnusedElement = true; | |
| 3970 Source source = addSource(r''' | |
| 3971 class A { | |
| 3972 int _f; | |
| 3973 } | |
| 3974 main() { | |
| 3975 var a = new A(); | |
| 3976 print(a._f); | |
| 3977 } | |
| 3978 print(x) {}'''); | |
| 3979 resolve(source); | |
| 3980 assertErrors(source); | |
| 3981 verify([source]); | |
| 3982 } | |
| 3983 | |
| 3984 void test_unusedField_isUsed_reference_qualified_unresolved() { | |
|
Brian Wilkerson
2014/11/13 21:54:49
Perhaps a test like:
class A {
int _f;
int m(
scheglov
2014/11/13 23:13:16
Done.
| |
| 3985 enableUnusedElement = true; | |
| 3986 Source source = addSource(r''' | |
| 3987 class A { | |
| 3988 int _f; | |
| 3989 } | |
| 3990 main(a) { | |
| 3991 print(a._f); | |
| 3992 } | |
| 3993 print(x) {}'''); | |
| 3994 resolve(source); | |
| 3995 assertErrors(source); | |
| 3996 verify([source]); | |
| 3997 } | |
| 3998 | |
| 3832 void test_useOfVoidResult_assignmentExpression_function() { | 3999 void test_useOfVoidResult_assignmentExpression_function() { |
| 3833 Source source = addSource(r''' | 4000 Source source = addSource(r''' |
| 3834 void f() {} | 4001 void f() {} |
| 3835 class A { | 4002 class A { |
| 3836 n() { | 4003 n() { |
| 3837 var a; | 4004 var a; |
| 3838 a = f(); | 4005 a = f(); |
| 3839 } | 4006 } |
| 3840 }'''); | 4007 }'''); |
| 3841 resolve(source); | 4008 resolve(source); |
| (...skipping 2975 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6817 } | 6984 } |
| 6818 } | 6985 } |
| 6819 | 6986 |
| 6820 class ResolverTestCase extends EngineTestCase { | 6987 class ResolverTestCase extends EngineTestCase { |
| 6821 /** | 6988 /** |
| 6822 * The analysis context used to parse the compilation units being resolved. | 6989 * The analysis context used to parse the compilation units being resolved. |
| 6823 */ | 6990 */ |
| 6824 AnalysisContextImpl analysisContext2; | 6991 AnalysisContextImpl analysisContext2; |
| 6825 | 6992 |
| 6826 /** | 6993 /** |
| 6827 * Specifies if [assertErrors] should check for [HintCode.UNUSED_ELEMENT]. | 6994 * Specifies if [assertErrors] should check for [HintCode.UNUSED_ELEMENT] and |
| 6995 * [HintCode.UNUSED_FIELD]. | |
| 6828 */ | 6996 */ |
| 6829 bool enableUnusedElement = false; | 6997 bool enableUnusedElement = false; |
| 6830 | 6998 |
| 6831 /** | 6999 /** |
| 6832 * Specifies if [assertErrors] should check for [HintCode.UNUSED_LOCAL_VARIABL E]. | 7000 * Specifies if [assertErrors] should check for [HintCode.UNUSED_LOCAL_VARIABL E]. |
| 6833 */ | 7001 */ |
| 6834 bool enableUnusedLocalVariable = false; | 7002 bool enableUnusedLocalVariable = false; |
| 6835 | 7003 |
| 6836 @override | 7004 @override |
| 6837 void setUp() { | 7005 void setUp() { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 6868 * | 7036 * |
| 6869 * @param source the source against which the errors should have been reported | 7037 * @param source the source against which the errors should have been reported |
| 6870 * @param expectedErrorCodes the error codes of the errors that should have be en reported | 7038 * @param expectedErrorCodes the error codes of the errors that should have be en reported |
| 6871 * @throws AnalysisException if the reported errors could not be computed | 7039 * @throws AnalysisException if the reported errors could not be computed |
| 6872 * @throws AssertionFailedError if a different number of errors have been repo rted than were | 7040 * @throws AssertionFailedError if a different number of errors have been repo rted than were |
| 6873 * expected | 7041 * expected |
| 6874 */ | 7042 */ |
| 6875 void assertErrors(Source source, [List<ErrorCode> expectedErrorCodes = ErrorCo de.EMPTY_LIST]) { | 7043 void assertErrors(Source source, [List<ErrorCode> expectedErrorCodes = ErrorCo de.EMPTY_LIST]) { |
| 6876 GatheringErrorListener errorListener = new GatheringErrorListener(); | 7044 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 6877 for (AnalysisError error in analysisContext2.computeErrors(source)) { | 7045 for (AnalysisError error in analysisContext2.computeErrors(source)) { |
| 6878 if (error.errorCode == HintCode.UNUSED_ELEMENT && | 7046 ErrorCode errorCode = error.errorCode; |
| 6879 !enableUnusedElement) { | 7047 if (!enableUnusedElement && |
| 7048 (errorCode == HintCode.UNUSED_ELEMENT || errorCode == HintCode.UNUSED_ FIELD)) { | |
| 6880 continue; | 7049 continue; |
| 6881 } | 7050 } |
| 6882 if (error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE && | 7051 if (!enableUnusedLocalVariable && |
| 6883 !enableUnusedLocalVariable) { | 7052 errorCode == HintCode.UNUSED_LOCAL_VARIABLE) { |
| 6884 continue; | 7053 continue; |
| 6885 } | 7054 } |
| 6886 errorListener.onError(error); | 7055 errorListener.onError(error); |
| 6887 } | 7056 } |
| 6888 errorListener.assertErrorsWithCodes(expectedErrorCodes); | 7057 errorListener.assertErrorsWithCodes(expectedErrorCodes); |
| 6889 } | 7058 } |
| 6890 | 7059 |
| 6891 /** | 7060 /** |
| 6892 * Assert that no errors have been reported against the given source. | 7061 * Assert that no errors have been reported against the given source. |
| 6893 * | 7062 * |
| (...skipping 5360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 12254 runReflectiveTests(TypeResolverVisitorTest); | 12423 runReflectiveTests(TypeResolverVisitorTest); |
| 12255 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); | 12424 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); |
| 12256 runReflectiveTests(ErrorResolverTest); | 12425 runReflectiveTests(ErrorResolverTest); |
| 12257 runReflectiveTests(HintCodeTest); | 12426 runReflectiveTests(HintCodeTest); |
| 12258 runReflectiveTests(MemberMapTest); | 12427 runReflectiveTests(MemberMapTest); |
| 12259 runReflectiveTests(NonHintCodeTest); | 12428 runReflectiveTests(NonHintCodeTest); |
| 12260 runReflectiveTests(SimpleResolverTest); | 12429 runReflectiveTests(SimpleResolverTest); |
| 12261 runReflectiveTests(StrictModeTest); | 12430 runReflectiveTests(StrictModeTest); |
| 12262 runReflectiveTests(TypePropagationTest); | 12431 runReflectiveTests(TypePropagationTest); |
| 12263 } | 12432 } |
| OLD | NEW |