Clean up some types

This commit is contained in:
Timothy Warren 2024-07-09 16:12:28 -04:00
parent 88bf3da4e7
commit e0e7849fe4
6 changed files with 138 additions and 69 deletions

View File

@ -16,7 +16,7 @@ fmt:
deno fmt
# Run tests with all the runtimes
test: deno-test bun-test
test: deno-test tsx-test bun-test
# Run all code-quality related tasks
quality: check test

View File

@ -1,7 +1,8 @@
/**
* Adapt the bun test interface to the shared testing interface
*/
import { describe, expect, test } from 'bun:test';
import { deepStrictEqual, notStrictEqual, strictEqual } from 'node:assert';
import { describe, test } from 'bun:test';
import { ITestBase } from '../common/types.ts';
export function testSuite(testObj: any) {
@ -16,18 +17,17 @@ export function testSuite(testObj: any) {
}
const BunTestBase: ITestBase = {
assertEquals: (actual: unknown, expected: unknown) =>
expect(actual).toEqual(expected),
assertExists: (actual: unknown) => expect(actual).toBeDefined(),
assertFalse: (actual: boolean) => expect(actual).toBe(false),
assertEquivalent: (actual: unknown, expected: unknown) =>
deepStrictEqual(actual, expected),
assertExists: (actual: unknown) => notStrictEqual(actual, undefined),
assertFalse: (actual: boolean) => strictEqual(actual, false),
assertInstanceOf: (actual: unknown, expectedType: any) =>
expect(actual).toBeInstanceOf(expectedType),
strictEqual(actual instanceof expectedType, true),
assertNotEquals: (actual: unknown, expected: unknown) =>
expect(actual).not.toBe(expected),
assertNull: (actual: unknown) => expect(actual).toBeNull(),
assertStrictEquals: (actual: unknown, expected: unknown) =>
expect(actual).toBe(expected),
assertTrue: (actual: boolean) => expect(actual).toBe(true),
notStrictEqual(actual, expected),
assertEquals: (actual: unknown, expected: unknown) =>
strictEqual(actual, expected),
assertTrue: (actual: boolean) => strictEqual(actual, true),
testSuite,
};

View File

@ -12,8 +12,8 @@ import { defaultTerminalSize, SCROLL_TAB_SIZE } from './config.ts';
import { getTestRunner } from './runtime.ts';
const {
assertStrictEquals: assertEquals,
assertEquals: assertEquivalent,
assertEquals,
assertEquivalent,
assertExists,
assertInstanceOf,
assertNotEquals,

View File

@ -10,10 +10,81 @@ export interface ITerminalSize {
cols: number;
}
/**
* Which direction to search in the current document
*/
export enum SearchDirection {
Forward = 1,
Backward = -1,
}
// ----------------------------------------------------------------------------
// Runtime adapter interfaces
// ----------------------------------------------------------------------------
/**
* Runtime-specific terminal functionality
*/
export interface ITerminal {
/**
* The arguments passed to the program on launch
*/
argv: string[];
/**
* The generator function returning chunks of input from the stdin stream
*/
inputLoop(): AsyncGenerator<Uint8Array, null>;
/**
* Get the size of the terminal
*/
getTerminalSize(): Promise<ITerminalSize>;
/**
* Get the current chunk of input, if it exists
*/
readStdin(): Promise<string | null>;
/**
* Get the raw chunk of input
*/
readStdinRaw(): Promise<Uint8Array | null>;
/**
* Pipe a string to stdout
*/
writeStdout(s: string): Promise<void>;
}
/**
* Runtime-specific file system io
*/
export interface IFileIO {
/**
* Open an entire file
*
* @param path
*/
openFile(path: string): Promise<string>;
/**
* Append to a file, or create it if it doesn't exist
*
* @param path
* @param contents
*/
appendFile(path: string, contents: string): Promise<void>;
/**
* Save a string into a file
*
* @param path
* @param contents
*/
saveFile(path: string, contents: string): Promise<void>;
}
/**
* The common interface for runtime adapters
*/
@ -26,46 +97,12 @@ export interface IRuntime {
/**
* Runtime-specific terminal functionality
*/
term: {
/**
* The arguments passed to the program on launch
*/
argv: string[];
/**
* The generator function returning chunks of input from the stdin stream
*/
inputLoop(): AsyncGenerator<Uint8Array, null>;
/**
* Get the size of the terminal
*/
getTerminalSize(): Promise<ITerminalSize>;
/**
* Get the current chunk of input, if it exists
*/
readStdin(): Promise<string | null>;
/**
* Get the raw chunk of input
*/
readStdinRaw(): Promise<Uint8Array | null>;
/**
* Pipe a string to stdout
*/
writeStdout(s: string): Promise<void>;
};
term: ITerminal;
/**
* Runtime-specific file system io
*/
file: {
openFile(path: string): Promise<string>;
appendFile(path: string, contents: string): Promise<void>;
saveFile(path: string, contents: string): Promise<void>;
};
file: IFileIO;
/**
* Set up an event handler
@ -92,16 +129,6 @@ export interface IRuntime {
exit(code?: number): void;
}
/**
* Runtime-specific terminal functionality
*/
export type ITerminal = IRuntime['term'];
/**
* Runtime-specific file handling
*/
export type IFileIO = IRuntime['file'];
// ----------------------------------------------------------------------------
// Testing
// ----------------------------------------------------------------------------
@ -110,13 +137,57 @@ export type IFileIO = IRuntime['file'];
* The shared test interface, so tests can be run by both runtimes
*/
export interface ITestBase {
assertEquals(actual: unknown, expected: unknown): void;
/**
* The values (often objects) have all the same property values
*
* @param actual
* @param expected
*/
assertEquivalent(actual: unknown, expected: unknown): void;
/**
* The value is not null or undefined
*
* @param actual
*/
assertExists(actual: unknown): void;
/**
* The value is false
*
* @param actual
*/
assertFalse(actual: boolean): void;
/**
* `actual` is an object implementing `expectedType`
*
* @param actual
* @param expectedType
*/
assertInstanceOf(actual: unknown, expectedType: any): void;
/**
* The values are not exactly equal (Different instance, type, value, etc)
*
* @param actual
* @param expected
*/
assertNotEquals(actual: unknown, expected: unknown): void;
assertNull(actual: unknown): void;
assertStrictEquals(actual: unknown, expected: unknown): void;
/**
* The values are exactly the same
*
* @param actual
* @param expected
*/
assertEquals(actual: unknown, expected: unknown): void;
/**
* The value is true
*
* @param actual
*/
assertTrue(actual: boolean): void;
testSuite(testObj: any): void;
}

View File

@ -18,14 +18,13 @@ export function testSuite(testObj: any) {
}
const DenoTestBase: ITestBase = {
assertEquals,
assertEquivalent: assertEquals,
assertExists,
assertInstanceOf,
assertNotEquals,
assertStrictEquals,
assertEquals: assertStrictEquals,
assertTrue: (actual: boolean) => assertStrictEquals(actual, true),
assertFalse: (actual: boolean) => assertStrictEquals(actual, false),
assertNull: (actual: any) => assertEquals(actual, null),
testSuite,
};

View File

@ -22,7 +22,7 @@ export function testSuite(testObj: any) {
}
const TsxTestBase: ITestBase = {
assertEquals: (actual: unknown, expected: unknown) =>
assertEquivalent: (actual: unknown, expected: unknown) =>
deepStrictEqual(actual, expected),
assertExists: (actual: unknown) => notStrictEqual(actual, undefined),
assertFalse: (actual: boolean) => strictEqual(actual, false),
@ -30,8 +30,7 @@ const TsxTestBase: ITestBase = {
strictEqual(actual instanceof expectedType, true),
assertNotEquals: (actual: unknown, expected: unknown) =>
notStrictEqual(actual, expected),
assertNull: (actual: unknown) => strictEqual(actual, null),
assertStrictEquals: (actual: unknown, expected: unknown) =>
assertEquals: (actual: unknown, expected: unknown) =>
strictEqual(actual, expected),
assertTrue: (actual: boolean) => strictEqual(actual, true),
testSuite,