Files
sdk/pkg/kernel/test/text_util_test.dart
T
Jens Johansen 430abc54e8 [CFE] Add toText to Reference and CanonicalName
Add `toText` to both Reference and CanonicalName.
Change how CanonicalNames are printed through `text_util.dart` to be
more aligned with how the corresponding node would be printed had
it existed (and the CanonicalName thus normally not been used for
printing).
Add a test for (some definition of) common cases where printing should
be the same wherther done though the node, the reference or the
canonical name.
Fix usage of toStringInternal in the constant evaluator (though only
in use through an experiment) and add a test that showed that the
previous output was bad and the new isn't (at least in this case).

Change-Id: I10cbc1a542c7d8b079e0510bbd5eb5173b2e7563
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/242102
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2022-04-25 11:13:44 +00:00

63 lines
2.1 KiB
Dart

// Copyright (c) 2022, 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:kernel/ast.dart';
import 'package:kernel/src/printer.dart';
import "reference_name_test.dart" show createComponent;
void main() {
testQualifiedCanonicalNameToString();
}
void testQualifiedCanonicalNameToString() {
Component component = createComponent();
component.computeCanonicalNames();
NamedNodeCollector namedNodeCollector = new NamedNodeCollector();
component.accept(namedNodeCollector);
bool foundMismatch = false;
for (NamedNode namedNode in namedNodeCollector.namedNodes) {
for (AstTextStrategy strategy in [
const AstTextStrategy(
includeLibraryNamesInMembers: false,
includeLibraryNamesInTypes: false),
const AstTextStrategy(includeLibraryNamesInMembers: true),
const AstTextStrategy(includeLibraryNamesInTypes: true),
const AstTextStrategy(
includeLibraryNamesInMembers: true, includeLibraryNamesInTypes: true),
]) {
String throughNode = namedNode.toText(strategy);
String throughReference = namedNode.reference.toText(strategy);
String throughCanonicalName =
namedNode.reference.canonicalName!.toText(strategy);
if (throughNode == throughReference &&
throughReference == throughCanonicalName) continue;
print("${namedNode.runtimeType} "
"(${strategy.includeLibraryNamesInMembers},"
"${strategy.includeLibraryNamesInTypes}): "
"$throughNode <--> $throughReference <--> $throughCanonicalName");
foundMismatch = true;
}
}
if (foundMismatch) throw "Found mismatch.";
}
class NamedNodeCollector extends RecursiveVisitor {
List<NamedNode> namedNodes = [];
@override
void defaultNode(Node node) {
if (node is NamedNode) {
namedNodes.add(node);
}
if (node is Extension) return;
if (node is Typedef) return;
if (node is Member) return;
super.defaultNode(node);
}
}