From e0e7849fe452b87d279d86592a4ab2eebebb3eaf Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 9 Jul 2024 16:12:28 -0400 Subject: [PATCH] Clean up some types --- justfile | 2 +- src/bun/test_base.ts | 22 +++--- src/common/all_test.ts | 4 +- src/common/types.ts | 169 +++++++++++++++++++++++++++++------------ src/deno/test_base.ts | 5 +- src/tsx/test_base.ts | 5 +- 6 files changed, 138 insertions(+), 69 deletions(-) diff --git a/justfile b/justfile index ff58d36..92e2c46 100644 --- a/justfile +++ b/justfile @@ -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 diff --git a/src/bun/test_base.ts b/src/bun/test_base.ts index 312acde..89dc27c 100644 --- a/src/bun/test_base.ts +++ b/src/bun/test_base.ts @@ -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, }; diff --git a/src/common/all_test.ts b/src/common/all_test.ts index 1c74ba7..cfdda8d 100644 --- a/src/common/all_test.ts +++ b/src/common/all_test.ts @@ -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, diff --git a/src/common/types.ts b/src/common/types.ts index 2838c2f..ad7f03f 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -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; + + /** + * Get the size of the terminal + */ + getTerminalSize(): Promise; + + /** + * Get the current chunk of input, if it exists + */ + readStdin(): Promise; + + /** + * Get the raw chunk of input + */ + readStdinRaw(): Promise; + + /** + * Pipe a string to stdout + */ + writeStdout(s: string): Promise; +} + +/** + * Runtime-specific file system io + */ +export interface IFileIO { + /** + * Open an entire file + * + * @param path + */ + openFile(path: string): Promise; + + /** + * Append to a file, or create it if it doesn't exist + * + * @param path + * @param contents + */ + appendFile(path: string, contents: string): Promise; + + /** + * Save a string into a file + * + * @param path + * @param contents + */ + saveFile(path: string, contents: string): Promise; +} + /** * 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; - - /** - * Get the size of the terminal - */ - getTerminalSize(): Promise; - - /** - * Get the current chunk of input, if it exists - */ - readStdin(): Promise; - - /** - * Get the raw chunk of input - */ - readStdinRaw(): Promise; - - /** - * Pipe a string to stdout - */ - writeStdout(s: string): Promise; - }; + term: ITerminal; /** * Runtime-specific file system io */ - file: { - openFile(path: string): Promise; - appendFile(path: string, contents: string): Promise; - saveFile(path: string, contents: string): Promise; - }; + 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; } diff --git a/src/deno/test_base.ts b/src/deno/test_base.ts index c7f1820..99b236a 100644 --- a/src/deno/test_base.ts +++ b/src/deno/test_base.ts @@ -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, }; diff --git a/src/tsx/test_base.ts b/src/tsx/test_base.ts index a51a1ac..49eb714 100644 --- a/src/tsx/test_base.ts +++ b/src/tsx/test_base.ts @@ -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,