test(dart/transform): Move BindGenerator tests to their own dir.

Move existing BindGenerator test to its own directory and to test that
phase specificially, rather than the whole pipeline.
Add another BindGenerator test.
This commit is contained in:
Tim Blasi 2015-03-12 14:10:26 -07:00
parent 4b12c19560
commit 5c1c534894
12 changed files with 123 additions and 46 deletions

View File

@ -27,7 +27,7 @@ List<String> _generateSetters(Map<String, String> bindMap) {
var setters = []; var setters = [];
// TODO(kegluneq): Include types for receivers. See #886. // TODO(kegluneq): Include types for receivers. See #886.
bindMap.forEach((prop, type) { bindMap.forEach((prop, type) {
setters.add('\'$prop\': (o, String v) => o.$prop = v'); setters.add(''''$prop': (o, String v) => o.$prop = v''');
}); });
return setters; return setters;
} }

View File

@ -10,13 +10,13 @@ class RegisteredType {
/// The actual call to `Reflector#registerType`. /// The actual call to `Reflector#registerType`.
final MethodInvocation registerMethod; final MethodInvocation registerMethod;
/// The factory method registered. /// The factory method registered.
final Expression factory; final Expression factoryFn;
/// The parameters registered. /// The parameters registered.
final Expression parameters; final Expression parameters;
/// The annotations registered. /// The annotations registered.
final Expression annotations; final Expression annotations;
RegisteredType._(this.typeName, this.registerMethod, this.factory, RegisteredType._(this.typeName, this.registerMethod, this.factoryFn,
this.parameters, this.annotations); this.parameters, this.annotations);
/// Creates a [RegisteredType] given a [MethodInvocation] node representing /// Creates a [RegisteredType] given a [MethodInvocation] node representing
@ -25,14 +25,14 @@ class RegisteredType {
var visitor = new _ParseRegisterTypeVisitor(); var visitor = new _ParseRegisterTypeVisitor();
registerMethod.accept(visitor); registerMethod.accept(visitor);
return new RegisteredType._(visitor.typeName, registerMethod, return new RegisteredType._(visitor.typeName, registerMethod,
visitor.factory, visitor.parameters, visitor.annotations); visitor.factoryFn, visitor.parameters, visitor.annotations);
} }
} }
class _ParseRegisterTypeVisitor extends Object class _ParseRegisterTypeVisitor extends Object
with RecursiveAstVisitor<Object> { with RecursiveAstVisitor<Object> {
Identifier typeName; Identifier typeName;
Expression factory; Expression factoryFn;
Expression parameters; Expression parameters;
Expression annotations; Expression annotations;
@ -56,7 +56,7 @@ class _ParseRegisterTypeVisitor extends Object
annotations = node.value; annotations = node.value;
break; break;
case 'factory': case 'factory':
factory = node.value; factoryFn = node.value;
break; break;
case 'parameters': case 'parameters':
parameters = node.value; parameters = node.value;

View File

@ -64,7 +64,7 @@ class CreateNgDepsVisitor extends Object
} }
void _writeImport() { void _writeImport() {
writer.print('import \'${path.basename(importPath)}\';'); writer.print('''import '${path.basename(importPath)}';''');
} }
@override @override
@ -150,19 +150,19 @@ class CreateNgDepsVisitor extends Object
} }
writer.print('..registerType('); writer.print('..registerType(');
visitNode(node.name); visitNode(node.name);
writer.print(', {\'factory\': '); writer.print(''', {'factory': ''');
if (ctor == null) { if (ctor == null) {
_generateEmptyFactory(node.name.toString()); _generateEmptyFactory(node.name.toString());
} else { } else {
ctor.accept(_factoryVisitor); ctor.accept(_factoryVisitor);
} }
writer.print(', \'parameters\': '); writer.print(''', 'parameters': ''');
if (ctor == null) { if (ctor == null) {
_generateEmptyParams(); _generateEmptyParams();
} else { } else {
ctor.accept(_paramsVisitor); ctor.accept(_paramsVisitor);
} }
writer.print(', \'annotations\': '); writer.print(''', 'annotations': ''');
node.accept(_metaVisitor); node.accept(_metaVisitor);
writer.print('})'); writer.print('})');

View File

@ -0,0 +1,41 @@
library angular2.test.transform.bind_generator.all_tests;
import 'dart:io';
import 'package:barback/barback.dart';
import 'package:angular2/src/transform/bind_generator/generator.dart';
import 'package:angular2/src/transform/common/formatter.dart';
import 'package:code_transformers/tests.dart';
import 'package:dart_style/dart_style.dart';
import 'package:path/path.dart' as path;
import 'package:unittest/unittest.dart';
import 'package:unittest/vm_config.dart';
import '../common/read_file.dart';
var formatter = new DartFormatter();
void allTests() {
var reader = new TestAssetReader();
test('should generate a setter for a `bind` property in an annotation.',
() async {
var inputPath = 'bind_generator/basic_bind_files/bar.ngDeps.dart';
var expected = formatter.format(
readFile('bind_generator/basic_bind_files/expected/bar.ngDeps.dart'));
var output = formatter
.format(await createNgSetters(reader, new AssetId('a', inputPath)));
expect(output, equals(expected));
});
test('should generate a single setter when multiple annotations bind to the '
'same property.', () async {
var inputPath = 'bind_generator/duplicate_bind_name_files/soup.ngDeps.dart';
var expected = formatter.format(readFile(
'bind_generator/duplicate_bind_name_files/expected/soup.ngDeps.dart'));
var output = formatter
.format(await createNgSetters(reader, new AssetId('a', inputPath)));
expect(output, equals(expected));
});
}

