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