test(dart/transform): e2e test inliner_for_test

Add an e2e test for the `inliner_for_test` transformer.
This commit is contained in:
Tim Blasi 2015-10-02 17:01:27 -07:00
parent f638834fcf
commit 349416ea53
5 changed files with 106 additions and 14 deletions

View File

@ -7,19 +7,22 @@ import 'package:analyzer/src/generated/ast.dart';
import 'package:angular2/src/core/compiler/xhr.dart' show XHR;
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:barback/barback.dart';
import 'package:dart_style/dart_style.dart';
import 'common/asset_reader.dart';
import 'common/async_string_writer.dart';
import 'common/logging.dart';
import 'common/options_reader.dart';
import 'common/url_resolver.dart';
import 'common/xhr_impl.dart';
import 'directive_processor/inliner.dart';
/// Processes .dart files and inlines `templateUrl` and styleUrls` values.
class InlinerForTest extends Transformer implements DeclaringTransformer {
final BarbackSettings settings;
final DartFormatter _formatter;
InlinerForTest(this.settings);
InlinerForTest({bool formatCode: false})
: _formatter = formatCode ? new DartFormatter() : null;
@override
bool isPrimary(AssetId id) => id.extension.endsWith('dart');
@ -40,13 +43,17 @@ class InlinerForTest extends Transformer implements DeclaringTransformer {
if (inlinedCode == null || inlinedCode.isEmpty) {
transform.addOutput(transform.primaryInput);
} else {
if (_formatter != null) {
inlinedCode = _formatter.format(inlinedCode);
}
transform.addOutput(new Asset.fromString(primaryId, inlinedCode));
}
});
}
factory InlinerForTest.asPlugin(BarbackSettings settings) {
return new InlinerForTest(settings);
return new InlinerForTest(
formatCode: parseBarbackSettings(settings).formatCode);
}
}
@ -89,10 +96,10 @@ class _ViewPropInliner extends ToSourceVisitor {
switch (keyString) {
case 'templateUrl':
_populateTemplateUrl(node.expression);
break;
return null;
case 'styleUrls':
_populateStyleUrls(node.expression);
break;
return null;
}
return super.visitNamedExpression(node);
}
@ -113,7 +120,7 @@ class _ViewPropInliner extends ToSourceVisitor {
logger.warning('style url is not a String (${url})');
}
}
_writer.println('],');
_writer.println(']');
}
void _populateTemplateUrl(Expression value) {
@ -124,7 +131,7 @@ class _ViewPropInliner extends ToSourceVisitor {
}
_writer.print("template: r'''");
_writer.asyncPrint(_readOrEmptyString(url));
_writer.println("''',");
_writer.println("'''");
}
/// Attempts to read the content from [url]. If [url] is relative, uses

View File

@ -0,0 +1,12 @@
library examples.src.hello_world.absolute_url_expression_files;
import 'package:angular2/angular2.dart'
show Component, Directive, View, NgElement;
@Component(selector: 'hello-app')
@View(
template: r'''{{greeting}}''',
styles: const [r'''.greeting { .color: blue; }''',])
class HelloCmp {}
@Injectable() hello() {}

View File

@ -9,5 +9,4 @@ import 'package:angular2/angular2.dart'
styleUrls: const ['package:other_package/template.css'])
class HelloCmp {}
@Injectable()
hello() {}
@Injectable() hello() {}

View File

@ -1,11 +1,9 @@
library angular2.test.transform.inliner_for_test.all_tests;
import 'dart:async';
import 'dart:convert';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/inliner_for_test.dart';
import 'package:barback/barback.dart';
import 'package:code_transformers/tests.dart';
import 'package:guinness/guinness.dart';
import 'package:dart_style/dart_style.dart';
@ -15,9 +13,10 @@ main() {
allTests();
}
allTests() {
DartFormatter formatter = new DartFormatter();
void allTests() {
AssetReader absoluteReader;
DartFormatter formatter = new DartFormatter();
beforeEach(() {
absoluteReader = new TestAssetReader();
@ -79,6 +78,68 @@ allTests() {
expect(output).toContain("{{greeting}}");
});
_runAbsoluteUrlEndToEndTest();
_runMultiStylesEndToEndTest();
}
AssetId _assetId(String path) => new AssetId('a', 'inliner_for_test/$path');
void _runAbsoluteUrlEndToEndTest() {
InlinerForTest transformer = new InlinerForTest(formatCode: true);
var inputMap = {
'a|absolute_url_expression_files/hello.dart':
_readFile('absolute_url_expression_files/hello.dart'),
'other_package|lib/template.css':
_readFile('absolute_url_expression_files/template.css'),
'other_package|lib/template.html':
_readFile('absolute_url_expression_files/template.html')
};
var outputMap = {
'a|absolute_url_expression_files/hello.dart':
_readFile('absolute_url_expression_files/expected/hello.dart')
};
testPhases(
'Inliner For Test should inline `templateUrl` and `styleUrls` values '
'expressed as absolute urls',
[
[transformer]
],
inputMap,
outputMap,
[]);
}
void _runMultiStylesEndToEndTest() {
InlinerForTest transformer = new InlinerForTest(formatCode: true);
var inputMap = {
'pkg|web/hello.dart': _readFile('multiple_style_urls_files/hello.dart'),
'pkg|web/template.css': _readFile('multiple_style_urls_files/template.css'),
'pkg|web/template_other.css':
_readFile('multiple_style_urls_files/template_other.css'),
'pkg|web/template.html':
_readFile('multiple_style_urls_files/template.html')
};
var outputMap = {
'pkg|web/hello.dart':
_readFile('multiple_style_urls_files/expected/hello.dart')
};
testPhases(
'Inliner For Test should inline `templateUrl` and `styleUrls` values '
'expressed as relative urls',
[
[transformer]
],
inputMap,
outputMap,
[]);
}
/// Smooths over differences in CWD between IDEs and running tests in Travis.
String _readFile(String path) {
var code = readFile('inliner_for_test/$path');
if (path.endsWith('.dart')) {
code = formatter.format(code);
}
return code;
}

View File

@ -0,0 +1,13 @@
library examples.src.hello_world.multiple_style_urls_files;
import 'package:angular2/angular2.dart'
show Component, Directive, View, NgElement;
@Component(selector: 'hello-app')
@View(
template: r'''{{greeting}}''',
styles: const [
r'''.greeting { .color: blue; }''',
r'''.hello { .color: red; }''',
])
class HelloCmp {}