feat(dart/transform): Inline templateUrl values

Modify DirectiveProcessor to inline `templateUrl` values to avoid making
additional browser requests.

Closes #1035
This commit is contained in:
Tim Blasi
2015-05-08 17:29:21 -07:00
parent 655ed851f0
commit 97d24563f4
13 changed files with 294 additions and 24 deletions
@@ -0,0 +1,106 @@
library angular2.test.transform.common.async_string_writer;
import 'dart:async';
import 'package:angular2/src/transform/common/async_string_writer.dart';
import 'package:guinness/guinness.dart';
void allTests() {
it('should function as a basic Writer without async calls.', () {
var writer = new AsyncStringWriter();
writer.print('hello');
expect('$writer').toEqual('hello');
writer.println(', world');
expect('$writer').toEqual('hello, world\n');
});
it('should concatenate futures added with `asyncPrint`.', () async {
var writer = new AsyncStringWriter();
writer.print('hello');
expect('$writer').toEqual('hello');
writer.asyncPrint(new Future.value(', world.'));
writer.print(' It is a beautiful day.');
expect(await writer.asyncToString())
.toEqual('hello, world. It is a beautiful day.');
});
it('should concatenate multiple futures regardless of order.', () async {
var completer1 = new Completer<String>();
var completer2 = new Completer<String>();
var writer = new AsyncStringWriter();
writer.print('hello');
expect('$writer').toEqual('hello');
writer.asyncPrint(completer1.future);
writer.asyncPrint(completer2.future);
completer2.complete(' It is a beautiful day.');
completer1.complete(', world.');
expect(await writer.asyncToString())
.toEqual('hello, world. It is a beautiful day.');
});
it('should allow multiple "rounds" of `asyncPrint`.', () async {
var writer = new AsyncStringWriter();
writer.print('hello');
expect('$writer').toEqual('hello');
writer.asyncPrint(new Future.value(', world.'));
expect(await writer.asyncToString()).toEqual('hello, world.');
writer.asyncPrint(new Future.value(' It is '));
writer.asyncPrint(new Future.value('a beautiful '));
writer.asyncPrint(new Future.value('day.'));
expect(await writer.asyncToString())
.toEqual('hello, world. It is a beautiful day.');
});
it('should handle calls to async methods while waiting.', () {
var completer1 = new Completer<String>();
var completer2 = new Completer<String>();
var writer = new AsyncStringWriter();
writer.print('hello');
expect('$writer').toEqual('hello');
writer.asyncPrint(completer1.future);
var f1 = writer.asyncToString().then((result) {
expect(result).toEqual('hello, world.');
});
writer.asyncPrint(completer2.future);
var f2 = writer.asyncToString().then((result) {
expect(result).toEqual('hello, world. It is a beautiful day.');
});
completer1.complete(', world.');
completer2.complete(' It is a beautiful day.');
return Future.wait([f1, f2]);
});
it('should handle calls to async methods that complete in reverse '
'order while waiting.', () {
var completer1 = new Completer<String>();
var completer2 = new Completer<String>();
var writer = new AsyncStringWriter();
writer.print('hello');
expect('$writer').toEqual('hello');
writer.asyncPrint(completer1.future);
var f1 = writer.asyncToString().then((result) {
expect(result).toEqual('hello, world.');
});
writer.asyncPrint(completer2.future);
var f2 = writer.asyncToString().then((result) {
expect(result).toEqual('hello, world. It is a beautiful day.');
});
completer2.complete(' It is a beautiful day.');
completer1.complete(', world.');
return Future.wait([f1, f2]);
});
}
@@ -19,15 +19,29 @@ String readFile(String path) {
}
class TestAssetReader implements AssetReader {
Future<String> readAsString(AssetId id, {Encoding encoding}) =>
new Future.value(readFile(id.path));
/// This allows "faking"
final Map<AssetId, String> _overrideAssets = <AssetId, String>{};
Future<String> readAsString(AssetId id, {Encoding encoding}) {
if (_overrideAssets.containsKey(id)) {
return new Future.value(_overrideAssets[id]);
} else {
return new Future.value(readFile(id.path));
}
}
Future<bool> hasInput(AssetId id) {
var exists = false;
var exists = _overrideAssets.containsKey(id);
if (exists) return new Future.value(true);
for (var myPath in [id.path, 'test/transform/${id.path}']) {
var file = new File(myPath);
exists = exists || file.existsSync();
}
return new Future.value(exists);
}
void addAsset(AssetId id, String contents) {
_overrideAssets[id] = contents;
}
}