| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {beforeEach, ddescribe, describe, expect, iit, it} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | import * as ts from 'typescript'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-20 08:09:16 -07:00
										 |  |  | import {ReflectorHost} from '../src/reflector_host'; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {Directory, Entry, MockCompilerHost, MockContext} from './mocks'; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('reflector_host', () => { | 
					
						
							|  |  |  |   var context: MockContext; | 
					
						
							|  |  |  |   var host: ts.CompilerHost; | 
					
						
							|  |  |  |   var program: ts.Program; | 
					
						
							|  |  |  |   var reflectorHost: ReflectorHost; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     context = new MockContext('/tmp/src', clone(FILES)); | 
					
						
							|  |  |  |     host = new MockCompilerHost(context) | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     program = ts.createProgram( | 
					
						
							|  |  |  |         ['main.ts'], { | 
					
						
							|  |  |  |           module: ts.ModuleKind.CommonJS, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         host); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     // Force a typecheck
 | 
					
						
							|  |  |  |     let errors = program.getSemanticDiagnostics(); | 
					
						
							|  |  |  |     if (errors && errors.length) { | 
					
						
							|  |  |  |       throw new Error('Expected no errors'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     reflectorHost = new ReflectorHost( | 
					
						
							|  |  |  |         program, host, { | 
					
						
							|  |  |  |           genDir: '/tmp/dist', | 
					
						
							|  |  |  |           basePath: '/tmp/src', | 
					
						
							|  |  |  |           skipMetadataEmit: false, | 
					
						
							|  |  |  |           skipTemplateCodegen: false, | 
					
						
							|  |  |  |           trace: false | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         context); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should provide the import locations for angular', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     let {coreDecorators, diDecorators, diMetadata, animationMetadata, provider} = | 
					
						
							|  |  |  |         reflectorHost.angularImportLocations(); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     expect(coreDecorators).toEqual('@angular/core/src/metadata'); | 
					
						
							|  |  |  |     expect(diDecorators).toEqual('@angular/core/src/di/decorators'); | 
					
						
							|  |  |  |     expect(diMetadata).toEqual('@angular/core/src/di/metadata'); | 
					
						
							|  |  |  |     expect(animationMetadata).toEqual('@angular/core/src/animation/metadata'); | 
					
						
							|  |  |  |     expect(provider).toEqual('@angular/core/src/di/provider'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should be able to produce an import from main @angular/core', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     expect(reflectorHost.getImportPath('main.ts', 'node_modules/@angular/core.d.ts')) | 
					
						
							|  |  |  |         .toEqual('@angular/core'); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should be ble to produce an import from main to a sub-directory', () => { | 
					
						
							|  |  |  |     expect(reflectorHost.getImportPath('main.ts', 'lib/utils.ts')).toEqual('./lib/utils'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should be able to produce an import from to a peer file', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     expect(reflectorHost.getImportPath('lib/utils.ts', 'lib/collections.ts')) | 
					
						
							|  |  |  |         .toEqual('./collections'); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should be able to produce an import from to a sibling directory', () => { | 
					
						
							|  |  |  |     expect(reflectorHost.getImportPath('lib2/utils2.ts', 'lib/utils.ts')).toEqual('../lib/utils'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should be able to produce a symbol for an exported symbol', () => { | 
					
						
							| 
									
										
										
										
											2016-06-20 08:09:16 -07:00
										 |  |  |     expect(reflectorHost.findDeclaration('@angular/router-deprecated', 'foo', 'main.ts')) | 
					
						
							|  |  |  |         .toBeDefined(); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should be able to produce a symbol for values space only reference', () => { | 
					
						
							| 
									
										
										
										
											2016-06-20 08:09:16 -07:00
										 |  |  |     expect( | 
					
						
							|  |  |  |         reflectorHost.findDeclaration('@angular/router-deprecated/src/providers', 'foo', 'main.ts')) | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         .toBeDefined(); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should be produce the same symbol if asked twice', () => { | 
					
						
							|  |  |  |     let foo1 = reflectorHost.getStaticSymbol('main.ts', 'foo'); | 
					
						
							|  |  |  |     let foo2 = reflectorHost.getStaticSymbol('main.ts', 'foo'); | 
					
						
							|  |  |  |     expect(foo1).toBe(foo2); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   it('should be able to read a metadata file', | 
					
						
							|  |  |  |      () => { | 
					
						
							|  |  |  |          expect(reflectorHost.getMetadataFor('node_modules/@angular/core.d.ts')) | 
					
						
							|  |  |  |              .toEqual({__symbolic: 'module', version: 1, metadata: {foo: {__symbolic: 'class'}}})}); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const dummyModule = 'export let foo: any[];' | 
					
						
							|  |  |  | const FILES: Entry = { | 
					
						
							|  |  |  |   'tmp': { | 
					
						
							|  |  |  |     'src': { | 
					
						
							|  |  |  |       'main.ts': `
 | 
					
						
							|  |  |  |         import * as c from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-06-20 08:09:16 -07:00
										 |  |  |         import * as r from '@angular/router-deprecated'; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |         import * as u from './lib/utils'; | 
					
						
							|  |  |  |         import * as cs from './lib/collections'; | 
					
						
							|  |  |  |         import * as u2 from './lib2/utils2'; | 
					
						
							|  |  |  |       `,
 | 
					
						
							|  |  |  |       'lib': { | 
					
						
							|  |  |  |         'utils.ts': dummyModule, | 
					
						
							|  |  |  |         'collections.ts': dummyModule, | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       'lib2': {'utils2.ts': dummyModule}, | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |       'node_modules': { | 
					
						
							|  |  |  |         '@angular': { | 
					
						
							|  |  |  |           'core.d.ts': dummyModule, | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           'core.metadata.json': | 
					
						
							|  |  |  |               `{"__symbolic":"module", "version": 1, "metadata": {"foo": {"__symbolic": "class"}}}`, | 
					
						
							| 
									
										
										
										
											2016-06-20 08:09:16 -07:00
										 |  |  |           'router-deprecated': | 
					
						
							|  |  |  |               {'index.d.ts': dummyModule, 'src': {'providers.d.ts': dummyModule}} | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function clone(entry: Entry): Entry { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   if (typeof entry === 'string') { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     return entry; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     let result: Directory = {}; | 
					
						
							|  |  |  |     for (let name in entry) { | 
					
						
							|  |  |  |       result[name] = clone(entry[name]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |