dart-sdk/pkg/test_runner/test/utils.dart
Robert Nystrom fdb6ca6d01 Add support for context messages to static error tests.
Currently only CFE ("Fasta") tests have their context message output
parsed. It should be easy to extend that to dart2js and DDC if that's
useful. Analyzer might be more work.

This also adds support to the test updater for inserting context
messages when updating tests. By default, that flag is off, so the
existing behavior is preserved where context messages are ignnored. If
you want them, pass "-c" when updating a test.

When validating test output, if the test file contains context messages,
then they are validated. Otherwise, any context messages in the CFE
output are ignored. This way existing tests still pass.


Change StaticError to represent a single error for a single front end.

Before, the data model collapsed errors for different front-ends at the
same location into a single StaticError object which tracked different
messages for each front end. The idea was to move towards a world where
they really are the "same" error with eventually the same message.

But this adds a lot of complexity with things like merging errors and
doesn't reflect the reality that each error from each front end is
basically its own thing. Also, critically, it makes it much harder to
attach context messages to a specific front end's error object.

This changes it so that an instance of StaticError represents a single
error for a single front end. The test file syntax is unchanged and the
updated tool behaves the same. In a static error test, multiple
expectations can still share the same "//   ^^^" marker line. They are
just expanded to multiple StaticError objects at parse time.

This eliminates all of the complexity around merging and simplifying
errors.

Change-Id: Ida1736bfcde436fc2d1ce2963d91fa9cb154afa8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193281
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2021-03-30 18:41:21 +00:00

79 lines
2.6 KiB
Dart

// 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 'package:test_runner/src/configuration.dart';
import 'package:test_runner/src/options.dart';
import 'package:test_runner/src/path.dart';
import 'package:test_runner/src/static_error.dart';
import 'package:test_runner/src/test_file.dart';
import 'package:test_runner/src/test_suite.dart';
TestFile parseTestFile(String source,
{String path = "some_test.dart", String suite = "language"}) {
final suiteDirectory = Path(suite);
path = suiteDirectory.absolute.append(path).toNativePath();
return TestFile.parse(suiteDirectory.absolute, path, source);
}
// TODO(rnystrom): Would be nice if there was a simpler way to create a
// configuration for use in unit tests.
TestConfiguration makeConfiguration(List<String> arguments, String suite) {
return OptionsParser().parse([...arguments, suite]).first;
}
/// Creates a [StandardTestSuite] hardcoded to contain [testFiles].
StandardTestSuite makeTestSuite(TestConfiguration configuration,
List<TestFile> testFiles, String suite) =>
_MockTestSuite(configuration, testFiles, suite);
/// Creates a [StaticError].
///
/// Only one of [analyzerError], [cfeError], [webError], or [contextError] may
/// be passed.
StaticError makeError(
{int line = 1,
int column = 2,
int length,
String analyzerError,
String cfeError,
String webError,
String contextError,
List<StaticError> context}) {
ErrorSource source;
String message;
if (analyzerError != null) {
assert(cfeError == null);
assert(webError == null);
assert(contextError == null);
source = ErrorSource.analyzer;
message = analyzerError;
} else if (cfeError != null) {
assert(webError == null);
assert(contextError == null);
source = ErrorSource.cfe;
message = cfeError;
} else if (webError != null) {
assert(contextError == null);
source = ErrorSource.web;
message = webError;
} else {
assert(contextError != null);
source = ErrorSource.context;
message = contextError;
}
var error =
StaticError(source, message, line: line, column: column, length: length);
if (context != null) error.contextMessages.addAll(context);
return error;
}
class _MockTestSuite extends StandardTestSuite {
final List<TestFile> _testFiles;
_MockTestSuite(TestConfiguration configuration, this._testFiles, String suite)
: super(configuration, suite, Path(suite), []);
@override
Iterable<TestFile> findTests() => _testFiles;
}