feat(dart/transform) Register parameter metadata information
Adds any metadata attached to a parameter to the "parameters" value passed in to `registerType`. For example: `MyComponent(@Inject(Foo) foo)` generates `"parameters": const [const [const Inject(Foo)]]` Also reorganizes the testing code. Closes #7
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
library angular2.test.transform.common.read_file;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
/// 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;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
library angular2.test.transform.directive_processor.all_tests;
|
||||
|
||||
import 'dart:io';
|
||||
import 'package:barback/barback.dart';
|
||||
import 'package:angular2/src/transform/directive_processor/rewriter.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() {
|
||||
test('should preserve parameter annotations as const instances', () {
|
||||
var inputPath = 'parameter_metadata/soup.dart';
|
||||
var expected = _readFile('parameter_metadata/expected/soup.ngDeps.dart');
|
||||
var output =
|
||||
formatter.format(createNgDeps(_readFile(inputPath), inputPath));
|
||||
expect(output, equals(expected));
|
||||
});
|
||||
}
|
||||
|
||||
var pathBase = 'directive_processor';
|
||||
|
||||
/// Smooths over differences in CWD between IDEs and running tests in Travis.
|
||||
String _readFile(String path) => readFile('$pathBase/$path');
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
library dinner.soup;
|
||||
|
||||
import 'soup.dart';
|
||||
import 'package:angular2/src/core/annotations/annotations.dart';
|
||||
|
||||
bool _visited = false;
|
||||
void setupReflection(reflector) {
|
||||
if (_visited) return;
|
||||
_visited = true;
|
||||
reflector
|
||||
..registerType(SoupComponent, {
|
||||
"factory":
|
||||
(String description, salt) => new SoupComponent(description, salt),
|
||||
"parameters": const [const [Tasty, String], const [const Inject(Salt)]],
|
||||
"annotations": const [const Component(selector: '[soup]')]
|
||||
});
|
||||
} // {"version":1,"importOffset":104,"registerOffset":451,"imports":["package:angular2/src/core/annotations/annotations.dart"]}
|
||||
@@ -0,0 +1,8 @@
|
||||
library dinner.soup;
|
||||
|
||||
import 'package:angular2/src/core/annotations/annotations.dart';
|
||||
|
||||
@Component(selector: '[soup]')
|
||||
class SoupComponent {
|
||||
SoupComponent(@Tasty String description, @Inject(Salt) salt);
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
library angular2.test.transform.integration;
|
||||
|
||||
import 'dart:io';
|
||||
import 'package:angular2/transformer.dart';
|
||||
import 'package:code_transformers/tests.dart';
|
||||
import 'package:dart_style/dart_style.dart';
|
||||
import 'package:unittest/unittest.dart';
|
||||
|
||||
import '../common/read_file.dart';
|
||||
|
||||
var formatter = new DartFormatter();
|
||||
var transform = new AngularTransformerGroup(new TransformerOptions(
|
||||
'web/index.dart', reflectionEntryPoint: 'web/index.dart'));
|
||||
|
||||
class IntegrationTestConfig {
|
||||
final String name;
|
||||
final Map<String, String> assetPathToInputPath;
|
||||
final Map<String, String> assetPathToExpectedOutputPath;
|
||||
|
||||
IntegrationTestConfig(this.name,
|
||||
{Map<String, String> inputs, Map<String, String> outputs})
|
||||
: this.assetPathToInputPath = inputs,
|
||||
this.assetPathToExpectedOutputPath = outputs;
|
||||
}
|
||||
|
||||
void allTests() {
|
||||
/*
|
||||
* Each test has its own directory for inputs & an `expected` directory for
|
||||
* expected outputs.
|
||||
*
|
||||
* In addition to these declared inputs, we inject a set of common inputs for
|
||||
* every test.
|
||||
*/
|
||||
var commonInputs = {
|
||||
'angular2|lib/src/core/annotations/annotations.dart':
|
||||
'../../../lib/src/core/annotations/annotations.dart',
|
||||
'angular2|lib/src/core/application.dart': '../common/application.dart',
|
||||
'angular2|lib/src/reflection/reflection_capabilities.dart':
|
||||
'../common/reflection_capabilities.dart'
|
||||
};
|
||||
|
||||
var tests = [
|
||||
new IntegrationTestConfig(
|
||||
'should generate proper code for a Component defining only a selector.',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'simple_annotation_files/index.dart',
|
||||
'a|web/bar.dart': 'simple_annotation_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart':
|
||||
'simple_annotation_files/expected/bar.ngDeps.dart',
|
||||
'a|web/index.ngDeps.dart':
|
||||
'simple_annotation_files/expected/index.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig(
|
||||
'should generate proper code for a Component using a selector defined '
|
||||
'in another file.',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'two_deps_files/index.dart',
|
||||
'a|web/foo.dart': 'two_deps_files/foo.dart',
|
||||
'a|web/bar.dart': 'two_deps_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'two_deps_files/expected/bar.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig(
|
||||
'should generate proper code for a Component declaring a '
|
||||
'componentService defined in another file.',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'list_of_types_files/index.dart',
|
||||
'a|web/foo.dart': 'list_of_types_files/foo.dart',
|
||||
'a|web/bar.dart': 'list_of_types_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'list_of_types_files/expected/bar.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig(
|
||||
'should generate a factory for a class with no declared ctor.',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'synthetic_ctor_files/index.dart',
|
||||
'a|web/bar.dart': 'synthetic_ctor_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'synthetic_ctor_files/expected/bar.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig('should preserve multiple annotations.',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'two_annotations_files/index.dart',
|
||||
'a|web/bar.dart': 'two_annotations_files/bar.dart',
|
||||
'angular2|lib/src/core/annotations/template.dart':
|
||||
'../../../lib/src/core/annotations/template.dart'
|
||||
},
|
||||
outputs: {
|
||||
'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(
|
||||
'should ensure that dependencies are property chained.',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'chained_deps_files/index.dart',
|
||||
'a|web/foo.dart': 'chained_deps_files/foo.dart',
|
||||
'a|web/bar.dart': 'chained_deps_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'chained_deps_files/expected/bar.ngDeps.dart',
|
||||
'a|web/foo.ngDeps.dart': 'chained_deps_files/expected/foo.ngDeps.dart'
|
||||
})
|
||||
];
|
||||
|
||||
var cache = {};
|
||||
|
||||
for (var config in tests) {
|
||||
|
||||
// Read in input & output files.
|
||||
config.assetPathToInputPath
|
||||
..addAll(commonInputs)
|
||||
..forEach((key, value) {
|
||||
config.assetPathToInputPath[key] =
|
||||
cache.putIfAbsent(value, () => _readFile(value));
|
||||
});
|
||||
config.assetPathToExpectedOutputPath.forEach((key, value) {
|
||||
config.assetPathToExpectedOutputPath[key] = cache.putIfAbsent(value, () {
|
||||
var code = _readFile(value);
|
||||
return value.endsWith('dart') ? formatter.format(code) : code;
|
||||
});
|
||||
});
|
||||
testPhases(config.name, [
|
||||
[transform]
|
||||
], config.assetPathToInputPath, config.assetPathToExpectedOutputPath, []);
|
||||
}
|
||||
}
|
||||
|
||||
/// Smooths over differences in CWD between IDEs and running tests in Travis.
|
||||
String _readFile(String path) => readFile('integration/$path');
|
||||
@@ -0,0 +1,21 @@
|
||||
library angular2.test.transform.reflection_remover;
|
||||
|
||||
import 'package:analyzer/analyzer.dart';
|
||||
import 'package:angular2/src/transform/reflection_remover/codegen.dart';
|
||||
import 'package:angular2/src/transform/reflection_remover/rewriter.dart';
|
||||
import 'package:unittest/unittest.dart';
|
||||
|
||||
import 'reflection_remover_files/expected/index.dart' as expected;
|
||||
import '../common/read_file.dart';
|
||||
|
||||
void allTests() {
|
||||
var codegen = new Codegen('web/index.dart', 'web/index.ngDeps.dart');
|
||||
|
||||
test('should remove uses of mirrors & insert calls to generated code', () {
|
||||
var code =
|
||||
readFile('reflection_remover/reflection_remover_files/index.dart');
|
||||
var output =
|
||||
new Rewriter(code, codegen).rewrite(parseCompilationUnit(code));
|
||||
expect(output, equals(expected.code));
|
||||
});
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
library web_foo;
|
||||
|
||||
import 'package:angular2/src/core/application.dart';
|
||||
import 'package:angular2/src/reflection/reflection.dart';
|
||||
import 'index.ngDeps.dart' as ngStaticInit;
|
||||
import 'package:angular2/src/reflection/reflection_capabilities.dart';
|
||||
import 'bar.dart';
|
||||
|
||||
void main() {
|
||||
ngStaticInit.setupReflection(reflector);
|
||||
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
||||
bootstrap(MyComponent);
|
||||
}
|
||||
@@ -1,153 +1,15 @@
|
||||
library angular2.test.transform;
|
||||
|
||||
import 'dart:io';
|
||||
import 'package:barback/barback.dart';
|
||||
import 'package:angular2/transformer.dart';
|
||||
import 'package:code_transformers/tests.dart';
|
||||
import 'package:dart_style/dart_style.dart';
|
||||
import 'package:unittest/unittest.dart';
|
||||
import 'package:unittest/vm_config.dart';
|
||||
|
||||
import 'reflection_remover_files/expected/index.dart'
|
||||
as reflection_remover_output;
|
||||
import 'directive_processor/all_tests.dart' as directiveProcessor;
|
||||
import 'integration/all_tests.dart' as integration;
|
||||
import 'reflection_remover/all_tests.dart' as reflectionRemover;
|
||||
|
||||
main() {
|
||||
useVMConfiguration();
|
||||
group('Integration tests:', _integrationTests);
|
||||
}
|
||||
|
||||
var formatter = new DartFormatter();
|
||||
var transform = new AngularTransformerGroup(new TransformerOptions(
|
||||
'web/index.dart', reflectionEntryPoint: 'web/index.dart'));
|
||||
|
||||
class IntegrationTestConfig {
|
||||
final String name;
|
||||
final Map<String, String> assetPathToInputPath;
|
||||
final Map<String, String> assetPathToExpectedOutputPath;
|
||||
|
||||
IntegrationTestConfig(this.name,
|
||||
{Map<String, String> inputs, Map<String, String> outputs})
|
||||
: this.assetPathToInputPath = inputs,
|
||||
this.assetPathToExpectedOutputPath = outputs;
|
||||
}
|
||||
|
||||
void _integrationTests() {
|
||||
/*
|
||||
* Each test has its own directory for inputs & an `expected` directory for
|
||||
* expected outputs.
|
||||
*
|
||||
* In addition to these declared inputs, we inject a set of common inputs for
|
||||
* every test.
|
||||
*/
|
||||
var commonInputs = {
|
||||
'angular2|lib/src/core/annotations/annotations.dart':
|
||||
'../../lib/src/core/annotations/annotations.dart',
|
||||
'angular2|lib/src/core/application.dart': 'common/application.dart',
|
||||
'angular2|lib/src/reflection/reflection_capabilities.dart':
|
||||
'common/reflection_capabilities.dart'
|
||||
};
|
||||
|
||||
var tests = [
|
||||
new IntegrationTestConfig('Simple',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'simple_annotation_files/index.dart',
|
||||
'a|web/bar.dart': 'simple_annotation_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart':
|
||||
'simple_annotation_files/expected/bar.ngDeps.dart',
|
||||
'a|web/index.ngDeps.dart':
|
||||
'simple_annotation_files/expected/index.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig('Reflection Remover',
|
||||
inputs: {'a|web/index.dart': 'reflection_remover_files/index.dart'},
|
||||
outputs: {'a|web/index.dart': reflection_remover_output.code}),
|
||||
new IntegrationTestConfig('Two injected dependencies',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'two_deps_files/index.dart',
|
||||
'a|web/foo.dart': 'two_deps_files/foo.dart',
|
||||
'a|web/bar.dart': 'two_deps_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'two_deps_files/expected/bar.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig('List of types',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'list_of_types_files/index.dart',
|
||||
'a|web/foo.dart': 'list_of_types_files/foo.dart',
|
||||
'a|web/bar.dart': 'list_of_types_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'list_of_types_files/expected/bar.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig('Component with synthetic Constructor',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'synthetic_ctor_files/index.dart',
|
||||
'a|web/bar.dart': 'synthetic_ctor_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'synthetic_ctor_files/expected/bar.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig('Component with two annotations',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'two_annotations_files/index.dart',
|
||||
'a|web/bar.dart': 'two_annotations_files/bar.dart',
|
||||
'angular2|lib/src/core/annotations/template.dart':
|
||||
'../../lib/src/core/annotations/template.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'two_annotations_files/expected/bar.ngDeps.dart'
|
||||
}),
|
||||
new IntegrationTestConfig('Basic `bind`',
|
||||
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('Chained dependencies',
|
||||
inputs: {
|
||||
'a|web/index.dart': 'chained_deps_files/index.dart',
|
||||
'a|web/foo.dart': 'chained_deps_files/foo.dart',
|
||||
'a|web/bar.dart': 'chained_deps_files/bar.dart'
|
||||
},
|
||||
outputs: {
|
||||
'a|web/bar.ngDeps.dart': 'chained_deps_files/expected/bar.ngDeps.dart',
|
||||
'a|web/foo.ngDeps.dart': 'chained_deps_files/expected/foo.ngDeps.dart'
|
||||
})
|
||||
];
|
||||
|
||||
var cache = {};
|
||||
|
||||
for (var config in tests) {
|
||||
|
||||
// Read in input & output files.
|
||||
config.assetPathToInputPath
|
||||
..addAll(commonInputs)
|
||||
..forEach((key, value) {
|
||||
config.assetPathToInputPath[key] =
|
||||
cache.putIfAbsent(value, () => _readFile(value));
|
||||
});
|
||||
config.assetPathToExpectedOutputPath.forEach((key, value) {
|
||||
config.assetPathToExpectedOutputPath[key] = cache.putIfAbsent(value, () {
|
||||
var code = _readFile(value);
|
||||
return value.endsWith('dart') ? formatter.format(code) : code;
|
||||
});
|
||||
});
|
||||
testPhases(config.name, [
|
||||
[transform]
|
||||
], config.assetPathToInputPath, config.assetPathToExpectedOutputPath, []);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 path;
|
||||
group('Directive Processor', directiveProcessor.allTests);
|
||||
group('Reflection Remover', reflectionRemover.allTests);
|
||||
group('Transformer Pipeline', integration.allTests);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user