Skip to content

Commit e58ae4e

Browse files
authored
Add end-to-end tests for Mustachio AOT compiler (dart-lang#2664)
Add end-to-end tests for Mustachio AOT compiler These tests, in aot_compiler_render_test.dart, are a little irregular. The goal is to write short test cases in which we specify the text of a Mustache template, and the expected rendered text, visually near each other, which is a challenge, as there are several steps to get from Mustache template text to rendered output, including running the builders, and executing freshly generated Dart code. The solution here is a test system which takes a Mustache template text, runs the mustachio AOT compiler to generate a Dart script which renders a context object into that template, then executes the generated Dart script, and asserts on the output. Writing these tests revealed a few bugs that are fixed concurrently: * imports in the generated script should be accurate; this requires using code_builder, which requires a sizeable change to the AOT compiler code. code_builder allows you to "reference" symbols like types or top-level functions and the URL where they may be found. * handle partial template paths with dots and slashes. Additionally, fix some nits in runtime_renderer_builder_test.dart and runtime_renderer_render_test.dart.
1 parent 0922660 commit e58ae4e

12 files changed

+2276
-1152
lines changed

lib/src/generator/templates.aot_renderers_for_html.dart

Lines changed: 911 additions & 614 deletions
Large diffs are not rendered by default.

lib/src/generator/templates.aot_renderers_for_md.dart

Lines changed: 638 additions & 358 deletions
Large diffs are not rendered by default.

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dev_dependencies:
3030
build_runner: ^2.0.1
3131
build_test: ^2.0.0
3232
build_version: ^2.0.1
33+
code_builder: ^4.0.0
3334
coverage: ^1.0.2
3435
dart_style: ^2.0.0
3536
grinder: ^0.9.0-nullsafety.0

test/mustachio/aot_compiler_builder_test.dart

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,26 @@ import 'package:build/build.dart';
99
import 'package:build_test/build_test.dart';
1010
import 'package:test/test.dart';
1111

12-
import '../../tool/mustachio/builder.dart';
1312
import 'builder_test_base.dart';
1413

1514
void main() {
1615
InMemoryAssetWriter writer;
1716

18-
Future<LibraryElement> resolveGeneratedLibrary(
19-
InMemoryAssetWriter writer) async {
17+
Future<LibraryElement> resolveGeneratedLibrary() async {
2018
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
2119
var writtenStrings = writer.assets
2220
.map((id, content) => MapEntry(id.toString(), utf8.decode(content)));
2321
return await resolveSources(writtenStrings,
2422
(Resolver resolver) => resolver.libraryFor(rendererAsset));
2523
}
2624

27-
Future<void> testMustachioBuilder(
28-
String sourceLibraryContent, {
29-
String libraryFrontMatter = libraryFrontMatter,
30-
Map<String, String> additionalAssets,
31-
}) async {
32-
sourceLibraryContent = '''
33-
$libraryFrontMatter
34-
$sourceLibraryContent
35-
''';
36-
await testBuilder(
37-
mustachioBuilder(BuilderOptions({})),
38-
{
39-
...annotationsAsset,
40-
'foo|lib/foo.dart': sourceLibraryContent,
41-
'foo|lib/templates/html/foo.html': 'EMPTY',
42-
'foo|lib/templates/md/foo.md': 'EMPTY',
43-
'foo|lib/templates/html/bar.html': 'EMPTY',
44-
'foo|lib/templates/md/bar.md': 'EMPTY',
45-
...?additionalAssets,
46-
},
47-
writer: writer,
48-
);
49-
}
50-
5125
setUp(() {
5226
writer = InMemoryAssetWriter();
5327
});
5428

5529
test('builds renderers from multiple annotations', () async {
5630
await testMustachioBuilder(
31+
writer,
5732
'''
5833
class Foo {}
5934
class Bar {}
@@ -70,7 +45,7 @@ import 'package:mustachio/annotations.dart';
7045
'foo|lib/templates/html/_foo_header.html': 'EMPTY',
7146
},
7247
);
73-
var renderersLibrary = await resolveGeneratedLibrary(writer);
48+
var renderersLibrary = await resolveGeneratedLibrary();
7449

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

8257
test('builds a public API render function', () async {
83-
writer = InMemoryAssetWriter();
84-
await testMustachioBuilder('''
58+
await testMustachioBuilder(writer, '''
8559
class Foo<T> {}
8660
''', libraryFrontMatter: '''
8761
@Renderer(#renderFoo, Context<Foo>(), 'foo')
@@ -90,12 +64,13 @@ import 'package:mustachio/annotations.dart';
9064
''');
9165
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
9266
var generatedContent = utf8.decode(writer.assets[rendererAsset]);
93-
expect(generatedContent, contains('String renderFoo<T>(Foo<T> context0)'));
67+
expect(
68+
generatedContent, contains('String renderFoo<T>(_i1.Foo<T> context0)'));
9469
});
9570

9671
test('builds a private render function for a partial', () async {
97-
writer = InMemoryAssetWriter();
9872
await testMustachioBuilder(
73+
writer,
9974
'''
10075
class Foo<T> {}
10176
''',
@@ -111,17 +86,19 @@ import 'package:mustachio/annotations.dart';
11186
);
11287
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
11388
var generatedContent = utf8.decode(writer.assets[rendererAsset]);
114-
expect(generatedContent,
115-
contains('String _renderFoo_partial_foo_header_0<T>(Foo<T> context0)'));
89+
expect(
90+
generatedContent,
91+
contains(
92+
'String _renderFoo_partial_foo_header_0<T>(_i1.Foo<T> context0)'));
11693
});
11794

11895
test('builds a renderer for a generic, bounded type', () async {
119-
await testMustachioBuilder('''
96+
await testMustachioBuilder(writer, '''
12097
class Foo<T extends num> {}
12198
class Bar {}
12299
class Baz {}
123100
''');
124-
var renderersLibrary = await resolveGeneratedLibrary(writer);
101+
var renderersLibrary = await resolveGeneratedLibrary();
125102

126103
var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo');
127104
expect(fooRenderFunction.typeParameters, hasLength(1));

0 commit comments

Comments
 (0)