Skip to content

Commit

Permalink
Actually adding some typescript tests and test infrastructure (#155)
Browse files Browse the repository at this point in the history
* Add testdata

* Actually testing things

* Formatting

* Missed crap

* Final cleanup
  • Loading branch information
UnstoppableMango authored Jun 11, 2024
1 parent 91419b7 commit c0a22e9
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 25 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ docker
**/bin/
**/obj/
launchSettings.json
testdata/
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"excludes": [
"**/dist/",
"**/node_modules",
"**/testdata",
"**/*-lock.json",
"./gen/",
"./src/"
Expand Down
32 changes: 8 additions & 24 deletions packages/ts/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
import * as tdl from '@unmango/tdl-es';
import { Spec } from '@unmango/tdl-es';
import { ArrayBufferSink } from 'bun';
import { describe, expect, it } from 'bun:test';
import * as YAML from 'yaml';
import { gen } from './generator';
import { tests } from './testdata';

describe('Generator', () => {
it('should work', async () => {
const spec = new tdl.Spec({
version: '0.1.0',
name: 'test-name',
description: 'Some description',
displayName: 'Test Name',
source: 'https://github.com/UnstoppableMango/tdl',
labels: {
test: 'label',
},
types: {
'test': {
type: 'string',
fields: {
test: {
type: 'string',
},
},
},
},
});
describe('gen', () => {
it.each(tests)('should generate %p', async (_, source, target) => {
const spec = new Spec(YAML.parse(source));
expect(spec).not.toBeNull();

const buf = new ArrayBufferSink();
await gen(spec, buf);
const decoder = new TextDecoder();
const actual = decoder.decode(buf.end());

expect(actual).not.toBeNull();
expect(actual).toEqual(`export interface test {\n readonly test: string;\n}\n`);
expect(actual).toEqual(target);
});
});
3 changes: 2 additions & 1 deletion packages/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dependencies": {
"@unmango/tdl": "workspace:*",
"@unmango/tdl-es": "workspace:*",
"typescript": "5.4.5"
"typescript": "5.4.5",
"yaml": "^2.4.5"
},
"devDependencies": {
"@types/bun": "latest"
Expand Down
31 changes: 31 additions & 0 deletions packages/ts/testdata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from 'node:fs'
import path from 'node:path';

export type Test = [name: string, source: string, target: string];

function testFromDir(dirent: fs.Dirent): Test {
let source: string = '', target: string = '';
const dir = path.join(dirent.path, dirent.name);

fs.readdirSync(dir).forEach(fileName => {
const file = path.join(dir, fileName);
switch (path.basename(file, path.extname(file))) {
case 'source':
source = fs.readFileSync(file, 'utf-8');
break;
case 'target':
target = fs.readFileSync(file, 'utf-8');
break;
}
});

if (source.length <= 0 || target.length <= 0) {
throw new Error(`Invalid testdata in ${dir}`);
}

return [dirent.name, source, target];
}

export const tests = fs.readdirSync(import.meta.dir, { withFileTypes: true })
.filter(x => x.isDirectory())
.map(testFromDir);
8 changes: 8 additions & 0 deletions packages/ts/testdata/type/source.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
types:
TestType:
type: object
fields:
stringField:
type: string
numberField:
type: number
4 changes: 4 additions & 0 deletions packages/ts/testdata/type/target.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface TestType {
readonly stringField: string;
readonly numberField: number;
}

0 comments on commit c0a22e9

Please sign in to comment.