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 deno fmt
# Run tests with all the runtimes # Run tests with all the runtimes
test: deno-test bun-test test: deno-test tsx-test bun-test
# Run all code-quality related tasks # Run all code-quality related tasks
quality: check test quality: check test

View File

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

View File

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

View File

@ -10,10 +10,81 @@ export interface ITerminalSize {
cols: number; cols: number;
} }
/**
* Which direction to search in the current document
*/
export enum SearchDirection {
Forward = 1,
Backward = -1,
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Runtime adapter interfaces // 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 * The common interface for runtime adapters
*/ */
@ -26,46 +97,12 @@ export interface IRuntime {
/** /**
* Runtime-specific terminal functionality * Runtime-specific terminal functionality
*/ */
term: { term: 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 * Runtime-specific file system io
*/ */
file: { file: IFileIO;
openFile(path: string): Promise<string>;
appendFile(path: string, contents: string): Promise<void>;
saveFile(path: string, contents: string): Promise<void>;
};
/** /**
* Set up an event handler * Set up an event handler
@ -92,16 +129,6 @@ export interface IRuntime {
exit(code?: number): void; exit(code?: number): void;
} }
/**
* Runtime-specific terminal functionality
*/
export type ITerminal = IRuntime['term'];
/**
* Runtime-specific file handling
*/
export type IFileIO = IRuntime['file'];
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Testing // Testing
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -110,13 +137,57 @@ export type IFileIO = IRuntime['file'];
* The shared test interface, so tests can be run by both runtimes * The shared test interface, so tests can be run by both runtimes
*/ */
export interface ITestBase { 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; assertExists(actual: unknown): void;
/**
* The value is false
*
* @param actual
*/
assertFalse(actual: boolean): void; assertFalse(actual: boolean): void;
/**
* `actual` is an object implementing `expectedType`
*
* @param actual
* @param expectedType
*/
assertInstanceOf(actual: unknown, expectedType: any): void; 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; 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; assertTrue(actual: boolean): void;
testSuite(testObj: any): void; testSuite(testObj: any): void;
} }

View File

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

View File

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