| OLD | NEW |
| 1 library source_metadata_extractor_spec; | 1 library source_metadata_extractor_spec; |
| 2 | 2 |
| 3 import 'package:analyzer/src/generated/ast.dart'; | 3 import 'package:analyzer/src/generated/ast.dart'; |
| 4 import 'package:angular/tools/common.dart'; | 4 import 'package:angular/tools/common.dart'; |
| 5 import 'package:angular/tools/source_crawler.dart'; | 5 import 'package:angular/tools/source_crawler.dart'; |
| 6 import 'package:angular/tools/source_metadata_extractor.dart'; | 6 import 'package:angular/tools/source_metadata_extractor.dart'; |
| 7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
| 8 | 8 |
| 9 import '../jasmine_syntax.dart'; | 9 import '../jasmine_syntax.dart'; |
| 10 | 10 |
| 11 main() => describe('SourceMetadataExtractor', () { | 11 void main() { |
| 12 describe('SourceMetadataExtractor', () { |
| 12 | 13 |
| 13 it('should extract expressions and attribute names with expressions', () { | 14 it('should extract expressions and attribute names with expressions', () { |
| 14 var info = extractDirectiveInfo([ | 15 var info = extractDirectiveInfo([ |
| 15 new DirectiveMetadata('FooComponent', COMPONENT, 'foo-component', { | 16 new DirectiveMetadata('FooComponent', COMPONENT, 'foo-component', { |
| 16 'barVal': '@bar', | 17 'barVal': '@bar', |
| 17 'baz-expr1': '<=>baz1', | 18 'baz-expr1': '<=>baz1', |
| 18 'baz-expr2': '=>baz2', | 19 'baz-expr2': '=>baz2', |
| 19 'baz-expr3': '=>!baz3', | 20 'baz-expr3': '=>!baz3', |
| 20 'baz-callback': '&aux', | 21 'baz-callback': '&aux', |
| 21 }) | 22 }) |
| 22 ]); | 23 ]); |
| 23 | 24 |
| 24 expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs), | 25 expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs), |
| 25 equals(['baz-expr1', | 26 equals(['baz-expr1', |
| 26 'baz-expr2', | 27 'baz-expr2', |
| 27 'baz-expr3', | 28 'baz-expr3', |
| 28 'baz-callback'])); | 29 'baz-callback'])); |
| 29 expect(flattenList(info, (DirectiveInfo i) => i.expressions), | 30 expect(flattenList(info, (DirectiveInfo i) => i.expressions), |
| 30 equals(['bar', | 31 equals(['bar', |
| 31 'baz1', | 32 'baz1', |
| 32 'baz2', | 33 'baz2', |
| 33 'baz3', | 34 'baz3', |
| 34 'aux'])); | 35 'aux'])); |
| 36 }); |
| 37 |
| 38 it('should build a component selector if one is not explicitly specified', (
) { |
| 39 var info = extractDirectiveInfo([ |
| 40 new DirectiveMetadata('MyFooComponent', COMPONENT, 'my-foo', { |
| 41 'foo-expr': '=>fooExpr' |
| 42 }) |
| 43 ]); |
| 44 |
| 45 expect(info, hasLength(1)); |
| 46 expect(info[0].selector, equals('my-foo')); |
| 47 }); |
| 48 |
| 49 it('should build an element directive selector if one is not explicitly spec
ified', () { |
| 50 var info = extractDirectiveInfo([ |
| 51 new DirectiveMetadata('MyFooDirective', DIRECTIVE, 'my-foo', { |
| 52 'foo-expr': '=>fooExpr' |
| 53 }) |
| 54 ]); |
| 55 |
| 56 expect(info, hasLength(1)); |
| 57 expect(info[0].selector, equals('my-foo')); |
| 58 }); |
| 59 |
| 60 it('should build an attr directive selector if one is not explicitly specifi
ed', () { |
| 61 var info = extractDirectiveInfo([ |
| 62 new DirectiveMetadata('MyFooAttrDirective', '[my-foo]', '[my-foo]', { |
| 63 'foo-expr': '=>fooExpr' |
| 64 }) |
| 65 ]); |
| 66 |
| 67 expect(info, hasLength(1)); |
| 68 expect(info[0].selector, equals('[my-foo]')); |
| 69 }); |
| 70 |
| 71 it('should figure out attribute name if dot(.) is used', () { |
| 72 var info = extractDirectiveInfo([ |
| 73 new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[my-foo]', { |
| 74 '.': '=>fooExpr' |
| 75 }) |
| 76 ]); |
| 77 |
| 78 expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs), |
| 79 equals(['my-foo'])); |
| 80 }); |
| 81 |
| 82 it('should figure out attribute name from selector if dot(.) is used', () { |
| 83 var info = extractDirectiveInfo([ |
| 84 new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]',
{ |
| 85 '.': '=>fooExpr' |
| 86 }) |
| 87 ]); |
| 88 |
| 89 expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs), |
| 90 equals(['foo'])); |
| 91 }); |
| 92 |
| 93 it('should include exported expression attributes', () { |
| 94 var info = extractDirectiveInfo([ |
| 95 new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]',
{ |
| 96 '.': '=>fooExpr' |
| 97 }, ['baz']) |
| 98 ]); |
| 99 |
| 100 expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs), |
| 101 equals(['foo', 'baz'])); |
| 102 }); |
| 103 |
| 104 it('should include exported expressions', () { |
| 105 var info = extractDirectiveInfo([ |
| 106 new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]',
{ |
| 107 '.': '=>fooExpr' |
| 108 }, null, ['ctrl.baz']) |
| 109 ]); |
| 110 |
| 111 expect(flattenList(info, (DirectiveInfo i) => i.expressions), |
| 112 equals(['fooExpr', 'ctrl.baz'])); |
| 113 }); |
| 114 |
| 35 }); | 115 }); |
| 36 | 116 } |
| 37 it('should build a component selector if one is not explicitly specified', ()
{ | |
| 38 var info = extractDirectiveInfo([ | |
| 39 new DirectiveMetadata('MyFooComponent', COMPONENT, 'my-foo', { | |
| 40 'foo-expr': '=>fooExpr' | |
| 41 }) | |
| 42 ]); | |
| 43 | |
| 44 expect(info, hasLength(1)); | |
| 45 expect(info[0].selector, equals('my-foo')); | |
| 46 }); | |
| 47 | |
| 48 it('should build an element directive selector if one is not explicitly specif
ied', () { | |
| 49 var info = extractDirectiveInfo([ | |
| 50 new DirectiveMetadata('MyFooDirective', DIRECTIVE, 'my-foo', { | |
| 51 'foo-expr': '=>fooExpr' | |
| 52 }) | |
| 53 ]); | |
| 54 | |
| 55 expect(info, hasLength(1)); | |
| 56 expect(info[0].selector, equals('my-foo')); | |
| 57 }); | |
| 58 | |
| 59 it('should build an attr directive selector if one is not explicitly specified
', () { | |
| 60 var info = extractDirectiveInfo([ | |
| 61 new DirectiveMetadata('MyFooAttrDirective', '[my-foo]', '[my-foo]', { | |
| 62 'foo-expr': '=>fooExpr' | |
| 63 }) | |
| 64 ]); | |
| 65 | |
| 66 expect(info, hasLength(1)); | |
| 67 expect(info[0].selector, equals('[my-foo]')); | |
| 68 }); | |
| 69 | |
| 70 it('should figure out attribute name if dot(.) is used', () { | |
| 71 var info = extractDirectiveInfo([ | |
| 72 new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[my-foo]', { | |
| 73 '.': '=>fooExpr' | |
| 74 }) | |
| 75 ]); | |
| 76 | |
| 77 expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs), | |
| 78 equals(['my-foo'])); | |
| 79 }); | |
| 80 | |
| 81 it('should figure out attribute name from selector if dot(.) is used', () { | |
| 82 var info = extractDirectiveInfo([ | |
| 83 new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', { | |
| 84 '.': '=>fooExpr' | |
| 85 }) | |
| 86 ]); | |
| 87 | |
| 88 expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs), | |
| 89 equals(['foo'])); | |
| 90 }); | |
| 91 | |
| 92 it('should include exported expression attributes', () { | |
| 93 var info = extractDirectiveInfo([ | |
| 94 new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', { | |
| 95 '.': '=>fooExpr' | |
| 96 }, ['baz']) | |
| 97 ]); | |
| 98 | |
| 99 expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs), | |
| 100 equals(['foo', 'baz'])); | |
| 101 }); | |
| 102 | |
| 103 it('should include exported expressions', () { | |
| 104 var info = extractDirectiveInfo([ | |
| 105 new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', { | |
| 106 '.': '=>fooExpr' | |
| 107 }, null, ['ctrl.baz']) | |
| 108 ]); | |
| 109 | |
| 110 expect(flattenList(info, (DirectiveInfo i) => i.expressions), | |
| 111 equals(['fooExpr', 'ctrl.baz'])); | |
| 112 }); | |
| 113 | |
| 114 }); | |
| 115 | 117 |
| 116 flattenList(list, map) => list.map(map).fold([], (prev, exprs) => | 118 flattenList(list, map) => list.map(map).fold([], (prev, exprs) => |
| 117 new List.from(prev)..addAll(exprs)); | 119 new List.from(prev)..addAll(exprs)); |
| 118 | 120 |
| 119 List<DirectiveInfo> extractDirectiveInfo(List<DirectiveMetadata> metadata) { | 121 List<DirectiveInfo> extractDirectiveInfo(List<DirectiveMetadata> metadata) { |
| 120 var sourceCrawler = new MockSourceCrawler(); | 122 var sourceCrawler = new MockSourceCrawler(); |
| 121 var metadataCollector = new MockDirectiveMetadataCollectingVisitor(metadata); | 123 var metadataCollector = new MockDirectiveMetadataCollectingVisitor(metadata); |
| 122 var extractor = new SourceMetadataExtractor(metadataCollector); | 124 var extractor = new SourceMetadataExtractor(metadataCollector); |
| 123 return extractor.gatherDirectiveInfo('', sourceCrawler); | 125 return extractor.gatherDirectiveInfo('', sourceCrawler); |
| 124 } | 126 } |
| 125 | 127 |
| 126 class MockDirectiveMetadataCollectingVisitor | 128 class MockDirectiveMetadataCollectingVisitor |
| 127 implements DirectiveMetadataCollectingVisitor { | 129 implements DirectiveMetadataCollectingVisitor { |
| 128 List<DirectiveMetadata> metadata; | 130 List<DirectiveMetadata> metadata; |
| 131 List<String> templates = <String>[]; |
| 129 | 132 |
| 130 MockDirectiveMetadataCollectingVisitor(List<DirectiveMetadata> this.metadata); | 133 MockDirectiveMetadataCollectingVisitor(List<DirectiveMetadata> this.metadata); |
| 131 | 134 |
| 132 call(CompilationUnit cu) { | 135 call(CompilationUnit cu) { |
| 133 // do nothing | 136 // do nothing |
| 134 } | 137 } |
| 135 } | 138 } |
| 136 | 139 |
| 137 class MockSourceCrawler implements SourceCrawler { | 140 class MockSourceCrawler implements SourceCrawler { |
| 138 | 141 |
| 139 void crawl(String entryPoint, visitor(CompilationUnit cu)) { | 142 void crawl(String entryPoint, visitor(CompilationUnit cu)) { |
| 140 // do nothing | 143 // do nothing |
| 141 } | 144 } |
| 142 } | 145 } |
| OLD | NEW |