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 import 'package:analyzer/dart/ast/ast.dart'; | 5 import 'package:analyzer/dart/ast/ast.dart'; |
6 import 'package:analyzer/dart/ast/token.dart'; | 6 import 'package:analyzer/dart/ast/token.dart'; |
7 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
8 import 'package:analyzer/file_system/memory_file_system.dart'; | 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
9 import 'package:analyzer/src/dart/sdk/patch.dart'; | 9 import 'package:analyzer/src/dart/sdk/patch.dart'; |
10 import 'package:analyzer/src/dart/sdk/sdk.dart'; | 10 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
11 import 'package:analyzer/src/generated/engine.dart'; | 11 import 'package:analyzer/src/generated/engine.dart'; |
12 import 'package:analyzer/src/generated/sdk.dart'; | |
13 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
14 import 'package:analyzer/src/util/fast_uri.dart'; | 13 import 'package:analyzer/src/util/fast_uri.dart'; |
15 import 'package:test/test.dart'; | 14 import 'package:test/test.dart'; |
16 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 15 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
17 | 16 |
18 main() { | 17 main() { |
19 defineReflectiveSuite(() { | 18 defineReflectiveSuite(() { |
20 defineReflectiveTests(SdkPatcherTest); | 19 defineReflectiveTests(SdkPatcherTest); |
21 }); | 20 }); |
22 } | 21 } |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 part 'b.dart'; | 445 part 'b.dart'; |
447 int bar() => 0; | 446 int bar() => 0; |
448 ''', | 447 ''', |
449 r''' | 448 r''' |
450 import 'c.dart'; | 449 import 'c.dart'; |
451 '''); | 450 '''); |
452 _assertUnitCode(unit, | 451 _assertUnitCode(unit, |
453 "import 'a.dart'; part 'b.dart'; import 'c.dart'; int bar() => 0;"); | 452 "import 'a.dart'; part 'b.dart'; import 'c.dart'; int bar() => 0;"); |
454 } | 453 } |
455 | 454 |
456 test_fail_noSuchLibrary() { | |
457 expect(() { | |
458 _setSdkLibraries('const LIBRARIES = const {};'); | |
459 _createSdk(); | |
460 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), ''); | |
461 Source source = file.createSource(FastUri.parse('dart:test')); | |
462 CompilationUnit unit = SdkPatcher.parse(source, true, listener); | |
463 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); | |
464 }, throwsArgumentError); | |
465 } | |
466 | |
467 test_fail_patchFileDoesNotExist() { | 455 test_fail_patchFileDoesNotExist() { |
468 expect(() { | 456 expect(() { |
469 _setSdkLibraries(r''' | 457 _setSdkLibraries(r''' |
470 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { | 458 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { |
471 'test' : const LibraryInfo( | 459 'test' : const LibraryInfo( |
472 'test/test.dart', | 460 'test/test.dart'), |
473 patches: {VM_PLATFORM: ['does_not_exists.dart']}), | |
474 };'''); | 461 };'''); |
475 _createSdk(); | 462 _createSdk(); |
| 463 var patchPaths = { |
| 464 'dart:test': [_p('/sdk/lib/does_not_exist.dart')] |
| 465 }; |
476 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), ''); | 466 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), ''); |
477 Source source = file.createSource(FastUri.parse('dart:test')); | 467 Source source = file.createSource(FastUri.parse('dart:test')); |
478 CompilationUnit unit = SdkPatcher.parse(source, true, listener); | 468 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
479 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); | 469 patcher.patch(provider, true, patchPaths, listener, source, unit); |
480 }, throwsArgumentError); | 470 }, throwsArgumentError); |
481 } | 471 } |
482 | 472 |
483 test_internal_allowNewPublicNames() { | 473 test_internal_allowNewPublicNames() { |
484 _setSdkLibraries(r''' | 474 _setSdkLibraries(r''' |
485 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { | 475 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { |
486 '_internal' : const LibraryInfo( | 476 '_internal' : const LibraryInfo( |
487 'internal/internal.dart', | 477 'internal/internal.dart'), |
488 patches: {VM_PLATFORM: ['internal/internal_patch.dart']}), | |
489 };'''); | 478 };'''); |
| 479 var patchPaths = { |
| 480 'dart:_internal': [_p('/sdk/lib/internal/internal_patch.dart')] |
| 481 }; |
490 File file = provider.newFile( | 482 File file = provider.newFile( |
491 _p('/sdk/lib/internal/internal.dart'), | 483 _p('/sdk/lib/internal/internal.dart'), |
492 r''' | 484 r''' |
493 library dart._internal; | 485 library dart._internal; |
494 class A {} | 486 class A {} |
495 class B { | 487 class B { |
496 B(); | 488 B(); |
497 } | 489 } |
498 '''); | 490 '''); |
499 provider.newFile( | 491 provider.newFile( |
500 _p('/sdk/lib/internal/internal_patch.dart'), | 492 _p('/sdk/lib/internal/internal_patch.dart'), |
501 r''' | 493 r''' |
502 @patch | 494 @patch |
503 class B { | 495 class B { |
504 int newField; | 496 int newField; |
505 B.newConstructor(); | 497 B.newConstructor(); |
506 int newMethod() => 1; | 498 int newMethod() => 1; |
507 } | 499 } |
508 class NewClass {} | 500 class NewClass {} |
509 int newFunction() => 2; | 501 int newFunction() => 2; |
510 '''); | 502 '''); |
511 | 503 |
512 _createSdk(); | 504 _createSdk(); |
513 | 505 |
514 Source source = file.createSource(FastUri.parse('dart:_internal')); | 506 Source source = file.createSource(FastUri.parse('dart:_internal')); |
515 CompilationUnit unit = SdkPatcher.parse(source, true, listener); | 507 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
516 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); | 508 patcher.patch(provider, true, patchPaths, listener, source, unit); |
517 _assertUnitCode( | 509 _assertUnitCode( |
518 unit, | 510 unit, |
519 'library dart._internal; class A {} ' | 511 'library dart._internal; class A {} ' |
520 'class B {B(); int newField; B.newConstructor(); int newMethod() => 1;}
' | 512 'class B {B(); int newField; B.newConstructor(); int newMethod() => 1;}
' |
521 'class NewClass {} int newFunction() => 2;'); | 513 'class NewClass {} int newFunction() => 2;'); |
522 } | 514 } |
523 | 515 |
524 test_part() { | 516 test_part() { |
525 String baseLibCode = r''' | 517 String baseLibCode = r''' |
526 library test; | 518 library test; |
527 part 'test_part.dart'; | 519 part 'test_part.dart'; |
528 class A {} | 520 class A {} |
529 '''; | 521 '''; |
530 String basePartCode = r''' | 522 String basePartCode = r''' |
531 part of test; | 523 part of test; |
532 class B {} | 524 class B {} |
533 '''; | 525 '''; |
534 _setSdkLibraries(r''' | 526 _setSdkLibraries(r''' |
535 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { | 527 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { |
536 'test' : const LibraryInfo( | 528 'test' : const LibraryInfo( |
537 'test/test.dart', | 529 'test/test.dart', |
538 patches: {VM_PLATFORM: ['test/test_patch.dart']}), | 530 patches: {VM_PLATFORM: ['test/test_patch.dart']}), |
539 };'''); | 531 };'''); |
| 532 var patchPaths = { |
| 533 'dart:test': [_p('/sdk/lib/test/test_patch.dart')] |
| 534 }; |
540 File fileLib = provider.newFile(_p('/sdk/lib/test/test.dart'), baseLibCode); | 535 File fileLib = provider.newFile(_p('/sdk/lib/test/test.dart'), baseLibCode); |
541 File filePart = | 536 File filePart = |
542 provider.newFile(_p('/sdk/lib/test/test_part.dart'), basePartCode); | 537 provider.newFile(_p('/sdk/lib/test/test_part.dart'), basePartCode); |
543 provider.newFile( | 538 provider.newFile( |
544 _p('/sdk/lib/test/test_patch.dart'), | 539 _p('/sdk/lib/test/test_patch.dart'), |
545 r''' | 540 r''' |
546 import 'foo.dart'; | 541 import 'foo.dart'; |
547 | 542 |
548 @patch | 543 @patch |
549 class A { | 544 class A { |
550 int _a() => 1; | 545 int _a() => 1; |
551 } | 546 } |
552 | 547 |
553 @patch | 548 @patch |
554 class B { | 549 class B { |
555 int _b() => 1; | 550 int _b() => 1; |
556 } | 551 } |
557 | 552 |
558 class _C {} | 553 class _C {} |
559 '''); | 554 '''); |
560 | 555 |
561 _createSdk(); | 556 _createSdk(); |
562 | 557 |
563 { | 558 { |
564 Uri uri = FastUri.parse('dart:test'); | 559 Uri uri = FastUri.parse('dart:test'); |
565 Source source = fileLib.createSource(uri); | 560 Source source = fileLib.createSource(uri); |
566 CompilationUnit unit = SdkPatcher.parse(source, true, listener); | 561 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
567 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); | 562 patcher.patch(provider, true, patchPaths, listener, source, unit); |
568 _assertUnitCode( | 563 _assertUnitCode( |
569 unit, | 564 unit, |
570 "library test; part 'test_part.dart'; import 'foo.dart'; " | 565 "library test; part 'test_part.dart'; import 'foo.dart'; " |
571 "class A {int _a() => 1;} class _C {}"); | 566 "class A {int _a() => 1;} class _C {}"); |
572 } | 567 } |
573 | 568 |
574 { | 569 { |
575 Uri uri = FastUri.parse('dart:test/test_part.dart'); | 570 Uri uri = FastUri.parse('dart:test/test_part.dart'); |
576 Source source = filePart.createSource(uri); | 571 Source source = filePart.createSource(uri); |
577 CompilationUnit unit = SdkPatcher.parse(source, true, listener); | 572 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
578 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); | 573 patcher.patch(provider, true, patchPaths, listener, source, unit); |
579 _assertUnitCode(unit, "part of test; class B {int _b() => 1;}"); | 574 _assertUnitCode(unit, "part of test; class B {int _b() => 1;}"); |
580 } | 575 } |
581 } | 576 } |
582 | 577 |
583 test_topLevel_class_append() { | 578 test_topLevel_class_append() { |
584 CompilationUnit unit = _doTopLevelPatching( | 579 CompilationUnit unit = _doTopLevelPatching( |
585 r''' | 580 r''' |
586 class A {} | 581 class A {} |
587 ''', | 582 ''', |
588 r''' | 583 r''' |
(...skipping 25 matching lines...) Expand all Loading... |
614 _doTopLevelPatching( | 609 _doTopLevelPatching( |
615 r''' | 610 r''' |
616 class A {} | 611 class A {} |
617 ''', | 612 ''', |
618 r''' | 613 r''' |
619 class B {} | 614 class B {} |
620 '''); | 615 '''); |
621 }, throwsArgumentError); | 616 }, throwsArgumentError); |
622 } | 617 } |
623 | 618 |
624 test_topLevel_topLevelVariable_append() { | |
625 CompilationUnit unit = _doTopLevelPatching( | |
626 r''' | |
627 int foo() => 0; | |
628 ''', | |
629 r''' | |
630 int _bar; | |
631 '''); | |
632 _assertUnitCode(unit, 'int foo() => 0; int _bar;'); | |
633 FunctionDeclaration a = unit.declarations[0]; | |
634 TopLevelVariableDeclaration b = unit.declarations[1]; | |
635 _assertPrevNextToken(a.endToken, b.beginToken); | |
636 } | |
637 | |
638 test_topLevel_function_append() { | 619 test_topLevel_function_append() { |
639 CompilationUnit unit = _doTopLevelPatching( | 620 CompilationUnit unit = _doTopLevelPatching( |
640 r''' | 621 r''' |
641 int foo() => 0; | 622 int foo() => 0; |
642 ''', | 623 ''', |
643 r''' | 624 r''' |
644 int _bar1() => 1; | 625 int _bar1() => 1; |
645 int _bar2() => 2; | 626 int _bar2() => 2; |
646 '''); | 627 '''); |
647 _assertUnitCode( | 628 _assertUnitCode( |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 external void set foo(int val); | 772 external void set foo(int val); |
792 int bar() => 2; | 773 int bar() => 2; |
793 ''', | 774 ''', |
794 r''' | 775 r''' |
795 @patch | 776 @patch |
796 void set foo(int val) {} | 777 void set foo(int val) {} |
797 '''); | 778 '''); |
798 _assertUnitCode(unit, 'void set foo(int val) {} int bar() => 2;'); | 779 _assertUnitCode(unit, 'void set foo(int val) {} int bar() => 2;'); |
799 } | 780 } |
800 | 781 |
| 782 test_topLevel_topLevelVariable_append() { |
| 783 CompilationUnit unit = _doTopLevelPatching( |
| 784 r''' |
| 785 int foo() => 0; |
| 786 ''', |
| 787 r''' |
| 788 int _bar; |
| 789 '''); |
| 790 _assertUnitCode(unit, 'int foo() => 0; int _bar;'); |
| 791 FunctionDeclaration a = unit.declarations[0]; |
| 792 TopLevelVariableDeclaration b = unit.declarations[1]; |
| 793 _assertPrevNextToken(a.endToken, b.beginToken); |
| 794 } |
| 795 |
801 void _assertUnitCode(CompilationUnit unit, String expectedCode) { | 796 void _assertUnitCode(CompilationUnit unit, String expectedCode) { |
802 expect(unit.toSource(), expectedCode); | 797 expect(unit.toSource(), expectedCode); |
803 } | 798 } |
804 | 799 |
805 void _createSdk() { | 800 void _createSdk() { |
806 sdk = new FolderBasedDartSdk(provider, sdkFolder); | 801 sdk = new FolderBasedDartSdk(provider, sdkFolder); |
807 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; | 802 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; |
808 } | 803 } |
809 | 804 |
810 CompilationUnit _doTopLevelPatching(String baseCode, String patchCode) { | 805 CompilationUnit _doTopLevelPatching(String baseCode, String patchCode) { |
811 _setSdkLibraries(r''' | 806 _setSdkLibraries(r''' |
812 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { | 807 final Map<String, LibraryInfo> LIBRARIES = const <String, LibraryInfo> { |
813 'test' : const LibraryInfo( | 808 'test' : const LibraryInfo( |
814 'test/test.dart', | 809 'test/test.dart'), |
815 patches: {VM_PLATFORM: ['test/test_patch.dart']}), | |
816 };'''); | 810 };'''); |
| 811 var patchPaths = { |
| 812 'dart:test': [_p('/sdk/lib/test/test_patch.dart')] |
| 813 }; |
817 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), baseCode); | 814 File file = provider.newFile(_p('/sdk/lib/test/test.dart'), baseCode); |
818 provider.newFile(_p('/sdk/lib/test/test_patch.dart'), patchCode); | 815 provider.newFile(_p('/sdk/lib/test/test_patch.dart'), patchCode); |
819 | 816 |
820 _createSdk(); | 817 _createSdk(); |
821 | 818 |
822 Source source = file.createSource(FastUri.parse('dart:test')); | 819 Source source = file.createSource(FastUri.parse('dart:test')); |
823 CompilationUnit unit = SdkPatcher.parse(source, true, listener); | 820 CompilationUnit unit = SdkPatcher.parse(source, true, listener); |
824 patcher.patch(sdk, SdkLibraryImpl.VM_PLATFORM, listener, source, unit); | 821 patcher.patch(provider, true, patchPaths, listener, source, unit); |
825 return unit; | 822 return unit; |
826 } | 823 } |
827 | 824 |
828 String _p(String path) => provider.convertPath(path); | 825 String _p(String path) => provider.convertPath(path); |
829 | 826 |
830 void _setSdkLibraries(String code) { | 827 void _setSdkLibraries(String code) { |
831 provider.newFile( | 828 provider.newFile( |
832 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code); | 829 _p('/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart'), code); |
833 } | 830 } |
834 | 831 |
835 static void _assertPrevNextToken(Token prev, Token next) { | 832 static void _assertPrevNextToken(Token prev, Token next) { |
836 expect(prev.next, same(next)); | 833 expect(prev.next, same(next)); |
837 expect(next.previous, same(prev)); | 834 expect(next.previous, same(prev)); |
838 } | 835 } |
839 } | 836 } |
OLD | NEW |