Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(697)

Unified Diff: pkg/front_end/test/src/incremental/kernel_driver_test.dart

Issue 2975093002: Add tests for KernelDriver. (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/front_end/test/src/incremental/kernel_driver_test.dart
diff --git a/pkg/front_end/test/incremental_kernel_generator_test.dart b/pkg/front_end/test/src/incremental/kernel_driver_test.dart
similarity index 58%
copy from pkg/front_end/test/incremental_kernel_generator_test.dart
copy to pkg/front_end/test/src/incremental/kernel_driver_test.dart
index b504c15baeea294076f3506d803739cea2559154..78abc5fc4508eb164c2889dd9e4689be1f518fd5 100644
--- a/pkg/front_end/test/incremental_kernel_generator_test.dart
+++ b/pkg/front_end/test/src/incremental/kernel_driver_test.dart
@@ -4,12 +4,12 @@
import 'dart:async';
-import 'package:front_end/compiler_options.dart';
-import 'package:front_end/incremental_kernel_generator.dart';
import 'package:front_end/memory_file_system.dart';
+import 'package:front_end/src/base/performace_logger.dart';
import 'package:front_end/src/fasta/kernel/utils.dart';
+import 'package:front_end/src/fasta/uri_translator_impl.dart';
import 'package:front_end/src/incremental/byte_store.dart';
-import 'package:front_end/src/incremental_kernel_generator_impl.dart';
+import 'package:front_end/src/incremental/kernel_driver.dart';
import 'package:kernel/ast.dart';
import 'package:kernel/binary/ast_from_binary.dart';
import 'package:kernel/text/ast_to_text.dart';
@@ -17,43 +17,24 @@ import 'package:kernel/verifier.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
-import 'src/incremental/mock_sdk.dart';
+import 'mock_sdk.dart';
main() {
defineReflectiveSuite(() {
- defineReflectiveTests(IncrementalKernelGeneratorTest);
+ defineReflectiveTests(KernelDriverTest);
});
}
@reflectiveTest
-class IncrementalKernelGeneratorTest {
+class KernelDriverTest {
/// Virtual filesystem for testing.
final fileSystem = new MemoryFileSystem(Uri.parse('file:///'));
- /// The used file watcher.
- WatchUsedFilesFn watchFn = (uri, used) {};
-
/// The object under test.
- IncrementalKernelGeneratorImpl incrementalKernelGenerator;
+ KernelDriver driver;
- /// Compute the initial [Program] for the given [entryPoint].
- Future<Program> getInitialState(Uri entryPoint) async {
- Map<String, Uri> dartLibraries = createSdkFiles(fileSystem);
- // TODO(scheglov) Builder the SDK kernel and set it into the options.
-
- // TODO(scheglov) Make `.packages` file optional.
-
- var compilerOptions = new CompilerOptions()
- ..fileSystem = fileSystem
- ..byteStore = new MemoryByteStore()
-// ..logger = new PerformanceLog(stdout)
- ..strongMode = true
- ..chaseDependencies = true
- ..dartLibraries = dartLibraries
- ..packagesFileUri = Uri.parse('file:///test/.packages');
- incrementalKernelGenerator = await IncrementalKernelGenerator
- .newInstance(compilerOptions, entryPoint, watch: watchFn);
- return (await incrementalKernelGenerator.computeDelta()).newProgram;
+ void setUp() {
+ _createDriver();
}
test_compile_chain() async {
@@ -79,10 +60,10 @@ void main() {}
''');
{
- Program program = await getInitialState(cUri);
- _assertLibraryUris(program,
+ KernelResult result = await driver.getKernel(cUri);
+ _assertLibraryUris(result,
includes: [aUri, bUri, cUri, Uri.parse('dart:core')]);
- Library library = _getLibrary(program, cUri);
+ Library library = _getLibrary(result, cUri);
expect(
_getLibraryText(library),
r'''
@@ -96,9 +77,6 @@ static field core::int c1 = a::a;
static field core::int c2 = b::b;
static method main() → void {}
''');
- // The main method is set.
- expect(program.mainMethod, isNotNull);
- expect(program.mainMethod.enclosingLibrary.fileUri, cUri.toString());
}
// Update b.dart and recompile c.dart
@@ -108,13 +86,12 @@ static method main() → void {}
import 'a.dart';
var b = 1.2;
''');
- incrementalKernelGenerator.invalidate(bUri);
+ driver.invalidate(bUri);
{
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- Program program = delta.newProgram;
- _assertLibraryUris(program,
- includes: [bUri, cUri], excludes: [aUri, Uri.parse('dart:core')]);
- Library library = _getLibrary(program, cUri);
+ KernelResult result = await driver.getKernel(cUri);
+ _assertLibraryUris(result,
+ includes: [aUri, bUri, cUri, Uri.parse('dart:core')]);
+ Library library = _getLibrary(result, cUri);
expect(
_getLibraryText(library),
r'''
@@ -128,9 +105,6 @@ static field core::int c1 = a::a;
static field core::double c2 = b::b;
static method main() → void {}
''');
- // The main method is set even though not the entry point is updated.
- expect(program.mainMethod, isNotNull);
- expect(program.mainMethod.enclosingLibrary.fileUri, cUri.toString());
}
}
@@ -148,8 +122,8 @@ import 'b.dart';
A a;
''');
- Program program = await getInitialState(cUri);
- Library library = _getLibrary(program, cUri);
+ KernelResult result = await driver.getKernel(cUri);
+ Library library = _getLibrary(result, cUri);
expect(
_getLibraryText(library),
r'''
@@ -177,8 +151,8 @@ B b;
''');
{
- Program program = await getInitialState(cUri);
- Library library = _getLibrary(program, cUri);
+ KernelResult result = await driver.getKernel(cUri);
+ Library library = _getLibrary(result, cUri);
expect(
_getLibraryText(library),
r'''
@@ -203,11 +177,10 @@ A a;
B b;
int c;
''');
- incrementalKernelGenerator.invalidate(cUri);
+ driver.invalidate(cUri);
{
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- Program program = delta.newProgram;
- Library library = _getLibrary(program, cUri);
+ KernelResult result = await driver.getKernel(cUri);
+ Library library = _getLibrary(result, cUri);
expect(
_getLibraryText(library),
r'''
@@ -239,8 +212,8 @@ A a;
B b;
''');
- Program program = await getInitialState(cUri);
- Library library = _getLibrary(program, cUri);
+ KernelResult result = await driver.getKernel(cUri);
+ Library library = _getLibrary(result, cUri);
expect(
_getLibraryText(library),
r'''
@@ -254,61 +227,6 @@ static field b::B b;
''');
}
- test_compile_includePathToMain() async {
- writeFile('/test/.packages', 'test:lib/');
- String aPath = '/test/lib/a.dart';
- String bPath = '/test/lib/b.dart';
- String cPath = '/test/lib/c.dart';
- String dPath = '/test/lib/d.dart';
-
- // A --> B -> C
- // \-> D
-
- Uri aUri = writeFile(
- aPath,
- r'''
-import 'b.dart';
-import 'd.dart';
-main() {
- b();
- d();
-}
-''');
- Uri bUri = writeFile(
- bPath,
- r'''
-import 'c.dart';
-b() {
- c();
-}
-''');
- Uri cUri = writeFile(cPath, 'c() { print(0); }');
- Uri dUri = writeFile(dPath, 'd() {}');
-
- {
- Program program = await getInitialState(aUri);
- _assertLibraryUris(program,
- includes: [aUri, bUri, cUri, dUri, Uri.parse('dart:core')]);
- }
-
- // Update c.dart and compute the delta.
- // It should include the changed c.dart, plus b.dart and a.dart because VM
- // requires this (because of possible inlining). But d.dart is not on the
- // path from main() to the changed c.dart, so it is not included.
- writeFile(cPath, 'c() { print(1); }');
- incrementalKernelGenerator.invalidate(cUri);
- {
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- Program program = delta.newProgram;
- _assertLibraryUris(program,
- includes: [aUri, bUri, cUri],
- excludes: [dUri, Uri.parse('dart:core')]);
- // While a.dart and b.dart are is included (VM needs them), they were not
- // recompiled, because the change to c.dart was in the function body.
- _assertCompiledUris([cUri]);
- }
- }
-
test_compile_recompileMixin() async {
writeFile('/test/.packages', 'test:lib/');
String aPath = '/test/lib/a.dart';
@@ -340,8 +258,8 @@ class C {
''');
{
- Program program = await getInitialState(aUri);
- _assertLibraryUris(program,
+ KernelResult result = await driver.getKernel(aUri);
+ _assertLibraryUris(result,
includes: [aUri, bUri, cUri, Uri.parse('dart:core')]);
}
@@ -357,12 +275,11 @@ class C {
}
}
''');
- incrementalKernelGenerator.invalidate(cUri);
+ driver.invalidate(cUri);
{
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- Program program = delta.newProgram;
- _assertLibraryUris(program,
- includes: [aUri, bUri, cUri], excludes: [Uri.parse('dart:core')]);
+ KernelResult result = await driver.getKernel(aUri);
+ _assertLibraryUris(result,
+ includes: [aUri, bUri, cUri, Uri.parse('dart:core')]);
// Compiled: c.dart (changed), and b.dart (has mixin).
_assertCompiledUris([cUri, bUri]);
}
@@ -380,8 +297,8 @@ import 'a.dart';
F<String> f;
''');
- Program program = await getInitialState(bUri);
- Library library = _getLibrary(program, bUri);
+ KernelResult result = await driver.getKernel(bUri);
+ Library library = _getLibrary(result, bUri);
expect(
_getLibraryText(library),
r'''
@@ -393,26 +310,6 @@ static field (core::String) → core::int f;
''');
}
- test_invalidateAll() async {
- writeFile('/test/.packages', '');
- Uri aUri = writeFile('/test/a.dart', "import 'b.dart';\nint a = b;");
- Uri bUri = writeFile('/test/b.dart', 'var b = 1;');
-
- Program program = await getInitialState(aUri);
- expect(_getLibraryText(_getLibrary(program, aUri)), contains("int a ="));
- expect(_getLibraryText(_getLibrary(program, bUri)), contains("b = 1"));
-
- writeFile('/test/a.dart', "import 'b.dart';\ndouble a = b;");
- writeFile('/test/b.dart', 'var b = 2;');
- incrementalKernelGenerator.invalidateAll();
-
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- program = delta.newProgram;
- _assertLibraryUris(program, includes: [aUri, bUri]);
- expect(_getLibraryText(_getLibrary(program, aUri)), contains("double a ="));
- expect(_getLibraryText(_getLibrary(program, bUri)), contains("b = 2"));
- }
-
test_limited_ast_to_binary() async {
writeFile('/test/.packages', 'test:lib/');
String aPath = '/test/lib/a.dart';
@@ -429,7 +326,7 @@ abstract class I {
int get interfaceGetter;
int interfaceMethod();
}
-
+
class A implements I {
static int staticField;
static int get staticGetter => 0;
@@ -442,7 +339,7 @@ class A implements I {
int interfaceField;
int get interfaceGetter => 0;
int interfaceMethod() => 0;
-
+
A();
A.named();
}
@@ -455,19 +352,19 @@ import 'a.dart';
class B extends A {
B() : super();
B.named() : super.named();
-
+
void foo() {
super.instanceMethod();
instanceMethod();
-
+
super.interfaceField;
super.interfaceField = 0;
super.interfaceGetter;
super.interfaceMethod();
}
-
+
int instanceMethod() => 0;
-
+
int interfaceField;
int get interfaceGetter => 0;
int interfaceMethod() => 0;
@@ -498,12 +395,15 @@ main() {
}
''');
- Program program = await getInitialState(bUri);
+ KernelResult result = await driver.getKernel(bUri);
+
+ Program program = new Program(
+ nameRoot: result.nameRoot, libraries: _allLibraries(result));
String initialKernelText;
List<int> bytes;
{
- Library initialLibrary = _getLibrary(program, bUri);
+ Library initialLibrary = _getLibraryFromProgram(program, bUri);
initialKernelText = _getLibraryText(initialLibrary);
bytes = serializeProgram(program,
@@ -523,7 +423,7 @@ main() {
var programForLoading = new Program(nameRoot: program.root);
var reader = new BinaryBuilder(bytes);
reader.readProgram(programForLoading);
- loadedLibrary = _getLibrary(programForLoading, bUri);
+ loadedLibrary = _getLibraryFromProgram(programForLoading, bUri);
}
// Add the library into the program.
@@ -536,71 +436,9 @@ main() {
verifyProgram(program);
}
- test_updateEntryPoint() async {
- writeFile('/test/.packages', 'test:lib/');
- String path = '/test/lib/test.dart';
- Uri uri = writeFile(
- path,
- r'''
-main() {
- var v = 1;
-}
-''');
-
- String initialText = r'''
-library;
-import self as self;
-import "dart:core" as core;
-
-static method main() → dynamic {
- core::int v = 1;
-}
-''';
-
- // Compute the initial state.
- {
- Program program = await getInitialState(uri);
- Library library = _getLibrary(program, uri);
- expect(_getLibraryText(library), initialText);
- }
-
- // Update the entry point library.
- writeFile(
- path,
- r'''
-main() {
- var v = 2.3;
-}
-''');
-
- // We have not invalidated the file, so the delta is empty.
- {
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- expect(delta.newProgram.libraries, isEmpty);
- }
-
- // Invalidate the file, so get the new text.
- incrementalKernelGenerator.invalidate(uri);
- {
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- Program program = delta.newProgram;
- _assertLibraryUris(program, includes: [uri]);
- Library library = _getLibrary(program, uri);
- expect(
- _getLibraryText(library),
- r'''
-library;
-import self as self;
-import "dart:core" as core;
-
-static method main() → dynamic {
- core::double v = 2.3;
-}
-''');
- }
- }
-
test_updatePackageSourceUsingFileUri() async {
+ _createDriver(packages: {'test': _folderUri('/test/lib')});
+
writeFile('/test/.packages', 'test:lib/');
Uri aFileUri = writeFile(
'/test/bin/a.dart',
@@ -613,8 +451,8 @@ var a = b;
// Compute the initial state.
{
- Program program = await getInitialState(aFileUri);
- Library library = _getLibrary(program, aFileUri);
+ KernelResult result = await driver.getKernel(aFileUri);
+ Library library = _getLibrary(result, aFileUri);
expect(
_getLibraryText(library),
r'''
@@ -630,12 +468,11 @@ static field core::int a = b::b;
// Update b.dart and use file URI to invalidate it.
// The delta is recomputed even though b.dart is used with the package URI.
writeFile('/test/lib/b.dart', 'var b = 1.2;');
- incrementalKernelGenerator.invalidate(bFileUri);
+ driver.invalidate(bFileUri);
{
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- Program program = delta.newProgram;
- _assertLibraryUris(program, includes: [aFileUri, bPackageUri]);
- Library library = _getLibrary(program, aFileUri);
+ KernelResult result = await driver.getKernel(aFileUri);
+ _assertLibraryUris(result, includes: [aFileUri, bPackageUri]);
+ Library library = _getLibrary(result, aFileUri);
expect(
_getLibraryText(library),
r'''
@@ -671,8 +508,8 @@ var d = a;
''');
// Check the initial state - types flow between the part and the library.
- Program program = await getInitialState(libUri);
- Library library = _getLibrary(program, libUri);
+ KernelResult result = await driver.getKernel(libUri);
+ Library library = _getLibrary(result, libUri);
expect(
_getLibraryText(library),
r'''
@@ -696,9 +533,9 @@ part of foo;
var b = 2.3;
var d = a;
''');
- incrementalKernelGenerator.invalidate(partUri);
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- Library library = _getLibrary(delta.newProgram, libUri);
+ driver.invalidate(partUri);
+ KernelResult result = await driver.getKernel(libUri);
+ Library library = _getLibrary(result, libUri);
expect(
_getLibraryText(library),
r'''
@@ -725,9 +562,9 @@ var a = 'aaa';
var c = b;
void main() {}
''');
- incrementalKernelGenerator.invalidate(libUri);
- DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
- Library library = _getLibrary(delta.newProgram, libUri);
+ driver.invalidate(libUri);
+ KernelResult result = await driver.getKernel(libUri);
+ Library library = _getLibrary(result, libUri);
expect(
_getLibraryText(library),
r'''
@@ -758,23 +595,17 @@ import 'a.dart';
''');
var usedFiles = <Uri>[];
- var unusedFiles = <Uri>[];
- watchFn = (Uri uri, bool used) {
- if (used) {
- usedFiles.add(uri);
- } else {
- unusedFiles.add(uri);
- }
+ _createDriver(fileAddedFn: (Uri uri) {
+ usedFiles.add(uri);
return new Future.value();
- };
+ });
{
- await getInitialState(cUri);
+ await driver.getKernel(cUri);
// We use at least c.dart and a.dart now.
expect(usedFiles, contains(cUri));
expect(usedFiles, contains(aUri));
usedFiles.clear();
- expect(unusedFiles, isEmpty);
}
// Update c.dart to reference also b.dart file.
@@ -784,50 +615,13 @@ import 'a.dart';
import 'a.dart';
import 'b.dart';
''');
- incrementalKernelGenerator.invalidate(cUri);
+ driver.invalidate(cUri);
{
- await incrementalKernelGenerator.computeDelta();
+ await driver.getKernel(cUri);
// The only new file is b.dart now.
expect(usedFiles, [bUri]);
usedFiles.clear();
- expect(unusedFiles, isEmpty);
}
-
- // Update c.dart to stop referencing b.dart file.
- writeFile(
- cPath,
- r'''
-import 'a.dart';
-''');
- incrementalKernelGenerator.invalidate(cUri);
- {
- await incrementalKernelGenerator.computeDelta();
- // No new used files.
- expect(usedFiles, isEmpty);
- // The file b.dart is not used anymore.
- expect(unusedFiles, [bUri]);
- unusedFiles.clear();
- }
- }
-
- test_watch_null() async {
- writeFile('/test/.packages', 'test:lib/');
- String aPath = '/test/lib/a.dart';
- String bPath = '/test/lib/b.dart';
- writeFile(aPath, "");
- Uri bUri = writeFile(bPath, "");
-
- // Set null, as if the watch function is not provided.
- watchFn = null;
-
- await getInitialState(bUri);
-
- // Update b.dart to import a.dart file.
- writeFile(bPath, "import 'a.dart';");
- incrementalKernelGenerator.invalidate(bUri);
- await incrementalKernelGenerator.computeDelta();
-
- // No exception even though the watcher function is null.
}
/// Write the given [text] of the file with the given [path] into the
@@ -838,14 +632,15 @@ import 'a.dart';
return uri;
}
- /// Write the given file contents to the virtual filesystem.
- void writeFiles(Map<String, String> contents) {
- contents.forEach(writeFile);
+ List<Library> _allLibraries(KernelResult result) {
+ return result.results
+ .map((cycle) => cycle.kernelLibraries)
+ .expand((libraries) => libraries)
+ .toList();
}
void _assertCompiledUris(Iterable<Uri> expected) {
- var compiledCycles =
- incrementalKernelGenerator.test.driver.test.compiledCycles;
+ var compiledCycles = driver.test.compiledCycles;
Set<Uri> compiledUris = compiledCycles
.map((cycle) => cycle.libraries.map((file) => file.uri))
.expand((uris) => uris)
@@ -853,10 +648,12 @@ import 'a.dart';
expect(compiledUris, unorderedEquals(expected));
}
- void _assertLibraryUris(Program program,
+ void _assertLibraryUris(KernelResult result,
{List<Uri> includes: const [], List<Uri> excludes: const []}) {
- List<Uri> libraryUris =
- program.libraries.map((library) => library.importUri).toList();
+ List<Uri> libraryUris = result.results
+ .map((cycle) => cycle.kernelLibraries.map((lib) => lib.importUri))
+ .expand((uris) => uris)
+ .toList();
for (var shouldInclude in includes) {
expect(libraryUris, contains(shouldInclude));
}
@@ -865,7 +662,26 @@ import 'a.dart';
}
}
- Library _getLibrary(Program program, Uri uri) {
+ /// Create new [KernelDriver] instance and put it into the [driver] field.
+ void _createDriver(
+ {Map<String, Uri> packages, KernelDriverFileAddedFn fileAddedFn}) {
+ Map<String, Uri> dartLibraries = createSdkFiles(fileSystem);
+ var uriTranslator = new UriTranslatorImpl(dartLibraries, {}, packages);
+ driver = new KernelDriver(new PerformanceLog(null), fileSystem,
+ new MemoryByteStore(), uriTranslator, true,
+ fileAddedFn: fileAddedFn);
+ }
+
+ Library _getLibrary(KernelResult result, Uri uri) {
+ for (var cycleResult in result.results) {
+ for (var library in cycleResult.kernelLibraries) {
+ if (library.importUri == uri) return library;
+ }
+ }
+ throw fail('No library found with URI "$uri"');
+ }
+
+ Library _getLibraryFromProgram(Program program, Uri uri) {
for (var library in program.libraries) {
if (library.importUri == uri) return library;
}
@@ -878,4 +694,10 @@ import 'a.dart';
.writeLibraryFile(library);
return buffer.toString();
}
+
+ /// Return the [Uri] for the given Posix [path].
+ static Uri _folderUri(String path) {
+ if (!path.endsWith('/')) path += '/';
+ return Uri.parse('file://$path');
+ }
}
« no previous file with comments | « pkg/front_end/test/incremental_kernel_generator_test.dart ('k') | pkg/front_end/test/src/incremental/test_all.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698