Skip to content

Commit 8d5e20e

Browse files
chloestefantsovaCommit Bot
authored and
Commit Bot
committed
[cfe] Add temporary testing framework for pattern desugarings
Part of #49749 Change-Id: Ifb46078eb24b5c5288847ae95f1a0e584830335e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/259180 Commit-Queue: Chloe Stefantsova <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 35e35f5 commit 8d5e20e

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

pkg/front_end/lib/src/fasta/kernel/internal_ast.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5119,3 +5119,55 @@ class WildcardBinder extends Binder {
51195119
return "WildcardBinder(${toStringInternal()})";
51205120
}
51215121
}
5122+
5123+
class PatternVariableDeclaration extends Statement {
5124+
final Binder binder;
5125+
final Expression initializer;
5126+
5127+
PatternVariableDeclaration(this.binder, this.initializer);
5128+
5129+
@override
5130+
R accept<R>(StatementVisitor<R> visitor) {
5131+
if (visitor is Printer || visitor is Precedence || visitor is Transformer) {
5132+
// Allow visitors needed for toString and replaceWith.
5133+
return visitor.defaultStatement(this);
5134+
}
5135+
return unsupported(
5136+
"${runtimeType}.accept on ${visitor.runtimeType}", -1, null);
5137+
}
5138+
5139+
@override
5140+
R accept1<R, A>(StatementVisitor1<R, A> visitor, A arg) {
5141+
return unsupported(
5142+
"${runtimeType}.accept1 on ${visitor.runtimeType}", -1, null);
5143+
}
5144+
5145+
@override
5146+
void toTextInternal(AstPrinter printer) {
5147+
binder.toTextInternal(printer);
5148+
printer.write(" = ");
5149+
printer.writeExpression(initializer);
5150+
}
5151+
5152+
@override
5153+
void transformChildren(Transformer v) {
5154+
unsupported(
5155+
"${runtimeType}.transformChildren on ${v.runtimeType}", -1, null);
5156+
}
5157+
5158+
@override
5159+
void transformOrRemoveChildren(RemovingTransformer v) {
5160+
unsupported("${runtimeType}.transformOrRemoveChildren on ${v.runtimeType}",
5161+
-1, null);
5162+
}
5163+
5164+
@override
5165+
void visitChildren(Visitor v) {
5166+
unsupported("${runtimeType}.visitChildren on ${v.runtimeType}", -1, null);
5167+
}
5168+
5169+
@override
5170+
String toString() {
5171+
return "PatternVariableDeclaration(${toStringInternal()})";
5172+
}
5173+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:kernel/ast.dart';
6+
import 'package:kernel/text/ast_to_text.dart';
7+
8+
import 'package:front_end/src/fasta/kernel/internal_ast.dart';
9+
10+
void runTests() {
11+
checkExpected(
12+
makeSimpleComponentWithPatternVariableDeclaration(
13+
new PatternVariableDeclaration(
14+
new ListBinder(const DynamicType(), [new WildcardBinder(null)]),
15+
new IntLiteral(0))),
16+
makeExpectationOfSimpleComponentWithPatternVariableDeclaration(
17+
"PatternVariableDeclaration"));
18+
}
19+
20+
Component makeSimpleComponentWithPatternVariableDeclaration(
21+
PatternVariableDeclaration patternVariableDeclaration) {
22+
Uri fileUri = Uri.parse("test:///test.dart");
23+
Uri importUri = Uri.parse("test.dart");
24+
25+
Block body = new Block([patternVariableDeclaration]);
26+
FunctionNode functionNode = new FunctionNode(body);
27+
Procedure procedure = new Procedure(
28+
new Name("test"), ProcedureKind.Method, functionNode,
29+
fileUri: fileUri);
30+
Library library =
31+
new Library(importUri, procedures: [procedure], fileUri: fileUri);
32+
Component component = new Component(libraries: [library]);
33+
return component;
34+
}
35+
36+
String makeExpectationOfSimpleComponentWithPatternVariableDeclaration(
37+
String patternVariableDeclarationExpectation) {
38+
return """
39+
main = <No Member>;
40+
library from "test.dart" as test {
41+
42+
method test() → dynamic {
43+
${patternVariableDeclarationExpectation}
44+
}
45+
}
46+
""";
47+
}
48+
49+
void checkExpected(Component component, String expected) {
50+
String actual = componentToString(component);
51+
if (actual != expected) {
52+
// This is a very primitive substitute for the diff output of the actual
53+
// expectation tests.
54+
List<String> actualLines = actual.split("\n");
55+
List<String> expectedLines = expected.split("\n");
56+
StringBuffer output = new StringBuffer();
57+
if (actualLines.length < expectedLines.length) {
58+
output.writeln("Actual output is shorter than the expected.");
59+
} else if (actualLines.length > expectedLines.length) {
60+
output.writeln("Actual output is longer than the expected.");
61+
}
62+
int minLines = actualLines.length < expectedLines.length
63+
? actualLines.length
64+
: expectedLines.length;
65+
for (int i = 0; i < minLines; i++) {
66+
// Include one line of the difference.
67+
if (actualLines[i] != expectedLines[i]) {
68+
output.writeln(
69+
"Line $i is different in the actual output and the expected.\n"
70+
"Actual : ${actualLines[i]}\n"
71+
"Expected: ${expectedLines[i]}");
72+
break;
73+
}
74+
}
75+
output.writeln("Full actual output:\n${'=' * 72}\n${actual}\n${'=' * 72}");
76+
output.writeln(
77+
"Full expected output:\n${'=' * 72}\n${expected}\n${'=' * 72}");
78+
throw new StateError("${output}");
79+
}
80+
}
81+
82+
void main(List<String> args) {
83+
runTests();
84+
}

0 commit comments

Comments
 (0)