| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:front_end/compiler_options.dart'; | 7 import 'package:front_end/compiler_options.dart'; |
| 8 import 'package:front_end/incremental_kernel_generator.dart'; | 8 import 'package:front_end/incremental_kernel_generator.dart'; |
| 9 import 'package:front_end/memory_file_system.dart'; | 9 import 'package:front_end/memory_file_system.dart'; |
| 10 import 'package:front_end/src/incremental/byte_store.dart'; | 10 import 'package:front_end/src/incremental/byte_store.dart'; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 defineReflectiveSuite(() { | 23 defineReflectiveSuite(() { |
| 24 defineReflectiveTests(IncrementalKernelGeneratorTest); | 24 defineReflectiveTests(IncrementalKernelGeneratorTest); |
| 25 }); | 25 }); |
| 26 } | 26 } |
| 27 | 27 |
| 28 @reflectiveTest | 28 @reflectiveTest |
| 29 class IncrementalKernelGeneratorTest { | 29 class IncrementalKernelGeneratorTest { |
| 30 /// Virtual filesystem for testing. | 30 /// Virtual filesystem for testing. |
| 31 final fileSystem = new MemoryFileSystem(Uri.parse('file:///')); | 31 final fileSystem = new MemoryFileSystem(Uri.parse('file:///')); |
| 32 | 32 |
| 33 /// The used file watcher. |
| 34 WatchUsedFilesFn watchFn = (uri, used) {}; |
| 35 |
| 33 /// The object under test. | 36 /// The object under test. |
| 34 IncrementalKernelGenerator incrementalKernelGenerator; | 37 IncrementalKernelGenerator incrementalKernelGenerator; |
| 35 | 38 |
| 36 /// Compute the initial [Program] for the given [entryPoint]. | 39 /// Compute the initial [Program] for the given [entryPoint]. |
| 37 Future<Program> getInitialState(Uri entryPoint) async { | 40 Future<Program> getInitialState(Uri entryPoint) async { |
| 38 Map<String, Uri> dartLibraries = createSdkFiles(fileSystem); | 41 Map<String, Uri> dartLibraries = createSdkFiles(fileSystem); |
| 39 // TODO(scheglov) Builder the SDK kernel and set it into the options. | 42 // TODO(scheglov) Builder the SDK kernel and set it into the options. |
| 40 | 43 |
| 41 // TODO(scheglov) Make `.packages` file optional. | 44 // TODO(scheglov) Make `.packages` file optional. |
| 42 | 45 |
| 43 var compilerOptions = new CompilerOptions() | 46 var compilerOptions = new CompilerOptions() |
| 44 ..fileSystem = fileSystem | 47 ..fileSystem = fileSystem |
| 45 ..byteStore = new MemoryByteStore() | 48 ..byteStore = new MemoryByteStore() |
| 46 // ..logger = new PerformanceLog(stdout) | 49 // ..logger = new PerformanceLog(stdout) |
| 47 ..strongMode = true | 50 ..strongMode = true |
| 48 ..chaseDependencies = true | 51 ..chaseDependencies = true |
| 49 ..dartLibraries = dartLibraries | 52 ..dartLibraries = dartLibraries |
| 50 ..packagesFileUri = Uri.parse('file:///test/.packages'); | 53 ..packagesFileUri = Uri.parse('file:///test/.packages'); |
| 51 incrementalKernelGenerator = await IncrementalKernelGenerator.newInstance( | 54 incrementalKernelGenerator = await IncrementalKernelGenerator |
| 52 compilerOptions, entryPoint); | 55 .newInstance(compilerOptions, entryPoint, watch: watchFn); |
| 53 return (await incrementalKernelGenerator.computeDelta()).newProgram; | 56 return (await incrementalKernelGenerator.computeDelta()).newProgram; |
| 54 } | 57 } |
| 55 | 58 |
| 56 test_compile_chain() async { | 59 test_compile_chain() async { |
| 57 writeFile('/test/.packages', 'test:lib/'); | 60 writeFile('/test/.packages', 'test:lib/'); |
| 58 String aPath = '/test/lib/a.dart'; | 61 String aPath = '/test/lib/a.dart'; |
| 59 String bPath = '/test/lib/b.dart'; | 62 String bPath = '/test/lib/b.dart'; |
| 60 String cPath = '/test/lib/c.dart'; | 63 String cPath = '/test/lib/c.dart'; |
| 61 Uri aUri = writeFile(aPath, 'var a = 1;'); | 64 Uri aUri = writeFile(aPath, 'var a = 1;'); |
| 62 Uri bUri = writeFile( | 65 Uri bUri = writeFile( |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 _getLibraryText(library), | 325 _getLibraryText(library), |
| 323 r''' | 326 r''' |
| 324 library; | 327 library; |
| 325 import self as self; | 328 import self as self; |
| 326 import "dart:core" as core; | 329 import "dart:core" as core; |
| 327 | 330 |
| 328 static field (core::String) → core::int f; | 331 static field (core::String) → core::int f; |
| 329 '''); | 332 '''); |
| 330 } | 333 } |
| 331 | 334 |
| 335 test_invalidateAll() async { |
| 336 writeFile('/test/.packages', ''); |
| 337 Uri aUri = writeFile('/test/a.dart', "import 'b.dart';\nint a = b;"); |
| 338 Uri bUri = writeFile('/test/b.dart', 'var b = 1;'); |
| 339 |
| 340 Program program = await getInitialState(aUri); |
| 341 expect(_getLibraryText(_getLibrary(program, aUri)), contains("int a =")); |
| 342 expect(_getLibraryText(_getLibrary(program, bUri)), contains("b = 1")); |
| 343 |
| 344 writeFile('/test/a.dart', "import 'b.dart';\ndouble a = b;"); |
| 345 writeFile('/test/b.dart', 'var b = 2;'); |
| 346 incrementalKernelGenerator.invalidateAll(); |
| 347 |
| 348 DeltaProgram delta = await incrementalKernelGenerator.computeDelta(); |
| 349 program = delta.newProgram; |
| 350 _assertLibraryUris(program, includes: [aUri, bUri]); |
| 351 expect(_getLibraryText(_getLibrary(program, aUri)), contains("double a =")); |
| 352 expect(_getLibraryText(_getLibrary(program, bUri)), contains("b = 2")); |
| 353 } |
| 354 |
| 332 test_limited_ast_to_binary() async { | 355 test_limited_ast_to_binary() async { |
| 333 writeFile('/test/.packages', 'test:lib/'); | 356 writeFile('/test/.packages', 'test:lib/'); |
| 334 String aPath = '/test/lib/a.dart'; | 357 String aPath = '/test/lib/a.dart'; |
| 335 String bPath = '/test/lib/b.dart'; | 358 String bPath = '/test/lib/b.dart'; |
| 336 writeFile( | 359 writeFile( |
| 337 aPath, | 360 aPath, |
| 338 r''' | 361 r''' |
| 339 int topField = 0; | 362 int topField = 0; |
| 340 int get topGetter => 0; | 363 int get topGetter => 0; |
| 341 int topFunction({p}) => 0; | 364 int topFunction({p}) => 0; |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 | 679 |
| 657 static field core::String a = "aaa"; | 680 static field core::String a = "aaa"; |
| 658 static field core::double c = self::b; | 681 static field core::double c = self::b; |
| 659 static field core::double b = 2.3 /* from file:///test/lib/bar.dart */; | 682 static field core::double b = 2.3 /* from file:///test/lib/bar.dart */; |
| 660 static field core::String d = self::a /* from file:///test/lib/bar.dart */; | 683 static field core::String d = self::a /* from file:///test/lib/bar.dart */; |
| 661 static method main() → void {} | 684 static method main() → void {} |
| 662 '''); | 685 '''); |
| 663 } | 686 } |
| 664 } | 687 } |
| 665 | 688 |
| 666 test_invalidateAll() async { | 689 test_watch() async { |
| 667 writeFile('/test/.packages', ''); | 690 writeFile('/test/.packages', 'test:lib/'); |
| 668 Uri aUri = writeFile('/test/a.dart', "import 'b.dart';\nint a = b;"); | 691 String aPath = '/test/lib/a.dart'; |
| 669 Uri bUri = writeFile('/test/b.dart', 'var b = 1;'); | 692 String bPath = '/test/lib/b.dart'; |
| 693 String cPath = '/test/lib/c.dart'; |
| 694 Uri aUri = writeFile(aPath, ''); |
| 695 Uri bUri = writeFile(bPath, ''); |
| 696 Uri cUri = writeFile( |
| 697 cPath, |
| 698 r''' |
| 699 import 'a.dart'; |
| 700 '''); |
| 670 | 701 |
| 671 Program program = await getInitialState(aUri); | 702 var usedFiles = <Uri>[]; |
| 672 expect(_getLibraryText(_getLibrary(program, aUri)), contains("int a =")); | 703 watchFn = (Uri uri, bool used) { |
| 673 expect(_getLibraryText(_getLibrary(program, bUri)), contains("b = 1")); | 704 expect(used, isTrue); |
| 705 usedFiles.add(uri); |
| 706 return new Future.value(); |
| 707 }; |
| 674 | 708 |
| 675 writeFile('/test/a.dart', "import 'b.dart';\ndouble a = b;"); | 709 { |
| 676 writeFile('/test/b.dart', 'var b = 2;'); | 710 await getInitialState(cUri); |
| 677 incrementalKernelGenerator.invalidateAll(); | 711 // We use at least c.dart and a.dart now. |
| 712 expect(usedFiles, contains(cUri)); |
| 713 expect(usedFiles, contains(aUri)); |
| 714 usedFiles.clear(); |
| 715 } |
| 678 | 716 |
| 679 DeltaProgram delta = await incrementalKernelGenerator.computeDelta(); | 717 // Update c.dart to reference also b.dart file. |
| 680 program = delta.newProgram; | 718 writeFile( |
| 681 _assertLibraryUris(program, includes: [aUri, bUri]); | 719 cPath, |
| 682 expect(_getLibraryText(_getLibrary(program, aUri)), contains("double a =")); | 720 r''' |
| 683 expect(_getLibraryText(_getLibrary(program, bUri)), contains("b = 2")); | 721 import 'a.dart'; |
| 722 import 'b.dart'; |
| 723 '''); |
| 724 incrementalKernelGenerator.invalidate(cUri); |
| 725 { |
| 726 await incrementalKernelGenerator.computeDelta(); |
| 727 // The only new file is b.dart now. |
| 728 expect(usedFiles, [bUri]); |
| 729 usedFiles.clear(); |
| 730 } |
| 684 } | 731 } |
| 685 | 732 |
| 686 /// Write the given [text] of the file with the given [path] into the | 733 /// Write the given [text] of the file with the given [path] into the |
| 687 /// virtual filesystem. Return the URI of the file. | 734 /// virtual filesystem. Return the URI of the file. |
| 688 Uri writeFile(String path, String text) { | 735 Uri writeFile(String path, String text) { |
| 689 Uri uri = Uri.parse('file://$path'); | 736 Uri uri = Uri.parse('file://$path'); |
| 690 fileSystem.entityForUri(uri).writeAsStringSync(text); | 737 fileSystem.entityForUri(uri).writeAsStringSync(text); |
| 691 return uri; | 738 return uri; |
| 692 } | 739 } |
| 693 | 740 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 715 throw fail('No library found with URI "$uri"'); | 762 throw fail('No library found with URI "$uri"'); |
| 716 } | 763 } |
| 717 | 764 |
| 718 String _getLibraryText(Library library) { | 765 String _getLibraryText(Library library) { |
| 719 StringBuffer buffer = new StringBuffer(); | 766 StringBuffer buffer = new StringBuffer(); |
| 720 new Printer(buffer, syntheticNames: new NameSystem()) | 767 new Printer(buffer, syntheticNames: new NameSystem()) |
| 721 .writeLibraryFile(library); | 768 .writeLibraryFile(library); |
| 722 return buffer.toString(); | 769 return buffer.toString(); |
| 723 } | 770 } |
| 724 } | 771 } |
| OLD | NEW |