Skip to content

Commit 349416e

Browse files
author
Tim Blasi
committed
test(dart/transform): e2e test inliner_for_test
Add an e2e test for the `inliner_for_test` transformer.
1 parent f638834 commit 349416e

File tree

5 files changed

+106
-14
lines changed

5 files changed

+106
-14
lines changed

modules_dart/transform/lib/src/transform/inliner_for_test.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ import 'package:analyzer/src/generated/ast.dart';
77
import 'package:angular2/src/core/compiler/xhr.dart' show XHR;
88
import 'package:angular2/src/transform/common/asset_reader.dart';
99
import 'package:barback/barback.dart';
10+
import 'package:dart_style/dart_style.dart';
1011

1112
import 'common/asset_reader.dart';
1213
import 'common/async_string_writer.dart';
1314
import 'common/logging.dart';
15+
import 'common/options_reader.dart';
1416
import 'common/url_resolver.dart';
1517
import 'common/xhr_impl.dart';
1618
import 'directive_processor/inliner.dart';
1719

1820
/// Processes .dart files and inlines `templateUrl` and styleUrls` values.
1921
class InlinerForTest extends Transformer implements DeclaringTransformer {
20-
final BarbackSettings settings;
22+
final DartFormatter _formatter;
2123

22-
InlinerForTest(this.settings);
24+
InlinerForTest({bool formatCode: false})
25+
: _formatter = formatCode ? new DartFormatter() : null;
2326

2427
@override
2528
bool isPrimary(AssetId id) => id.extension.endsWith('dart');
@@ -40,13 +43,17 @@ class InlinerForTest extends Transformer implements DeclaringTransformer {
4043
if (inlinedCode == null || inlinedCode.isEmpty) {
4144
transform.addOutput(transform.primaryInput);
4245
} else {
46+
if (_formatter != null) {
47+
inlinedCode = _formatter.format(inlinedCode);
48+
}
4349
transform.addOutput(new Asset.fromString(primaryId, inlinedCode));
4450
}
4551
});
4652
}
4753

4854
factory InlinerForTest.asPlugin(BarbackSettings settings) {
49-
return new InlinerForTest(settings);
55+
return new InlinerForTest(
56+
formatCode: parseBarbackSettings(settings).formatCode);
5057
}
5158
}
5259

@@ -89,10 +96,10 @@ class _ViewPropInliner extends ToSourceVisitor {
8996
switch (keyString) {
9097
case 'templateUrl':
9198
_populateTemplateUrl(node.expression);
92-
break;
99+
return null;
93100
case 'styleUrls':
94101
_populateStyleUrls(node.expression);
95-
break;
102+
return null;
96103
}
97104
return super.visitNamedExpression(node);
98105
}
@@ -113,7 +120,7 @@ class _ViewPropInliner extends ToSourceVisitor {
113120
logger.warning('style url is not a String (${url})');
114121
}
115122
}
116-
_writer.println('],');
123+
_writer.println(']');
117124
}
118125

119126
void _populateTemplateUrl(Expression value) {
@@ -124,7 +131,7 @@ class _ViewPropInliner extends ToSourceVisitor {
124131
}
125132
_writer.print("template: r'''");
126133
_writer.asyncPrint(_readOrEmptyString(url));
127-
_writer.println("''',");
134+
_writer.println("'''");
128135
}
129136

130137
/// Attempts to read the content from [url]. If [url] is relative, uses
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
library examples.src.hello_world.absolute_url_expression_files;
2+
3+
import 'package:angular2/angular2.dart'
4+
show Component, Directive, View, NgElement;
5+
6+
@Component(selector: 'hello-app')
7+
@View(
8+
template: r'''{{greeting}}''',
9+
styles: const [r'''.greeting { .color: blue; }''',])
10+
class HelloCmp {}
11+
12+
@Injectable() hello() {}

modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/hello.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ import 'package:angular2/angular2.dart'
99
styleUrls: const ['package:other_package/template.css'])
1010
class HelloCmp {}
1111

12-
@Injectable()
13-
hello() {}
12+
@Injectable() hello() {}

