-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actually adding some typescript tests and test infrastructure (#155)
* Add testdata * Actually testing things * Formatting * Missed crap * Final cleanup
- Loading branch information
1 parent
91419b7
commit c0a22e9
Showing
8 changed files
with
55 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ docker | |
**/bin/ | ||
**/obj/ | ||
launchSettings.json | ||
testdata/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"excludes": [ | ||
"**/dist/", | ||
"**/node_modules", | ||
"**/testdata", | ||
"**/*-lock.json", | ||
"./gen/", | ||
"./src/" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface TestType { | ||
readonly stringField: string; | ||
readonly numberField: number; | ||
} |