blob: f3a5cbd6e720dff4ac838d83c57305fd3c39fc00 [file] [log] [blame]
// 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:_fe_analyzer_shared/src/macros/executor_shared/remote_instance.dart';
import 'package:test/fake.dart';
import 'package:test/test.dart';
import 'package:_fe_analyzer_shared/src/macros/executor.dart';
import 'package:_fe_analyzer_shared/src/macros/api.dart';
import 'package:_fe_analyzer_shared/src/macros/executor_shared/augmentation_library.dart';
import 'package:_fe_analyzer_shared/src/macros/executor_shared/response_impls.dart';
import '../util.dart';
void main() {
group('AugmentationLibraryBuilder', () {
test('can combine multiple execution results', () {
var results = [
for (var i = 0; i < 2; i++)
MacroExecutionResultImpl(augmentations: [
for (var j = 0; j < 3; j++)
DeclarationCode.fromParts([
'augment class Foo$i$j {\n',
DeclarationCode.fromParts([
'int get i => $i;\n',
'int get j => $j;\n',
]),
'}',
]),
], newTypeNames: [
'Foo${i}0',
'Foo${i}1',
'Foo${i}2',
]),
];
var library = _TestExecutor().buildAugmentationLibrary(
results, (Identifier i) => (i as TestIdentifier).libraryImportUri);
expect(library, equalsIgnoringWhitespace('''
augment class Foo00 {
int get i => 0;
int get j => 0;
}
augment class Foo01 {
int get i => 0;
int get j => 1;
}
augment class Foo02 {
int get i => 0;
int get j => 2;
}
augment class Foo10 {
int get i => 1;
int get j => 0;
}
augment class Foo11 {
int get i => 1;
int get j => 1;
}
augment class Foo12 {
int get i => 1;
int get j => 2;
}
'''));
});
test('can add imports for identifiers', () {
var fooIdentifier = TestIdentifier(
id: RemoteInstance.uniqueId,
name: 'Foo',
libraryImportUri: Uri.parse('package:foo/foo.dart'));
var barIdentifier = TestIdentifier(
id: RemoteInstance.uniqueId,
name: 'Bar',
libraryImportUri: Uri.parse('package:bar/bar.dart'));
var builderIdentifier = TestIdentifier(
id: RemoteInstance.uniqueId,
name: 'Builder',
libraryImportUri: Uri.parse('package:builder/builder.dart'));
var results = [
MacroExecutionResultImpl(augmentations: [
DeclarationCode.fromParts([
'class FooBuilder<T extends ',
fooIdentifier,
'> implements ',
builderIdentifier,
'<',
barIdentifier,
'<T>> {\n',
barIdentifier,
'<T> build() => new ',
barIdentifier,
'();\n}',
]),
], newTypeNames: [
'FooBuilder',
])
];
var library = _TestExecutor().buildAugmentationLibrary(
results, (Identifier i) => (i as TestIdentifier).libraryImportUri);
expect(library, equalsIgnoringWhitespace('''
import 'package:foo/foo.dart' as i0;
import 'package:builder/builder.dart' as i1;
import 'package:bar/bar.dart' as i2;
class FooBuilder<T extends i0.Foo> implements i1.Builder<i2.Bar<T>> {
i2.Bar<T> build() => new i2.Bar();
}
'''));
});
});
}
class _TestExecutor extends MacroExecutor
with AugmentationLibraryBuilder, Fake {}