| 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 | |
| 37 @reflectiveTest | 35 @reflectiveTest |
| 38 class IncrementalKernelGeneratorTest { | 36 class IncrementalKernelGeneratorTest { |
| 39 static final sdkSummaryUri = Uri.parse('special:sdk_summary'); | 37 static final sdkSummaryUri = Uri.parse('special:sdk_summary'); |
| 40 | 38 |
| 41 /// Virtual filesystem for testing. | 39 /// Virtual filesystem for testing. |
| 42 final fileSystem = new MemoryFileSystem(pathos.posix, Uri.parse('file:///')); | 40 final fileSystem = new MemoryFileSystem(pathos.posix, Uri.parse('file:///')); |
| 43 | 41 |
| 44 /// The object under test. | 42 /// The object under test. |
| 45 IncrementalKernelGenerator incrementalKernelGenerator; | 43 IncrementalKernelGenerator incrementalKernelGenerator; |
| 46 | 44 |
| 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 | |
| 58 Future<Map<Uri, Program>> getInitialState(Uri startingUri) async { | 45 Future<Map<Uri, Program>> getInitialState(Uri startingUri) async { |
| 59 fileSystem.entityForUri(sdkSummaryUri).writeAsBytesSync(_sdkSummary); | 46 fileSystem.entityForUri(sdkSummaryUri).writeAsBytesSync(_sdkSummary); |
| 60 incrementalKernelGenerator = new IncrementalKernelGenerator( | 47 incrementalKernelGenerator = new IncrementalKernelGenerator( |
| 61 startingUri, | 48 startingUri, |
| 62 new CompilerOptions() | 49 new CompilerOptions() |
| 63 ..fileSystem = fileSystem | 50 ..fileSystem = fileSystem |
| 64 ..chaseDependencies = true | 51 ..chaseDependencies = true |
| 65 ..sdkSummary = sdkSummaryUri | 52 ..sdkSummary = sdkSummaryUri |
| 66 ..packagesFileUri = new Uri()); | 53 ..packagesFileUri = new Uri()); |
| 67 return (await incrementalKernelGenerator.computeDelta()).newState; | 54 return (await incrementalKernelGenerator.computeDelta()).newState; |
| 68 } | 55 } |
| 69 | 56 |
| 70 test_incrementalUpdate_referenceToCore() async { | 57 test_incrementalUpdate_referenceToCore() async { |
| 71 // TODO(paulberry): test parts. | |
| 72 writeFiles({'/foo.dart': 'main() { print(1); }'}); | 58 writeFiles({'/foo.dart': 'main() { print(1); }'}); |
| 73 var fileUri = Uri.parse('file:///foo.dart'); | 59 var fooUri = Uri.parse('file:///foo.dart'); |
| 74 var initialState = await getInitialState(fileUri); | 60 var coreUri = Uri.parse('dart:core'); |
| 75 expect(initialState.keys, unorderedEquals([fileUri])); | 61 var initialState = await getInitialState(fooUri); |
| 76 void _checkMain(List<Library> libraries, int expectedArgument) { | 62 expect(initialState.keys, unorderedEquals([fooUri])); |
| 77 checkLibraries(libraries, { | 63 |
| 78 fileUri: (library) { | 64 void _checkMain(Program program, int expectedArgument) { |
| 79 expect(library.importUri, fileUri); | 65 expect(_getLibraryUris(program), unorderedEquals([fooUri, coreUri])); |
| 80 expect(library.classes, isEmpty); | 66 var mainStatements = _getProcedureStatements( |
| 81 expect(library.procedures, hasLength(1)); | 67 _getProcedure(_getLibrary(program, fooUri), 'main')); |
| 82 expect(library.procedures[0].name.name, 'main'); | 68 expect(mainStatements, hasLength(1)); |
| 83 var body = library.procedures[0].function.body; | 69 _checkPrintLiteralInt(mainStatements[0], expectedArgument); |
| 84 expect(body, new isInstanceOf<Block>()); | 70 var coreLibrary = _getLibrary(program, coreUri); |
| 85 var block = body as Block; | 71 expect(coreLibrary.procedures, hasLength(1)); |
| 86 expect(block.statements, hasLength(1)); | 72 expect(coreLibrary.procedures[0].name.name, 'print'); |
| 87 expect(block.statements[0], new isInstanceOf<ExpressionStatement>()); | 73 expect(coreLibrary.procedures[0].function.body, isNull); |
| 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 } | 74 } |
| 109 | 75 |
| 110 _checkMain(initialState[fileUri].libraries, 1); | 76 _checkMain(initialState[fooUri], 1); |
| 111 writeFiles({'/foo.dart': 'main() { print(2); }'}); | 77 writeFiles({'/foo.dart': 'main() { print(2); }'}); |
| 112 incrementalKernelGenerator.invalidateAll(); | 78 incrementalKernelGenerator.invalidateAll(); |
| 113 var deltaProgram = await incrementalKernelGenerator.computeDelta(); | 79 var deltaProgram = await incrementalKernelGenerator.computeDelta(); |
| 114 expect(deltaProgram.newState.keys, unorderedEquals([fileUri])); | 80 expect(deltaProgram.newState.keys, unorderedEquals([fooUri])); |
| 115 _checkMain(deltaProgram.newState[fileUri].libraries, 2); | 81 _checkMain(deltaProgram.newState[fooUri], 2); |
| 82 } |
| 83 |
| 84 test_part() async { |
| 85 writeFiles({ |
| 86 '/foo.dart': 'library foo; part "bar.dart"; main() { print(1); f(); }', |
| 87 '/bar.dart': 'part of foo; f() { print(2); }' |
| 88 }); |
| 89 var fooUri = Uri.parse('file:///foo.dart'); |
| 90 var initialState = await getInitialState(fooUri); |
| 91 expect(initialState.keys, unorderedEquals([fooUri])); |
| 92 var library = _getLibrary(initialState[fooUri], fooUri); |
| 93 var mainStatements = |
| 94 _getProcedureStatements(_getProcedure(library, 'main')); |
| 95 var fProcedure = _getProcedure(library, 'f'); |
| 96 var fStatements = _getProcedureStatements(fProcedure); |
| 97 expect(mainStatements, hasLength(2)); |
| 98 _checkPrintLiteralInt(mainStatements[0], 1); |
| 99 _checkFunctionCall(mainStatements[1], fProcedure); |
| 100 expect(fStatements, hasLength(1)); |
| 101 _checkPrintLiteralInt(fStatements[0], 2); |
| 102 // TODO(paulberry): now test incremental updates |
| 116 } | 103 } |
| 117 | 104 |
| 118 /// Write the given file contents to the virtual filesystem. | 105 /// Write the given file contents to the virtual filesystem. |
| 119 void writeFiles(Map<String, String> contents) { | 106 void writeFiles(Map<String, String> contents) { |
| 120 contents.forEach((path, text) { | 107 contents.forEach((path, text) { |
| 121 fileSystem | 108 fileSystem |
| 122 .entityForUri(Uri.parse('file://$path')) | 109 .entityForUri(Uri.parse('file://$path')) |
| 123 .writeAsStringSync(text); | 110 .writeAsStringSync(text); |
| 124 }); | 111 }); |
| 125 } | 112 } |
| 113 |
| 114 void _checkFunctionCall(Statement statement, Procedure expectedTarget) { |
| 115 expect(statement, new isInstanceOf<ExpressionStatement>()); |
| 116 var expressionStatement = statement as ExpressionStatement; |
| 117 expect( |
| 118 expressionStatement.expression, new isInstanceOf<StaticInvocation>()); |
| 119 var staticInvocation = expressionStatement.expression as StaticInvocation; |
| 120 expect(staticInvocation.target, same(expectedTarget)); |
| 121 } |
| 122 |
| 123 void _checkPrintLiteralInt(Statement statement, int expectedArgument) { |
| 124 expect(statement, new isInstanceOf<ExpressionStatement>()); |
| 125 var expressionStatement = statement as ExpressionStatement; |
| 126 expect( |
| 127 expressionStatement.expression, new isInstanceOf<StaticInvocation>()); |
| 128 var staticInvocation = expressionStatement.expression as StaticInvocation; |
| 129 expect(staticInvocation.target.name.name, 'print'); |
| 130 expect(staticInvocation.arguments.positional, hasLength(1)); |
| 131 expect(staticInvocation.arguments.positional[0], |
| 132 new isInstanceOf<IntLiteral>()); |
| 133 var intLiteral = staticInvocation.arguments.positional[0] as IntLiteral; |
| 134 expect(intLiteral.value, expectedArgument); |
| 135 } |
| 136 |
| 137 Library _getLibrary(Program program, Uri uri) { |
| 138 for (var library in program.libraries) { |
| 139 if (library.importUri == uri) return library; |
| 140 } |
| 141 throw fail('No library found with URI "$uri"'); |
| 142 } |
| 143 |
| 144 List<Uri> _getLibraryUris(Program program) => |
| 145 program.libraries.map((library) => library.importUri).toList(); |
| 146 |
| 147 Procedure _getProcedure(Library library, String name) { |
| 148 for (var procedure in library.procedures) { |
| 149 if (procedure.name.name == name) return procedure; |
| 150 } |
| 151 throw fail('No function declaration found with name "$name"'); |
| 152 } |
| 153 |
| 154 List<Statement> _getProcedureStatements(Procedure procedure) { |
| 155 var body = procedure.function.body; |
| 156 expect(body, new isInstanceOf<Block>()); |
| 157 return (body as Block).statements; |
| 158 } |
| 126 } | 159 } |
| OLD | NEW |