modules_dart/transform/test/transform/inliner_for_test/all_tests.dart

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
library angular2.test.transform.inliner_for_test.all_tests;
22

3-
import 'dart:async';
4-
import 'dart:convert';
5-
63
import 'package:angular2/src/transform/common/asset_reader.dart';
74
import 'package:angular2/src/transform/inliner_for_test.dart';
85
import 'package:barback/barback.dart';
6+
import 'package:code_transformers/tests.dart';
97
import 'package:guinness/guinness.dart';
108
import 'package:dart_style/dart_style.dart';
119

@@ -15,9 +13,10 @@ main() {
1513
allTests();
1614
}
1715

18-
allTests() {
16+
DartFormatter formatter = new DartFormatter();
17+
18+
void allTests() {
1919
AssetReader absoluteReader;
20-
DartFormatter formatter = new DartFormatter();
2120

2221
beforeEach(() {
2322
absoluteReader = new TestAssetReader();
@@ -79,6 +78,68 @@ allTests() {
7978

8079
expect(output).toContain("{{greeting}}");
8180
});
81+
82+
_runAbsoluteUrlEndToEndTest();
83+
_runMultiStylesEndToEndTest();
8284
}
8385

8486
AssetId _assetId(String path) => new AssetId('a', 'inliner_for_test/$path');
87+
88+
void _runAbsoluteUrlEndToEndTest() {
89+
InlinerForTest transformer = new InlinerForTest(formatCode: true);
90+
var inputMap = {
91+
'a|absolute_url_expression_files/hello.dart':
92+
_readFile('absolute_url_expression_files/hello.dart'),
93+
'other_package|lib/template.css':
94+
_readFile('absolute_url_expression_files/template.css'),
95+
'other_package|lib/template.html':
96+
_readFile('absolute_url_expression_files/template.html')
97+
};
98+
var outputMap = {
99+
'a|absolute_url_expression_files/hello.dart':
100+
_readFile('absolute_url_expression_files/expected/hello.dart')
101+
};
102+
testPhases(
103+
'Inliner For Test should inline `templateUrl` and `styleUrls` values '
104+
'expressed as absolute urls',
105+
[
106+
[transformer]
107+
],
108+
inputMap,
109+
outputMap,
110+
[]);
111+
}
112+
113+
void _runMultiStylesEndToEndTest() {
114+
InlinerForTest transformer = new InlinerForTest(formatCode: true);
115+
var inputMap = {
116+
'pkg|web/hello.dart': _readFile('multiple_style_urls_files/hello.dart'),
117+
'pkg|web/template.css': _readFile('multiple_style_urls_files/template.css'),
118+
'pkg|web/template_other.css':
119+
_readFile('multiple_style_urls_files/template_other.css'),
120+
'pkg|web/template.html':
121+
_readFile('multiple_style_urls_files/template.html')
122+
};
123+
var outputMap = {
124+
'pkg|web/hello.dart':
125+
_readFile('multiple_style_urls_files/expected/hello.dart')
126+
};
127+
testPhases(
128+
'Inliner For Test should inline `templateUrl` and `styleUrls` values '
129+
'expressed as relative urls',
130+
[
131+
[transformer]
132+
],
133+
inputMap,
134+
outputMap,
135+
[]);
136+
}
137+
138+
/// Smooths over differences in CWD between IDEs and running tests in Travis.
139+
String _readFile(String path) {
140+
var code = readFile('inliner_for_test/$path');
141+
if (path.endsWith('.dart')) {
142+
code = formatter.format(code);
143+
}
144+
return code;
145+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
library examples.src.hello_world.multiple_style_urls_files;
2+
3+
import 'package:angular2/angular2.dart'
4+
show Component, Directive, View, NgElement;
5+
6+
@Component(selector: 'hello-app')
7+
@View(
8+
template: r'''{{greeting}}''',
9+
styles: const [
10+
r'''.greeting { .color: blue; }''',
11+
r'''.hello { .color: red; }''',
12+
])
13+
class HelloCmp {}

0 commit comments

Comments
 (0)