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 3188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3199 Source source2 = addNamedSource("/lib1.dart", r''' | 3199 Source source2 = addNamedSource("/lib1.dart", r''' |
3200 library lib1; | 3200 library lib1; |
3201 class A {} | 3201 class A {} |
3202 class B {}'''); | 3202 class B {}'''); |
3203 resolve(source); | 3203 resolve(source); |
3204 assertErrors(source, [HintCode.UNUSED_IMPORT]); | 3204 assertErrors(source, [HintCode.UNUSED_IMPORT]); |
3205 assertNoErrors(source2); | 3205 assertNoErrors(source2); |
3206 verify([source, source2]); | 3206 verify([source, source2]); |
3207 } | 3207 } |
3208 | 3208 |
| 3209 void test_unusedLocalVariable() { |
| 3210 enableUnusedLocalVariable = true; |
| 3211 Source source = addSource(r''' |
| 3212 main() { |
| 3213 var v = 1; |
| 3214 v = 2; |
| 3215 }'''); |
| 3216 resolve(source); |
| 3217 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 3218 verify([source]); |
| 3219 } |
| 3220 |
| 3221 void test_unusedLocalVariable_isRead_notUsed_compoundAssign() { |
| 3222 enableUnusedLocalVariable = true; |
| 3223 Source source = addSource(r''' |
| 3224 main() { |
| 3225 var v = 1; |
| 3226 v += 2; |
| 3227 }'''); |
| 3228 resolve(source); |
| 3229 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 3230 verify([source]); |
| 3231 } |
| 3232 |
| 3233 void test_unusedLocalVariable_isRead_notUsed_postfixExpr() { |
| 3234 enableUnusedLocalVariable = true; |
| 3235 Source source = addSource(r''' |
| 3236 main() { |
| 3237 var v = 1; |
| 3238 v++; |
| 3239 }'''); |
| 3240 resolve(source); |
| 3241 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 3242 verify([source]); |
| 3243 } |
| 3244 |
| 3245 void test_unusedLocalVariable_isRead_notUsed_prefixExpr() { |
| 3246 enableUnusedLocalVariable = true; |
| 3247 Source source = addSource(r''' |
| 3248 main() { |
| 3249 var v = 1; |
| 3250 ++v; |
| 3251 }'''); |
| 3252 resolve(source); |
| 3253 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 3254 verify([source]); |
| 3255 } |
| 3256 |
| 3257 void test_unusedLocalVariable_isRead_usedArgument() { |
| 3258 enableUnusedLocalVariable = true; |
| 3259 Source source = addSource(r''' |
| 3260 main() { |
| 3261 var v = 1; |
| 3262 print(++v); |
| 3263 } |
| 3264 print(x) {}'''); |
| 3265 resolve(source); |
| 3266 assertErrors(source, []); |
| 3267 verify([source]); |
| 3268 } |
| 3269 |
| 3270 void test_unusedLocalVariable_isRead_usedInvocationTarget() { |
| 3271 enableUnusedLocalVariable = true; |
| 3272 Source source = addSource(r''' |
| 3273 class A { |
| 3274 foo() {} |
| 3275 } |
| 3276 main() { |
| 3277 var a = new A(); |
| 3278 a.foo(); |
| 3279 } |
| 3280 '''); |
| 3281 resolve(source); |
| 3282 assertErrors(source, []); |
| 3283 verify([source]); |
| 3284 } |
| 3285 |
| 3286 void test_unusedLocalVariable_isInvoked() { |
| 3287 enableUnusedLocalVariable = true; |
| 3288 Source source = addSource(r''' |
| 3289 typedef Foo(); |
| 3290 main() { |
| 3291 Foo foo; |
| 3292 foo(); |
| 3293 }'''); |
| 3294 resolve(source); |
| 3295 assertErrors(source, []); |
| 3296 verify([source]); |
| 3297 } |
| 3298 |
| 3299 void test_unusedLocalVariable_inCatch_exception() { |
| 3300 enableUnusedLocalVariable = true; |
| 3301 Source source = addSource(r''' |
| 3302 main() { |
| 3303 try { |
| 3304 } catch (exception) { |
| 3305 } |
| 3306 }'''); |
| 3307 resolve(source); |
| 3308 assertErrors(source, []); |
| 3309 verify([source]); |
| 3310 } |
| 3311 |
| 3312 void test_unusedLocalVariable_inCatch_stackTrace() { |
| 3313 enableUnusedLocalVariable = true; |
| 3314 Source source = addSource(r''' |
| 3315 main() { |
| 3316 try { |
| 3317 } catch (exception, stackTrace) { |
| 3318 } |
| 3319 }'''); |
| 3320 resolve(source); |
| 3321 assertErrors(source, [HintCode.UNUSED_LOCAL_VARIABLE]); |
| 3322 verify([source]); |
| 3323 } |
| 3324 |
| 3325 void test_unusedLocalVariable_inCatch_stackTrace_used() { |
| 3326 enableUnusedLocalVariable = true; |
| 3327 Source source = addSource(r''' |
| 3328 main() { |
| 3329 try { |
| 3330 } catch (exception, stackTrace) { |
| 3331 print('exceptino at $stackTrace'); |
| 3332 } |
| 3333 } |
| 3334 print(x) {}'''); |
| 3335 resolve(source); |
| 3336 assertErrors(source, []); |
| 3337 verify([source]); |
| 3338 } |
| 3339 |
3209 void test_useOfVoidResult_assignmentExpression_function() { | 3340 void test_useOfVoidResult_assignmentExpression_function() { |
3210 Source source = addSource(r''' | 3341 Source source = addSource(r''' |
3211 void f() {} | 3342 void f() {} |
3212 class A { | 3343 class A { |
3213 n() { | 3344 n() { |
3214 var a; | 3345 var a; |
3215 a = f(); | 3346 a = f(); |
3216 } | 3347 } |
3217 }'''); | 3348 }'''); |
3218 resolve(source); | 3349 resolve(source); |
(...skipping 2974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6193 } | 6324 } |
6194 } | 6325 } |
6195 } | 6326 } |
6196 | 6327 |
6197 class ResolverTestCase extends EngineTestCase { | 6328 class ResolverTestCase extends EngineTestCase { |
6198 /** | 6329 /** |
6199 * The analysis context used to parse the compilation units being resolved. | 6330 * The analysis context used to parse the compilation units being resolved. |
6200 */ | 6331 */ |
6201 AnalysisContextImpl analysisContext2; | 6332 AnalysisContextImpl analysisContext2; |
6202 | 6333 |
| 6334 /** |
| 6335 * Specifies if [assertErrors] should check for [HintCode.UNUSED_LOCAL_VARIABL
E]. |
| 6336 */ |
| 6337 bool enableUnusedLocalVariable = false; |
| 6338 |
6203 @override | 6339 @override |
6204 void setUp() { | 6340 void setUp() { |
6205 reset(); | 6341 reset(); |
6206 } | 6342 } |
6207 | 6343 |
6208 /** | 6344 /** |
6209 * Add a source file to the content provider. The file path should be absolute
. | 6345 * Add a source file to the content provider. The file path should be absolute
. |
6210 * | 6346 * |
6211 * @param filePath the path of the file being added | 6347 * @param filePath the path of the file being added |
6212 * @param contents the contents to be returned by the content provider for the
specified file | 6348 * @param contents the contents to be returned by the content provider for the
specified file |
(...skipping 22 matching lines...) Expand all Loading... |
6235 * | 6371 * |
6236 * @param source the source against which the errors should have been reported | 6372 * @param source the source against which the errors should have been reported |
6237 * @param expectedErrorCodes the error codes of the errors that should have be
en reported | 6373 * @param expectedErrorCodes the error codes of the errors that should have be
en reported |
6238 * @throws AnalysisException if the reported errors could not be computed | 6374 * @throws AnalysisException if the reported errors could not be computed |
6239 * @throws AssertionFailedError if a different number of errors have been repo
rted than were | 6375 * @throws AssertionFailedError if a different number of errors have been repo
rted than were |
6240 * expected | 6376 * expected |
6241 */ | 6377 */ |
6242 void assertErrors(Source source, List<ErrorCode> expectedErrorCodes) { | 6378 void assertErrors(Source source, List<ErrorCode> expectedErrorCodes) { |
6243 GatheringErrorListener errorListener = new GatheringErrorListener(); | 6379 GatheringErrorListener errorListener = new GatheringErrorListener(); |
6244 for (AnalysisError error in analysisContext2.computeErrors(source)) { | 6380 for (AnalysisError error in analysisContext2.computeErrors(source)) { |
| 6381 if (error.errorCode == HintCode.UNUSED_LOCAL_VARIABLE && |
| 6382 !enableUnusedLocalVariable) { |
| 6383 continue; |
| 6384 } |
6245 errorListener.onError(error); | 6385 errorListener.onError(error); |
6246 } | 6386 } |
6247 errorListener.assertErrorsWithCodes(expectedErrorCodes); | 6387 errorListener.assertErrorsWithCodes(expectedErrorCodes); |
6248 } | 6388 } |
6249 | 6389 |
6250 /** | 6390 /** |
6251 * Assert that no errors have been reported against the given source. | 6391 * Assert that no errors have been reported against the given source. |
6252 * | 6392 * |
6253 * @param source the source against which no errors should have been reported | 6393 * @param source the source against which no errors should have been reported |
6254 * @throws AnalysisException if the reported errors could not be computed | 6394 * @throws AnalysisException if the reported errors could not be computed |
(...skipping 5246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11501 runReflectiveTests(TypeResolverVisitorTest); | 11641 runReflectiveTests(TypeResolverVisitorTest); |
11502 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); | 11642 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); |
11503 runReflectiveTests(ErrorResolverTest); | 11643 runReflectiveTests(ErrorResolverTest); |
11504 runReflectiveTests(HintCodeTest); | 11644 runReflectiveTests(HintCodeTest); |
11505 runReflectiveTests(MemberMapTest); | 11645 runReflectiveTests(MemberMapTest); |
11506 runReflectiveTests(NonHintCodeTest); | 11646 runReflectiveTests(NonHintCodeTest); |
11507 runReflectiveTests(SimpleResolverTest); | 11647 runReflectiveTests(SimpleResolverTest); |
11508 runReflectiveTests(StrictModeTest); | 11648 runReflectiveTests(StrictModeTest); |
11509 runReflectiveTests(TypePropagationTest); | 11649 runReflectiveTests(TypePropagationTest); |
11510 } | 11650 } |
OLD | NEW |