Skip to content
11 changes: 6 additions & 5 deletions lib/src/generate_for_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ final _testAnnotationWarnings = <String>{};
Future<String> generateForElement<T>(
GeneratorForAnnotation<T> generator,
LibraryReader libraryReader,
String name,
) async {
String name, {
String Function(String code)? formatOutput,
}) async {
final elements =
libraryReader.allElements.where((e) => e.name3 == name).toList();

Expand Down Expand Up @@ -93,11 +94,11 @@ Future<String> generateForElement<T>(

final generated = await generatedStream.join('\n\n');

final formatter = dart_style.DartFormatter(
formatOutput ??= dart_style.DartFormatter(
languageVersion: libraryReader.element.languageVersion.effective,
);
).format;

return formatter.format(generated);
return formatOutput(generated);
}

class _MockBuildStep extends BuildStep {
Expand Down
18 changes: 14 additions & 4 deletions lib/src/test_annotated_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ void testAnnotatedElements<T>(
Map<String, GeneratorForAnnotation<T>>? additionalGenerators,
Iterable<String>? expectedAnnotatedTests,
Iterable<String>? defaultConfiguration,
String Function(String code)? formatOutput,
}) {
for (var entry in getAnnotatedClasses<T>(
libraryReader,
defaultGenerator,
additionalGenerators: additionalGenerators,
expectedAnnotatedTests: expectedAnnotatedTests,
defaultConfiguration: defaultConfiguration,
formatOutput: formatOutput,
)) {
entry._registerTest();
}
Expand All @@ -54,6 +56,7 @@ List<AnnotatedTest<T>> getAnnotatedClasses<T>(
Map<String, GeneratorForAnnotation<T>>? additionalGenerators,
Iterable<String>? expectedAnnotatedTests,
Iterable<String>? defaultConfiguration,
String Function(String code)? formatOutput,
}) {
final generators = <String, GeneratorForAnnotation<T>>{
_defaultConfigurationName: defaultGenerator,
Expand Down Expand Up @@ -181,6 +184,7 @@ List<AnnotatedTest<T>> getAnnotatedClasses<T>(
configuration,
entry.elementName,
entry.expectation,
formatOutput: formatOutput,
),
);
}
Expand Down Expand Up @@ -216,6 +220,7 @@ class AnnotatedTest<T> {
final LibraryReader _libraryReader;
final TestExpectation expectation;
final String _elementName;
final String Function(String code)? _formatOutput;

String get _testName {
var value = _elementName;
Expand All @@ -230,8 +235,9 @@ class AnnotatedTest<T> {
this.generator,
this.configuration,
this._elementName,
this.expectation,
);
this.expectation, {
String Function(String code)? formatOutput,
}) : _formatOutput = formatOutput;

void _registerTest() {
if (expectation is ShouldGenerate) {
Expand All @@ -247,8 +253,12 @@ class AnnotatedTest<T> {
throw StateError('Should never get here.');
}

Future<String> _generate() =>
generateForElement<T>(generator, _libraryReader, _elementName);
Future<String> _generate() => generateForElement<T>(
generator,
_libraryReader,
_elementName,
formatOutput: _formatOutput,
);

Future<void> _shouldGenerateTest() async {
final output = await _generate();
Expand Down
69 changes: 69 additions & 0 deletions test/generate_for_element_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,75 @@ const TestClass2NameLength = 10;
const TestClass2NameLowerCase = 'testclass2';
''');
});

group(
'formatOutput',
() {
test(
'should format the generated source code with `DartFormatter` when `formatOutput` is `null`.',
() async {
final output = await generateForElement(
const TestGenerator(),
reader,
'TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop',
formatOutput: null,
);
printOnFailure(output);
expect(
output,
r'''
const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLength =
68;

const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLowerCase =
testclassthathasaverylongnamethatshouldnotwrapwhenformatoutputisanop;
''',
);
},
);

test(
'should not format the generated source code when `formatOutput` is a NOP.',
() async {
final output = await generateForElement(
const TestGenerator(),
reader,
'TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop',
formatOutput: (code) => code,
);
printOnFailure(output);
expect(
output,
r'''
const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLength = 68;

const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLowerCase = testclassthathasaverylongnamethatshouldnotwrapwhenformatoutputisanop;''',
);
},
);

test(
'should format the generated source code with a custom formatter when `formatOutput` is a custom formatter.',
() async {
final output = await generateForElement(
const TestGenerator(),
reader,
'TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop',
formatOutput: (code) => '$code\n',
);
printOnFailure(output);
expect(
output,
r'''
const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLength = 68;

const TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANopNameLowerCase = testclassthathasaverylongnamethatshouldnotwrapwhenformatoutputisanop;
''',
);
},
);
},
);
});

test('throwsInvalidGenerationSourceError', () async {
Expand Down
1 change: 1 addition & 0 deletions test/init_library_reader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void main() {
'class TestClassWithBadMember',
'int badTestFunc()',
'int badTestField',
'class TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop',
'class TestClass2',
]),
);
Expand Down
3 changes: 3 additions & 0 deletions test/src/test_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ int badTestFunc() => 42;
@ShouldThrow('Uh...', configurations: ['vague'], element: false)
@TestAnnotation()
const badTestField = 42;

@TestAnnotation()
class TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop {}