7fce125300
This CL adds a Node.toText method together with an AstPrinter. These facility and better toString implementation on AST nodes while allowing for toString independent printing of AST to use in testing. This also add support for an integrated toString of custom/internal nodes. Some work is still needed in bringing the toString implementation on all nodes to the old quality, and not all internal nodes have customized textual representations yet. This work is left for future CLs. Change-Id: Ib0bf8a0bc02f489dfacdc8aa5f96da9c52f26058 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150923 Commit-Queue: Johnni Winther <johnniwinther@google.com> Reviewed-by: Alexander Markov <alexmarkov@google.com> Reviewed-by: Jens Johansen <jensj@google.com>
153 lines
5.4 KiB
Dart
153 lines
5.4 KiB
Dart
// Copyright (c) 2020, 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:expect/expect.dart';
|
|
import 'package:kernel/ast.dart';
|
|
import 'text_representation_test.dart';
|
|
|
|
testExpression(Expression node, String normal,
|
|
{String verbose, String limited}) {
|
|
Expect.stringEquals(normal, node.toText(normalStrategy),
|
|
"Unexpected normal strategy text for ${node.runtimeType}");
|
|
Expect.stringEquals(verbose ?? normal, node.toText(verboseStrategy),
|
|
"Unexpected verbose strategy text for ${node.runtimeType}");
|
|
Expect.stringEquals(limited ?? normal, node.toText(limitedStrategy),
|
|
"Unexpected limited strategy text for ${node.runtimeType}");
|
|
}
|
|
|
|
testType(DartType node, String normal, {String verbose, String limited}) {
|
|
Expect.stringEquals(normal, node.toText(normalStrategy),
|
|
"Unexpected normal strategy text for ${node.runtimeType}");
|
|
Expect.stringEquals(verbose ?? normal, node.toText(verboseStrategy),
|
|
"Unexpected verbose strategy text for ${node.runtimeType}");
|
|
Expect.stringEquals(limited ?? normal, node.toText(limitedStrategy),
|
|
"Unexpected limited strategy text for ${node.runtimeType}");
|
|
}
|
|
|
|
main() {
|
|
testTypes();
|
|
testMembers();
|
|
}
|
|
|
|
void testTypes() {
|
|
testType(new InterfaceType.byReference(null, Nullability.nonNullable, []),
|
|
'<missing-class-reference>');
|
|
testType(new TypedefType.byReference(null, Nullability.nonNullable, []),
|
|
'<missing-typedef-reference>');
|
|
|
|
Reference unlinkedClassName = new Reference();
|
|
testType(
|
|
new InterfaceType.byReference(
|
|
unlinkedClassName, Nullability.nonNullable, []),
|
|
'<unlinked-class-reference>');
|
|
testType(
|
|
new TypedefType.byReference(
|
|
unlinkedClassName, Nullability.nonNullable, []),
|
|
'<unlinked-typedef-reference>');
|
|
|
|
CanonicalName root = new CanonicalName.root();
|
|
Reference rootReference = new Reference()..canonicalName = root;
|
|
testType(
|
|
new InterfaceType.byReference(rootReference, Nullability.nonNullable, []),
|
|
'<root>');
|
|
testType(
|
|
new TypedefType.byReference(rootReference, Nullability.nonNullable, []),
|
|
'<root>');
|
|
|
|
CanonicalName library = root.getChild('library');
|
|
Reference libraryReference = new Reference()..canonicalName = library;
|
|
testType(
|
|
new InterfaceType.byReference(
|
|
libraryReference, Nullability.nonNullable, []),
|
|
'library');
|
|
testType(
|
|
new TypedefType.byReference(
|
|
libraryReference, Nullability.nonNullable, []),
|
|
'library');
|
|
|
|
CanonicalName className = library.getChild('Class');
|
|
Reference classNameReference = new Reference()..canonicalName = className;
|
|
testType(
|
|
new InterfaceType.byReference(
|
|
classNameReference, Nullability.nonNullable, []),
|
|
'Class',
|
|
verbose: 'library::Class');
|
|
testType(
|
|
new TypedefType.byReference(
|
|
classNameReference, Nullability.nonNullable, []),
|
|
'Class',
|
|
verbose: 'library::Class');
|
|
}
|
|
|
|
void testMembers() {
|
|
testExpression(new PropertyGet(new IntLiteral(0), new Name('foo')), '''
|
|
0.foo''');
|
|
testExpression(new StaticGet(null), '''
|
|
<missing-member-reference>''');
|
|
|
|
Reference unlinkedMemberName = new Reference();
|
|
testExpression(
|
|
new PropertyGet.byReference(
|
|
new IntLiteral(0), new Name('foo'), unlinkedMemberName),
|
|
'''
|
|
0.foo''');
|
|
testExpression(new StaticGet.byReference(unlinkedMemberName), '''
|
|
<unlinked-member-reference>''');
|
|
|
|
CanonicalName root = new CanonicalName.root();
|
|
Reference rootReference = new Reference()..canonicalName = root;
|
|
testExpression(
|
|
new PropertyGet.byReference(
|
|
new IntLiteral(0), new Name('foo'), rootReference),
|
|
'''
|
|
0.foo''');
|
|
testExpression(new StaticGet.byReference(rootReference), '''
|
|
<root>''');
|
|
|
|
CanonicalName library = root.getChild('library');
|
|
Reference libraryReference = new Reference()..canonicalName = library;
|
|
testExpression(
|
|
new PropertyGet.byReference(
|
|
new IntLiteral(0), new Name('foo'), libraryReference),
|
|
'''
|
|
0.foo''');
|
|
testExpression(new StaticGet.byReference(libraryReference), '''
|
|
library''');
|
|
|
|
CanonicalName topLevelMemberName = library.getChild('member');
|
|
Reference topLevelMemberNameReference = new Reference()
|
|
..canonicalName = topLevelMemberName;
|
|
testExpression(
|
|
new PropertyGet.byReference(
|
|
new IntLiteral(0), new Name('foo'), topLevelMemberNameReference),
|
|
'''
|
|
0.foo''');
|
|
testExpression(new StaticGet.byReference(topLevelMemberNameReference), '''
|
|
member''', verbose: '''
|
|
library::member''');
|
|
|
|
CanonicalName className = library.getChild('Class');
|
|
Reference classNameReference = new Reference()..canonicalName = className;
|
|
testExpression(
|
|
new PropertyGet.byReference(
|
|
new IntLiteral(0), new Name('foo'), classNameReference),
|
|
'''
|
|
0.foo''');
|
|
testExpression(new StaticGet.byReference(classNameReference), '''
|
|
Class''', verbose: '''
|
|
library::Class''');
|
|
|
|
CanonicalName classMemberName = className.getChild('member');
|
|
Reference classMemberNameReference = new Reference()
|
|
..canonicalName = classMemberName;
|
|
testExpression(
|
|
new PropertyGet.byReference(
|
|
new IntLiteral(0), new Name('foo'), classMemberNameReference),
|
|
'''
|
|
0.foo''');
|
|
testExpression(new StaticGet.byReference(classMemberNameReference), '''
|
|
Class.member''', verbose: '''
|
|
library::Class.member''');
|
|
}
|