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