chore(transform): move transform module to modules_dart
The build/pure-packages.dart gulp task has also been updated to move the files into the angular2 tree. Closes #3729
This commit is contained in:
@ -0,0 +1,104 @@
|
||||
library angular2.test.transform.common.annotation_matcher_test;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:analyzer/analyzer.dart';
|
||||
import 'package:angular2/src/transform/common/annotation_matcher.dart';
|
||||
import 'package:barback/barback.dart' show AssetId;
|
||||
import 'package:guinness/guinness.dart';
|
||||
|
||||
main() {
|
||||
allTests();
|
||||
}
|
||||
|
||||
var simpleAst = parseCompilationUnit('''
|
||||
import 'package:test/test.dart';
|
||||
|
||||
@Test()
|
||||
var foo;
|
||||
''');
|
||||
|
||||
var namespacedAst = parseCompilationUnit('''
|
||||
import 'package:test/test.dart' as test;
|
||||
|
||||
@test.Test()
|
||||
var foo;
|
||||
''');
|
||||
|
||||
var relativePathAst = parseCompilationUnit('''
|
||||
import 'test.dart';
|
||||
|
||||
@Test()
|
||||
var foo;
|
||||
''');
|
||||
|
||||
var namespacedRelativePathAst = parseCompilationUnit('''
|
||||
import 'test.dart' as test;
|
||||
|
||||
@test.Test()
|
||||
var foo;
|
||||
''');
|
||||
|
||||
void allTests() {
|
||||
it('should be able to match basic annotations.', () {
|
||||
var matcher = new AnnotationMatcher()
|
||||
..add(const AnnotationDescriptor('Test', 'package:test/test.dart', null));
|
||||
var visitor = new MatchRecordingVisitor(matcher);
|
||||
simpleAst.accept(visitor);
|
||||
expect(visitor.matches.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should be able to match namespaced annotations.', () {
|
||||
var matcher = new AnnotationMatcher()
|
||||
..add(const AnnotationDescriptor('Test', 'package:test/test.dart', null));
|
||||
var visitor = new MatchRecordingVisitor(matcher);
|
||||
namespacedAst.accept(visitor);
|
||||
expect(visitor.matches.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should be able to match relative imports.', () {
|
||||
var matcher = new AnnotationMatcher()
|
||||
..add(const AnnotationDescriptor('Test', 'package:test/test.dart', null));
|
||||
var visitor =
|
||||
new MatchRecordingVisitor(matcher, new AssetId('test', 'lib/foo.dart'));
|
||||
relativePathAst.accept(visitor);
|
||||
expect(visitor.matches.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should be able to match relative imports with a namespace.', () {
|
||||
var matcher = new AnnotationMatcher()
|
||||
..add(const AnnotationDescriptor('Test', 'package:test/test.dart', null));
|
||||
var visitor =
|
||||
new MatchRecordingVisitor(matcher, new AssetId('test', 'lib/foo.dart'));
|
||||
namespacedRelativePathAst.accept(visitor);
|
||||
expect(visitor.matches.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should not match annotations if the import is missing.', () {
|
||||
var matcher = new AnnotationMatcher()
|
||||
..add(const AnnotationDescriptor('Test', 'package:test/foo.dart', null));
|
||||
var visitor = new MatchRecordingVisitor(matcher);
|
||||
simpleAst.accept(visitor);
|
||||
expect(visitor.matches.isEmpty).toBeTrue();
|
||||
});
|
||||
|
||||
it('should not match annotations if the name is different.', () {
|
||||
var matcher = new AnnotationMatcher()
|
||||
..add(const AnnotationDescriptor('Foo', 'package:test/test.dart', null));
|
||||
var visitor = new MatchRecordingVisitor(matcher);
|
||||
simpleAst.accept(visitor);
|
||||
expect(visitor.matches.isEmpty).toBeTrue();
|
||||
});
|
||||
}
|
||||
|
||||
class MatchRecordingVisitor extends RecursiveAstVisitor {
|
||||
final AssetId assetId;
|
||||
final AnnotationMatcher matcher;
|
||||
final List<Annotation> matches = [];
|
||||
|
||||
MatchRecordingVisitor(this.matcher, [this.assetId]) : super();
|
||||
|
||||
@override
|
||||
void visitAnnotation(Annotation annotation) {
|
||||
if (matcher.hasMatch(annotation, assetId)) matches.add(annotation);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
library angular2.test.transform;
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
/// Mocked out version of `bootstrap`, defined in application.dart. Importing
|
||||
/// the actual file in tests causes issues with resolution due to its
|
||||
/// transitive dependencies.
|
||||
Future bootstrap(Type appComponentType,
|
||||
[List bindings = null, Function givenBootstrapErrorReporter = null]) {
|
||||
return null;
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
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]);
|
||||
});
|
||||
}
|
101
modules_dart/transform/test/transform/common/ng_meta_test.dart
Normal file
101
modules_dart/transform/test/transform/common/ng_meta_test.dart
Normal file
@ -0,0 +1,101 @@
|
||||
library angular2.test.transform.common.annotation_matcher_test;
|
||||
|
||||
import 'package:angular2/src/render/api.dart';
|
||||
import 'package:angular2/src/transform/common/ng_meta.dart';
|
||||
import 'package:guinness/guinness.dart';
|
||||
|
||||
main() => allTests();
|
||||
|
||||
void allTests() {
|
||||
var mockData = [
|
||||
new RenderDirectiveMetadata(id: 'm1'),
|
||||
new RenderDirectiveMetadata(id: 'm2'),
|
||||
new RenderDirectiveMetadata(id: 'm3'),
|
||||
new RenderDirectiveMetadata(id: 'm4')
|
||||
];
|
||||
|
||||
it('should allow empty data.', () {
|
||||
var ngMeta = new NgMeta.empty();
|
||||
expect(ngMeta.isEmpty).toBeTrue();
|
||||
});
|
||||
|
||||
describe('serialization', () {
|
||||
it('should parse empty data correctly.', () {
|
||||
var ngMeta = new NgMeta.fromJson({});
|
||||
expect(ngMeta.isEmpty).toBeTrue();
|
||||
});
|
||||
|
||||
it('should be lossless', () {
|
||||
var a = new NgMeta.empty();
|
||||
a.types['T0'] = mockData[0];
|
||||
a.types['T1'] = mockData[1];
|
||||
a.types['T2'] = mockData[2];
|
||||
a.types['T3'] = mockData[3];
|
||||
a.aliases['a1'] = ['T1'];
|
||||
a.aliases['a2'] = ['a1'];
|
||||
a.aliases['a3'] = ['T3', 'a2'];
|
||||
a.aliases['a4'] = ['a3', 'T3'];
|
||||
_checkSimilar(a, new NgMeta.fromJson(a.toJson()));
|
||||
});
|
||||
});
|
||||
|
||||
describe('flatten', () {
|
||||
it('should include recursive aliases.', () {
|
||||
var a = new NgMeta.empty();
|
||||
a.types['T0'] = mockData[0];
|
||||
a.types['T1'] = mockData[1];
|
||||
a.types['T2'] = mockData[2];
|
||||
a.types['T3'] = mockData[3];
|
||||
a.aliases['a1'] = ['T1'];
|
||||
a.aliases['a2'] = ['a1'];
|
||||
a.aliases['a3'] = ['T3', 'a2'];
|
||||
a.aliases['a4'] = ['a3', 'T0'];
|
||||
expect(a.flatten('a4')).toEqual([mockData[3], mockData[1], mockData[0]]);
|
||||
});
|
||||
|
||||
it('should detect cycles.', () {
|
||||
var a = new NgMeta.empty();
|
||||
a.types['T0'] = mockData[0];
|
||||
a.aliases['a1'] = ['T0', 'a1'];
|
||||
a.aliases['a2'] = ['a1'];
|
||||
expect(a.flatten('a1')).toEqual([mockData[0]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('merge', () {
|
||||
it('should merge all types on addAll', () {
|
||||
var a = new NgMeta.empty();
|
||||
var b = new NgMeta.empty();
|
||||
a.types['T0'] = mockData[0];
|
||||
b.types['T1'] = mockData[1];
|
||||
a.addAll(b);
|
||||
expect(a.types).toContain('T1');
|
||||
expect(a.types['T1']).toEqual(mockData[1]);
|
||||
});
|
||||
|
||||
it('should merge all aliases on addAll', () {
|
||||
var a = new NgMeta.empty();
|
||||
var b = new NgMeta.empty();
|
||||
a.aliases['a'] = ['x'];
|
||||
b.aliases['b'] = ['y'];
|
||||
a.addAll(b);
|
||||
expect(a.aliases).toContain('b');
|
||||
expect(a.aliases['b']).toEqual(['y']);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
_checkSimilar(NgMeta a, NgMeta b) {
|
||||
expect(a.types.length).toEqual(b.types.length);
|
||||
expect(a.aliases.length).toEqual(b.aliases.length);
|
||||
for (var k in a.types.keys) {
|
||||
expect(b.types).toContain(k);
|
||||
var at = a.types[k];
|
||||
var bt = b.types[k];
|
||||
expect(at.id).toEqual(bt.id);
|
||||
}
|
||||
for (var k in a.aliases.keys) {
|
||||
expect(b.aliases).toContain(k);
|
||||
expect(b.aliases[k]).toEqual(a.aliases[k]);
|
||||
}
|
||||
}
|
47
modules_dart/transform/test/transform/common/read_file.dart
Normal file
47
modules_dart/transform/test/transform/common/read_file.dart
Normal file
@ -0,0 +1,47 @@
|
||||
library angular2.test.transform.common.read_file;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:angular2/src/transform/common/asset_reader.dart';
|
||||
import 'package:barback/barback.dart';
|
||||
|
||||
/// Smooths over differences in CWD between IDEs and running tests in Travis.
|
||||
String readFile(String path) {
|
||||
for (var myPath in [path, 'test/transform/${path}']) {
|
||||
var file = new File(myPath);
|
||||
if (file.existsSync()) {
|
||||
return file.readAsStringSync();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
class TestAssetReader implements AssetReader {
|
||||
/// 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 = _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;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
library angular2.test.transform;
|
||||
|
||||
/// Mocked out version of {@link ReflectionCapabilities}, defined in
|
||||
/// src/reflection/reflection_capabilities.dart. Importing the actual file in
|
||||
/// tests causes issues with resolution due to transitive dependencies.
|
||||
class ReflectionCapabilities {}
|
Reference in New Issue
Block a user