OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 |
| 5 library test.kernel.closures.suite; |
| 6 |
| 7 import 'dart:async' show Future; |
| 8 |
| 9 import 'dart:io' show File; |
| 10 |
| 11 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; |
| 12 |
| 13 import 'package:analyzer/src/kernel/loader.dart' show DartLoader; |
| 14 |
| 15 import 'package:testing/testing.dart' |
| 16 show Chain, Result, Step, TestDescription, runMe; |
| 17 |
| 18 import 'package:kernel/ast.dart' show Program, Library; |
| 19 |
| 20 import 'package:kernel/target/targets.dart' show Target, TargetFlags, getTarget; |
| 21 |
| 22 import 'package:front_end/src/fasta/testing/kernel_chain.dart'; |
| 23 |
| 24 import 'package:front_end/src/fasta/ticker.dart' show Ticker; |
| 25 |
| 26 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; |
| 27 |
| 28 import 'package:front_end/src/fasta/kernel/kernel_target.dart' |
| 29 show KernelTarget; |
| 30 |
| 31 import 'package:front_end/src/fasta/translate_uri.dart' show TranslateUri; |
| 32 |
| 33 import 'package:front_end/src/fasta/errors.dart' show InputError; |
| 34 |
| 35 import 'package:kernel/interpreter/interpreter.dart'; |
| 36 |
| 37 class InterpreterContext extends TestContext { |
| 38 final TranslateUri uriTranslator; |
| 39 |
| 40 final List<Step> steps; |
| 41 |
| 42 Future<Program> platform; |
| 43 |
| 44 InterpreterContext(Uri sdk, Uri vm, Uri packages, bool strongMode, |
| 45 DartSdk dartSdk, this.uriTranslator) |
| 46 : steps = <Step>[ |
| 47 const FastaCompile(), |
| 48 const Interpret(), |
| 49 const MatchLogExpectation(".expect"), |
| 50 ], |
| 51 super(sdk, vm, packages, strongMode, dartSdk); |
| 52 |
| 53 Future<Program> createPlatform() { |
| 54 return new Future<Program>(() async { |
| 55 DartLoader loader = await createLoader(); |
| 56 Target target = |
| 57 getTarget("vm", new TargetFlags(strongMode: options.strongMode)); |
| 58 loader.loadProgram(Uri.base.resolve("pkg/fasta/test/platform.dart"), |
| 59 target: target); |
| 60 var program = loader.program; |
| 61 if (loader.errors.isNotEmpty) { |
| 62 throw loader.errors.join("\n"); |
| 63 } |
| 64 Library mainLibrary = program.mainMethod.enclosingLibrary; |
| 65 program.uriToSource.remove(mainLibrary.fileUri); |
| 66 program = new Program( |
| 67 program.libraries.where((Library l) => l != mainLibrary).toList(), |
| 68 program.uriToSource); |
| 69 target.performModularTransformations(program); |
| 70 target.performGlobalTransformations(program); |
| 71 return program; |
| 72 }); |
| 73 } |
| 74 |
| 75 static Future<InterpreterContext> create( |
| 76 Chain suite, Map<String, String> environment) async { |
| 77 return TestContext.create(suite, environment, (Chain suite, |
| 78 Map<String, String> environment, |
| 79 Uri sdk, |
| 80 Uri vm, |
| 81 Uri packages, |
| 82 bool strongMode, |
| 83 DartSdk dartSdk, |
| 84 bool updateExpectations) async { |
| 85 TranslateUri uriTranslator = await TranslateUri.parse(packages); |
| 86 return new InterpreterContext( |
| 87 sdk, vm, packages, strongMode, dartSdk, uriTranslator); |
| 88 }); |
| 89 } |
| 90 } |
| 91 |
| 92 class FastaCompile extends Step<TestDescription, Program, InterpreterContext> { |
| 93 const FastaCompile(); |
| 94 |
| 95 String get name => "fasta compile"; |
| 96 |
| 97 Future<Result<Program>> run( |
| 98 TestDescription description, InterpreterContext context) async { |
| 99 Program platform = await context.createPlatform(); |
| 100 Ticker ticker = new Ticker(); |
| 101 DillTarget dillTarget = new DillTarget(ticker, context.uriTranslator); |
| 102 dillTarget.loader |
| 103 ..input = Uri.parse("org.dartlang:platform") |
| 104 ..setProgram(platform); |
| 105 KernelTarget sourceTarget = |
| 106 new KernelTarget(dillTarget, context.uriTranslator, false); |
| 107 |
| 108 Program p; |
| 109 try { |
| 110 sourceTarget.read(description.uri); |
| 111 await dillTarget.writeOutline(null); |
| 112 await sourceTarget.writeOutline(null); |
| 113 p = await sourceTarget.writeProgram(null); |
| 114 } on InputError catch (e, s) { |
| 115 return fail(null, e.error, s); |
| 116 } |
| 117 |
| 118 return pass(p); |
| 119 } |
| 120 } |
| 121 |
| 122 class Interpret extends Step<Program, EvaluationLog, InterpreterContext> { |
| 123 const Interpret(); |
| 124 |
| 125 String get name => "interpret"; |
| 126 |
| 127 Future<Result<EvaluationLog>> run(Program program, _) async { |
| 128 Library library = program.libraries |
| 129 .firstWhere((Library library) => library.importUri.scheme != "dart"); |
| 130 Uri uri = library.importUri; |
| 131 |
| 132 StringBuffer buffer = new StringBuffer(); |
| 133 log.onRecord.listen((LogRecord rec) => buffer.write(rec.message)); |
| 134 try { |
| 135 new Interpreter(program).run(); |
| 136 } catch (e, s) { |
| 137 return crash(e, s); |
| 138 } |
| 139 |
| 140 return pass(new EvaluationLog(uri, "$buffer")); |
| 141 } |
| 142 } |
| 143 |
| 144 class MatchLogExpectation extends Step<EvaluationLog, int, InterpreterContext> { |
| 145 final String suffix; |
| 146 |
| 147 String get name => "match log expectation"; |
| 148 |
| 149 const MatchLogExpectation(this.suffix); |
| 150 |
| 151 Future<Result<int>> run(EvaluationLog result, _) async { |
| 152 Uri uri = result.uri; |
| 153 |
| 154 File expectedFile = new File("${uri.toFilePath()}$suffix"); |
| 155 if (await expectedFile.exists()) { |
| 156 String expected = await expectedFile.readAsString(); |
| 157 if (expected.trim() != result.log.trim()) { |
| 158 String diff = await runDiff(expectedFile.uri, result.log); |
| 159 return fail(null, "$uri doesn't match ${expectedFile.uri}\n$diff"); |
| 160 } else { |
| 161 return pass(0); |
| 162 } |
| 163 } |
| 164 return fail( |
| 165 null, |
| 166 """Please create file ${expectedFile.path} with this content: |
| 167 ${result.log}"""); |
| 168 } |
| 169 } |
| 170 |
| 171 class EvaluationLog { |
| 172 /// Evaluated program uri. |
| 173 final Uri uri; |
| 174 |
| 175 /// Evaluated program log. |
| 176 final String log; |
| 177 |
| 178 EvaluationLog(this.uri, this.log); |
| 179 } |
| 180 |
| 181 main(List<String> arguments) => |
| 182 runMe(arguments, InterpreterContext.create, "testing.json"); |
OLD | NEW |