Skip to content

Commit c3bdb6c

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Refactor summary resynthesis tests in preparation for using the one-phase summary API.
This CL refactors the summary resynthesis tests similar to how 05ab41c refactored the summary generation tests. It introduces the following classes: - The interface `ResynthesizeTestStrategy`, defining the methods that can be invoked by tests of summary resynthesis. - An implementation of that interface: `ResynthesizeTestStrategyTwoPhase`. This drives the summary mechanism using the old two-phase summary API. - Mixin classes `ExprBuilderTestCases` and `ResynthesizeTestCases` containing the test cases themselves. - Mixin classes `ExprBuilderTestHelpers` and `ResynthesizeTestHelpers` containing helper methods used by the test cases. There should be no functional change introduced by this CL, only code motion. Change-Id: Ifb84d4d2d8fa17bbc32b833b1f56af7e1217b5ae Reviewed-on: https://dart-review.googlesource.com/75124 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent ddcb8b7 commit c3bdb6c

File tree

4 files changed

+7051
-7059
lines changed

4 files changed

+7051
-7059
lines changed

pkg/analyzer/test/src/summary/expr_builder_test.dart

Lines changed: 100 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import 'package:analyzer/src/summary/resynthesize.dart';
1111
import 'package:test/test.dart';
1212
import 'package:test_reflective_loader/test_reflective_loader.dart';
1313

14-
import '../abstract_single_unit.dart';
15-
import 'resynthesize_ast_test.dart';
1614
import 'resynthesize_common.dart';
15+
import 'test_strategies.dart';
1716

