Skip to content

Add end-to-end tests for Mustachio AOT compiler #2664

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

Merged
merged 12 commits into from
Jun 3, 2021
Merged
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
1,525 changes: 911 additions & 614 deletions lib/src/generator/templates.aot_renderers_for_html.dart

Large diffs are not rendered by default.

996 changes: 638 additions & 358 deletions lib/src/generator/templates.aot_renderers_for_md.dart

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dev_dependencies:
build_runner: ^2.0.1
build_test: ^2.0.0
build_version: ^2.0.1
code_builder: ^4.0.0
coverage: ^1.0.2
dart_style: ^2.0.0
grinder: ^0.9.0-nullsafety.0
Expand Down
49 changes: 13 additions & 36 deletions test/mustachio/aot_compiler_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,26 @@ import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
import 'package:test/test.dart';

import '../../tool/mustachio/builder.dart';
import 'builder_test_base.dart';

void main() {
InMemoryAssetWriter writer;

Future<LibraryElement> resolveGeneratedLibrary(
InMemoryAssetWriter writer) async {
Future<LibraryElement> resolveGeneratedLibrary() async {
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var writtenStrings = writer.assets
.map((id, content) => MapEntry(id.toString(), utf8.decode(content)));
return await resolveSources(writtenStrings,
(Resolver resolver) => resolver.libraryFor(rendererAsset));
}

Future<void> testMustachioBuilder(
String sourceLibraryContent, {
String libraryFrontMatter = libraryFrontMatter,
Map<String, String> additionalAssets,
}) async {
sourceLibraryContent = '''
$libraryFrontMatter
$sourceLibraryContent
''';
await testBuilder(
mustachioBuilder(BuilderOptions({})),
{
...annotationsAsset,
'foo|lib/foo.dart': sourceLibraryContent,
'foo|lib/templates/html/foo.html': 'EMPTY',
'foo|lib/templates/md/foo.md': 'EMPTY',
'foo|lib/templates/html/bar.html': 'EMPTY',
'foo|lib/templates/md/bar.md': 'EMPTY',
...?additionalAssets,
},
writer: writer,
);
}

setUp(() {
writer = InMemoryAssetWriter();
});

test('builds renderers from multiple annotations', () async {
await testMustachioBuilder(
writer,
'''
class Foo {}
class Bar {}
Expand All @@ -70,7 +45,7 @@ import 'package:mustachio/annotations.dart';
'foo|lib/templates/html/_foo_header.html': 'EMPTY',
},
);
var renderersLibrary = await resolveGeneratedLibrary(writer);
var renderersLibrary = await resolveGeneratedLibrary();

expect(renderersLibrary.getTopLevelFunction('renderFoo'), isNotNull);
expect(renderersLibrary.getTopLevelFunction('renderBar'), isNotNull);
Expand All @@ -80,8 +55,7 @@ import 'package:mustachio/annotations.dart';
}, timeout: Timeout.factor(2));

test('builds a public API render function', () async {
writer = InMemoryAssetWriter();
await testMustachioBuilder('''
await testMustachioBuilder(writer, '''
class Foo<T> {}
''', libraryFrontMatter: '''
@Renderer(#renderFoo, Context<Foo>(), 'foo')
Expand All @@ -90,12 +64,13 @@ import 'package:mustachio/annotations.dart';
''');
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var generatedContent = utf8.decode(writer.assets[rendererAsset]);
expect(generatedContent, contains('String renderFoo<T>(Foo<T> context0)'));
expect(
generatedContent, contains('String renderFoo<T>(_i1.Foo<T> context0)'));
});

test('builds a private render function for a partial', () async {
writer = InMemoryAssetWriter();
await testMustachioBuilder(
writer,
'''
class Foo<T> {}
''',
Expand All @@ -111,17 +86,19 @@ import 'package:mustachio/annotations.dart';
);
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var generatedContent = utf8.decode(writer.assets[rendererAsset]);
expect(generatedContent,
contains('String _renderFoo_partial_foo_header_0<T>(Foo<T> context0)'));
expect(
generatedContent,
contains(
'String _renderFoo_partial_foo_header_0<T>(_i1.Foo<T> context0)'));
});

test('builds a renderer for a generic, bounded type', () async {
await testMustachioBuilder('''
await testMustachioBuilder(writer, '''
class Foo<T extends num> {}
class Bar {}
class Baz {}
''');
var renderersLibrary = await resolveGeneratedLibrary(writer);
var renderersLibrary = await resolveGeneratedLibrary();

var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo');
expect(fooRenderFunction.typeParameters, hasLength(1));
Expand Down
Loading