2019-04-28 15:47:57 -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
|
|
|
|
*/
|
|
|
|
|
2019-12-24 19:10:01 -05:00
|
|
|
import {FactoryMap, isRelativePath, stripExtension} from '../src/utils';
|
2019-12-20 08:38:11 -05:00
|
|
|
|
2019-12-24 19:10:01 -05:00
|
|
|
describe('FactoryMap', () => {
|
2019-12-20 08:38:11 -05:00
|
|
|
it('should return an existing value', () => {
|
2019-12-24 19:10:01 -05:00
|
|
|
const factoryFnSpy = jasmine.createSpy('factory');
|
|
|
|
const factoryMap = new FactoryMap<string, string>(factoryFnSpy, [['k1', 'v1'], ['k2', 'v2']]);
|
2019-12-20 08:38:11 -05:00
|
|
|
|
2019-12-24 19:10:01 -05:00
|
|
|
expect(factoryMap.get('k1')).toBe('v1');
|
|
|
|
expect(factoryMap.get('k2')).toBe('v2');
|
|
|
|
expect(factoryFnSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not treat falsy values as missing', () => {
|
|
|
|
const factoryFnSpy = jasmine.createSpy('factory').and.returnValue('never gonna happen');
|
|
|
|
const factoryMap = new FactoryMap<string, any>(factoryFnSpy, [
|
|
|
|
['k1', ''],
|
|
|
|
['k2', 0],
|
|
|
|
['k3', false],
|
|
|
|
['k4', null],
|
|
|
|
['k5', undefined],
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(factoryMap.get('k1')).toBe('');
|
|
|
|
expect(factoryMap.get('k2')).toBe(0);
|
|
|
|
expect(factoryMap.get('k3')).toBe(false);
|
|
|
|
expect(factoryMap.get('k4')).toBe(null);
|
|
|
|
expect(factoryMap.get('k5')).toBe(undefined);
|
|
|
|
expect(factoryFnSpy).not.toHaveBeenCalled();
|
2019-12-20 08:38:11 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create, store and return the value if it does not exist', () => {
|
2019-12-24 19:10:01 -05:00
|
|
|
const factoryFnSpy = jasmine.createSpy('factory').and.returnValues('v3', 'never gonna happen');
|
|
|
|
const factoryMap = new FactoryMap(factoryFnSpy, [['k1', 'v1'], ['k2', 'v2']]);
|
2019-12-20 08:38:11 -05:00
|
|
|
|
2019-12-24 19:10:01 -05:00
|
|
|
expect(factoryMap.get('k3')).toBe('v3');
|
|
|
|
expect(factoryFnSpy).toHaveBeenCalledTimes(1);
|
2019-12-20 08:38:11 -05:00
|
|
|
|
2019-12-24 19:10:01 -05:00
|
|
|
factoryFnSpy.calls.reset();
|
2019-12-20 08:38:11 -05:00
|
|
|
|
2019-12-24 19:10:01 -05:00
|
|
|
expect(factoryMap.get('k3')).toBe('v3');
|
|
|
|
expect(factoryFnSpy).not.toHaveBeenCalled();
|
2019-12-20 08:38:11 -05:00
|
|
|
});
|
|
|
|
});
|
2019-04-28 15:47:57 -04:00
|
|
|
|
|
|
|
describe('isRelativePath()', () => {
|
|
|
|
it('should return true for relative paths', () => {
|
|
|
|
expect(isRelativePath('.')).toBe(true);
|
|
|
|
expect(isRelativePath('..')).toBe(true);
|
|
|
|
expect(isRelativePath('./')).toBe(true);
|
|
|
|
expect(isRelativePath('../')).toBe(true);
|
|
|
|
expect(isRelativePath('./abc/xyz')).toBe(true);
|
|
|
|
expect(isRelativePath('../abc/xyz')).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true for absolute paths', () => {
|
|
|
|
expect(isRelativePath('/')).toBe(true);
|
|
|
|
expect(isRelativePath('/abc/xyz')).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for other paths', () => {
|
|
|
|
expect(isRelativePath('abc')).toBe(false);
|
|
|
|
expect(isRelativePath('abc/xyz')).toBe(false);
|
|
|
|
expect(isRelativePath('.abc')).toBe(false);
|
|
|
|
expect(isRelativePath('..abc')).toBe(false);
|
|
|
|
expect(isRelativePath('@abc')).toBe(false);
|
|
|
|
expect(isRelativePath('.abc/xyz')).toBe(false);
|
|
|
|
expect(isRelativePath('..abc/xyz')).toBe(false);
|
|
|
|
expect(isRelativePath('@abc/xyz')).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2019-12-20 08:38:11 -05:00
|
|
|
|
|
|
|
describe('stripExtension()', () => {
|
|
|
|
it('should strip the extension from a file name', () => {
|
|
|
|
expect(stripExtension('foo.ts')).toBe('foo');
|
|
|
|
expect(stripExtension('/foo/bar.ts')).toBe('/foo/bar');
|
|
|
|
expect(stripExtension('/foo/bar.d.ts')).toBe('/foo/bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do nothing if there is no extension in a file name', () => {
|
|
|
|
expect(stripExtension('foo')).toBe('foo');
|
|
|
|
expect(stripExtension('/foo/bar')).toBe('/foo/bar');
|
|
|
|
expect(stripExtension('/fo-o/b_ar')).toBe('/fo-o/b_ar');
|
|
|
|
});
|
|
|
|
});
|