OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:io'; | 4 import 'dart:io'; |
5 | 5 |
6 import 'package:analyzer/src/kernel/loader.dart'; | 6 import 'package:analyzer/src/kernel/loader.dart'; |
7 import 'package:kernel/application_root.dart'; | 7 import 'package:kernel/application_root.dart'; |
| 8 import 'package:kernel/core_types.dart'; |
8 import 'package:kernel/kernel.dart'; | 9 import 'package:kernel/kernel.dart'; |
9 import 'package:kernel/target/targets.dart'; | 10 import 'package:kernel/target/targets.dart'; |
10 import 'package:kernel/text/ast_to_text.dart'; | 11 import 'package:kernel/text/ast_to_text.dart'; |
11 import 'package:kernel/verifier.dart'; | 12 import 'package:kernel/verifier.dart'; |
12 import 'package:path/path.dart' as pathlib; | 13 import 'package:path/path.dart' as pathlib; |
13 import 'package:test/test.dart'; | 14 import 'package:test/test.dart'; |
14 | 15 |
15 final String testcaseDirectory = 'pkg/kernel/testcases'; | 16 final String testcaseDirectory = 'pkg/kernel/testcases'; |
16 final String inputDirectory = 'pkg/kernel/testcases/input'; | 17 final String inputDirectory = 'pkg/kernel/testcases/input'; |
17 final String sdkDirectory = 'sdk'; | 18 final String sdkDirectory = 'sdk'; |
18 | 19 |
19 /// A target to be used for testing. | 20 /// A target to be used for testing. |
20 /// | 21 /// |
21 /// To simplify testing dependencies, we avoid transformations that rely on | 22 /// To simplify testing dependencies, we avoid transformations that rely on |
22 /// a patched SDK or any SDK changes that have not landed in the main SDK. | 23 /// a patched SDK or any SDK changes that have not landed in the main SDK. |
23 abstract class TestTarget extends Target { | 24 abstract class TestTarget extends Target { |
24 /// Annotations to apply on the textual output. | 25 /// Annotations to apply on the textual output. |
25 Annotator get annotator => null; | 26 Annotator get annotator => null; |
26 | 27 |
27 // Return a list of strings so that we can accumulate errors. | 28 // Return a list of strings so that we can accumulate errors. |
28 List<String> performModularTransformations(Program program); | 29 List<String> performModularTransformations( |
29 List<String> performGlobalTransformations(Program program); | 30 CoreTypes coreTypes, Program program); |
| 31 List<String> performGlobalTransformations( |
| 32 CoreTypes coreTypes, Program program); |
30 } | 33 } |
31 | 34 |
32 void runBaselineTests(String folderName, TestTarget target) { | 35 void runBaselineTests(String folderName, TestTarget target) { |
33 String outputDirectory = '$testcaseDirectory/$folderName'; | 36 String outputDirectory = '$testcaseDirectory/$folderName'; |
34 var batch = new DartLoaderBatch(); | 37 var batch = new DartLoaderBatch(); |
35 Directory directory = new Directory(inputDirectory); | 38 Directory directory = new Directory(inputDirectory); |
36 var applicationRoot = new ApplicationRoot(directory.absolute.path); | 39 var applicationRoot = new ApplicationRoot(directory.absolute.path); |
37 for (FileSystemEntity file in directory.listSync()) { | 40 for (FileSystemEntity file in directory.listSync()) { |
38 if (file is File && file.path.endsWith('.dart')) { | 41 if (file is File && file.path.endsWith('.dart')) { |
39 String name = pathlib.basename(file.path); | 42 String name = pathlib.basename(file.path); |
40 test(name, () async { | 43 test(name, () async { |
41 Uri dartPath = | 44 Uri dartPath = |
42 new Uri(scheme: 'file', path: pathlib.absolute(file.path)); | 45 new Uri(scheme: 'file', path: pathlib.absolute(file.path)); |
43 String shortName = pathlib.withoutExtension(name); | 46 String shortName = pathlib.withoutExtension(name); |
44 String filenameOfBaseline = '$outputDirectory/$shortName.baseline.txt'; | 47 String filenameOfBaseline = '$outputDirectory/$shortName.baseline.txt'; |
45 String filenameOfCurrent = '$outputDirectory/$shortName.current.txt'; | 48 String filenameOfCurrent = '$outputDirectory/$shortName.current.txt'; |
46 | 49 |
47 var program = new Program(); | 50 var program = new Program(); |
| 51 var coreTypes = new CoreTypes(program); |
48 var loader = await batch.getLoader( | 52 var loader = await batch.getLoader( |
49 program, | 53 program, |
50 new DartOptions( | 54 new DartOptions( |
51 strongMode: target.strongMode, | 55 strongMode: target.strongMode, |
52 sdk: sdkDirectory, | 56 sdk: sdkDirectory, |
53 declaredVariables: target.extraDeclaredVariables, | 57 declaredVariables: target.extraDeclaredVariables, |
54 applicationRoot: applicationRoot)); | 58 applicationRoot: applicationRoot)); |
55 loader.loadProgram(dartPath, target: target); | 59 loader.loadProgram(dartPath, target: target); |
56 verifyProgram(program); | 60 verifyProgram(program); |
57 var errors = <String>[]; | 61 var errors = <String>[]; |
58 errors.addAll(target.performModularTransformations(program)); | 62 errors.addAll(target.performModularTransformations(coreTypes, program)); |
59 verifyProgram(program); | 63 verifyProgram(program); |
60 errors.addAll(target.performGlobalTransformations(program)); | 64 errors.addAll(target.performGlobalTransformations(coreTypes, program)); |
61 verifyProgram(program); | 65 verifyProgram(program); |
62 | 66 |
63 var buffer = new StringBuffer(); | 67 var buffer = new StringBuffer(); |
64 for (var error in errors) { | 68 for (var error in errors) { |
65 buffer.writeln('// $error'); | 69 buffer.writeln('// $error'); |
66 } | 70 } |
67 new Printer(buffer, annotator: target.annotator) | 71 new Printer(buffer, annotator: target.annotator) |
68 .writeLibraryFile(program.mainMethod.enclosingLibrary); | 72 .writeLibraryFile(program.mainMethod.enclosingLibrary); |
69 String current = '$buffer'; | 73 String current = '$buffer'; |
70 new File(filenameOfCurrent).writeAsStringSync(current); | 74 new File(filenameOfCurrent).writeAsStringSync(current); |
71 | 75 |
72 var baselineFile = new File(filenameOfBaseline); | 76 var baselineFile = new File(filenameOfBaseline); |
73 if (!baselineFile.existsSync()) { | 77 if (!baselineFile.existsSync()) { |
74 new File(filenameOfBaseline).writeAsStringSync(current); | 78 new File(filenameOfBaseline).writeAsStringSync(current); |
75 } else { | 79 } else { |
76 var baseline = baselineFile.readAsStringSync(); | 80 var baseline = baselineFile.readAsStringSync(); |
77 if (baseline != current) { | 81 if (baseline != current) { |
78 fail('Output of `$name` changed for $folderName.\n' | 82 fail('Output of `$name` changed for $folderName.\n' |
79 'Command to reset the baseline:\n' | 83 'Command to reset the baseline:\n' |
80 ' rm $filenameOfBaseline\n' | 84 ' rm $filenameOfBaseline\n' |
81 'Command to see the diff:\n' | 85 'Command to see the diff:\n' |
82 ' diff -cd $outputDirectory/$shortName.{baseline,current}.txt' | 86 ' diff -cd $outputDirectory/$shortName.{baseline,current}.txt' |
83 '\n'); | 87 '\n'); |
84 } | 88 } |
85 } | 89 } |
86 }); | 90 }); |
87 } | 91 } |
88 } | 92 } |
89 } | 93 } |
OLD | NEW |