| 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:analyzer/file_system/physical_file_system.dart'; | 7 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 8 import 'package:analyzer/src/dart/sdk/sdk.dart'; | 8 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
| 9 import 'package:front_end/compiler_options.dart'; | 9 import 'package:front_end/compiler_options.dart'; |
| 10 import 'package:front_end/incremental_kernel_generator.dart'; | 10 import 'package:front_end/incremental_kernel_generator.dart'; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 List<int> _readSdkSummary() { | 25 List<int> _readSdkSummary() { |
| 26 var resourceProvider = PhysicalResourceProvider.INSTANCE; | 26 var resourceProvider = PhysicalResourceProvider.INSTANCE; |
| 27 var sdk = new FolderBasedDartSdk(resourceProvider, | 27 var sdk = new FolderBasedDartSdk(resourceProvider, |
| 28 FolderBasedDartSdk.defaultSdkDirectory(resourceProvider)) | 28 FolderBasedDartSdk.defaultSdkDirectory(resourceProvider)) |
| 29 ..useSummary = true; | 29 ..useSummary = true; |
| 30 var path = resourceProvider.pathContext | 30 var path = resourceProvider.pathContext |
| 31 .join(sdk.directory.path, 'lib', '_internal', 'strong.sum'); | 31 .join(sdk.directory.path, 'lib', '_internal', 'strong.sum'); |
| 32 return resourceProvider.getFile(path).readAsBytesSync(); | 32 return resourceProvider.getFile(path).readAsBytesSync(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 typedef void LibraryChecker(Library lib); |
| 36 |
| 35 @reflectiveTest | 37 @reflectiveTest |
| 36 class IncrementalKernelGeneratorTest { | 38 class IncrementalKernelGeneratorTest { |
| 37 static final sdkSummaryUri = Uri.parse('special:sdk_summary'); | 39 static final sdkSummaryUri = Uri.parse('special:sdk_summary'); |
| 38 | 40 |
| 39 /// Virtual filesystem for testing. | 41 /// Virtual filesystem for testing. |
| 40 final fileSystem = new MemoryFileSystem(pathos.posix, Uri.parse('file:///')); | 42 final fileSystem = new MemoryFileSystem(pathos.posix, Uri.parse('file:///')); |
| 41 | 43 |
| 42 /// The object under test. | 44 /// The object under test. |
| 43 IncrementalKernelGenerator incrementalKernelGenerator; | 45 IncrementalKernelGenerator incrementalKernelGenerator; |
| 44 | 46 |
| 47 void checkLibraries( |
| 48 List<Library> libraries, Map<Uri, LibraryChecker> expected) { |
| 49 expect( |
| 50 libraries.map((lib) => lib.importUri), unorderedEquals(expected.keys)); |
| 51 var librariesMap = <Uri, Library>{}; |
| 52 for (var lib in libraries) { |
| 53 librariesMap[lib.importUri] = lib; |
| 54 } |
| 55 expected.forEach((uri, checker) => checker(librariesMap[uri])); |
| 56 } |
| 57 |
| 45 Future<Map<Uri, Program>> getInitialState(Uri startingUri) async { | 58 Future<Map<Uri, Program>> getInitialState(Uri startingUri) async { |
| 46 fileSystem.entityForUri(sdkSummaryUri).writeAsBytesSync(_sdkSummary); | 59 fileSystem.entityForUri(sdkSummaryUri).writeAsBytesSync(_sdkSummary); |
| 47 incrementalKernelGenerator = new IncrementalKernelGenerator( | 60 incrementalKernelGenerator = new IncrementalKernelGenerator( |
| 48 startingUri, | 61 startingUri, |
| 49 new CompilerOptions() | 62 new CompilerOptions() |
| 50 ..fileSystem = fileSystem | 63 ..fileSystem = fileSystem |
| 51 ..chaseDependencies = true | 64 ..chaseDependencies = true |
| 52 ..sdkSummary = sdkSummaryUri | 65 ..sdkSummary = sdkSummaryUri |
| 53 ..packagesFileUri = new Uri()); | 66 ..packagesFileUri = new Uri()); |
| 54 return (await incrementalKernelGenerator.computeDelta()).newState; | 67 return (await incrementalKernelGenerator.computeDelta()).newState; |
| 55 } | 68 } |
| 56 | 69 |
| 57 test_emptyProgram() async { | 70 test_incrementalUpdate_referenceToCore() async { |
| 58 writeFiles({'/foo.dart': 'main() {}'}); | 71 // TODO(paulberry): test parts. |
| 72 writeFiles({'/foo.dart': 'main() { print(1); }'}); |
| 59 var fileUri = Uri.parse('file:///foo.dart'); | 73 var fileUri = Uri.parse('file:///foo.dart'); |
| 60 var initialState = await getInitialState(fileUri); | 74 var initialState = await getInitialState(fileUri); |
| 61 expect(initialState.keys, unorderedEquals([fileUri])); | 75 expect(initialState.keys, unorderedEquals([fileUri])); |
| 62 var program = initialState[fileUri]; | 76 void _checkMain(List<Library> libraries, int expectedArgument) { |
| 63 expect(program.libraries, hasLength(1)); | 77 checkLibraries(libraries, { |
| 64 var library = program.libraries[0]; | 78 fileUri: (library) { |
| 65 expect(library.importUri, fileUri); | 79 expect(library.importUri, fileUri); |
| 66 expect(library.classes, isEmpty); | 80 expect(library.classes, isEmpty); |
| 67 expect(library.procedures, hasLength(1)); | 81 expect(library.procedures, hasLength(1)); |
| 68 expect(library.procedures[0].name.name, 'main'); | 82 expect(library.procedures[0].name.name, 'main'); |
| 69 var body = library.procedures[0].function.body; | 83 var body = library.procedures[0].function.body; |
| 70 expect(body, new isInstanceOf<Block>()); | 84 expect(body, new isInstanceOf<Block>()); |
| 71 var block = body as Block; | 85 var block = body as Block; |
| 72 expect(block.statements, isEmpty); | 86 expect(block.statements, hasLength(1)); |
| 87 expect(block.statements[0], new isInstanceOf<ExpressionStatement>()); |
| 88 var expressionStatement = block.statements[0] as ExpressionStatement; |
| 89 expect(expressionStatement.expression, |
| 90 new isInstanceOf<StaticInvocation>()); |
| 91 var staticInvocation = |
| 92 expressionStatement.expression as StaticInvocation; |
| 93 expect(staticInvocation.target.name.name, 'print'); |
| 94 expect(staticInvocation.arguments.positional, hasLength(1)); |
| 95 expect(staticInvocation.arguments.positional[0], |
| 96 new isInstanceOf<IntLiteral>()); |
| 97 var intLiteral = |
| 98 staticInvocation.arguments.positional[0] as IntLiteral; |
| 99 expect(intLiteral.value, expectedArgument); |
| 100 }, |
| 101 Uri.parse('dart:core'): (library) { |
| 102 // Should contain the procedure "print" but not its definition. |
| 103 expect(library.procedures, hasLength(1)); |
| 104 expect(library.procedures[0].name.name, 'print'); |
| 105 expect(library.procedures[0].function.body, isNull); |
| 106 } |
| 107 }); |
| 108 } |
| 109 |
| 110 _checkMain(initialState[fileUri].libraries, 1); |
| 111 writeFiles({'/foo.dart': 'main() { print(2); }'}); |
| 112 incrementalKernelGenerator.invalidateAll(); |
| 113 var deltaProgram = await incrementalKernelGenerator.computeDelta(); |
| 114 expect(deltaProgram.newState.keys, unorderedEquals([fileUri])); |
| 115 _checkMain(deltaProgram.newState[fileUri].libraries, 2); |
| 73 } | 116 } |
| 74 | 117 |
| 75 /// Write the given file contents to the virtual filesystem. | 118 /// Write the given file contents to the virtual filesystem. |
| 76 void writeFiles(Map<String, String> contents) { | 119 void writeFiles(Map<String, String> contents) { |
| 77 contents.forEach((path, text) { | 120 contents.forEach((path, text) { |
| 78 fileSystem | 121 fileSystem |
| 79 .entityForUri(Uri.parse('file://$path')) | 122 .entityForUri(Uri.parse('file://$path')) |
| 80 .writeAsStringSync(text); | 123 .writeAsStringSync(text); |
| 81 }); | 124 }); |
| 82 } | 125 } |
| 83 } | 126 } |
| OLD | NEW |