Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for custom output formatting #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .github/dependabot.yml

This file was deleted.

60 changes: 0 additions & 60 deletions .github/workflows/ci.yml

This file was deleted.

37 changes: 0 additions & 37 deletions .github/workflows/no-response.yml

This file was deleted.

17 changes: 0 additions & 17 deletions .github/workflows/publish.yaml

This file was deleted.

9 changes: 6 additions & 3 deletions lib/src/generate_for_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ final _testAnnotationWarnings = <String>{};
Future<String> generateForElement<T>(
GeneratorForAnnotation<T> generator,
LibraryReader libraryReader,
String name,
) async {
String name, {
String Function(String code)? formatOutput,
}) async {
formatOutput ??= _formatter.format;

final elements =
libraryReader.allElements.where((e) => e.name == name).toList();

Expand Down Expand Up @@ -94,7 +97,7 @@ Future<String> generateForElement<T>(

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

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

// ignore: subtype_of_sealed_class
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 @@ -29,13 +29,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 @@ -50,6 +52,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 @@ -174,6 +177,7 @@ List<AnnotatedTest<T>> getAnnotatedClasses<T>(
configuration,
entry.elementName,
entry.expectation,
formatOutput: formatOutput,
),
);
}
Expand Down Expand Up @@ -205,6 +209,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 @@ -219,8 +224,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 @@ -233,8 +239,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 @@ -98,6 +98,75 @@ 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 @@ -32,6 +32,7 @@ void main() {
'class TestClassWithBadMember',
'int badTestFunc()',
'int badTestField',
'class TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop',
'class TestClass2',
'import source /source_gen_test/lib/annotations.dart',
'import source /__test__/lib/test_annotation.dart',
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 @@ -78,3 +78,6 @@ int badTestFunc() => 42;
)
@TestAnnotation()
const badTestField = 42;

@TestAnnotation()
class TestClassThatHasAVeryLongNameThatShouldNotWrapWhenFormatOutputIsANop {}