Skip to content

Commit

Permalink
Migrate two builder tests
Browse files Browse the repository at this point in the history
Change-Id: I2886e2f50e5974185709716f01fd4defc74126fc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400801
Commit-Queue: Brian Wilkerson <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
  • Loading branch information
bwilkerson authored and Commit Queue committed Dec 13, 2024
1 parent 4d7d290 commit ae07ae6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 60 deletions.
2 changes: 0 additions & 2 deletions pkg/analyzer/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ test/generated/static_type_analyzer_test.dart
test/generated/type_system_test.dart
test/id_tests/assigned_variables_test.dart
test/id_tests/type_constraint_generation_test.dart
test/src/dart/analysis/driver_caching_test.dart
test/src/dart/analysis/driver_test.dart
test/src/dart/analysis/index_test.dart
test/src/dart/analysis/result_printer.dart
test/src/dart/analysis/results/get_element_declaration_test.dart
Expand Down
32 changes: 16 additions & 16 deletions pkg/analyzer/test/src/dart/analysis/driver_caching_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'dart:async';
import 'dart:collection';

import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/dart/analysis/driver.dart';
Expand Down Expand Up @@ -141,7 +141,7 @@ class A {
''');

await resolveTestFile();
assertType(findElement.field('f').type, 'Set<int>');
assertType(findElement2.field('f').type, 'Set<int>');

// The summary for the library was linked.
_assertContainsLinkedCycle({testFile}, andClear: true);
Expand All @@ -161,7 +161,7 @@ class A {
''');

await resolveTestFile();
assertType(findElement.field('f').type, 'Set<int>');
assertType(findElement2.field('f').type, 'Set<int>');

// We changed the initializer of the final field. But it is static, so
// even though the class hsa a constant constructor, we don't need its
Expand Down Expand Up @@ -225,7 +225,7 @@ import 'a.dart';
// from the `LibraryReader` current at the moment of `exportNamespace`
// access, not necessary the same that created this instance.
var aResult = await driver.getLibraryByUri('package:test/a.dart');
var aElement = (aResult as LibraryElementResult).element;
var aElement = (aResult as LibraryElementResult).element2;

// The element is valid at this point.
expect(driver.isValidLibraryElement(aElement), isTrue);
Expand All @@ -241,7 +241,7 @@ import 'a.dart';
expect(driver.isValidLibraryElement(aElement), isFalse);

// But its `exportNamespace` can be accessed.
expect(aElement.exportNamespace.definedNames, isNotEmpty);
expect(aElement.exportNamespace.definedNames2, isNotEmpty);

// TODO(scheglov): This is not quite right.
// When we return `LibraryElement` that is not fully read, and read
Expand Down Expand Up @@ -361,21 +361,21 @@ export 'a.dart';

var analysisContext = contextFor(macroFile);

