"Fossies" - the Fresh Open Source Software Archive

Member "angular-13.3.9/packages/compiler-cli/src/ngtsc/shims/test/adapter_spec.ts" (18 May 2022, 3068 Bytes) of package /linux/www/angular-13.3.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * @license
    3  * Copyright Google LLC All Rights Reserved.
    4  *
    5  * Use of this source code is governed by an MIT-style license that can be
    6  * found in the LICENSE file at https://angular.io/license
    7  */
    8 
    9 import ts from 'typescript';
   10 
   11 import {absoluteFrom as _} from '../../file_system';
   12 import {runInEachFileSystem} from '../../file_system/testing';
   13 import {makeProgram} from '../../testing';
   14 import {ShimAdapter} from '../src/adapter';
   15 import {TestShimGenerator} from './util';
   16 
   17 runInEachFileSystem(() => {
   18   describe('ShimAdapter', () => {
   19     it('should recognize a basic shim name', () => {
   20       const {host} = makeProgram([{
   21         name: _('/test.ts'),
   22         contents: `export class A {}`,
   23       }]);
   24 
   25       const adapter =
   26           new ShimAdapter(host, [], [], [new TestShimGenerator()], /* oldProgram */ null);
   27       const shimSf = adapter.maybeGenerate(_('/test.testshim.ts'));
   28       expect(shimSf).not.toBeNull();
   29       expect(shimSf!.fileName).toBe(_('/test.testshim.ts'));
   30       expect(shimSf!.text).toContain('SHIM_FOR_FILE');
   31     });
   32 
   33     it('should not recognize a normal file in the program', () => {
   34       const {host} = makeProgram([{
   35         name: _('/test.ts'),
   36         contents: `export class A {}`,
   37       }]);
   38 
   39       const adapter =
   40           new ShimAdapter(host, [], [], [new TestShimGenerator()], /* oldProgram */ null);
   41       const shimSf = adapter.maybeGenerate(_('/test.ts'));
   42       expect(shimSf).toBeNull();
   43     });
   44 
   45     it('should not recognize a shim-named file without a source file', () => {
   46       const {host} = makeProgram([{
   47         name: _('/test.ts'),
   48         contents: `export class A {}`,
   49       }]);
   50 
   51       const adapter =
   52           new ShimAdapter(host, [], [], [new TestShimGenerator()], /* oldProgram */ null);
   53       const shimSf = adapter.maybeGenerate(_('/other.testshim.ts'));
   54 
   55       // Expect undefined, not null, since that indicates a valid shim path but an invalid source
   56       // file.
   57       expect(shimSf).toBeUndefined();
   58     });
   59 
   60     it('should detect a prior shim if one is available', () => {
   61       // Create a shim via the ShimAdapter, then create a second ShimAdapter simulating an
   62       // incremental compilation, with a stub passed for the oldProgram that includes the original
   63       // shim file. Verify that the new ShimAdapter incorporates the original shim in generation of
   64       // the new one.
   65       const {host, program} = makeProgram([
   66         {
   67           name: _('/test.ts'),
   68           contents: `export class A {}`,
   69         },
   70       ]);
   71       const adapter =
   72           new ShimAdapter(host, [], [], [new TestShimGenerator()], /* oldProgram */ null);
   73       const originalShim = adapter.maybeGenerate(_('/test.testshim.ts'))!;
   74       const oldProgramStub = {
   75         getSourceFiles: () => [...program.getSourceFiles(), originalShim],
   76       } as unknown as ts.Program;
   77 
   78       const adapter2 = new ShimAdapter(host, [], [], [new TestShimGenerator()], oldProgramStub);
   79       const newShim = adapter.maybeGenerate(_('/test.testshim.ts'));
   80       expect(newShim).toBe(originalShim);
   81     });
   82   });
   83 });