1817
main() {
1918
defineReflectiveSuite(() {
@@ -22,100 +21,13 @@ main() {
2221
}
2322

2423
@reflectiveTest
25-
class ExprBuilderTest extends AbstractSingleUnitTest
26-
with AstSerializeTestMixin {
27-
@override
28-
bool get allowMissingFiles => false;
29-
30-
Expression buildConstructorInitializer(String sourceText,
31-
{String className: 'C',
32-
String initializerName: 'x',
33-
bool requireValidConst: false}) {
34-
var resynthesizer = encodeSource(sourceText);
35-
var library = resynthesizer.getLibraryElement(testSource.uri.toString());
36-
var c = library.getType(className);
37-
var constructor = c.unnamedConstructor as ConstructorElementImpl;
38-
var serializedExecutable = constructor.serializedExecutable;
39-
var x = serializedExecutable.constantInitializers
40-
.singleWhere((i) => i.name == initializerName);
41-
return buildExpression(resynthesizer, constructor, x.expression,
42-
serializedExecutable.localFunctions,
43-
requireValidConst: requireValidConst);
44-
}
45-
46-
Expression buildExpression(
47-
TestSummaryResynthesizer resynthesizer,
48-
ElementImpl context,
49-
UnlinkedExpr unlinkedExpr,
50-
List<UnlinkedExecutable> localFunctions,
51-
{bool requireValidConst: false}) {
52-
var library = resynthesizer.getLibraryElement(testSource.uri.toString());
53-
var unit = library.definingCompilationUnit as CompilationUnitElementImpl;
54-
var unitResynthesizerContext =
55-
unit.resynthesizerContext as SummaryResynthesizerContext;
56-
var unitResynthesizer = unitResynthesizerContext.unitResynthesizer;
57-
var exprBuilder = new ExprBuilder(unitResynthesizer, context, unlinkedExpr,
58-
requireValidConst: requireValidConst, localFunctions: localFunctions);
59-
return exprBuilder.build();
60-
}
61-
62-
Expression buildTopLevelVariable(String sourceText,
63-
{String variableName: 'x', bool requireValidConst: false}) {
64-
var resynthesizer = encodeSource(sourceText);
65-
var library = resynthesizer.getLibraryElement(testSource.uri.toString());
66-
var unit = library.definingCompilationUnit as CompilationUnitElementImpl;
67-
TopLevelVariableElementImpl x =
68-
unit.topLevelVariables.singleWhere((e) => e.name == variableName);
69-
return buildExpression(
70-
resynthesizer,
71-
x,
72-
x.unlinkedVariableForTesting.initializer.bodyExpr,
73-
x.unlinkedVariableForTesting.initializer.localFunctions,
74-
requireValidConst: requireValidConst);
75-
}
76-
77-
void checkCompoundAssignment(String exprText) {
78-
checkSimpleExpression(exprText, extraDeclarations: 'var y;');
79-
}
80-
81-
void checkConstructorInitializer(String sourceText, String expectedText,
82-
{String className: 'C',
83-
String initializerName: 'x',
84-
bool requireValidConst: false}) {
85-
Expression expr = buildConstructorInitializer(sourceText,
86-
className: className,
87-
initializerName: initializerName,
88-
requireValidConst: requireValidConst);
89-
expect(expr.toString(), expectedText);
90-
}
91-
92-
void checkInvalidConst(String expressionText) {
93-
checkTopLevelVariable('var x = $expressionText;', 'null',
94-
requireValidConst: true);
95-
}
96-
97-
Expression checkSimpleExpression(String expressionText,
98-
{String expectedText,
99-
String extraDeclarations: '',
100-
bool requireValidConst: false}) {
101-
return checkTopLevelVariable('var x = $expressionText;\n$extraDeclarations',
102-
expectedText ?? expressionText,
103-
requireValidConst: requireValidConst);
104-
}
105-
106-
Expression checkTopLevelVariable(String sourceText, String expectedText,
107-
{String variableName: 'x', bool requireValidConst: false}) {
108-
Expression expr = buildTopLevelVariable(sourceText,
109-
variableName: variableName, requireValidConst: requireValidConst);
110-
expect(expr.toString(), expectedText);
111-
return expr;
112-
}
113-
114-
TestSummaryResynthesizer encodeSource(String text) {
115-
var source = addTestSource(text);
116-
return encodeLibrary(source);
117-
}
24+
class ExprBuilderTest extends ResynthesizeTestStrategyTwoPhase
25+
with ExprBuilderTestCases, ExprBuilderTestHelpers {}
11826

27+
/// Mixin containing test cases exercising the [ExprBuilder]. Intended to be
28+
/// applied to a class implementing [ResynthesizeTestStrategy], along with the
29+
/// mixin [ExprBuilderTestHelpers].
30+
abstract class ExprBuilderTestCases implements ExprBuilderTestHelpers {
11931
void test_add() {
12032
checkSimpleExpression('0 + 1');
12133
}
@@ -565,3 +477,96 @@ class B {
565477
checkSimpleExpression('0 is! num', expectedText: '!(0 is num)');
566478
}
567479
}
480+
481+
/// Mixin containing helper methods for testing the [ExprBuilder]. Intended to
482+
/// be applied to a class implementing [ResynthesizeTestStrategy].
483+
abstract class ExprBuilderTestHelpers implements ResynthesizeTestStrategy {
484+
Expression buildConstructorInitializer(String sourceText,
485+
{String className: 'C',
486+
String initializerName: 'x',
487+
bool requireValidConst: false}) {
488+
var resynthesizer = encodeSource(sourceText);
489+
var library = resynthesizer.getLibraryElement(testSource.uri.toString());
490+
var c = library.getType(className);
491+
var constructor = c.unnamedConstructor as ConstructorElementImpl;
492+
var serializedExecutable = constructor.serializedExecutable;
493+
var x = serializedExecutable.constantInitializers
494+
.singleWhere((i) => i.name == initializerName);
495+
return buildExpression(resynthesizer, constructor, x.expression,
496+
serializedExecutable.localFunctions,
497+
requireValidConst: requireValidConst);
498+
}
499+
500+
Expression buildExpression(
501+
TestSummaryResynthesizer resynthesizer,
502+
ElementImpl context,
503+
UnlinkedExpr unlinkedExpr,
504+
List<UnlinkedExecutable> localFunctions,
505+
{bool requireValidConst: false}) {
506+
var library = resynthesizer.getLibraryElement(testSource.uri.toString());
507+
var unit = library.definingCompilationUnit as CompilationUnitElementImpl;
508+
var unitResynthesizerContext =
509+
unit.resynthesizerContext as SummaryResynthesizerContext;
510+
var unitResynthesizer = unitResynthesizerContext.unitResynthesizer;
511+
var exprBuilder = new ExprBuilder(unitResynthesizer, context, unlinkedExpr,
512+
requireValidConst: requireValidConst, localFunctions: localFunctions);
513+
return exprBuilder.build();
514+
}
515+
516+
Expression buildTopLevelVariable(String sourceText,
517+
{String variableName: 'x', bool requireValidConst: false}) {
518+
var resynthesizer = encodeSource(sourceText);
519+
var library = resynthesizer.getLibraryElement(testSource.uri.toString());
520+
var unit = library.definingCompilationUnit as CompilationUnitElementImpl;
521+
TopLevelVariableElementImpl x =
522+
unit.topLevelVariables.singleWhere((e) => e.name == variableName);
523+
return buildExpression(
524+
resynthesizer,
525+
x,
526+
x.unlinkedVariableForTesting.initializer.bodyExpr,
527+
x.unlinkedVariableForTesting.initializer.localFunctions,
528+
requireValidConst: requireValidConst);
529+
}
530+
531+
void checkCompoundAssignment(String exprText) {
532+
checkSimpleExpression(exprText, extraDeclarations: 'var y;');
533+
}
534+
535+
void checkConstructorInitializer(String sourceText, String expectedText,
536+
{String className: 'C',
537+
String initializerName: 'x',
538+
bool requireValidConst: false}) {
539+
Expression expr = buildConstructorInitializer(sourceText,
540+
className: className,
541+
initializerName: initializerName,
542+
requireValidConst: requireValidConst);
543+
expect(expr.toString(), expectedText);
544+
}
545+
546+
void checkInvalidConst(String expressionText) {
547+
checkTopLevelVariable('var x = $expressionText;', 'null',
548+
requireValidConst: true);
549+
}
550+
551+
Expression checkSimpleExpression(String expressionText,
552+
{String expectedText,
553+
String extraDeclarations: '',
554+
bool requireValidConst: false}) {
555+
return checkTopLevelVariable('var x = $expressionText;\n$extraDeclarations',
556+
expectedText ?? expressionText,
557+
requireValidConst: requireValidConst);
558+
}
559+
560+
Expression checkTopLevelVariable(String sourceText, String expectedText,
561+
{String variableName: 'x', bool requireValidConst: false}) {
562+
Expression expr = buildTopLevelVariable(sourceText,
563+
variableName: variableName, requireValidConst: requireValidConst);
564+
expect(expr.toString(), expectedText);
565+
return expr;
566+
}
567+
568+
TestSummaryResynthesizer encodeSource(String text) {
569+
var source = addTestSource(text);
570+
return encodeLibrary(source);
571+
}
572+
}

0 commit comments

Comments
 (0)