2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- @Timeout .factor (4 )
6
- import 'dart:convert' ;
7
- import 'package:analyzer/dart/element/element.dart' ;
8
- import 'package:build/build.dart' ;
9
- import 'package:build_test/build_test.dart' ;
5
+ @TestOn ('vm && !windows' )
6
+ library ;
7
+
8
+ import 'dart:io' ;
10
9
import 'package:test/test.dart' ;
10
+ import 'package:test_descriptor/test_descriptor.dart' as d;
11
11
12
12
import 'builder_test_base.dart' ;
13
13
14
14
void main () {
15
- late InMemoryAssetWriter writer;
16
-
17
- Future <LibraryElement > resolveGeneratedLibrary () async {
18
- var rendererAsset = AssetId ('foo' , 'lib/foo.aot_renderers_for_html.dart' );
19
- var writtenStrings = writer.assets
20
- .map ((id, content) => MapEntry (id.toString (), utf8.decode (content)));
21
- return await resolveSources (writtenStrings,
22
- (Resolver resolver) => resolver.libraryFor (rendererAsset));
23
- }
24
-
25
- setUp (() {
26
- writer = InMemoryAssetWriter ();
27
- });
28
-
29
15
test ('builds renderers from multiple annotations' , () async {
30
16
await testMustachioBuilder (
31
- writer,
32
17
'''
33
18
class Foo {
34
19
String s1 = 'hello';
@@ -40,14 +25,17 @@ class Baz {}
40
25
@Renderer(#renderFoo, Context<Foo>(), 'foo')
41
26
@Renderer(#renderBar, Context<Bar>(), 'bar')
42
27
library foo;
43
- import 'package:mustachio/ annotations.dart';
28
+ import 'annotations.dart';
44
29
''' ,
45
- additionalAssets: {
46
- 'foo|lib/templates/html/foo.html' : '{{ >foo_header }}' ,
47
- 'foo|lib/templates/html/_foo_header.html' : 'EMPTY' ,
48
- },
30
+ additionalAssets: () => [
31
+ d.dir ('lib/templates/html' , [
32
+ d.file ('foo.html' , '{{ >foo_header }}' ),
33
+ d.file ('_foo_header.html' , 'EMPTY' ),
34
+ ]),
35
+ ],
49
36
);
50
- var renderersLibrary = await resolveGeneratedLibrary ();
37
+ var renderersLibrary =
38
+ await resolveGeneratedLibrary (aotRenderersForHtmlPath);
51
39
52
40
expect (renderersLibrary.getTopLevelFunction ('renderFoo' ), isNotNull);
53
41
expect (renderersLibrary.getTopLevelFunction ('renderBar' ), isNotNull);
@@ -57,24 +45,33 @@ import 'package:mustachio/annotations.dart';
57
45
}, timeout: Timeout .factor (2 ));
58
46
59
47
test ('builds a public API render function' , () async {
60
- await testMustachioBuilder (writer, '''
48
+ await testMustachioBuilder (
49
+ '''
61
50
class Foo<T> {
62
51
String s1 = 'hello';
63
52
}
64
- ''' , libraryFrontMatter: '''
53
+ ''' ,
54
+ libraryFrontMatter: '''
65
55
@Renderer(#renderFoo, Context<Foo>(), 'foo')
66
56
library foo;
67
- import 'package:mustachio/annotations.dart';
68
- ''' );
69
- var rendererAsset = AssetId ('foo' , 'lib/foo.aot_renderers_for_html.dart' );
70
- var generatedContent = utf8.decode (writer.assets[rendererAsset]! );
57
+ import 'annotations.dart';
58
+ ''' ,
59
+ additionalAssets: () => [
60
+ d.dir ('lib/templates/html' , [
61
+ d.file ('foo.html' , 's1 is {{ s1 }}' ),
62
+ ]),
63
+ d.dir ('md' , [
64
+ d.file ('foo.md' , 's1 is {{ s1 }}' ),
65
+ ]),
66
+ ],
67
+ );
68
+ var generatedContent = await File (aotRenderersForHtmlPath).readAsString ();
71
69
expect (
72
70
generatedContent, contains ('String renderFoo<T>(_i1.Foo<T> context0)' ));
73
71
});
74
72
75
73
test ('builds a private render function for a partial' , () async {
76
74
await testMustachioBuilder (
77
- writer,
78
75
'''
79
76
class Foo<T> {
80
77
String s1 = 'hello';
@@ -83,30 +80,36 @@ class Foo<T> {
83
80
libraryFrontMatter: '''
84
81
@Renderer(#renderFoo, Context<Foo>(), 'foo')
85
82
library foo;
86
- import 'package:mustachio/ annotations.dart';
83
+ import 'annotations.dart';
87
84
''' ,
88
- additionalAssets: {
89
- 'foo|lib/templates/html/foo.html' : '{{ >foo_header }}' ,
90
- 'foo|lib/templates/html/_foo_header.html' : 's1 is {{ s1 }}' ,
91
- },
85
+ additionalAssets: () => [
86
+ d.dir ('lib' , [
87
+ d.dir ('templates' , [
88
+ d.dir ('html' , [
89
+ d.file ('foo.html' , '{{ >foo_header }}' ),
90
+ d.file ('_foo_header.html' , 's1 is {{ s1 }}' ),
91
+ ]),
92
+ ]),
93
+ ]),
94
+ ],
92
95
);
93
- var rendererAsset = AssetId ('foo' , 'lib/foo.aot_renderers_for_html.dart' );
94
- var generatedContent = utf8.decode (writer.assets[rendererAsset]! );
96
+ var generatedContent = await File (aotRenderersForHtmlPath).readAsString ();
95
97
expect (
96
98
generatedContent,
97
99
contains (
98
100
'String _renderFoo_partial_foo_header_0<T>(_i1.Foo<T> context0)' ));
99
101
});
100
102
101
103
test ('builds a renderer for a generic, bounded type' , () async {
102
- await testMustachioBuilder (writer, '''
104
+ await testMustachioBuilder ('''
103
105
class Foo<T extends num> {
104
106
String s1 = 'hello';
105
107
}
106
108
class Bar {}
107
109
class Baz {}
108
110
''' );
109
- var renderersLibrary = await resolveGeneratedLibrary ();
111
+ var renderersLibrary =
112
+ await resolveGeneratedLibrary (aotRenderersForHtmlPath);
110
113
111
114
var fooRenderFunction = renderersLibrary.getTopLevelFunction ('renderFoo' )! ;
112
115
expect (fooRenderFunction.typeParameters, hasLength (1 ));
@@ -116,7 +119,6 @@ class Baz {}
116
119
117
120
test ('deduplicates partials which share context type LUB' , () async {
118
121
await testMustachioBuilder (
119
- writer,
120
122
'''
121
123
abstract class Base {
122
124
String get s1;
@@ -136,16 +138,17 @@ class Bar implements Base {
136
138
@Renderer(#renderFoo, Context<Foo>(), 'foo')
137
139
@Renderer(#renderBar, Context<Bar>(), 'bar')
138
140
library foo;
139
- import 'package:mustachio/ annotations.dart';
141
+ import 'annotations.dart';
140
142
''' ,
141
- additionalAssets: {
142
- 'foo|lib/templates/html/foo.html' : '{{ >base }}' ,
143
- 'foo|lib/templates/html/bar.html' : '{{ >base }}' ,
144
- 'foo|lib/templates/html/_base.html' : 's1 is {{ s1 }}' ,
145
- },
143
+ additionalAssets: () => [
144
+ d.dir ('lib/templates/html' , [
145
+ d.file ('foo.html' , '{{ >base }}' ),
146
+ d.file ('bar.html' , '{{ >base }}' ),
147
+ d.file ('_base.html' , 's1 is {{ s1 }}' ),
148
+ ]),
149
+ ],
146
150
);
147
- var rendererAsset = AssetId ('foo' , 'lib/foo.aot_renderers_for_html.dart' );
148
- var generatedContent = utf8.decode (writer.assets[rendererAsset]! );
151
+ var generatedContent = await File (aotRenderersForHtmlPath).readAsString ();
149
152
expect (
150
153
generatedContent,
151
154
contains ('String _renderFoo_partial_base_0(_i1.Foo context0) =>\n '
@@ -165,7 +168,6 @@ import 'package:mustachio/annotations.dart';
165
168
test ('does not deduplicate partials when attempting to do so throws' ,
166
169
() async {
167
170
await testMustachioBuilder (
168
- writer,
169
171
'''
170
172
abstract class Base {}
171
173
@@ -183,16 +185,17 @@ class Bar implements Base {
183
185
@Renderer(#renderFoo, Context<Foo>(), 'foo')
184
186
@Renderer(#renderBar, Context<Bar>(), 'bar')
185
187
library foo;
186
- import 'package:mustachio/ annotations.dart';
188
+ import 'annotations.dart';
187
189
''' ,
188
- additionalAssets: {
189
- 'foo|lib/templates/html/foo.html' : '{{ >base }}' ,
190
- 'foo|lib/templates/html/bar.html' : '{{ >base }}' ,
191
- 'foo|lib/templates/html/_base.html' : 's1 is {{ s1 }}' ,
192
- },
190
+ additionalAssets: () => [
191
+ d.dir ('lib/templates/html' , [
192
+ d.file ('foo.html' , '{{ >base }}' ),
193
+ d.file ('bar.html' , '{{ >base }}' ),
194
+ d.file ('_base.html' , 's1 is {{ s1 }}' ),
195
+ ]),
196
+ ],
193
197
);
194
- var rendererAsset = AssetId ('foo' , 'lib/foo.aot_renderers_for_html.dart' );
195
- var generatedContent = utf8.decode (writer.assets[rendererAsset]! );
198
+ var generatedContent = await File (aotRenderersForHtmlPath).readAsString ();
196
199
expect (
197
200
generatedContent,
198
201
contains ('String _renderFoo_partial_base_0(_i1.Foo context0) {' ),
@@ -203,3 +206,6 @@ import 'package:mustachio/annotations.dart';
203
206
);
204
207
});
205
208
}
209
+
210
+ String get aotRenderersForHtmlPath =>
211
+ '${d .sandbox }/foo_package/lib/foo.aot_renderers_for_html.dart' ;
0 commit comments