Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: pkg/analyzer/test/generated/resolver_test.dart

Issue 1723243002: Validation of `@protected` method invocations. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: fixes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 analyzer.test.generated.resolver_test; 5 library analyzer.test.generated.resolver_test;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
(...skipping 15 matching lines...) Expand all
26 import 'package:analyzer/src/generated/resolver.dart'; 26 import 'package:analyzer/src/generated/resolver.dart';
27 import 'package:analyzer/src/generated/sdk.dart'; 27 import 'package:analyzer/src/generated/sdk.dart';
28 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; 28 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk;
29 import 'package:analyzer/src/generated/source_io.dart'; 29 import 'package:analyzer/src/generated/source_io.dart';
30 import 'package:analyzer/src/generated/static_type_analyzer.dart'; 30 import 'package:analyzer/src/generated/static_type_analyzer.dart';
31 import 'package:analyzer/src/generated/testing/ast_factory.dart'; 31 import 'package:analyzer/src/generated/testing/ast_factory.dart';
32 import 'package:analyzer/src/generated/testing/element_factory.dart'; 32 import 'package:analyzer/src/generated/testing/element_factory.dart';
33 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; 33 import 'package:analyzer/src/generated/testing/test_type_provider.dart';
34 import 'package:analyzer/src/generated/testing/token_factory.dart'; 34 import 'package:analyzer/src/generated/testing/token_factory.dart';
35 import 'package:analyzer/src/generated/utilities_dart.dart'; 35 import 'package:analyzer/src/generated/utilities_dart.dart';
36 import 'package:analyzer/src/string_source.dart';
36 import 'package:unittest/unittest.dart'; 37 import 'package:unittest/unittest.dart';
37 38
38 import '../reflective_tests.dart'; 39 import '../reflective_tests.dart';
39 import '../utils.dart'; 40 import '../utils.dart';
40 import 'test_support.dart'; 41 import 'test_support.dart';
41 42
42 main() { 43 main() {
43 initializeTestEnvironment(); 44 initializeTestEnvironment();
44 runReflectiveTests(AnalysisDeltaTest); 45 runReflectiveTests(AnalysisDeltaTest);
45 runReflectiveTests(ChangeSetTest); 46 runReflectiveTests(ChangeSetTest);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 * Create an analysis context that uses the given [options] and has a fake 94 * Create an analysis context that uses the given [options] and has a fake
94 * core library already resolved. Return the context that was created. 95 * core library already resolved. Return the context that was created.
95 */ 96 */
96 static InternalAnalysisContext contextWithCoreAndOptions( 97 static InternalAnalysisContext contextWithCoreAndOptions(
97 AnalysisOptions options) { 98 AnalysisOptions options) {
98 AnalysisContextForTests context = new AnalysisContextForTests(); 99 AnalysisContextForTests context = new AnalysisContextForTests();
99 context._internalSetAnalysisOptions(options); 100 context._internalSetAnalysisOptions(options);
100 return initContextWithCore(context); 101 return initContextWithCore(context);
101 } 102 }
102 103
104 static InternalAnalysisContext contextWithCoreAndPackages(
105 Map<String, String> packages) {
106 AnalysisContextForTests context = new AnalysisContextForTests();
107 return initContextWithCore(context, new TestPackageUriResolver(packages));
108 }
109
103 /** 110 /**
104 * Initialize the given analysis context with a fake core library already reso lved. 111 * Initialize the given analysis context with a fake core library already reso lved.
105 * 112 *
106 * @param context the context to be initialized (not `null`) 113 * @param context the context to be initialized (not `null`)
107 * @return the analysis context that was created 114 * @return the analysis context that was created
108 */ 115 */
109 static InternalAnalysisContext initContextWithCore( 116 static InternalAnalysisContext initContextWithCore(
110 InternalAnalysisContext context) { 117 InternalAnalysisContext context,
118 [UriResolver contributedResolver]) {
111 DirectoryBasedDartSdk sdk = new _AnalysisContextFactory_initContextWithCore( 119 DirectoryBasedDartSdk sdk = new _AnalysisContextFactory_initContextWithCore(
112 new JavaFile("/fake/sdk"), 120 new JavaFile("/fake/sdk"),
113 enableAsync: context.analysisOptions.enableAsync); 121 enableAsync: context.analysisOptions.enableAsync);
114 SourceFactory sourceFactory = 122 List<UriResolver> resolvers = <UriResolver>[
115 new SourceFactory([new DartUriResolver(sdk), new FileUriResolver()]); 123 new DartUriResolver(sdk),
124 new FileUriResolver()
125 ];
126 if (contributedResolver != null) {
127 resolvers.add(contributedResolver);
128 }
129 SourceFactory sourceFactory = new SourceFactory(resolvers);
116 context.sourceFactory = sourceFactory; 130 context.sourceFactory = sourceFactory;
117 AnalysisContext coreContext = sdk.context; 131 AnalysisContext coreContext = sdk.context;
118 (coreContext.analysisOptions as AnalysisOptionsImpl).strongMode = 132 (coreContext.analysisOptions as AnalysisOptionsImpl).strongMode =
119 context.analysisOptions.strongMode; 133 context.analysisOptions.strongMode;
120 // 134 //
121 // dart:core 135 // dart:core
122 // 136 //
123 TestTypeProvider provider = new TestTypeProvider(); 137 TestTypeProvider provider = new TestTypeProvider();
124 CompilationUnitElementImpl coreUnit = 138 CompilationUnitElementImpl coreUnit =
125 new CompilationUnitElementImpl("core.dart"); 139 new CompilationUnitElementImpl("core.dart");
(...skipping 2334 matching lines...) Expand 10 before | Expand all | Expand 10 after
2460 r''' 2474 r'''
2461 library lib2; 2475 library lib2;
2462 class B {}'''); 2476 class B {}''');
2463 computeLibrarySourceErrors(source); 2477 computeLibrarySourceErrors(source);
2464 assertErrors(source, [HintCode.UNUSED_IMPORT]); 2478 assertErrors(source, [HintCode.UNUSED_IMPORT]);
2465 assertNoErrors(source2); 2479 assertNoErrors(source2);
2466 assertNoErrors(source3); 2480 assertNoErrors(source3);
2467 verify([source, source2, source3]); 2481 verify([source, source2, source3]);
2468 } 2482 }
2469 2483
2484 @override
2485 void reset() {
2486 analysisContext2 = AnalysisContextFactory.contextWithCoreAndPackages({
2487 'package:meta/meta.dart': r'''
2488 library meta;
2489
2490 const _Protected protected = const _Protected();
2491
2492 class _Protected {
2493 const _Protected();
2494 }
2495 '''
2496 });
2497 }
2498
2470 void test_argumentTypeNotAssignable_functionType() { 2499 void test_argumentTypeNotAssignable_functionType() {
2471 Source source = addSource(r''' 2500 Source source = addSource(r'''
2472 m() { 2501 m() {
2473 var a = new A(); 2502 var a = new A();
2474 a.n(() => 0); 2503 a.n(() => 0);
2475 } 2504 }
2476 class A { 2505 class A {
2477 n(void f(int i)) {} 2506 n(void f(int i)) {}
2478 }'''); 2507 }''');
2479 computeLibrarySourceErrors(source); 2508 computeLibrarySourceErrors(source);
(...skipping 1062 matching lines...) Expand 10 before | Expand all | Expand 10 after
3542 } 3571 }
3543 class B extends A { 3572 class B extends A {
3544 @override 3573 @override
3545 set m(int x) {} 3574 set m(int x) {}
3546 }'''); 3575 }''');
3547 computeLibrarySourceErrors(source); 3576 computeLibrarySourceErrors(source);
3548 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_SETTER]); 3577 assertErrors(source, [HintCode.OVERRIDE_ON_NON_OVERRIDING_SETTER]);
3549 verify([source]); 3578 verify([source]);
3550 } 3579 }
3551 3580
3581 void test_protected_method_call_1() {
Brian Wilkerson 2016/02/23 23:42:58 nit: there's a naming convention here of test meth
pquitslund 2016/02/24 17:12:17 Done.
3582 Source source = addSource(r'''
3583 import 'package:meta/meta.dart';
3584 class A {
3585 @protected
3586 void a(){ }
3587 }
3588 main() {
3589 new A().a();
3590 }''');
3591 computeLibrarySourceErrors(source);
3592 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
3593 verify([source]);
3594 }
3595
3596 void test_protected_method_call_2() {
3597 Source source = addSource(r'''
3598 import 'package:meta/meta.dart';
3599 class A {
3600 @protected
3601 void a(){ }
3602 }
3603 class B {
3604 void b() => new A().a();
3605 }''');
3606 computeLibrarySourceErrors(source);
3607 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
3608 verify([source]);
3609 }
3610
3611 void test_protected_method_call_3() {
3612 Source source = addSource(r'''
3613 import 'package:meta/meta.dart';
3614 class A {
3615 @protected
3616 void a(){ }
3617 }
3618 abstract class B implements A {
3619 void b() => a();
3620 }''');
3621 computeLibrarySourceErrors(source);
3622 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
3623 verify([source]);
3624 }
3625
3626 void test_protected_method_call_OK_1() {
Brian Wilkerson 2016/02/23 23:42:58 nit: tests to ensure that a hint is not generated
3627 Source source = addSource(r'''
3628 import 'package:meta/meta.dart';
3629 class A {
3630 @protected
3631 void a(){ }
3632 }
3633 class B extends A {
3634 void b() => a();
3635 }''');
3636 computeLibrarySourceErrors(source);
3637 assertErrors(source, []);
3638 verify([source]);
3639 }
3640
3641 void test_protected_method_call_OK_2() {
3642 Source source = addSource(r'''
3643 import 'package:meta/meta.dart';
3644 class A {
3645 @protected
3646 void a(){ }
3647 }
3648 class B extends Object with A {
3649 void b() => a();
3650 }''');
3651 computeLibrarySourceErrors(source);
3652 assertErrors(source, []);
3653 verify([source]);
3654 }
3655
3552 void test_typeCheck_type_is_Null() { 3656 void test_typeCheck_type_is_Null() {
3553 Source source = addSource(r''' 3657 Source source = addSource(r'''
3554 m(i) { 3658 m(i) {
3555 bool b = i is Null; 3659 bool b = i is Null;
3556 }'''); 3660 }''');
3557 computeLibrarySourceErrors(source); 3661 computeLibrarySourceErrors(source);
3558 assertErrors(source, [HintCode.TYPE_CHECK_IS_NULL]); 3662 assertErrors(source, [HintCode.TYPE_CHECK_IS_NULL]);
3559 verify([source]); 3663 verify([source]);
3560 } 3664 }
3561 3665
(...skipping 10841 matching lines...) Expand 10 before | Expand all | Expand 10 after
14403 ClassElementImpl classB = ElementFactory.classElement("B", classA.type); 14507 ClassElementImpl classB = ElementFactory.classElement("B", classA.type);
14404 _definingCompilationUnit.types = <ClassElement>[classA, classB]; 14508 _definingCompilationUnit.types = <ClassElement>[classA, classB];
14405 HashSet<ClassElement> subtypesOfA = 14509 HashSet<ClassElement> subtypesOfA =
14406 _subtypeManager.computeAllSubtypes(classA); 14510 _subtypeManager.computeAllSubtypes(classA);
14407 List<ClassElement> arraySubtypesOfA = new List.from(subtypesOfA); 14511 List<ClassElement> arraySubtypesOfA = new List.from(subtypesOfA);
14408 expect(subtypesOfA, hasLength(1)); 14512 expect(subtypesOfA, hasLength(1));
14409 expect(arraySubtypesOfA, unorderedEquals([classB])); 14513 expect(arraySubtypesOfA, unorderedEquals([classB]));
14410 } 14514 }
14411 } 14515 }
14412 14516
14517 class TestPackageUriResolver extends UriResolver {
14518 Map<Uri, Source> sourceMap = new HashMap<Uri, Source>();
14519
14520 TestPackageUriResolver(Map<String, String> map) {
14521 map.forEach((String uri, String contents) {
14522 sourceMap[Uri.parse(uri)] =
14523 new StringSource(contents, '/test_pkg_source.dart');
14524 });
14525 }
14526
14527 @override
14528 Source resolveAbsolute(Uri uri, [Uri actualUri]) => sourceMap[uri];
14529
14530 @override
14531 Uri restoreAbsolute(Source source) => throw new UnimplementedError();
14532 }
14533
14413 @reflectiveTest 14534 @reflectiveTest
14414 class TypeOverrideManagerTest extends EngineTestCase { 14535 class TypeOverrideManagerTest extends EngineTestCase {
14415 void test_exitScope_noScopes() { 14536 void test_exitScope_noScopes() {
14416 TypeOverrideManager manager = new TypeOverrideManager(); 14537 TypeOverrideManager manager = new TypeOverrideManager();
14417 try { 14538 try {
14418 manager.exitScope(); 14539 manager.exitScope();
14419 fail("Expected IllegalStateException"); 14540 fail("Expected IllegalStateException");
14420 } on IllegalStateException { 14541 } on IllegalStateException {
14421 // Expected 14542 // Expected
14422 } 14543 }
(...skipping 2620 matching lines...) Expand 10 before | Expand all | Expand 10 after
17043 17164
17044 void _resolveTestUnit(String code) { 17165 void _resolveTestUnit(String code) {
17045 testCode = code; 17166 testCode = code;
17046 testSource = addSource(testCode); 17167 testSource = addSource(testCode);
17047 LibraryElement library = resolve2(testSource); 17168 LibraryElement library = resolve2(testSource);
17048 assertNoErrors(testSource); 17169 assertNoErrors(testSource);
17049 verify([testSource]); 17170 verify([testSource]);
17050 testUnit = resolveCompilationUnit(testSource, library); 17171 testUnit = resolveCompilationUnit(testSource, library);
17051 } 17172 }
17052 } 17173 }
OLDNEW
« pkg/analyzer/lib/src/generated/resolver.dart ('K') | « pkg/analyzer/lib/src/generated/resolver.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698