Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
**/.vitepress/dist
**/test/generated
**/__snapshots__
**/cache
.angular
.gen

**/CHANGELOG.md
pnpm-lock.yaml
62 changes: 44 additions & 18 deletions packages/codegen-core/src/__tests__/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { beforeEach, describe, expect, it } from 'vitest';

import { CodegenFile } from '../files/file';
import type { ICodegenImport } from '../imports/types';
import type { ICodegenSymbol } from '../symbols/types';
import { CodegenProject } from '../project/project';
import type { ICodegenSymbolIn } from '../symbols/types';

describe('CodegenFile', () => {
let file: CodegenFile;
let project: CodegenProject;

beforeEach(() => {
file = new CodegenFile('a.ts');
project = new CodegenProject();
file = new CodegenFile('a.ts', project);
});

it('initializes with empty imports and symbols', () => {
Expand Down Expand Up @@ -111,35 +114,52 @@ describe('CodegenFile', () => {
});

it('adds symbols', () => {
const sym1: ICodegenSymbol = { name: 'a' };
const sym2: ICodegenSymbol = { name: 'b' };
const sym1: ICodegenSymbolIn = { name: 'a' };
const sym2: ICodegenSymbolIn = { name: 'b' };
const sym3: ICodegenSymbolIn = { headless: true, name: 'c' };

file.addSymbol(sym1);
file.addSymbol(sym2);
file.addSymbol(sym3);

expect(file.symbols.length).toBe(2);
expect(file.symbols[0]).not.toBeUndefined();
expect(file.symbols[0]).not.toBe(sym1);
expect(file.symbols[0]).toEqual(sym1);
expect(file.symbols[0]).toEqual({
...sym1,
id: 0,
placeholder: '_heyapi_0_',
});
expect(file.symbols[1]).not.toBeUndefined();
expect(file.symbols[1]).not.toBe(sym2);
expect(file.symbols[1]).toEqual(sym2);
expect(file.symbols[1]).toEqual({
...sym2,
id: 1,
placeholder: '_heyapi_1_',
});
});

it('merges duplicate symbols', () => {
const sym1: ICodegenSymbol = {
it('updates symbols', () => {
const sym1: ICodegenSymbolIn = {
headless: true,
name: 'a',
value: 1,
};
const sym2: ICodegenSymbol = {
name: 'a',
const inserted = file.addSymbol(sym1);
expect(file.symbols.length).toBe(0);

const sym2: ICodegenSymbolIn = {
headless: false,
name: 'b',
value: 'foo',
};

file.addSymbol(sym1);
file.addSymbol(sym2);
file.patchSymbol(inserted.id, sym2);

expect(file.symbols.length).toBe(1);
expect(file.symbols[0]).toEqual({
name: 'a',
id: 0,
name: 'b',
placeholder: '_heyapi_0_',
value: 'foo',
});
});
Expand All @@ -162,9 +182,9 @@ describe('CodegenFile', () => {
});

it('hasSymbol returns true if symbol exists', () => {
file.addSymbol({ name: 'Exists', value: {} });
expect(file.hasSymbol('Exists')).toBe(true);
expect(file.hasSymbol('Missing')).toBe(false);
const symbol = file.addSymbol({ name: 'Exists', value: {} });
expect(file.hasSymbol(symbol.id)).toBe(true);
expect(file.hasSymbol(-1)).toBe(false);
});

it('imports, exports, and symbols getters cache arrays and update after add', () => {
Expand All @@ -182,7 +202,13 @@ describe('CodegenFile', () => {
expect(file.imports).toEqual([imp]);

file.addSymbol(symbol);
expect(file.symbols).toEqual([symbol]);
expect(file.symbols).toEqual([
{
...symbol,
id: 0,
placeholder: '_heyapi_0_',
},
]);
});

it('returns relative path to another files', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/codegen-core/src/__tests__/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* @ts-nocheck */

/**
* something about _heyapi_1_. Did you know that __heyapi_1__?
*/
export class _heyapi_1_ {
// _heyapi_1_ is great!
_heyapi_2_(_heyapi_12_: ReturnType<_heyapi_4_>): _heyapi_5_ {
return _heyapi_12_;
}
}
8 changes: 6 additions & 2 deletions packages/codegen-core/src/__tests__/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ describe('CodegenProject', () => {
const file = project.getFileByPath('a.ts')!;
expect(file).toBeDefined();
expect(file.symbols.length).toBe(1);
expect(file.symbols[0]).toEqual(symbol);
expect(file.symbols[0]).toEqual({
...symbol,
id: 0,
placeholder: '_heyapi_0_',
});
});

it('getAllSymbols returns all symbols from all files', () => {
Expand All @@ -87,7 +91,7 @@ describe('CodegenProject', () => {

// @ts-expect-error
// mutate returned array should not affect internal state
files.push(new CodegenFile('b.ts'));
files.push(new CodegenFile('b.ts', project));
expect(project.files).toEqual([file]);
});

Expand Down
40 changes: 40 additions & 0 deletions packages/codegen-core/src/__tests__/renderer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fs from 'node:fs';
import path from 'node:path';

import { describe, expect, it } from 'vitest';

import { replacePlaceholders } from '../renderers/renderer';

describe('replacePlaceholders', () => {
it('replaces ids with names', () => {
const source = fs.readFileSync(path.resolve(__dirname, 'file.ts'), {
encoding: 'utf8',
});

const substitutions: Record<string, string> = {
_heyapi_12_: 'baz',
_heyapi_1_: 'Foo',
_heyapi_2_: 'bar',
_heyapi_4_: '() => string',
_heyapi_5_: 'string',
};

const replaced = replacePlaceholders({
source,
substitutions,
});

expect(replaced).toEqual(`/* @ts-nocheck */

/**
* something about Foo. Did you know that _Foo_?
*/
export class Foo {
// Foo is great!
bar(baz: ReturnType<() => string>): string {
return baz;
}
}
`);
});
});
Loading