| 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_expressionFunctionBody() { |
| 3935 enableUnusedElement = true; |
| 3936 Source source = addSource(r''' |
| 3937 class A { |
| 3938 int _f; |
| 3939 m() => _f; |
| 3940 }'''); |
| 3941 resolve(source); |
| 3942 assertErrors(source); |
| 3943 verify([source]); |
| 3944 } |
| 3945 |
| 3946 void test_unusedField_isUsed_reference_implicitThis_subclass() { |
| 3947 enableUnusedElement = true; |
| 3948 Source source = addSource(r''' |
| 3949 class A { |
| 3950 int _f; |
| 3951 main() { |
| 3952 print(_f); |
| 3953 } |
| 3954 } |
| 3955 class B extends A { |
| 3956 int _f; |
| 3957 } |
| 3958 print(x) {}'''); |
| 3959 resolve(source); |
| 3960 assertErrors(source); |
| 3961 verify([source]); |
| 3962 } |
| 3963 |
| 3964 void test_unusedField_isUsed_reference_qualified_staticElement() { |
| 3965 enableUnusedElement = true; |
| 3966 Source source = addSource(r''' |
| 3967 class A { |
| 3968 int _f; |
| 3969 } |
| 3970 main() { |
| 3971 A a = new A(); |
| 3972 print(a._f); |
| 3973 } |
| 3974 print(x) {}'''); |
| 3975 resolve(source); |
| 3976 assertErrors(source); |
| 3977 verify([source]); |
| 3978 } |
| 3979 |
| 3980 void test_unusedField_isUsed_reference_qualified_propagatedElement() { |
| 3981 enableUnusedElement = true; |
| 3982 Source source = addSource(r''' |
| 3983 class A { |
| 3984 int _f; |
| 3985 } |
| 3986 main() { |
| 3987 var a = new A(); |
| 3988 print(a._f); |
| 3989 } |
| 3990 print(x) {}'''); |
| 3991 resolve(source); |
| 3992 assertErrors(source); |
| 3993 verify([source]); |
| 3994 } |
| 3995 |
| 3996 void test_unusedField_isUsed_reference_qualified_unresolved() { |
| 3997 enableUnusedElement = true; |
| 3998 Source source = addSource(r''' |
| 3999 class A { |
| 4000 int _f; |
| 4001 } |
| 4002 main(a) { |
| 4003 print(a._f); |
| 4004 } |
| 4005 print(x) {}'''); |
| 4006 resolve(source); |
| 4007 assertErrors(source); |
| 4008 verify([source]); |
| 4009 } |
| 4010 |
| 3832 void test_useOfVoidResult_assignmentExpression_function() { | 4011 void test_useOfVoidResult_assignmentExpression_function() { |
| 3833 Source source = addSource(r''' | 4012 Source source = addSource(r''' |
| 3834 void f() {} | 4013 void f() {} |
| 3835 class A { | 4014 class A { |
| 3836 n() { | 4015 n() { |
| 3837 var a; | 4016 var a; |
| 3838 a = f(); | 4017 a = f(); |
| 3839 } | 4018 } |
| 3840 }'''); | 4019 }'''); |
| 3841 resolve(source); | 4020 resolve(source); |
| (...skipping 2975 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6817 } | 6996 } |
| 6818 } | 6997 } |
| 6819 | 6998 |
| 6820 class ResolverTestCase extends EngineTestCase { | 6999 class ResolverTestCase extends EngineTestCase { |
| 6821 /** | 7000 /** |
| 6822 * The analysis context used to parse the compilation units being resolved. | 7001 * The analysis context used to parse the compilation units being resolved. |
| 6823 */ | 7002 */ |
| 6824 AnalysisContextImpl analysisContext2; | 7003 AnalysisContextImpl analysisContext2; |
| 6825 | 7004 |
| 6826 /** | 7005 /** |
| 6827 * Specifies if [assertErrors] should check for [HintCode.UNUSED_ELEMENT]. | 7006 * Specifies if [assertErrors] should check for [HintCode.UNUSED_ELEMENT] and |
| 7007 * [HintCode.UNUSED_FIELD]. |
| 6828 */ | 7008 */ |
| 6829 bool enableUnusedElement = false; | 7009 bool enableUnusedElement = false; |
| 6830 | 7010 |
| 6831 /** | 7011 /** |
| 6832 * Specifies if [assertErrors] should check for [HintCode.UNUSED_LOCAL_VARIABL
E]. | 7012 * Specifies if [assertErrors] should check for [HintCode.UNUSED_LOCAL_VARIABL
E]. |
| 6833 */ | 7013 */ |
| 6834 bool enableUnusedLocalVariable = false; | 7014 bool enableUnusedLocalVariable = false; |
| 6835 | 7015 |
| 6836 @override | 7016 @override |
| 6837 void setUp() { | 7017 void setUp() { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 6868 * | 7048 * |
| 6869 * @param source the source against which the errors should have been reported | 7049 * @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 | 7050 * @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 | 7051 * @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 | 7052 * @throws AssertionFailedError if a different number of errors have been repo
rted than were |
| 6873 * expected | 7053 * expected |
| 6874 */ | 7054 */ |
| 6875 void assertErrors(Source source, [List<ErrorCode> expectedErrorCodes = ErrorCo
de.EMPTY_LIST]) { | 7055 void assertErrors(Source source, [List<ErrorCode> expectedErrorCodes = ErrorCo
de.EMPTY_LIST]) { |
| 6876 GatheringErrorListener errorListener = new GatheringErrorListener(); | 7056 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 6877 for (AnalysisError error in analysisContext2.computeErrors(source)) { | 7057 for (AnalysisError error in analysisContext2.computeErrors(source)) { |
| 6878 if (error.errorCode == HintCode.UNUSED_ELEMENT && | 7058 ErrorCode errorCode = error.errorCode; |
| 6879 !enableUnusedElement) { | 7059 if (!enableUnusedElement && |
| 7060 (errorCode == HintCode.UNUSED_ELEMENT || errorCode == HintCode.UNUSED_
FIELD)) { |
| 6880 continue; | 7061 continue; |
| 6881 } | 7062 } |
| 6882 if (error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE && | 7063 if (!enableUnusedLocalVariable && |
| 6883 !enableUnusedLocalVariable) { | 7064 errorCode == HintCode.UNUSED_LOCAL_VARIABLE) { |
| 6884 continue; | 7065 continue; |
| 6885 } | 7066 } |
| 6886 errorListener.onError(error); | 7067 errorListener.onError(error); |
| 6887 } | 7068 } |
| 6888 errorListener.assertErrorsWithCodes(expectedErrorCodes); | 7069 errorListener.assertErrorsWithCodes(expectedErrorCodes); |
| 6889 } | 7070 } |
| 6890 | 7071 |
| 6891 /** | 7072 /** |
| 6892 * Assert that no errors have been reported against the given source. | 7073 * Assert that no errors have been reported against the given source. |
| 6893 * | 7074 * |
| (...skipping 5360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12254 runReflectiveTests(TypeResolverVisitorTest); | 12435 runReflectiveTests(TypeResolverVisitorTest); |
| 12255 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); | 12436 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); |
| 12256 runReflectiveTests(ErrorResolverTest); | 12437 runReflectiveTests(ErrorResolverTest); |
| 12257 runReflectiveTests(HintCodeTest); | 12438 runReflectiveTests(HintCodeTest); |
| 12258 runReflectiveTests(MemberMapTest); | 12439 runReflectiveTests(MemberMapTest); |
| 12259 runReflectiveTests(NonHintCodeTest); | 12440 runReflectiveTests(NonHintCodeTest); |
| 12260 runReflectiveTests(SimpleResolverTest); | 12441 runReflectiveTests(SimpleResolverTest); |
| 12261 runReflectiveTests(StrictModeTest); | 12442 runReflectiveTests(StrictModeTest); |
| 12262 runReflectiveTests(TypePropagationTest); | 12443 runReflectiveTests(TypePropagationTest); |
| 12263 } | 12444 } |
| OLD | NEW |