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

Side by Side 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 unified diff | Download patch
OLDNEW
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';
8 import 'package:front_end/incremental_kernel_generator.dart';
9 import 'package:front_end/memory_file_system.dart'; 7 import 'package:front_end/memory_file_system.dart';
8 import 'package:front_end/src/base/performace_logger.dart';
10 import 'package:front_end/src/fasta/kernel/utils.dart'; 9 import 'package:front_end/src/fasta/kernel/utils.dart';
10 import 'package:front_end/src/fasta/uri_translator_impl.dart';
11 import 'package:front_end/src/incremental/byte_store.dart'; 11 import 'package:front_end/src/incremental/byte_store.dart';
12 import 'package:front_end/src/incremental_kernel_generator_impl.dart'; 12 import 'package:front_end/src/incremental/kernel_driver.dart';
13 import 'package:kernel/ast.dart'; 13 import 'package:kernel/ast.dart';
14 import 'package:kernel/binary/ast_from_binary.dart'; 14 import 'package:kernel/binary/ast_from_binary.dart';
15 import 'package:kernel/text/ast_to_text.dart'; 15 import 'package:kernel/text/ast_to_text.dart';
16 import 'package:kernel/verifier.dart'; 16 import 'package:kernel/verifier.dart';
17 import 'package:test/test.dart'; 17 import 'package:test/test.dart';
18 import 'package:test_reflective_loader/test_reflective_loader.dart'; 18 import 'package:test_reflective_loader/test_reflective_loader.dart';
19 19
20 import 'src/incremental/mock_sdk.dart'; 20 import 'mock_sdk.dart';
21 21
22 main() { 22 main() {
23 defineReflectiveSuite(() { 23 defineReflectiveSuite(() {
24 defineReflectiveTests(IncrementalKernelGeneratorTest); 24 defineReflectiveTests(KernelDriverTest);
25 }); 25 });
26 } 26 }
27 27
28 @reflectiveTest 28 @reflectiveTest
29 class IncrementalKernelGeneratorTest { 29 class KernelDriverTest {
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. 33 /// The object under test.
34 WatchUsedFilesFn watchFn = (uri, used) {}; 34 KernelDriver driver;
35 35
36 /// The object under test. 36 void setUp() {
37 IncrementalKernelGeneratorImpl incrementalKernelGenerator; 37 _createDriver();
38
39 /// Compute the initial [Program] for the given [entryPoint].
40 Future<Program> getInitialState(Uri entryPoint) async {
41 Map<String, Uri> dartLibraries = createSdkFiles(fileSystem);
42 // TODO(scheglov) Builder the SDK kernel and set it into the options.
43
44 // TODO(scheglov) Make `.packages` file optional.
45
46 var compilerOptions = new CompilerOptions()
47 ..fileSystem = fileSystem
48 ..byteStore = new MemoryByteStore()
49 // ..logger = new PerformanceLog(stdout)
50 ..strongMode = true
51 ..chaseDependencies = true
52 ..dartLibraries = dartLibraries
53 ..packagesFileUri = Uri.parse('file:///test/.packages');
54 incrementalKernelGenerator = await IncrementalKernelGenerator
55 .newInstance(compilerOptions, entryPoint, watch: watchFn);
56 return (await incrementalKernelGenerator.computeDelta()).newProgram;
57 } 38 }
58 39
59 test_compile_chain() async { 40 test_compile_chain() async {
60 writeFile('/test/.packages', 'test:lib/'); 41 writeFile('/test/.packages', 'test:lib/');
61 String aPath = '/test/lib/a.dart'; 42 String aPath = '/test/lib/a.dart';
62 String bPath = '/test/lib/b.dart'; 43 String bPath = '/test/lib/b.dart';
63 String cPath = '/test/lib/c.dart'; 44 String cPath = '/test/lib/c.dart';
64 Uri aUri = writeFile(aPath, 'var a = 1;'); 45 Uri aUri = writeFile(aPath, 'var a = 1;');
65 Uri bUri = writeFile( 46 Uri bUri = writeFile(
66 bPath, 47 bPath,
67 r''' 48 r'''
68 import 'a.dart'; 49 import 'a.dart';
69 var b = a; 50 var b = a;
70 '''); 51 ''');
71 Uri cUri = writeFile( 52 Uri cUri = writeFile(
72 cPath, 53 cPath,
73 r''' 54 r'''
74 import 'a.dart'; 55 import 'a.dart';
75 import 'b.dart'; 56 import 'b.dart';
76 var c1 = a; 57 var c1 = a;
77 var c2 = b; 58 var c2 = b;
78 void main() {} 59 void main() {}
79 '''); 60 ''');
80 61
81 { 62 {
82 Program program = await getInitialState(cUri); 63 KernelResult result = await driver.getKernel(cUri);
83 _assertLibraryUris(program, 64 _assertLibraryUris(result,
84 includes: [aUri, bUri, cUri, Uri.parse('dart:core')]); 65 includes: [aUri, bUri, cUri, Uri.parse('dart:core')]);
85 Library library = _getLibrary(program, cUri); 66 Library library = _getLibrary(result, cUri);
86 expect( 67 expect(
87 _getLibraryText(library), 68 _getLibraryText(library),
88 r''' 69 r'''
89 library; 70 library;
90 import self as self; 71 import self as self;
91 import "dart:core" as core; 72 import "dart:core" as core;
92 import "./a.dart" as a; 73 import "./a.dart" as a;
93 import "./b.dart" as b; 74 import "./b.dart" as b;
94 75
95 static field core::int c1 = a::a; 76 static field core::int c1 = a::a;
96 static field core::int c2 = b::b; 77 static field core::int c2 = b::b;
97 static method main() → void {} 78 static method main() → void {}
98 '''); 79 ''');
99 // The main method is set.
100 expect(program.mainMethod, isNotNull);
101 expect(program.mainMethod.enclosingLibrary.fileUri, cUri.toString());
102 } 80 }
103 81
104 // Update b.dart and recompile c.dart 82 // Update b.dart and recompile c.dart
105 writeFile( 83 writeFile(
106 bPath, 84 bPath,
107 r''' 85 r'''
108 import 'a.dart'; 86 import 'a.dart';
109 var b = 1.2; 87 var b = 1.2;
110 '''); 88 ''');
111 incrementalKernelGenerator.invalidate(bUri); 89 driver.invalidate(bUri);
112 { 90 {
113 DeltaProgram delta = await incrementalKernelGenerator.computeDelta(); 91 KernelResult result = await driver.getKernel(cUri);
114 Program program = delta.newProgram; 92 _assertLibraryUris(result,
115 _assertLibraryUris(program, 93 includes: [aUri, bUri, cUri, Uri.parse('dart:core')]);
116 includes: [bUri, cUri], excludes: [aUri, Uri.parse('dart:core')]); 94 Library library = _getLibrary(result, cUri);
117 Library library = _getLibrary(program, cUri);
118 expect( 95 expect(
119 _getLibraryText(library), 96 _getLibraryText(library),
120 r''' 97 r'''
121 library; 98 library;
122 import self as self; 99 import self as self;
123 import "dart:core" as core; 100 import "dart:core" as core;
124 import "./a.dart" as a; 101 import "./a.dart" as a;
125 import "./b.dart" as b; 102 import "./b.dart" as b;
126 103
127 static field core::int c1 = a::a; 104 static field core::int c1 = a::a;
128 static field core::double c2 = b::b; 105 static field core::double c2 = b::b;
129 static method main() → void {} 106 static method main() → void {}
130 '''); 107 ''');
131 // The main method is set even though not the entry point is updated.
132 expect(program.mainMethod, isNotNull);
133 expect(program.mainMethod.enclosingLibrary.fileUri, cUri.toString());
134 } 108 }
135 } 109 }
136 110
137 test_compile_export() async { 111 test_compile_export() async {
138 writeFile('/test/.packages', 'test:lib/'); 112 writeFile('/test/.packages', 'test:lib/');
139 String aPath = '/test/lib/a.dart'; 113 String aPath = '/test/lib/a.dart';
140 String bPath = '/test/lib/b.dart'; 114 String bPath = '/test/lib/b.dart';
141 String cPath = '/test/lib/c.dart'; 115 String cPath = '/test/lib/c.dart';
142 writeFile(aPath, 'class A {}'); 116 writeFile(aPath, 'class A {}');
143 writeFile(bPath, 'export "a.dart";'); 117 writeFile(bPath, 'export "a.dart";');
144 Uri cUri = writeFile( 118 Uri cUri = writeFile(
145 cPath, 119 cPath,
146 r''' 120 r'''
147 import 'b.dart'; 121 import 'b.dart';
148 A a; 122 A a;
149 '''); 123 ''');
150 124
151 Program program = await getInitialState(cUri); 125 KernelResult result = await driver.getKernel(cUri);
152 Library library = _getLibrary(program, cUri); 126 Library library = _getLibrary(result, cUri);
153 expect( 127 expect(
154 _getLibraryText(library), 128 _getLibraryText(library),
155 r''' 129 r'''
156 library; 130 library;
157 import self as self; 131 import self as self;
158 import "./a.dart" as a; 132 import "./a.dart" as a;
159 133
160 static field a::A a; 134 static field a::A a;
161 '''); 135 ''');
162 } 136 }
163 137
164 test_compile_export_cycle() async { 138 test_compile_export_cycle() async {
165 writeFile('/test/.packages', 'test:lib/'); 139 writeFile('/test/.packages', 'test:lib/');
166 String aPath = '/test/lib/a.dart'; 140 String aPath = '/test/lib/a.dart';
167 String bPath = '/test/lib/b.dart'; 141 String bPath = '/test/lib/b.dart';
168 String cPath = '/test/lib/c.dart'; 142 String cPath = '/test/lib/c.dart';
169 writeFile(aPath, 'export "b.dart"; class A {}'); 143 writeFile(aPath, 'export "b.dart"; class A {}');
170 writeFile(bPath, 'export "a.dart"; class B {}'); 144 writeFile(bPath, 'export "a.dart"; class B {}');
171 Uri cUri = writeFile( 145 Uri cUri = writeFile(
172 cPath, 146 cPath,
173 r''' 147 r'''
174 import 'b.dart'; 148 import 'b.dart';
175 A a; 149 A a;
176 B b; 150 B b;
177 '''); 151 ''');
178 152
179 { 153 {
180 Program program = await getInitialState(cUri); 154 KernelResult result = await driver.getKernel(cUri);
181 Library library = _getLibrary(program, cUri); 155 Library library = _getLibrary(result, cUri);
182 expect( 156 expect(
183 _getLibraryText(library), 157 _getLibraryText(library),
184 r''' 158 r'''
185 library; 159 library;
186 import self as self; 160 import self as self;
187 import "./a.dart" as a; 161 import "./a.dart" as a;
188 import "./b.dart" as b; 162 import "./b.dart" as b;
189 163
190 static field a::A a; 164 static field a::A a;
191 static field b::B b; 165 static field b::B b;
192 '''); 166 ''');
193 } 167 }
194 168
195 // Update c.dart and compile. 169 // Update c.dart and compile.
196 // We should load the cycle [a.dart, b.dart] from the byte store. 170 // We should load the cycle [a.dart, b.dart] from the byte store.
197 // This tests that we compute export scopes after loading. 171 // This tests that we compute export scopes after loading.
198 writeFile( 172 writeFile(
199 cPath, 173 cPath,
200 r''' 174 r'''
201 import 'b.dart'; 175 import 'b.dart';
202 A a; 176 A a;
203 B b; 177 B b;
204 int c; 178 int c;
205 '''); 179 ''');
206 incrementalKernelGenerator.invalidate(cUri); 180 driver.invalidate(cUri);
207 { 181 {
208 DeltaProgram delta = await incrementalKernelGenerator.computeDelta(); 182 KernelResult result = await driver.getKernel(cUri);
209 Program program = delta.newProgram; 183 Library library = _getLibrary(result, cUri);
210 Library library = _getLibrary(program, cUri);
211 expect( 184 expect(
212 _getLibraryText(library), 185 _getLibraryText(library),
213 r''' 186 r'''
214 library; 187 library;
215 import self as self; 188 import self as self;
216 import "./a.dart" as a; 189 import "./a.dart" as a;
217 import "./b.dart" as b; 190 import "./b.dart" as b;
218 import "dart:core" as core; 191 import "dart:core" as core;
219 192
220 static field a::A a; 193 static field a::A a;
(...skipping 11 matching lines...) Expand all
232 writeFile(aPath, 'class A {} class B {}'); 205 writeFile(aPath, 'class A {} class B {}');
233 writeFile(bPath, 'export "a.dart"; class B {}'); 206 writeFile(bPath, 'export "a.dart"; class B {}');
234 Uri cUri = writeFile( 207 Uri cUri = writeFile(
235 cPath, 208 cPath,
236 r''' 209 r'''
237 import 'b.dart'; 210 import 'b.dart';
238 A a; 211 A a;
239 B b; 212 B b;
240 '''); 213 ''');
241 214
242 Program program = await getInitialState(cUri); 215 KernelResult result = await driver.getKernel(cUri);
243 Library library = _getLibrary(program, cUri); 216 Library library = _getLibrary(result, cUri);
244 expect( 217 expect(
245 _getLibraryText(library), 218 _getLibraryText(library),
246 r''' 219 r'''
247 library; 220 library;
248 import self as self; 221 import self as self;
249 import "./a.dart" as a; 222 import "./a.dart" as a;
250 import "./b.dart" as b; 223 import "./b.dart" as b;
251 224
252 static field a::A a; 225 static field a::A a;
253 static field b::B b; 226 static field b::B b;
254 '''); 227 ''');
255 } 228 }
256 229
257 test_compile_includePathToMain() async {
258 writeFile('/test/.packages', 'test:lib/');
259 String aPath = '/test/lib/a.dart';
260 String bPath = '/test/lib/b.dart';
261 String cPath = '/test/lib/c.dart';
262 String dPath = '/test/lib/d.dart';
263
264 // A --> B -> C
265 // \-> D
266
267 Uri aUri = writeFile(
268 aPath,
269 r'''
270 import 'b.dart';
271 import 'd.dart';
272 main() {
273 b();
274 d();
275 }
276 ''');
277 Uri bUri = writeFile(
278 bPath,
279 r'''
280 import 'c.dart';
281 b() {
282 c();
283 }
284 ''');
285 Uri cUri = writeFile(cPath, 'c() { print(0); }');
286 Uri dUri = writeFile(dPath, 'd() {}');
287
288 {
289 Program program = await getInitialState(aUri);
290 _assertLibraryUris(program,
291 includes: [aUri, bUri, cUri, dUri, Uri.parse('dart:core')]);
292 }
293
294 // Update c.dart and compute the delta.
295 // It should include the changed c.dart, plus b.dart and a.dart because VM
296 // requires this (because of possible inlining). But d.dart is not on the
297 // path from main() to the changed c.dart, so it is not included.
298 writeFile(cPath, 'c() { print(1); }');
299 incrementalKernelGenerator.invalidate(cUri);
300 {
301 DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
302 Program program = delta.newProgram;
303 _assertLibraryUris(program,
304 includes: [aUri, bUri, cUri],
305 excludes: [dUri, Uri.parse('dart:core')]);
306 // While a.dart and b.dart are is included (VM needs them), they were not
307 // recompiled, because the change to c.dart was in the function body.
308 _assertCompiledUris([cUri]);
309 }
310 }
311
312 test_compile_recompileMixin() async { 230 test_compile_recompileMixin() async {
313 writeFile('/test/.packages', 'test:lib/'); 231 writeFile('/test/.packages', 'test:lib/');
314 String aPath = '/test/lib/a.dart'; 232 String aPath = '/test/lib/a.dart';
315 String bPath = '/test/lib/b.dart'; 233 String bPath = '/test/lib/b.dart';
316 String cPath = '/test/lib/c.dart'; 234 String cPath = '/test/lib/c.dart';
317 235
318 Uri aUri = writeFile( 236 Uri aUri = writeFile(
319 aPath, 237 aPath,
320 r''' 238 r'''
321 import 'b.dart'; 239 import 'b.dart';
(...skipping 11 matching lines...) Expand all
333 cPath, 251 cPath,
334 r''' 252 r'''
335 class C { 253 class C {
336 void foo() { 254 void foo() {
337 print(0); 255 print(0);
338 } 256 }
339 } 257 }
340 '''); 258 ''');
341 259
342 { 260 {
343 Program program = await getInitialState(aUri); 261 KernelResult result = await driver.getKernel(aUri);
344 _assertLibraryUris(program, 262 _assertLibraryUris(result,
345 includes: [aUri, bUri, cUri, Uri.parse('dart:core')]); 263 includes: [aUri, bUri, cUri, Uri.parse('dart:core')]);
346 } 264 }
347 265
348 // Update c.dart and compute the delta. 266 // Update c.dart and compute the delta.
349 // Includes: c.dart, b.dart and a.dart files. 267 // Includes: c.dart, b.dart and a.dart files.
350 // Compiled: c.dart (changed) and b.dart (has mixin), but not a.dart file. 268 // Compiled: c.dart (changed) and b.dart (has mixin), but not a.dart file.
351 writeFile( 269 writeFile(
352 cPath, 270 cPath,
353 r''' 271 r'''
354 class C { 272 class C {
355 void foo() { 273 void foo() {
356 print(1); 274 print(1);
357 } 275 }
358 } 276 }
359 '''); 277 ''');
360 incrementalKernelGenerator.invalidate(cUri); 278 driver.invalidate(cUri);
361 { 279 {
362 DeltaProgram delta = await incrementalKernelGenerator.computeDelta(); 280 KernelResult result = await driver.getKernel(aUri);
363 Program program = delta.newProgram; 281 _assertLibraryUris(result,
364 _assertLibraryUris(program, 282 includes: [aUri, bUri, cUri, Uri.parse('dart:core')]);
365 includes: [aUri, bUri, cUri], excludes: [Uri.parse('dart:core')]);
366 // Compiled: c.dart (changed), and b.dart (has mixin). 283 // Compiled: c.dart (changed), and b.dart (has mixin).
367 _assertCompiledUris([cUri, bUri]); 284 _assertCompiledUris([cUri, bUri]);
368 } 285 }
369 } 286 }
370 287
371 test_compile_typedef() async { 288 test_compile_typedef() async {
372 writeFile('/test/.packages', 'test:lib/'); 289 writeFile('/test/.packages', 'test:lib/');
373 String aPath = '/test/lib/a.dart'; 290 String aPath = '/test/lib/a.dart';
374 String bPath = '/test/lib/b.dart'; 291 String bPath = '/test/lib/b.dart';
375 writeFile(aPath, 'typedef int F<T>(T x);'); 292 writeFile(aPath, 'typedef int F<T>(T x);');
376 Uri bUri = writeFile( 293 Uri bUri = writeFile(
377 bPath, 294 bPath,
378 r''' 295 r'''
379 import 'a.dart'; 296 import 'a.dart';
380 F<String> f; 297 F<String> f;
381 '''); 298 ''');
382 299
383 Program program = await getInitialState(bUri); 300 KernelResult result = await driver.getKernel(bUri);
384 Library library = _getLibrary(program, bUri); 301 Library library = _getLibrary(result, bUri);
385 expect( 302 expect(
386 _getLibraryText(library), 303 _getLibraryText(library),
387 r''' 304 r'''
388 library; 305 library;
389 import self as self; 306 import self as self;
390 import "dart:core" as core; 307 import "dart:core" as core;
391 308
392 static field (core::String) → core::int f; 309 static field (core::String) → core::int f;
393 '''); 310 ''');
394 } 311 }
395 312
396 test_invalidateAll() async {
397 writeFile('/test/.packages', '');
398 Uri aUri = writeFile('/test/a.dart', "import 'b.dart';\nint a = b;");
399 Uri bUri = writeFile('/test/b.dart', 'var b = 1;');
400
401 Program program = await getInitialState(aUri);
402 expect(_getLibraryText(_getLibrary(program, aUri)), contains("int a ="));
403 expect(_getLibraryText(_getLibrary(program, bUri)), contains("b = 1"));
404
405 writeFile('/test/a.dart', "import 'b.dart';\ndouble a = b;");
406 writeFile('/test/b.dart', 'var b = 2;');
407 incrementalKernelGenerator.invalidateAll();
408
409 DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
410 program = delta.newProgram;
411 _assertLibraryUris(program, includes: [aUri, bUri]);
412 expect(_getLibraryText(_getLibrary(program, aUri)), contains("double a ="));
413 expect(_getLibraryText(_getLibrary(program, bUri)), contains("b = 2"));
414 }
415
416 test_limited_ast_to_binary() async { 313 test_limited_ast_to_binary() async {
417 writeFile('/test/.packages', 'test:lib/'); 314 writeFile('/test/.packages', 'test:lib/');
418 String aPath = '/test/lib/a.dart'; 315 String aPath = '/test/lib/a.dart';
419 String bPath = '/test/lib/b.dart'; 316 String bPath = '/test/lib/b.dart';
420 writeFile( 317 writeFile(
421 aPath, 318 aPath,
422 r''' 319 r'''
423 int topField = 0; 320 int topField = 0;
424 int get topGetter => 0; 321 int get topGetter => 0;
425 int topFunction({p}) => 0; 322 int topFunction({p}) => 0;
426 323
427 abstract class I { 324 abstract class I {
428 int interfaceField; 325 int interfaceField;
429 int get interfaceGetter; 326 int get interfaceGetter;
430 int interfaceMethod(); 327 int interfaceMethod();
431 } 328 }
432 329
433 class A implements I { 330 class A implements I {
434 static int staticField; 331 static int staticField;
435 static int get staticGetter => 0; 332 static int get staticGetter => 0;
436 static int staticMethod() => 0; 333 static int staticMethod() => 0;
437 334
438 int instanceField; 335 int instanceField;
439 int get instanceGetter => 0; 336 int get instanceGetter => 0;
440 int instanceMethod() => 0; 337 int instanceMethod() => 0;
441 338
442 int interfaceField; 339 int interfaceField;
443 int get interfaceGetter => 0; 340 int get interfaceGetter => 0;
444 int interfaceMethod() => 0; 341 int interfaceMethod() => 0;
445 342
446 A(); 343 A();
447 A.named(); 344 A.named();
448 } 345 }
449 '''); 346 ''');
450 Uri bUri = writeFile( 347 Uri bUri = writeFile(
451 bPath, 348 bPath,
452 r''' 349 r'''
453 import 'a.dart'; 350 import 'a.dart';
454 351
455 class B extends A { 352 class B extends A {
456 B() : super(); 353 B() : super();
457 B.named() : super.named(); 354 B.named() : super.named();
458 355
459 void foo() { 356 void foo() {
460 super.instanceMethod(); 357 super.instanceMethod();
461 instanceMethod(); 358 instanceMethod();
462 359
463 super.interfaceField; 360 super.interfaceField;
464 super.interfaceField = 0; 361 super.interfaceField = 0;
465 super.interfaceGetter; 362 super.interfaceGetter;
466 super.interfaceMethod(); 363 super.interfaceMethod();
467 } 364 }
468 365
469 int instanceMethod() => 0; 366 int instanceMethod() => 0;
470 367
471 int interfaceField; 368 int interfaceField;
472 int get interfaceGetter => 0; 369 int get interfaceGetter => 0;
473 int interfaceMethod() => 0; 370 int interfaceMethod() => 0;
474 } 371 }
475 372
476 main() { 373 main() {
477 topField; 374 topField;
478 topField = 0; 375 topField = 0;
479 var v1 = topGetter; 376 var v1 = topGetter;
480 var v2 = topFunction(p: 0); 377 var v2 = topFunction(p: 0);
(...skipping 10 matching lines...) Expand all
491 var v5 = a.instanceGetter; 388 var v5 = a.instanceGetter;
492 var v6 = a.instanceMethod(); 389 var v6 = a.instanceMethod();
493 390
494 a.interfaceField; 391 a.interfaceField;
495 a.interfaceField = 0; 392 a.interfaceField = 0;
496 var v7 = a.interfaceGetter; 393 var v7 = a.interfaceGetter;
497 var v8 = a.interfaceMethod(); 394 var v8 = a.interfaceMethod();
498 } 395 }
499 '''); 396 ''');
500 397
501 Program program = await getInitialState(bUri); 398 KernelResult result = await driver.getKernel(bUri);
399
400 Program program = new Program(
401 nameRoot: result.nameRoot, libraries: _allLibraries(result));
502 402
503 String initialKernelText; 403 String initialKernelText;
504 List<int> bytes; 404 List<int> bytes;
505 { 405 {
506 Library initialLibrary = _getLibrary(program, bUri); 406 Library initialLibrary = _getLibraryFromProgram(program, bUri);
507 initialKernelText = _getLibraryText(initialLibrary); 407 initialKernelText = _getLibraryText(initialLibrary);
508 408
509 bytes = serializeProgram(program, 409 bytes = serializeProgram(program,
510 filter: (library) => library.importUri == bUri); 410 filter: (library) => library.importUri == bUri);
511 411
512 // Remove b.dart from the program. 412 // Remove b.dart from the program.
513 // So, the program is now ready for re-adding the library. 413 // So, the program is now ready for re-adding the library.
514 program.mainMethod = null; 414 program.mainMethod = null;
515 program.libraries.remove(initialLibrary); 415 program.libraries.remove(initialLibrary);
516 program.root.removeChild(initialLibrary.importUri.toString()); 416 program.root.removeChild(initialLibrary.importUri.toString());
517 } 417 }
518 418
519 // Load b.dart from bytes using the initial name root, so that 419 // Load b.dart from bytes using the initial name root, so that
520 // serialized canonical names can be linked to corresponding nodes. 420 // serialized canonical names can be linked to corresponding nodes.
521 Library loadedLibrary; 421 Library loadedLibrary;
522 { 422 {
523 var programForLoading = new Program(nameRoot: program.root); 423 var programForLoading = new Program(nameRoot: program.root);
524 var reader = new BinaryBuilder(bytes); 424 var reader = new BinaryBuilder(bytes);
525 reader.readProgram(programForLoading); 425 reader.readProgram(programForLoading);
526 loadedLibrary = _getLibrary(programForLoading, bUri); 426 loadedLibrary = _getLibraryFromProgram(programForLoading, bUri);
527 } 427 }
528 428
529 // Add the library into the program. 429 // Add the library into the program.
530 program.libraries.add(loadedLibrary); 430 program.libraries.add(loadedLibrary);
531 loadedLibrary.parent = program; 431 loadedLibrary.parent = program;
532 program.mainMethod = loadedLibrary.procedures 432 program.mainMethod = loadedLibrary.procedures
533 .firstWhere((procedure) => procedure.name.name == 'main'); 433 .firstWhere((procedure) => procedure.name.name == 'main');
534 434
535 expect(_getLibraryText(loadedLibrary), initialKernelText); 435 expect(_getLibraryText(loadedLibrary), initialKernelText);
536 verifyProgram(program); 436 verifyProgram(program);
537 } 437 }
538 438
539 test_updateEntryPoint() async { 439 test_updatePackageSourceUsingFileUri() async {
540 writeFile('/test/.packages', 'test:lib/'); 440 _createDriver(packages: {'test': _folderUri('/test/lib')});
541 String path = '/test/lib/test.dart';
542 Uri uri = writeFile(
543 path,
544 r'''
545 main() {
546 var v = 1;
547 }
548 ''');
549 441
550 String initialText = r'''
551 library;
552 import self as self;
553 import "dart:core" as core;
554
555 static method main() → dynamic {
556 core::int v = 1;
557 }
558 ''';
559
560 // Compute the initial state.
561 {
562 Program program = await getInitialState(uri);
563 Library library = _getLibrary(program, uri);
564 expect(_getLibraryText(library), initialText);
565 }
566
567 // Update the entry point library.
568 writeFile(
569 path,
570 r'''
571 main() {
572 var v = 2.3;
573 }
574 ''');
575
576 // We have not invalidated the file, so the delta is empty.
577 {
578 DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
579 expect(delta.newProgram.libraries, isEmpty);
580 }
581
582 // Invalidate the file, so get the new text.
583 incrementalKernelGenerator.invalidate(uri);
584 {
585 DeltaProgram delta = await incrementalKernelGenerator.computeDelta();
586 Program program = delta.newProgram;
587 _assertLibraryUris(program, includes: [uri]);
588 Library library = _getLibrary(program, uri);
589 expect(
590 _getLibraryText(library),
591 r'''
592 library;
593 import self as self;
594 import "dart:core" as core;
595
596 static method main() → dynamic {
597 core::double v = 2.3;
598 }
599 ''');
600 }
601 }
602
603 test_updatePackageSourceUsingFileUri() async {
604 writeFile('/test/.packages', 'test:lib/'); 442 writeFile('/test/.packages', 'test:lib/');
605 Uri aFileUri = writeFile( 443 Uri aFileUri = writeFile(
606 '/test/bin/a.dart', 444 '/test/bin/a.dart',
607 r''' 445 r'''
608 import 'package:test/b.dart'; 446 import 'package:test/b.dart';
609 var a = b; 447 var a = b;
610 '''); 448 ''');
611 Uri bFileUri = writeFile('/test/lib/b.dart', 'var b = 1;'); 449 Uri bFileUri = writeFile('/test/lib/b.dart', 'var b = 1;');
612 Uri bPackageUri = Uri.parse('package:test/b.dart'); 450 Uri bPackageUri = Uri.parse('package:test/b.dart');
613 451
614 // Compute the initial state. 452 // Compute the initial state.
615 { 453 {
616 Program program = await getInitialState(aFileUri); 454 KernelResult result = await driver.getKernel(aFileUri);
617 Library library = _getLibrary(program, aFileUri); 455 Library library = _getLibrary(result, aFileUri);
618 expect( 456 expect(
619 _getLibraryText(library), 457 _getLibraryText(library),
620 r''' 458 r'''
621 library; 459 library;
622 import self as self; 460 import self as self;
623 import "dart:core" as core; 461 import "dart:core" as core;
624 import "package:test/b.dart" as b; 462 import "package:test/b.dart" as b;
625 463
626 static field core::int a = b::b; 464 static field core::int a = b::b;
627 '''); 465 ''');
628 } 466 }
629 467
630 // Update b.dart and use file URI to invalidate it. 468 // Update b.dart and use file URI to invalidate it.
631 // The delta is recomputed even though b.dart is used with the package URI. 469 // The delta is recomputed even though b.dart is used with the package URI.
632 writeFile('/test/lib/b.dart', 'var b = 1.2;'); 470 writeFile('/test/lib/b.dart', 'var b = 1.2;');
633 incrementalKernelGenerator.invalidate(bFileUri); 471 driver.invalidate(bFileUri);
634 { 472 {
635 DeltaProgram delta = await incrementalKernelGenerator.computeDelta(); 473 KernelResult result = await driver.getKernel(aFileUri);
636 Program program = delta.newProgram; 474 _assertLibraryUris(result, includes: [aFileUri, bPackageUri]);
637 _assertLibraryUris(program, includes: [aFileUri, bPackageUri]); 475 Library library = _getLibrary(result, aFileUri);
638 Library library = _getLibrary(program, aFileUri);
639 expect( 476 expect(
640 _getLibraryText(library), 477 _getLibraryText(library),
641 r''' 478 r'''
642 library; 479 library;
643 import self as self; 480 import self as self;
644 import "dart:core" as core; 481 import "dart:core" as core;
645 import "package:test/b.dart" as b; 482 import "package:test/b.dart" as b;
646 483
647 static field core::double a = b::b; 484 static field core::double a = b::b;
648 '''); 485 ''');
(...skipping 15 matching lines...) Expand all
664 '''); 501 ''');
665 Uri partUri = writeFile( 502 Uri partUri = writeFile(
666 partPath, 503 partPath,
667 r''' 504 r'''
668 part of foo; 505 part of foo;
669 var b = 2; 506 var b = 2;
670 var d = a; 507 var d = a;
671 '''); 508 ''');
672 509
673 // Check the initial state - types flow between the part and the library. 510 // Check the initial state - types flow between the part and the library.
674 Program program = await getInitialState(libUri); 511 KernelResult result = await driver.getKernel(libUri);
675 Library library = _getLibrary(program, libUri); 512 Library library = _getLibrary(result, libUri);
676 expect( 513 expect(
677 _getLibraryText(library), 514 _getLibraryText(library),
678 r''' 515 r'''
679 library foo; 516 library foo;
680 import self as self; 517 import self as self;
681 import "dart:core" as core; 518 import "dart:core" as core;
682 519
683 static field core::int a = 1; 520 static field core::int a = 1;
684 static field core::int c = self::b; 521 static field core::int c = self::b;
685 static field core::int b = 2 /* from file:///test/lib/bar.dart */; 522 static field core::int b = 2 /* from file:///test/lib/bar.dart */;
686 static field core::int d = self::a /* from file:///test/lib/bar.dart */; 523 static field core::int d = self::a /* from file:///test/lib/bar.dart */;
687 static method main() → void {} 524 static method main() → void {}
688 '''); 525 ''');
689 526
690 // Update [b] in the part, the type is changed in the part and library. 527 // Update [b] in the part, the type is changed in the part and library.
691 { 528 {
692 writeFile( 529 writeFile(
693 partPath, 530 partPath,
694 r''' 531 r'''
695 part of foo; 532 part of foo;
696 var b = 2.3; 533 var b = 2.3;
697 var d = a; 534 var d = a;
698 '''); 535 ''');
699 incrementalKernelGenerator.invalidate(partUri); 536 driver.invalidate(partUri);
700 DeltaProgram delta = await incrementalKernelGenerator.computeDelta(); 537 KernelResult result = await driver.getKernel(libUri);
701 Library library = _getLibrary(delta.newProgram, libUri); 538 Library library = _getLibrary(result, libUri);
702 expect( 539 expect(
703 _getLibraryText(library), 540 _getLibraryText(library),
704 r''' 541 r'''
705 library foo; 542 library foo;
706 import self as self; 543 import self as self;
707 import "dart:core" as core; 544 import "dart:core" as core;
708 545
709 static field core::int a = 1; 546 static field core::int a = 1;
710 static field core::double c = self::b; 547 static field core::double c = self::b;
711 static field core::double b = 2.3 /* from file:///test/lib/bar.dart */; 548 static field core::double b = 2.3 /* from file:///test/lib/bar.dart */;
712 static field core::int d = self::a /* from file:///test/lib/bar.dart */; 549 static field core::int d = self::a /* from file:///test/lib/bar.dart */;
713 static method main() → void {} 550 static method main() → void {}
714 '''); 551 ''');
715 } 552 }
716 553
717 // Update [a] in the library, the type is changed in the part and library. 554 // Update [a] in the library, the type is changed in the part and library.
718 { 555 {
719 writeFile( 556 writeFile(
720 libPath, 557 libPath,
721 r''' 558 r'''
722 library foo; 559 library foo;
723 part 'bar.dart'; 560 part 'bar.dart';
724 var a = 'aaa'; 561 var a = 'aaa';
725 var c = b; 562 var c = b;
726 void main() {} 563 void main() {}
727 '''); 564 ''');
728 incrementalKernelGenerator.invalidate(libUri); 565 driver.invalidate(libUri);
729 DeltaProgram delta = await incrementalKernelGenerator.computeDelta(); 566 KernelResult result = await driver.getKernel(libUri);
730 Library library = _getLibrary(delta.newProgram, libUri); 567 Library library = _getLibrary(result, libUri);
731 expect( 568 expect(
732 _getLibraryText(library), 569 _getLibraryText(library),
733 r''' 570 r'''
734 library foo; 571 library foo;
735 import self as self; 572 import self as self;
736 import "dart:core" as core; 573 import "dart:core" as core;
737 574
738 static field core::String a = "aaa"; 575 static field core::String a = "aaa";
739 static field core::double c = self::b; 576 static field core::double c = self::b;
740 static field core::double b = 2.3 /* from file:///test/lib/bar.dart */; 577 static field core::double b = 2.3 /* from file:///test/lib/bar.dart */;
(...skipping 10 matching lines...) Expand all
751 String cPath = '/test/lib/c.dart'; 588 String cPath = '/test/lib/c.dart';
752 Uri aUri = writeFile(aPath, ''); 589 Uri aUri = writeFile(aPath, '');
753 Uri bUri = writeFile(bPath, ''); 590 Uri bUri = writeFile(bPath, '');
754 Uri cUri = writeFile( 591 Uri cUri = writeFile(
755 cPath, 592 cPath,
756 r''' 593 r'''
757 import 'a.dart'; 594 import 'a.dart';
758 '''); 595 ''');
759 596
760 var usedFiles = <Uri>[]; 597 var usedFiles = <Uri>[];
761 var unusedFiles = <Uri>[]; 598 _createDriver(fileAddedFn: (Uri uri) {
762 watchFn = (Uri uri, bool used) { 599 usedFiles.add(uri);
763 if (used) {
764 usedFiles.add(uri);
765 } else {
766 unusedFiles.add(uri);
767 }
768 return new Future.value(); 600 return new Future.value();
769 }; 601 });
770 602
771 { 603 {
772 await getInitialState(cUri); 604 await driver.getKernel(cUri);
773 // We use at least c.dart and a.dart now. 605 // We use at least c.dart and a.dart now.
774 expect(usedFiles, contains(cUri)); 606 expect(usedFiles, contains(cUri));
775 expect(usedFiles, contains(aUri)); 607 expect(usedFiles, contains(aUri));
776 usedFiles.clear(); 608 usedFiles.clear();
777 expect(unusedFiles, isEmpty);
778 } 609 }
779 610
780 // Update c.dart to reference also b.dart file. 611 // Update c.dart to reference also b.dart file.
781 writeFile( 612 writeFile(
782 cPath, 613 cPath,
783 r''' 614 r'''
784 import 'a.dart'; 615 import 'a.dart';
785 import 'b.dart'; 616 import 'b.dart';
786 '''); 617 ''');
787 incrementalKernelGenerator.invalidate(cUri); 618 driver.invalidate(cUri);
788 { 619 {
789 await incrementalKernelGenerator.computeDelta(); 620 await driver.getKernel(cUri);
790 // The only new file is b.dart now. 621 // The only new file is b.dart now.
791 expect(usedFiles, [bUri]); 622 expect(usedFiles, [bUri]);
792 usedFiles.clear(); 623 usedFiles.clear();
793 expect(unusedFiles, isEmpty);
794 }
795
796 // Update c.dart to stop referencing b.dart file.
797 writeFile(
798 cPath,
799 r'''
800 import 'a.dart';
801 ''');
802 incrementalKernelGenerator.invalidate(cUri);
803 {
804 await incrementalKernelGenerator.computeDelta();
805 // No new used files.
806 expect(usedFiles, isEmpty);
807 // The file b.dart is not used anymore.
808 expect(unusedFiles, [bUri]);
809 unusedFiles.clear();
810 } 624 }
811 } 625 }
812 626
813 test_watch_null() async {
814 writeFile('/test/.packages', 'test:lib/');
815 String aPath = '/test/lib/a.dart';
816 String bPath = '/test/lib/b.dart';
817 writeFile(aPath, "");
818 Uri bUri = writeFile(bPath, "");
819
820 // Set null, as if the watch function is not provided.
821 watchFn = null;
822
823 await getInitialState(bUri);
824
825 // Update b.dart to import a.dart file.
826 writeFile(bPath, "import 'a.dart';");
827 incrementalKernelGenerator.invalidate(bUri);
828 await incrementalKernelGenerator.computeDelta();
829
830 // No exception even though the watcher function is null.
831 }
832
833 /// Write the given [text] of the file with the given [path] into the 627 /// Write the given [text] of the file with the given [path] into the
834 /// virtual filesystem. Return the URI of the file. 628 /// virtual filesystem. Return the URI of the file.
835 Uri writeFile(String path, String text) { 629 Uri writeFile(String path, String text) {
836 Uri uri = Uri.parse('file://$path'); 630 Uri uri = Uri.parse('file://$path');
837 fileSystem.entityForUri(uri).writeAsStringSync(text); 631 fileSystem.entityForUri(uri).writeAsStringSync(text);
838 return uri; 632 return uri;
839 } 633 }
840 634
841 /// Write the given file contents to the virtual filesystem. 635 List<Library> _allLibraries(KernelResult result) {
842 void writeFiles(Map<String, String> contents) { 636 return result.results
843 contents.forEach(writeFile); 637 .map((cycle) => cycle.kernelLibraries)
638 .expand((libraries) => libraries)
639 .toList();
844 } 640 }
845 641
846 void _assertCompiledUris(Iterable<Uri> expected) { 642 void _assertCompiledUris(Iterable<Uri> expected) {
847 var compiledCycles = 643 var compiledCycles = driver.test.compiledCycles;
848 incrementalKernelGenerator.test.driver.test.compiledCycles;
849 Set<Uri> compiledUris = compiledCycles 644 Set<Uri> compiledUris = compiledCycles
850 .map((cycle) => cycle.libraries.map((file) => file.uri)) 645 .map((cycle) => cycle.libraries.map((file) => file.uri))
851 .expand((uris) => uris) 646 .expand((uris) => uris)
852 .toSet(); 647 .toSet();
853 expect(compiledUris, unorderedEquals(expected)); 648 expect(compiledUris, unorderedEquals(expected));
854 } 649 }
855 650
856 void _assertLibraryUris(Program program, 651 void _assertLibraryUris(KernelResult result,
857 {List<Uri> includes: const [], List<Uri> excludes: const []}) { 652 {List<Uri> includes: const [], List<Uri> excludes: const []}) {
858 List<Uri> libraryUris = 653 List<Uri> libraryUris = result.results
859 program.libraries.map((library) => library.importUri).toList(); 654 .map((cycle) => cycle.kernelLibraries.map((lib) => lib.importUri))
655 .expand((uris) => uris)
656 .toList();
860 for (var shouldInclude in includes) { 657 for (var shouldInclude in includes) {
861 expect(libraryUris, contains(shouldInclude)); 658 expect(libraryUris, contains(shouldInclude));
862 } 659 }
863 for (var shouldExclude in excludes) { 660 for (var shouldExclude in excludes) {
864 expect(libraryUris, isNot(contains(shouldExclude))); 661 expect(libraryUris, isNot(contains(shouldExclude)));
865 } 662 }
866 } 663 }
867 664
868 Library _getLibrary(Program program, Uri uri) { 665 /// Create new [KernelDriver] instance and put it into the [driver] field.
666 void _createDriver(
667 {Map<String, Uri> packages, KernelDriverFileAddedFn fileAddedFn}) {
668 Map<String, Uri> dartLibraries = createSdkFiles(fileSystem);
669 var uriTranslator = new UriTranslatorImpl(dartLibraries, {}, packages);
670 driver = new KernelDriver(new PerformanceLog(null), fileSystem,
671 new MemoryByteStore(), uriTranslator, true,
672 fileAddedFn: fileAddedFn);
673 }
674
675 Library _getLibrary(KernelResult result, Uri uri) {
676 for (var cycleResult in result.results) {
677 for (var library in cycleResult.kernelLibraries) {
678 if (library.importUri == uri) return library;
679 }
680 }
681 throw fail('No library found with URI "$uri"');
682 }
683
684 Library _getLibraryFromProgram(Program program, Uri uri) {
869 for (var library in program.libraries) { 685 for (var library in program.libraries) {
870 if (library.importUri == uri) return library; 686 if (library.importUri == uri) return library;
871 } 687 }
872 throw fail('No library found with URI "$uri"'); 688 throw fail('No library found with URI "$uri"');
873 } 689 }
874 690
875 String _getLibraryText(Library library) { 691 String _getLibraryText(Library library) {
876 StringBuffer buffer = new StringBuffer(); 692 StringBuffer buffer = new StringBuffer();
877 new Printer(buffer, syntheticNames: new NameSystem()) 693 new Printer(buffer, syntheticNames: new NameSystem())
878 .writeLibraryFile(library); 694 .writeLibraryFile(library);
879 return buffer.toString(); 695 return buffer.toString();
880 } 696 }
697
698 /// Return the [Uri] for the given Posix [path].
699 static Uri _folderUri(String path) {
700 if (!path.endsWith('/')) path += '/';
701 return Uri.parse('file://$path');
702 }
881 } 703 }
OLDNEW
« 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