scroll/src/common/runtime/test_base.ts
Timothy J. Warren 8b44e250e2
All checks were successful
timw4mail/scroll/pipeline/head This commit looks good
Refactor again
2024-07-25 11:47:44 -04:00

96 lines
2.5 KiB
JavaScript

/**
* Adapt the node test interface to the shared testing interface
*/
import { deepStrictEqual, notStrictEqual, strictEqual } from 'node:assert';
import Option from '../option.ts';
/**
* The shared interface for tests, running on a different test
* runner for each runtime
*/
export interface ITestBase {
assertEquivalent(actual: unknown, expected: unknown): void;
assertExists(actual: unknown): void;
assertInstanceOf(actual: unknown, expectedType: any): void;
assertNotEquals(actual: unknown, expected: unknown): void;
assertEquals(actual: unknown, expected: unknown): void;
assertTrue(actual: boolean): void;
assertFalse(actual: boolean): void;
assertSome<T>(actual: Option<T>): void;
assertNone<T>(actual: Option<T>): void;
/**
* Convert the nested test object into a test suite for the current runtime
*/
testSuite(testObj: any): void;
}
/**
* The base testing implementation using Node assert API
*/
export abstract class AbstractTestBase implements Partial<ITestBase> {
/**
* The values (often objects) have all the same property values
*/
public static assertEquivalent(actual: unknown, expected: unknown): void {
return deepStrictEqual(actual, expected);
}
/**
* The value is not null or undefined
*/
public static assertExists(actual: unknown): void {
return notStrictEqual(actual, undefined);
}
/**
* `actual` is an object implementing `expectedType`
*/
public static assertInstanceOf(actual: unknown, expectedType: any): void {
return strictEqual(actual instanceof expectedType, true);
}
/**
* The values are not exactly equal (Different instance, type, value, etc)
*/
public static assertNotEquals(actual: unknown, expected: unknown): void {
return notStrictEqual(actual, expected);
}
/**
* The values are exactly the same
*/
public static assertEquals(actual: unknown, expected: unknown): void {
return strictEqual(actual, expected);
}
/**
* The value is true
*/
public static assertTrue(actual: boolean): void {
return strictEqual(actual, true);
}
/**
* The value is false
*/
public static assertFalse(actual: boolean): void {
return strictEqual(actual, false);
}
/**
* The value is a `Some` type `Option`
*/
public static assertSome<T>(actual: Option<T>): void {
return AbstractTestBase.assertTrue(actual.isSome());
}
/**
* The value is a `None` type `Option`
*/
public static assertNone<T>(actual: Option<T>): void {
return AbstractTestBase.assertTrue(actual.isNone());
}
}
export default AbstractTestBase;