-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transformer): Support prefixed annotations in the transformer.
closes #2754
- Loading branch information
Showing
7 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
modules/angular2/test/transform/common/annotation_matcher_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ular2/test/transform/template_compiler/with_prefix_files/expected/ng2_prefix.ng_deps.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
library angular2.test.transform.template_compiler.with_prefix_files.ng2_prefix.ng_deps.dart; | ||
|
||
import 'ng2_prefix.dart'; | ||
import 'package:angular2/angular2.dart' as ng2 | ||
show bootstrap, Component, Directive, View, NgElement; | ||
|
||
var _visited = false; | ||
void initReflector(reflector) { | ||
if (_visited) return; | ||
_visited = true; | ||
reflector | ||
..registerType(MyApp, { | ||
'factory': () => new MyApp(), | ||
'parameters': const [const []], | ||
'annotations': const [ | ||
const ng2.Component(selector: 'my-app'), | ||
const ng2.View(template: 'MyApp {{name}}') | ||
] | ||
}) | ||
..registerGetters({'name': (o) => o.name}) | ||
..registerSetters({'name': (o, v) => o.name = v}); | ||
} |
20 changes: 20 additions & 0 deletions
20
modules/angular2/test/transform/template_compiler/with_prefix_files/ng2_prefix.ng_deps.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
library angular2.test.transform.template_compiler.with_prefix_files.ng2_prefix.ng_deps.dart; | ||
|
||
import 'ng2_prefix.dart'; | ||
import 'package:angular2/angular2.dart' as ng2 | ||
show bootstrap, Component, Directive, View, NgElement; | ||
|
||
var _visited = false; | ||
void initReflector(reflector) { | ||
if (_visited) return; | ||
_visited = true; | ||
reflector | ||
..registerType(MyApp, { | ||
'factory': () => new MyApp(), | ||
'parameters': const [const []], | ||
'annotations': const [ | ||
const ng2.Component(selector: 'my-app'), | ||
const ng2.View(template: 'MyApp {{name}}') | ||
] | ||
}); | ||
} |
12 changes: 12 additions & 0 deletions
12
modules/angular2/test/transform/template_compiler/with_prefix_files/ng2_prefix.ng_meta.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"MyApp":{ | ||
"id":"MyApp", | ||
"selector":"my-app", | ||
"compileChildren":true, | ||
"host":{}, | ||
"properties":[], | ||
"readAttributes":[], | ||
"type":1, | ||
"version":1 | ||
} | ||
} |