|
| 1 | +import * as E from 'fp-ts/lib/Either'; |
| 2 | +import assert from 'node:assert'; |
| 3 | +import test from 'node:test'; |
| 4 | + |
| 5 | +import { TestProject } from './testProject'; |
| 6 | +import { parsePlainInitializer, type Schema } from '../src'; |
| 7 | +import { KNOWN_IMPORTS, type KnownImports } from '../src/knownImports'; |
| 8 | + |
| 9 | +async function testCase( |
| 10 | + description: string, |
| 11 | + src: string, |
| 12 | + knownImports: KnownImports, |
| 13 | + expected: Record<string, Schema>, |
| 14 | + expectedErrors: string[] = [], |
| 15 | +) { |
| 16 | + test(description, async () => { |
| 17 | + const project = new TestProject( |
| 18 | + { '/index.ts': src }, |
| 19 | + { ...KNOWN_IMPORTS, ...knownImports }, |
| 20 | + ); |
| 21 | + await project.parseEntryPoint('/index.ts'); |
| 22 | + const sourceFile = project.get('/index.ts'); |
| 23 | + if (sourceFile === undefined) { |
| 24 | + throw new Error('Source file not found'); |
| 25 | + } |
| 26 | + |
| 27 | + const actual: Record<string, Schema> = {}; |
| 28 | + const errors: string[] = []; |
| 29 | + for (const symbol of sourceFile.symbols.declarations) { |
| 30 | + if (symbol.init !== undefined) { |
| 31 | + const result = parsePlainInitializer(project, sourceFile, symbol.init); |
| 32 | + if (E.isLeft(result)) { |
| 33 | + errors.push(result.left); |
| 34 | + } else { |
| 35 | + if (symbol.comment !== undefined) { |
| 36 | + result.right.comment = symbol.comment; |
| 37 | + } |
| 38 | + actual[symbol.name] = result.right; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + assert.deepStrictEqual(errors, expectedErrors); |
| 44 | + assert.deepStrictEqual(actual, expected); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +const EXTERNAL_CUSTOM_CODEC: KnownImports = { |
| 49 | + foo: { |
| 50 | + bar: () => E.right({ type: 'primitive', value: 'string' }), |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +const EXTERNAL_CUSTOM_CODEC_SRC = ` |
| 55 | +import * as f from 'foo'; |
| 56 | +
|
| 57 | +export const FOO = f.bar; |
| 58 | +`; |
| 59 | + |
| 60 | +testCase( |
| 61 | + 'External custom codecs are parsed', |
| 62 | + EXTERNAL_CUSTOM_CODEC_SRC, |
| 63 | + EXTERNAL_CUSTOM_CODEC, |
| 64 | + { |
| 65 | + FOO: { type: 'primitive', value: 'string' }, |
| 66 | + }, |
| 67 | +); |
| 68 | + |
| 69 | +const INTERNAL_CODEC_OVERRIDE: KnownImports = { |
| 70 | + '.': { |
| 71 | + bar: () => E.right({ type: 'primitive', value: 'string' }), |
| 72 | + }, |
| 73 | +}; |
| 74 | + |
| 75 | +const INTERNAL_CODEC_OVERRIDE_SRC = ` |
| 76 | +import * as t from 'io-ts'; |
| 77 | +import { bar } from './bar'; |
| 78 | +
|
| 79 | +export const FOO = t.type({ bar: bar }); |
| 80 | +`; |
| 81 | + |
| 82 | +testCase( |
| 83 | + 'Internal codec overrides are parsed', |
| 84 | + INTERNAL_CODEC_OVERRIDE_SRC, |
| 85 | + INTERNAL_CODEC_OVERRIDE, |
| 86 | + { |
| 87 | + FOO: { |
| 88 | + type: 'object', |
| 89 | + properties: { |
| 90 | + bar: { type: 'primitive', value: 'string' }, |
| 91 | + }, |
| 92 | + required: ['bar'], |
| 93 | + }, |
| 94 | + }, |
| 95 | +); |
0 commit comments