2018-07-16 03:51:14 -04: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
|
|
|
|
*/
|
|
|
|
|
2018-08-22 15:33:17 -04:00
|
|
|
import * as fs from 'fs';
|
2018-07-16 03:51:14 -04:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2018-08-22 15:33:17 -04:00
|
|
|
import {DtsMapper} from '../../src/host/dts_mapper';
|
|
|
|
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
|
|
|
|
import {getDeclaration, makeProgram} from '../helpers/utils';
|
2018-07-16 03:51:14 -04:00
|
|
|
|
2018-08-22 15:33:17 -04:00
|
|
|
const CLASSES = [
|
2018-07-16 03:51:14 -04:00
|
|
|
{
|
2018-08-22 15:33:17 -04:00
|
|
|
name: '/src/class.js',
|
2018-07-16 03:51:14 -04:00
|
|
|
contents: `
|
2018-08-22 15:33:17 -04:00
|
|
|
export class NoTypeParam {}
|
|
|
|
export class OneTypeParam {}
|
|
|
|
export class TwoTypeParams {}
|
2018-07-16 03:51:14 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-08-22 15:33:17 -04:00
|
|
|
name: '/typings/class.d.ts',
|
2018-07-16 03:51:14 -04:00
|
|
|
contents: `
|
2018-08-22 15:33:17 -04:00
|
|
|
export class NoTypeParam {}
|
|
|
|
export class OneTypeParam<T> {}
|
|
|
|
export class TwoTypeParams<T, K> {}
|
2018-07-16 03:51:14 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2018-08-17 02:50:55 -04:00
|
|
|
const MARKER_FILE = {
|
|
|
|
name: '/marker.js',
|
|
|
|
contents: `
|
|
|
|
let compileNgModuleFactory = compileNgModuleFactory__PRE_NGCC__;
|
|
|
|
|
|
|
|
function compileNgModuleFactory__PRE_NGCC__(injector, options, moduleType) {
|
|
|
|
const compilerFactory = injector.get(CompilerFactory);
|
|
|
|
const compiler = compilerFactory.createCompiler([options]);
|
|
|
|
return compiler.compileModuleAsync(moduleType);
|
|
|
|
}
|
|
|
|
|
|
|
|
function compileNgModuleFactory__POST_NGCC__(injector, options, moduleType) {
|
|
|
|
ngDevMode && assertNgModuleType(moduleType);
|
|
|
|
return Promise.resolve(new R3NgModuleFactory(moduleType));
|
|
|
|
}
|
|
|
|
`
|
|
|
|
};
|
|
|
|
|
2018-07-16 03:51:14 -04:00
|
|
|
describe('Esm2015ReflectionHost', () => {
|
2018-08-22 15:33:17 -04:00
|
|
|
describe('getGenericArityOfClass()', () => {
|
|
|
|
it('should properly count type parameters', () => {
|
|
|
|
// Mock out reading the `d.ts` file from disk
|
|
|
|
const readFileSyncSpy = spyOn(fs, 'readFileSync').and.returnValue(CLASSES[1].contents);
|
|
|
|
const program = makeProgram(CLASSES[0]);
|
|
|
|
|
|
|
|
const dtsMapper = new DtsMapper('/src', '/typings');
|
2018-10-03 11:59:32 -04:00
|
|
|
const host = new Esm2015ReflectionHost(false, program.getTypeChecker(), dtsMapper);
|
2018-08-22 15:33:17 -04:00
|
|
|
const noTypeParamClass =
|
|
|
|
getDeclaration(program, '/src/class.js', 'NoTypeParam', ts.isClassDeclaration);
|
|
|
|
expect(host.getGenericArityOfClass(noTypeParamClass)).toBe(0);
|
|
|
|
const oneTypeParamClass =
|
|
|
|
getDeclaration(program, '/src/class.js', 'OneTypeParam', ts.isClassDeclaration);
|
|
|
|
expect(host.getGenericArityOfClass(oneTypeParamClass)).toBe(1);
|
|
|
|
const twoTypeParamsClass =
|
|
|
|
getDeclaration(program, '/src/class.js', 'TwoTypeParams', ts.isClassDeclaration);
|
|
|
|
expect(host.getGenericArityOfClass(twoTypeParamsClass)).toBe(2);
|
2018-07-16 03:51:14 -04:00
|
|
|
});
|
|
|
|
});
|
2018-08-17 02:50:55 -04:00
|
|
|
|
|
|
|
describe('getSwitchableDeclarations()', () => {
|
|
|
|
it('should return a collection of all the switchable variable declarations in the given module',
|
|
|
|
() => {
|
|
|
|
const program = makeProgram(MARKER_FILE);
|
|
|
|
const dtsMapper = new DtsMapper('/src', '/typings');
|
2018-10-03 11:59:32 -04:00
|
|
|
const host = new Esm2015ReflectionHost(false, program.getTypeChecker(), dtsMapper);
|
2018-08-17 02:50:55 -04:00
|
|
|
const file = program.getSourceFile(MARKER_FILE.name) !;
|
|
|
|
const declarations = host.getSwitchableDeclarations(file);
|
|
|
|
expect(declarations.map(d => [d.name.getText(), d.initializer !.getText()])).toEqual([
|
|
|
|
['compileNgModuleFactory', 'compileNgModuleFactory__PRE_NGCC__']
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2018-07-16 03:51:14 -04:00
|
|
|
});
|