File
Blob: types/test/print.spec.ts
| 1 | // Copyright (c) 2022-2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | import assert from 'assert'; |
| 6 | import { test } from 'node:test'; |
| 7 | import ts, { factory as f } from 'typescript'; |
| 8 | import { printNode, printNodeList } from '../src/print'; |
| 9 | |
| 10 | test('printNode: prints type', () => { |
| 11 | const type = f.createTypeReferenceNode('Promise', [ |
| 12 | f.createTypeReferenceNode('void'), |
| 13 | ]); |
| 14 | assert.strictEqual(printNode(type), 'Promise<void>'); |
| 15 | }); |
| 16 | |
| 17 | test('printNode: prints interface', () => { |
| 18 | const property = f.createPropertySignature( |
| 19 | [f.createToken(ts.SyntaxKind.ReadonlyKeyword)], |
| 20 | 'thing', |
| 21 | f.createToken(ts.SyntaxKind.QuestionToken), |
| 22 | f.createTypeReferenceNode('T') |
| 23 | ); |
| 24 | |
| 25 | const typeParam = f.createTypeParameterDeclaration( |
| 26 | /* modifiers */ undefined, |
| 27 | 'T', |
| 28 | f.createTypeReferenceNode('string') |
| 29 | ); |
| 30 | const declaration = f.createInterfaceDeclaration( |
| 31 | [f.createToken(ts.SyntaxKind.ExportKeyword)], |
| 32 | 'Test', |
| 33 | [typeParam], |
| 34 | /* heritageClauses */ undefined, |
| 35 | [property] |
| 36 | ); |
| 37 | |
| 38 | assert.strictEqual( |
| 39 | printNode(declaration), |
| 40 | `export interface Test<T extends string> { |
| 41 | readonly thing?: T; |
| 42 | }` |
| 43 | ); |
| 44 | }); |
| 45 | |
| 46 | test('printNodeList: prints statements', () => { |
| 47 | const interfaceDeclaration = f.createInterfaceDeclaration( |
| 48 | /* modifiers */ undefined, |
| 49 | 'Interface', |
| 50 | /* typeParams */ undefined, |
| 51 | /* heritageClauses */ undefined, |
| 52 | [] |
| 53 | ); |
| 54 | const classDeclaration = f.createClassDeclaration( |
| 55 | /* modifiers */ undefined, |
| 56 | 'Class', |
| 57 | /* typeParams */ undefined, |
| 58 | /* heritageClauses */ undefined, |
| 59 | [] |
| 60 | ); |
| 61 | const printed = printNodeList([interfaceDeclaration, classDeclaration]); |
| 62 | assert.strictEqual( |
| 63 | printed, |
| 64 | `interface Interface { |
| 65 | } |
| 66 | class Class { |
| 67 | } |
| 68 | ` |
| 69 | ); |
| 70 | }); |