From 349416ea533d247550758fc7c4739a16dba921a7 Mon Sep 17 00:00:00 2001 From: Tim Blasi Date: Fri, 2 Oct 2015 17:01:27 -0700 Subject: [PATCH] test(dart/transform): e2e test inliner_for_test Add an e2e test for the `inliner_for_test` transformer. --- .../lib/src/transform/inliner_for_test.dart | 21 ++++-- .../expected/hello.dart | 12 ++++ .../absolute_url_expression_files/hello.dart | 3 +- .../transform/inliner_for_test/all_tests.dart | 71 +++++++++++++++++-- .../expected/hello.dart | 13 ++++ 5 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/expected/hello.dart create mode 100644 modules_dart/transform/test/transform/inliner_for_test/multiple_style_urls_files/expected/hello.dart diff --git a/modules_dart/transform/lib/src/transform/inliner_for_test.dart b/modules_dart/transform/lib/src/transform/inliner_for_test.dart index 3fb0ee8ce2..c4fec90745 100644 --- a/modules_dart/transform/lib/src/transform/inliner_for_test.dart +++ b/modules_dart/transform/lib/src/transform/inliner_for_test.dart @@ -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 diff --git a/modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/expected/hello.dart b/modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/expected/hello.dart new file mode 100644 index 0000000000..079ee5ba91 --- /dev/null +++ b/modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/expected/hello.dart @@ -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() {} diff --git a/modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/hello.dart b/modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/hello.dart index 1015364625..ec73a38242 100644 --- a/modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/hello.dart +++ b/modules_dart/transform/test/transform/inliner_for_test/absolute_url_expression_files/hello.dart @@ -9,5 +9,4 @@ import 'package:angular2/angular2.dart' styleUrls: const ['package:other_package/template.css']) class HelloCmp {} -@Injectable() -hello() {} +@Injectable() hello() {} diff --git a/modules_dart/transform/test/transform/inliner_for_test/all_tests.dart b/modules_dart/transform/test/transform/inliner_for_test/all_tests.dart index 902f3acbb6..c3078f0d7f 100644 --- a/modules_dart/transform/test/transform/inliner_for_test/all_tests.dart +++ b/modules_dart/transform/test/transform/inliner_for_test/all_tests.dart @@ -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; +} diff --git a/modules_dart/transform/test/transform/inliner_for_test/multiple_style_urls_files/expected/hello.dart b/modules_dart/transform/test/transform/inliner_for_test/multiple_style_urls_files/expected/hello.dart new file mode 100644 index 0000000000..5e3095c23b --- /dev/null +++ b/modules_dart/transform/test/transform/inliner_for_test/multiple_style_urls_files/expected/hello.dart @@ -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 {}