View File

@ -0,0 +1,19 @@
library bar;
import 'bar.dart';
import 'package:angular2/src/core/annotations/annotations.dart';
bool _visited = false;
void setupReflection(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(ToolTip, {
'factory': () => new ToolTip(),
'parameters': const [],
'annotations': const [
const Decorator(
selector: '[tool-tip]', bind: const {'text': 'tool-tip'})
]
});
}

View File

@ -8,13 +8,6 @@ void setupReflection(reflector) {
if (_visited) return; if (_visited) return;
_visited = true; _visited = true;
reflector reflector
..registerType(MyComponent, {
'factory': () => new MyComponent(),
'parameters': const [],
'annotations': const [
const Component(selector: 'soup', services: const [ToolTip])
]
})
..registerType(ToolTip, { ..registerType(ToolTip, {
'factory': () => new ToolTip(), 'factory': () => new ToolTip(),
'parameters': const [], 'parameters': const [],

View File

@ -0,0 +1,26 @@
library dinner.soup;
import 'package:angular2/src/core/annotations/annotations.dart';
import 'soup.dart';
bool _visited = false;
void setupReflection(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(SoupComponent, {
'factory': () => new SoupComponent(),
'parameters': const [],
'annotations': const [
const Component(
componentServices: const [SaladComponent],
bind: const {'menu': 'menu'})
]
})
..registerType(SaladComponent, {
'factory': () => new SaladComponent(),
'parameters': const [],
'annotations': const [const Component(bind: const {'menu': 'menu'})]
})
..registerSetters({'menu': (o, String v) => o.menu = v});
}

View File

@ -0,0 +1,25 @@
library dinner.soup;
import 'package:angular2/src/core/annotations/annotations.dart';
import 'soup.dart';
bool _visited = false;
void setupReflection(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(SoupComponent, {
'factory': () => new SoupComponent(),
'parameters': const [],
'annotations': const [
const Component(
componentServices: const [SaladComponent],
bind: const {'menu': 'menu'})
]
})
..registerType(SaladComponent, {
'factory': () => new SaladComponent(),
'parameters': const [],
'annotations': const [const Component(bind: const {'menu': 'menu'})]
});
}

View File

@ -93,14 +93,6 @@ void allTests() {
outputs: { outputs: {
'a|web/bar.ngDeps.dart': 'two_annotations_files/expected/bar.ngDeps.dart' 'a|web/bar.ngDeps.dart': 'two_annotations_files/expected/bar.ngDeps.dart'
}), }),
new IntegrationTestConfig('should generate setters for `bind` values.',
inputs: {
'a|web/index.dart': 'basic_bind_files/index.dart',
'a|web/bar.dart': 'basic_bind_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'basic_bind_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig( new IntegrationTestConfig(
'should ensure that dependencies are property chained.', 'should ensure that dependencies are property chained.',
inputs: { inputs: {

View File

@ -1,11 +0,0 @@
library bar;
import 'package:angular2/src/core/annotations/annotations.dart';
@Component(selector: 'soup', services: const [ToolTip])
class MyComponent {}
@Decorator(selector: '[tool-tip]', bind: const {'text': 'tool-tip'})
class ToolTip {
String text;
}

View File

@ -1,10 +0,0 @@
library web_foo;
import 'package:angular2/src/core/application.dart';
import 'package:angular2/src/reflection/reflection_capabilities.dart';
import 'bar.dart';
void main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrap(MyComponent);
}

View File

@ -3,12 +3,14 @@ library angular2.test.transform;
import 'package:unittest/unittest.dart'; import 'package:unittest/unittest.dart';
import 'package:unittest/vm_config.dart'; import 'package:unittest/vm_config.dart';
import 'bind_generator/all_tests.dart' as bindGenerator;
import 'directive_processor/all_tests.dart' as directiveProcessor; import 'directive_processor/all_tests.dart' as directiveProcessor;
import 'integration/all_tests.dart' as integration; import 'integration/all_tests.dart' as integration;
import 'reflection_remover/all_tests.dart' as reflectionRemover; import 'reflection_remover/all_tests.dart' as reflectionRemover;
main() { main() {
useVMConfiguration(); useVMConfiguration();
group('Bind Generator', bindGenerator.allTests);
group('Directive Processor', directiveProcessor.allTests); group('Directive Processor', directiveProcessor.allTests);
group('Reflection Remover', reflectionRemover.allTests); group('Reflection Remover', reflectionRemover.allTests);
group('Transformer Pipeline', integration.allTests); group('Transformer Pipeline', integration.allTests);