Files
sdk/pkg/front_end/test/messages_json_test.dart
T
Jens Johansen 93f4ac0a83 [cfe] Actually have both ansi and plain text formatted messages (2nd try)
The CFEs FormattedMessage always had two getters to get the text inside
one that would supposedly give an ansi formated version of the message
and one that would supposedly give a plaintext formated version of the
message. They both returned the same string, though, which would either
be with ansi escape codes or plain text depending on the environment at
compile time.

This CL fixes that by having both messages, and letting the reporting
(i.e. whenever the message is read) decide which to use. That way we
can - for instance - report errors with color if the terminal supports
it correctly when reusing a dill (and reissuing problems, but where the
terminal support changes) and if printing the problem to an html <pre>
field (like observatory does).

It also cleans up two different implementations of whether we think
the terminal supports colors or not, by deleting one of them.

This is the second try. Patchset #1 is the original.
Patchset #2(and possibly beyond) is the changes.

TEST=Existing test suites.

Change-Id: I8e483049ce81ce1bd8e5396b588a31e0ad3a8630
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/187402
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
2021-02-26 13:06:17 +00:00

110 lines
3.4 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.
// @dart = 2.9
import 'package:_fe_analyzer_shared/src/messages/diagnostic_message.dart'
show DiagnosticMessage, getMessageUri;
import 'package:_fe_analyzer_shared/src/messages/severity.dart' show Severity;
import 'package:front_end/src/fasta/fasta_codes.dart'
show
Code,
DiagnosticMessageFromJson,
FormattedMessage,
LocatedMessage,
Message;
/// Test that turning a message into json and back again retains the wanted
/// information.
main() {
for (int i = 0; i < Severity.values.length; i++) {
Severity severity = Severity.values[i];
Code code = new Code("MyCodeName");
Message message = new Message(code, message: '');
LocatedMessage locatedMessage1 =
new LocatedMessage(Uri.parse("what:ever/fun_1.dart"), 117, 2, message);
FormattedMessage formattedMessage2 = new FormattedMessage(
null,
"Formatted string Plain #2",
"Formatted string Colorized #2",
13,
2,
Severity.error, []);
FormattedMessage formattedMessage3 = new FormattedMessage(
null,
"Formatted string Plain #3",
"Formatted string Colorized #3",
313,
32,
Severity.error, []);
FormattedMessage formattedMessage1 = new FormattedMessage(
locatedMessage1,
"Formatted string Plain",
"Formatted string Colorized",
42,
86,
severity, [
formattedMessage2,
formattedMessage3
],
involvedFiles: [
Uri.parse("what:ever/foo.dart"),
Uri.parse("what:ever/bar.dart")
]);
expect(formattedMessage1.codeName, "MyCodeName");
DiagnosticMessageFromJson diagnosticMessageFromJson =
new DiagnosticMessageFromJson.fromJson(
formattedMessage1.toJsonString());
compareMessages(formattedMessage1, diagnosticMessageFromJson);
DiagnosticMessageFromJson diagnosticMessageFromJson2 =
new DiagnosticMessageFromJson.fromJson(
diagnosticMessageFromJson.toJsonString());
compareMessages(diagnosticMessageFromJson, diagnosticMessageFromJson2);
expect(diagnosticMessageFromJson2.toJsonString(),
formattedMessage1.toJsonString());
}
}
void compareMessages(DiagnosticMessage a, DiagnosticMessage b) {
List<String> list1 = a.ansiFormatted.toList();
List<String> list2 = b.ansiFormatted.toList();
expect(list1.length, list2.length);
for (int i = 0; i < list1.length; i++) {
expect(list1[i], list2[i]);
}
list1 = a.plainTextFormatted.toList();
list2 = b.plainTextFormatted.toList();
expect(list1.length, list2.length);
for (int i = 0; i < list1.length; i++) {
expect(list1[i], list2[i]);
}
expect(a.severity, b.severity);
expect(getMessageUri(a), getMessageUri(b));
List<Uri> uriList1 = a.involvedFiles?.toList();
List<Uri> uriList2 = b.involvedFiles?.toList();
expect(uriList1?.length, uriList2?.length);
if (uriList1 != null) {
for (int i = 0; i < uriList1.length; i++) {
expect(uriList1[i], uriList2[i]);
}
}
String string1 = a.codeName;
String string2 = b.codeName;
expect(string1, string2);
}
void expect(Object actual, Object expect) {
if (expect != actual) throw "Expected $expect got $actual";
}