| 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.resolver_test_case; | 5 library analyzer.test.generated.resolver_test_case; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 bool get enableNewAnalysisDriver => false; | 343 bool get enableNewAnalysisDriver => false; |
| 344 | 344 |
| 345 /** | 345 /** |
| 346 * Return a type provider that can be used to test the results of resolution. | 346 * Return a type provider that can be used to test the results of resolution. |
| 347 * | 347 * |
| 348 * @return a type provider | 348 * @return a type provider |
| 349 * @throws AnalysisException if dart:core cannot be resolved | 349 * @throws AnalysisException if dart:core cannot be resolved |
| 350 */ | 350 */ |
| 351 TypeProvider get typeProvider { | 351 TypeProvider get typeProvider { |
| 352 if (enableNewAnalysisDriver) { | 352 if (enableNewAnalysisDriver) { |
| 353 return driver.sourceFactory.dartSdk.context.typeProvider; | 353 return analysisResults.values.first.unit.element.context.typeProvider; |
| 354 } else { | 354 } else { |
| 355 return analysisContext2.typeProvider; | 355 return analysisContext2.typeProvider; |
| 356 } | 356 } |
| 357 } | 357 } |
| 358 | 358 |
| 359 /** | 359 /** |
| 360 * Return a type system that can be used to test the results of resolution. | 360 * Return a type system that can be used to test the results of resolution. |
| 361 * | 361 * |
| 362 * @return a type system | 362 * @return a type system |
| 363 */ | 363 */ |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 Source source = addSource(code); | 465 Source source = addSource(code); |
| 466 await computeAnalysisResult(source); | 466 await computeAnalysisResult(source); |
| 467 assertNoErrors(source); | 467 assertNoErrors(source); |
| 468 verify([source]); | 468 verify([source]); |
| 469 } | 469 } |
| 470 | 470 |
| 471 /** | 471 /** |
| 472 * @param code the code that assigns the value to the variable "v", no matter
how. We check that | 472 * @param code the code that assigns the value to the variable "v", no matter
how. We check that |
| 473 * "v" has expected static and propagated type. | 473 * "v" has expected static and propagated type. |
| 474 */ | 474 */ |
| 475 Future<Null> assertPropagatedAssignedType(String code, | 475 void assertPropagatedAssignedType(String code, CompilationUnit unit, |
| 476 DartType expectedStaticType, DartType expectedPropagatedType) async { | 476 DartType expectedStaticType, DartType expectedPropagatedType) { |
| 477 SimpleIdentifier identifier = await findMarkedIdentifier(code, "v = "); | 477 SimpleIdentifier identifier = findMarkedIdentifier(code, unit, "v = "); |
| 478 expect(identifier.staticType, expectedStaticType); | 478 expect(identifier.staticType, expectedStaticType); |
| 479 expect(identifier.propagatedType, expectedPropagatedType); | 479 expect(identifier.propagatedType, expectedPropagatedType); |
| 480 } | 480 } |
| 481 | 481 |
| 482 /** | 482 /** |
| 483 * @param code the code that iterates using variable "v". We check that | 483 * @param code the code that iterates using variable "v". We check that |
| 484 * "v" has expected static and propagated type. | 484 * "v" has expected static and propagated type. |
| 485 */ | 485 */ |
| 486 Future<Null> assertPropagatedIterationType(String code, | 486 void assertPropagatedIterationType(String code, CompilationUnit unit, |
| 487 DartType expectedStaticType, DartType expectedPropagatedType) async { | 487 DartType expectedStaticType, DartType expectedPropagatedType) { |
| 488 SimpleIdentifier identifier = await findMarkedIdentifier(code, "v in "); | 488 SimpleIdentifier identifier = findMarkedIdentifier(code, unit, "v in "); |
| 489 expect(identifier.staticType, expectedStaticType); | 489 expect(identifier.staticType, expectedStaticType); |
| 490 expect(identifier.propagatedType, expectedPropagatedType); | 490 expect(identifier.propagatedType, expectedPropagatedType); |
| 491 } | 491 } |
| 492 | 492 |
| 493 /** | 493 /** |
| 494 * Check the static and propagated types of the expression marked with "; // m
arker" comment. | 494 * Check the static and propagated types of the expression marked with "; // m
arker" comment. |
| 495 * | 495 * |
| 496 * @param code source code to analyze, with the expression to check marked wit
h "// marker". | 496 * @param code source code to analyze, with the expression to check marked wit
h "// marker". |
| 497 * @param expectedStaticType if non-null, check actual static type is equal to
this. | 497 * @param expectedStaticType if non-null, check actual static type is equal to
this. |
| 498 * @param expectedPropagatedType if non-null, check actual static type is equa
l to this. | 498 * @param expectedPropagatedType if non-null, check actual static type is equa
l to this. |
| 499 * @throws Exception | 499 * @throws Exception |
| 500 */ | 500 */ |
| 501 Future<Null> assertTypeOfMarkedExpression(String code, | 501 void assertTypeOfMarkedExpression(String code, CompilationUnit unit, |
| 502 DartType expectedStaticType, DartType expectedPropagatedType) async { | 502 DartType expectedStaticType, DartType expectedPropagatedType) { |
| 503 SimpleIdentifier identifier = | 503 SimpleIdentifier identifier = |
| 504 await findMarkedIdentifier(code, "; // marker"); | 504 findMarkedIdentifier(code, unit, "; // marker"); |
| 505 if (expectedStaticType != null) { | 505 if (expectedStaticType != null) { |
| 506 expect(identifier.staticType, expectedStaticType); | 506 expect(identifier.staticType, expectedStaticType); |
| 507 } | 507 } |
| 508 expect(identifier.propagatedType, expectedPropagatedType); | 508 expect(identifier.propagatedType, expectedPropagatedType); |
| 509 } | 509 } |
| 510 | 510 |
| 511 /** | 511 /** |
| 512 * Change the contents of the given [source] to the given [contents]. | 512 * Change the contents of the given [source] to the given [contents]. |
| 513 */ | 513 */ |
| 514 void changeSource(Source source, String contents) { | 514 void changeSource(Source source, String contents) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 532 analysisContext.resolveCompilationUnit2(source, libraries.first); | 532 analysisContext.resolveCompilationUnit2(source, libraries.first); |
| 533 List<AnalysisError> errors = analysisContext.computeErrors(source); | 533 List<AnalysisError> errors = analysisContext.computeErrors(source); |
| 534 analysisResult = new TestAnalysisResult(source, unit, errors); | 534 analysisResult = new TestAnalysisResult(source, unit, errors); |
| 535 } | 535 } |
| 536 } | 536 } |
| 537 analysisResults[source] = analysisResult; | 537 analysisResults[source] = analysisResult; |
| 538 return analysisResult; | 538 return analysisResult; |
| 539 } | 539 } |
| 540 | 540 |
| 541 /** | 541 /** |
| 542 * Compute the analysis result to the given [code] in '/test.dart'. |
| 543 */ |
| 544 Future<TestAnalysisResult> computeTestAnalysisResult(String code) async { |
| 545 Source source = addSource(code); |
| 546 return await computeAnalysisResult(source); |
| 547 } |
| 548 |
| 549 /** |
| 542 * Create a library element that represents a library named `"test"` containin
g a single | 550 * Create a library element that represents a library named `"test"` containin
g a single |
| 543 * empty compilation unit. | 551 * empty compilation unit. |
| 544 * | 552 * |
| 545 * @return the library element that was created | 553 * @return the library element that was created |
| 546 */ | 554 */ |
| 547 LibraryElementImpl createDefaultTestLibrary() => | 555 LibraryElementImpl createDefaultTestLibrary() => |
| 548 createTestLibrary(AnalysisContextFactory.contextWithCore(), "test"); | 556 createTestLibrary(AnalysisContextFactory.contextWithCore(), "test"); |
| 549 | 557 |
| 550 /** | 558 /** |
| 551 * Create a source object representing a file with the given [fileName] and | 559 * Create a source object representing a file with the given [fileName] and |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 compilationUnit.librarySource = | 601 compilationUnit.librarySource = |
| 594 compilationUnit.source = definingCompilationUnitSource; | 602 compilationUnit.source = definingCompilationUnitSource; |
| 595 LibraryElementImpl library = new LibraryElementImpl.forNode( | 603 LibraryElementImpl library = new LibraryElementImpl.forNode( |
| 596 context, AstTestFactory.libraryIdentifier2([libraryName])); | 604 context, AstTestFactory.libraryIdentifier2([libraryName])); |
| 597 library.definingCompilationUnit = compilationUnit; | 605 library.definingCompilationUnit = compilationUnit; |
| 598 library.parts = sourcedCompilationUnits; | 606 library.parts = sourcedCompilationUnits; |
| 599 return library; | 607 return library; |
| 600 } | 608 } |
| 601 | 609 |
| 602 /** | 610 /** |
| 603 * Return the `SimpleIdentifier` marked by `marker`. The source code must have
no | 611 * Return the [SimpleIdentifier] from [unit] marked by [marker] in [code]. |
| 604 * errors and be verifiable. | 612 * The source code must have no errors and be verifiable. |
| 605 * | |
| 606 * @param code source code to analyze. | |
| 607 * @param marker marker identifying sought after expression in source code. | |
| 608 * @return expression marked by the marker. | |
| 609 * @throws Exception | |
| 610 */ | 613 */ |
| 611 Future<SimpleIdentifier> findMarkedIdentifier( | 614 SimpleIdentifier findMarkedIdentifier( |
| 612 String code, String marker) async { | 615 String code, CompilationUnit unit, String marker) { |
| 613 try { | 616 return EngineTestCase.findNode( |
| 614 Source source = addSource(code); | 617 unit, code, marker, (node) => node is SimpleIdentifier); |
| 615 await computeAnalysisResult(source); | |
| 616 assertNoErrors(source); | |
| 617 verify([source]); | |
| 618 CompilationUnit unit = analysisResults[source].unit; | |
| 619 return EngineTestCase.findNode( | |
| 620 unit, code, marker, (node) => node is SimpleIdentifier); | |
| 621 } catch (exception) { | |
| 622 // Is there a better exception to throw here? The point is that an | |
| 623 // assertion failure here should be a failure, in both "test_*" and | |
| 624 // "fail_*" tests. However, an assertion failure is success for the | |
| 625 // purpose of "fail_*" tests, so without catching them here "fail_*" tests | |
| 626 // can succeed by failing for the wrong reason. | |
| 627 throw new StateError("Unexpected assertion failure: $exception"); | |
| 628 } | |
| 629 } | 618 } |
| 630 | 619 |
| 631 Expression findTopLevelConstantExpression( | 620 Expression findTopLevelConstantExpression( |
| 632 CompilationUnit compilationUnit, String name) => | 621 CompilationUnit compilationUnit, String name) => |
| 633 findTopLevelDeclaration(compilationUnit, name).initializer; | 622 findTopLevelDeclaration(compilationUnit, name).initializer; |
| 634 VariableDeclaration findTopLevelDeclaration( | 623 VariableDeclaration findTopLevelDeclaration( |
| 635 CompilationUnit compilationUnit, String name) { | 624 CompilationUnit compilationUnit, String name) { |
| 636 for (CompilationUnitMember member in compilationUnit.declarations) { | 625 for (CompilationUnitMember member in compilationUnit.declarations) { |
| 637 if (member is TopLevelVariableDeclaration) { | 626 if (member is TopLevelVariableDeclaration) { |
| 638 for (VariableDeclaration variable in member.variables.variables) { | 627 for (VariableDeclaration variable in member.variables.variables) { |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 936 } | 925 } |
| 937 } | 926 } |
| 938 } | 927 } |
| 939 | 928 |
| 940 class TestAnalysisResult { | 929 class TestAnalysisResult { |
| 941 final Source source; | 930 final Source source; |
| 942 final CompilationUnit unit; | 931 final CompilationUnit unit; |
| 943 final List<AnalysisError> errors; | 932 final List<AnalysisError> errors; |
| 944 TestAnalysisResult(this.source, this.unit, this.errors); | 933 TestAnalysisResult(this.source, this.unit, this.errors); |
| 945 } | 934 } |
| OLD | NEW |