blob: 420d9fd97a22fd7bedb7d13ca43259bb01b64f64 [file] [log] [blame]
// Copyright (c) 2019, 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 file.
import 'dart:async' show Future;
import 'dart:convert' show jsonDecode;
import 'dart:io' show File;
import 'dart:typed_data' show Uint8List;
import 'package:front_end/src/fasta/parser.dart' show Parser;
import 'package:front_end/src/fasta/scanner.dart';
import 'package:front_end/src/fasta/scanner/utf8_bytes_scanner.dart'
show Utf8BytesScanner;
import 'package:front_end/src/scanner/token.dart' show Token;
import 'package:testing/testing.dart'
show
Chain,
ChainContext,
ExpectationSet,
Result,
Step,
TestDescription,
runMe;
import 'utils/kernel_chain.dart' show MatchContext;
import 'parser_test_listener.dart' show ParserTestListener;
import 'parser_test_parser.dart' show TestParser;
const String EXPECTATIONS = '''
[
{
"name": "ExpectationFileMismatch",
"group": "Fail"
},
{
"name": "ExpectationFileMissing",
"group": "Fail"
}
]
''';
main([List<String> arguments = const []]) =>
runMe(arguments, createContext, configurationPath: "../testing.json");
Future<Context> createContext(
Chain suite, Map<String, String> environment) async {
return new Context(environment["updateExpectations"] == "true",
environment["trace"] == "true");
}
ScannerConfiguration scannerConfiguration = new ScannerConfiguration(
enableTripleShift: true,
enableExtensionMethods: true,
enableNonNullable: true);
class Context extends ChainContext with MatchContext {
final bool updateExpectations;
final bool addTrace;
Context(this.updateExpectations, this.addTrace);
final List<Step> steps = const <Step>[
const ListenerStep(),
const IntertwinedStep(),
];
final ExpectationSet expectationSet =
new ExpectationSet.fromJsonList(jsonDecode(EXPECTATIONS));
}
class ListenerStep extends Step<TestDescription, TestDescription, Context> {
const ListenerStep();
String get name => "listener";
Future<Result<TestDescription>> run(
TestDescription description, Context context) async {
File f = new File.fromUri(description.uri);
List<int> rawBytes = f.readAsBytesSync();
Uint8List bytes = new Uint8List(rawBytes.length + 1);
bytes.setRange(0, rawBytes.length, rawBytes);
Utf8BytesScanner scanner = new Utf8BytesScanner(bytes,
includeComments: true, configuration: scannerConfiguration);
Token firstToken = scanner.tokenize();
if (firstToken == null) {
return crash(description, StackTrace.current);
}
ParserTestListener parserTestListener =
new ParserTestListener(context.addTrace);
Parser parser = new Parser(parserTestListener);
parser.parseUnit(firstToken);
return context.match<TestDescription>(
".expect", "${parserTestListener.sb}", description.uri, description);
}
}
class IntertwinedStep extends Step<TestDescription, TestDescription, Context> {
const IntertwinedStep();
String get name => "intertwined";
Future<Result<TestDescription>> run(
TestDescription description, Context context) async {
File f = new File.fromUri(description.uri);
List<int> rawBytes = f.readAsBytesSync();
Uint8List bytes = new Uint8List(rawBytes.length + 1);
bytes.setRange(0, rawBytes.length, rawBytes);
Utf8BytesScanner scanner = new Utf8BytesScanner(bytes,
includeComments: true, configuration: scannerConfiguration);
Token firstToken = scanner.tokenize();
if (firstToken == null) {
return crash(description, StackTrace.current);
}
ParserTestListener2 parserTestListener =
new ParserTestListener2(context.addTrace);
TestParser parser = new TestParser(parserTestListener, context.addTrace);
parserTestListener.parser = parser;
parser.sb = parserTestListener.sb;
parser.parseUnit(firstToken);
return context.match<TestDescription>(
".intertwined.expect", "${parser.sb}", description.uri, description);
}
}
class ParserTestListener2 extends ParserTestListener {
TestParser parser;
ParserTestListener2(bool trace) : super(trace);
void doPrint(String s) {
int prevIndent = super.indent;
super.indent = parser.indent;
super.doPrint("listener: " + s);
super.indent = prevIndent;
}
}