4f2bdff90b
Change-Id: I1a3cc03fba9783807fa637a9d42fdbad68ee7686 Reviewed-on: https://dart-review.googlesource.com/31040 Commit-Queue: Peter von der Ahé <ahe@google.com> Reviewed-by: Kevin Millikin <kmillikin@google.com>
105 lines
2.9 KiB
Dart
105 lines
2.9 KiB
Dart
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE.md file.
|
|
|
|
library test.kernel.closures.suite;
|
|
|
|
import 'dart:async' show Future;
|
|
|
|
import 'dart:io' show File;
|
|
|
|
import 'package:testing/testing.dart'
|
|
show Chain, ChainContext, Result, Step, runMe;
|
|
|
|
import 'package:kernel/ast.dart' show Component, Library;
|
|
|
|
import 'package:kernel/target/targets.dart' show Target;
|
|
|
|
import 'package:front_end/src/fasta/testing/kernel_chain.dart'
|
|
show runDiff, Compile, CompileContext;
|
|
|
|
import 'package:kernel/interpreter/interpreter.dart';
|
|
|
|
const String STRONG_MODE = " strong mode ";
|
|
|
|
class InterpreterContext extends ChainContext implements CompileContext {
|
|
final bool strongMode;
|
|
Target get target => null;
|
|
|
|
final List<Step> steps;
|
|
|
|
InterpreterContext(this.strongMode)
|
|
: steps = <Step>[
|
|
const Compile(),
|
|
const Interpret(),
|
|
const MatchLogExpectation(".expect"),
|
|
];
|
|
|
|
static Future<InterpreterContext> create(
|
|
Chain suite, Map<String, String> environment) async {
|
|
bool strongMode = environment.containsKey(STRONG_MODE);
|
|
return new InterpreterContext(strongMode);
|
|
}
|
|
}
|
|
|
|
class Interpret extends Step<Component, EvaluationLog, InterpreterContext> {
|
|
const Interpret();
|
|
|
|
String get name => "interpret";
|
|
|
|
Future<Result<EvaluationLog>> run(Component component, _) async {
|
|
Library library = component.libraries
|
|
.firstWhere((Library library) => library.importUri.scheme != "dart");
|
|
Uri uri = library.importUri;
|
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
log.onRecord.listen((LogRecord rec) => buffer.write(rec.message));
|
|
try {
|
|
new Interpreter(component).run();
|
|
} catch (e, s) {
|
|
return crash(e, s);
|
|
}
|
|
|
|
return pass(new EvaluationLog(uri, "$buffer"));
|
|
}
|
|
}
|
|
|
|
class MatchLogExpectation extends Step<EvaluationLog, int, InterpreterContext> {
|
|
final String suffix;
|
|
|
|
String get name => "match log expectation";
|
|
|
|
const MatchLogExpectation(this.suffix);
|
|
|
|
Future<Result<int>> run(EvaluationLog result, _) async {
|
|
Uri uri = result.uri;
|
|
|
|
File expectedFile = new File("${uri.toFilePath()}$suffix");
|
|
if (await expectedFile.exists()) {
|
|
String expected = await expectedFile.readAsString();
|
|
if (expected.trim() != result.log.trim()) {
|
|
String diff = await runDiff(expectedFile.uri, result.log);
|
|
return fail(null, "$uri doesn't match ${expectedFile.uri}\n$diff");
|
|
} else {
|
|
return pass(0);
|
|
}
|
|
}
|
|
return fail(
|
|
null, """Please create file ${expectedFile.path} with this content:
|
|
${result.log}""");
|
|
}
|
|
}
|
|
|
|
class EvaluationLog {
|
|
/// Evaluated program uri.
|
|
final Uri uri;
|
|
|
|
/// Evaluated program log.
|
|
final String log;
|
|
|
|
EvaluationLog(this.uri, this.log);
|
|
}
|
|
|
|
main(List<String> arguments) =>
|
|
runMe(arguments, InterpreterContext.create, "testing.json");
|