Future<LibraryElement> getLibrary(String uriStr) async {
Future<LibraryElement2> getLibrary(String uriStr) async {
var result = await analysisContext.currentSession.getLibraryByUri(uriStr)
as LibraryElementResult;
return result.element;
return result.element2;
}

// This macro generates `MacroA`, but not `MacroB`.
{
var libraryA = await getLibrary('package:test/a.dart');
expect(libraryA.getClass('MacroA'), isNotNull);
expect(libraryA.getClass('MacroB'), isNull);
expect(libraryA.getClass2('MacroA'), isNotNull);
expect(libraryA.getClass2('MacroB'), isNull);
// This propagates transitively.
var libraryB = await getLibrary('package:test/b.dart');
expect(libraryB.exportNamespace.get('MacroA'), isNotNull);
expect(libraryB.exportNamespace.get('MacroB'), isNull);
expect(libraryB.exportNamespace.get2('MacroA'), isNotNull);
expect(libraryB.exportNamespace.get2('MacroB'), isNull);
}

_assertContainsLinkedCycle({a});
Expand All @@ -391,12 +391,12 @@ export 'a.dart';
// This macro generates `MacroB`, but not `MacroA`.
{
var libraryA = await getLibrary('package:test/a.dart');
expect(libraryA.getClass('MacroA'), isNull);
expect(libraryA.getClass('MacroB'), isNotNull);
expect(libraryA.getClass2('MacroA'), isNull);
expect(libraryA.getClass2('MacroB'), isNotNull);
// This propagates transitively.
var libraryB = await getLibrary('package:test/b.dart');
expect(libraryB.exportNamespace.get('MacroA'), isNull);
expect(libraryB.exportNamespace.get('MacroB'), isNotNull);
expect(libraryB.exportNamespace.get2('MacroA'), isNull);
expect(libraryB.exportNamespace.get2('MacroB'), isNotNull);
}

_assertContainsLinkedCycle({a});
Expand Down Expand Up @@ -665,7 +665,7 @@ macro class MyMacro implements ClassTypesMacro {
}

extension on AnalysisDriver {
bool isValidLibraryElement(LibraryElement element) {
bool isValidLibraryElement(LibraryElement2 element) {
return identical(element.session, currentSession);
}
}
Expand Down
83 changes: 41 additions & 42 deletions pkg/analyzer/test/src/dart/analysis/driver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import 'dart:async';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/src/dart/analysis/driver.dart';
import 'package:analyzer/src/dart/analysis/driver_event.dart' as driver_events;
import 'package:analyzer/src/dart/analysis/file_state.dart';
import 'package:analyzer/src/dart/analysis/status.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/lint/linter.dart';
import 'package:analyzer/src/lint/registry.dart';
Expand Down Expand Up @@ -1421,10 +1421,10 @@ import 'c.dart';

var driver = driverFor(testFile);

Future<LibraryElementImpl> getLibrary(String shortName) async {
Future<LibraryElement2> getLibrary(String shortName) async {
var uriStr = 'package:test/$shortName';
var result = await driver.getLibraryByUriValid(uriStr);
return result.element as LibraryElementImpl;
return result.element2;
}

var a_element = await getLibrary('a.dart');
Expand Down Expand Up @@ -1500,10 +1500,10 @@ import 'b.dart';

var driver = driverFor(testFile);

Future<LibraryElementImpl> getLibrary(String shortName) async {
Future<LibraryElement2> getLibrary(String shortName) async {
var uriStr = 'package:test/$shortName';
var result = await driver.getLibraryByUriValid(uriStr);
return result.element as LibraryElementImpl;
return result.element2;
}

var b_element = await getLibrary('b.dart');
Expand Down Expand Up @@ -1572,21 +1572,18 @@ final B1 = A1;
driver.addFile2(a);
driver.addFile2(b);

configuration.libraryConfiguration.unitConfiguration.variableTypesSelector =
(result) {
switch (result.uriStr) {
case 'package:test/a.dart':
return [
result.findElement.topVar('A1'),
result.findElement.topVar('A2'),
];
case 'package:test/b.dart':
return [
result.findElement.topVar('B1'),
];
default:
return [];
}
configuration.libraryConfiguration.unitConfiguration
.variableTypesSelector2 = (result) {
return switch (result.uriStr) {
'package:test/a.dart' => [
result.findElement2.topVar('A1'),
result.findElement2.topVar('A2'),
],
'package:test/b.dart' => [
result.findElement2.topVar('B1'),
],
_ => []
};
};

// We have results for both "a" and "b".
Expand Down Expand Up @@ -1662,12 +1659,12 @@ final A2 = B1;
driver.addFile2(a);
driver.priorityFiles2 = [a];

configuration.libraryConfiguration.unitConfiguration.variableTypesSelector =
(result) {
configuration.libraryConfiguration.unitConfiguration
.variableTypesSelector2 = (result) {
switch (result.uriStr) {
case 'package:test/a.dart':
return [
result.findElement.topVar('V'),
result.findElement2.topVar('V'),
];
default:
return [];
Expand Down Expand Up @@ -2470,8 +2467,8 @@ class B {}

var result = await driver.getLibraryByUri(aUriStr);
result as LibraryElementResult;
expect(result.element.getClass('A'), isNotNull);
expect(result.element.getClass('B'), isNotNull);
expect(result.element2.getClass2('A'), isNotNull);
expect(result.element2.getClass2('B'), isNotNull);

// It is an error to ask for a library when we know that it is a part.
expect(
Expand Down Expand Up @@ -3359,10 +3356,10 @@ final foo = 0;
var driver = driverFor(testFile);
var collector = DriverEventCollector(driver);

configuration.libraryConfiguration.unitConfiguration.variableTypesSelector =
(result) {
configuration.libraryConfiguration.unitConfiguration
.variableTypesSelector2 = (result) {
return [
result.findElement.topVar('foo'),
result.findElement2.topVar('foo'),
];
};

Expand Down Expand Up @@ -3696,8 +3693,10 @@ void bar() {}
var driver = driverFor(testFile);
var collector = DriverEventCollector(driver);

configuration.unitElementConfiguration.elementSelector = (unitElement) {
return unitElement.functions;
configuration.unitElementConfiguration.elementSelector2 = (unitFragment) {
return unitFragment.functions2
.map((fragment) => fragment.element)
.toList();
};

collector.getUnitElement('A1', a);
Expand All @@ -3709,8 +3708,8 @@ void bar() {}
flags: isLibrary
enclosing: <null>
selectedElements
package:test/a.dart::<fragment>::@function::foo
package:test/a.dart::<fragment>::@function::bar
package:test/a.dart::@function::foo
package:test/a.dart::@function::bar
[status] idle
''');
}
Expand Down Expand Up @@ -3781,8 +3780,8 @@ class A {}
collector.getUnitElement('AM1', a_macro);
await collector.nextStatusIdle();

configuration.unitElementConfiguration.elementSelector = (unitElement) {
return unitElement.classes;
configuration.unitElementConfiguration.elementSelector2 = (fragment) {
return fragment.classes2.map((fragment) => fragment.element).toList();
};

// The enclosing element is an augmentation library, in a library.
Expand All @@ -3795,7 +3794,7 @@ class A {}
flags: isMacroPart isPart
enclosing: package:test/a.dart::<fragment>
selectedElements
package:test/a.dart::@fragment::package:test/a.macro.dart::@class::B
package:test/a.dart::@class::B
[status] idle
''');
}
Expand All @@ -3814,12 +3813,12 @@ final B = A;
var driver = driverFor(testFile);
var collector = DriverEventCollector(driver);

configuration.libraryConfiguration.unitConfiguration.variableTypesSelector =
(result) {
configuration.libraryConfiguration.unitConfiguration
.variableTypesSelector2 = (result) {
switch (result.uriStr) {
case 'package:test/b.dart':
return [
result.findElement.topVar('B'),
result.findElement2.topVar('B'),
];
default:
return [];
Expand Down Expand Up @@ -5324,16 +5323,16 @@ final B = 0;
driver.addFile2(a);
driver.addFile2(b);

configuration.libraryConfiguration.unitConfiguration.variableTypesSelector =
(result) {
configuration.libraryConfiguration.unitConfiguration
.variableTypesSelector2 = (result) {
switch (result.uriStr) {
case 'package:test/a.dart':
return [
result.findElement.topVar('A'),
result.findElement2.topVar('A'),
];
case 'package:test/b.dart':
return [
result.findElement.topVar('B'),
result.findElement2.topVar('B'),
];
default:
return [];
Expand Down
23 changes: 23 additions & 0 deletions pkg/analyzer/test/src/dart/analysis/result_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/dart/analysis/driver_event.dart' as events;
Expand Down Expand Up @@ -287,6 +288,10 @@ class DriverEventsPrinter {
var elementsToWrite =
configuration.unitElementConfiguration.elementSelector(unitElement);
elementPrinter.writeElementList('selectedElements', elementsToWrite);

var elementsToWrite2 = configuration.unitElementConfiguration
.elementSelector2(unitElement as LibraryFragment);
elementPrinter.writeElementList2('selectedElements', elementsToWrite2);
}
}

Expand Down Expand Up @@ -580,6 +585,21 @@ class ResolvedUnitResultPrinter {
elementPrinter.writeType(variable.type);
},
);

var variableTypesToWrite2 = configuration.variableTypesSelector2(result);
sink.writeElements(
'selectedVariableTypes',
variableTypesToWrite2,
(variable) {
sink.writeIndent();
sink.write('${variable.name3}: ');
if (variable is LocalVariableElement2) {
elementPrinter.writeType(variable.type);
} else if (variable is TopLevelVariableElement2) {
elementPrinter.writeType(variable.type);
}
},
);
});
}
}
Expand All @@ -590,6 +610,8 @@ class ResolvedUnitResultPrinterConfiguration {
Map<String, DartType> Function(ResolvedUnitResult) typesSelector = (_) => {};
List<VariableElement> Function(ResolvedUnitResult) variableTypesSelector =
(_) => [];
List<Element2> Function(ResolvedUnitResult) variableTypesSelector2 =
(_) => [];
bool Function(FileResult) withContentPredicate = (_) => false;
}

Expand All @@ -610,4 +632,5 @@ final class SchedulerStatusEvent extends DriverEvent {

class UnitElementPrinterConfiguration {
List<Element> Function(CompilationUnitElement) elementSelector = (_) => [];
List<Element2> Function(LibraryFragment) elementSelector2 = (_) => [];
}

0 comments on commit ae07ae6

Please sign in to comment.