Skip to content

Commit

Permalink
Completed test migration to node native runner
Browse files Browse the repository at this point in the history
  • Loading branch information
mlhaufe committed Jun 17, 2024
1 parent e8aeadc commit 02146f6
Show file tree
Hide file tree
Showing 13 changed files with 2,620 additions and 4,578 deletions.
24 changes: 1 addition & 23 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Current File",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}\\node_modules\\jest\\bin\\jest.js",
"runtimeArgs": [
"--experimental-vm-modules",
"--inspect-brk"
],
"args": [
"-i",
"${fileBasenameNoExtension}"
],
"sourceMaps": true,
"resolveSourceMapLocations": [
"${workspaceFolder}/**",
"!**/node_modules/**"
],
"smartStep": true,
"internalConsoleOptions": "openOnSessionStart",
}
]
"configurations": []
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.formatOnSave": true,
"typescript.tsdk": "node_modules\\typescript\\lib",
Expand Down
7,088 changes: 2,577 additions & 4,511 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,21 @@
"author": "Michael L Haufe <[email protected]> (https://final-hill.com)",
"license": "AGPL-3.0-only",
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.14.2",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"eslint": "^8.57.0",
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "^2.29.1",
"eslint-webpack-plugin": "^4.2.0",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"ts-jest": "^29.1.5",
"globstar": "^1.0.0",
"ts-loader": "^9.5.1",
"tsx": "^4.15.6",
"typescript": "^5.4.5",
"webpack": "^5.92.0",
"webpack-cli": "^5.1.4"
},
"files": [
"dist"
]
}
}
4 changes: 2 additions & 2 deletions src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import assert from './assert.mjs';
import implies from './implies.mjs';

export { AssertionError, Contracted, implies, innerContract, assert };
export { Contract, extend, invariant, Invariant, checkedMode, Rescue } from './Contract.mjs';
export { Constructor, AbstractConstructor, ClassType } from './lib/ClassType.mjs';
export { Contract, extend, invariant, type Invariant, checkedMode, type Rescue } from './Contract.mjs';
export { type Constructor, type AbstractConstructor, type ClassType } from './lib/ClassType.mjs';
2 changes: 1 addition & 1 deletion src/lib/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import assertEnsures from './assertEnsures.mjs';
import assertInvariants from './assertInvariants.mjs';
import classRegistry from './classRegistry.mjs';
import ClassRegistration from './ClassRegistration.mjs';
import { ClassType } from './ClassType.mjs';
import { type ClassType } from './ClassType.mjs';
import deepFreeze from './deepFreeze.mjs';
import Feature from './Feature.mjs';
import takeWhile from './takeWhile.mjs';
Expand Down
10 changes: 3 additions & 7 deletions src/tests/Contract.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ describe('A contract must be independently definable', () => {
let stackContract = new Contract<StackType<any>>({
pop: {
demands: undefined
},
push: {
// @ts-expect-error
demands: false
}
});
nodeAssert.ok(stackContract.assertions.push!.demands instanceof Function);
nodeAssert.ok(stackContract.assertions.pop!.demands instanceof Function);

stackContract = new Contract<StackType<any>>({
pop: {
Expand Down Expand Up @@ -147,7 +143,7 @@ describe('Only a single contract can be assigned to a class', () => {
class Foo { }

return new Foo();
}, Messages.MsgSingleContract);
}, { message: Messages.MsgSingleContract });
});
});

Expand Down Expand Up @@ -204,6 +200,6 @@ describe('A subclass can only be contracted by a subcontract of the base class c
const badContract = new Contract<Bar>({});
@Contracted(badContract)
class Bar extends Foo { }
}, Messages.MsgBadSubcontract);
}, { message: Messages.MsgBadSubcontract });
});
});
2 changes: 1 addition & 1 deletion src/tests/Stack.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ describe('Testing Stack', () => {
test('popping empty stack throws', () => {
const myStack = new Stack(3);

nodeAssert.throws(() => myStack.pop(), /^demands not met/);
nodeAssert.throws(() => myStack.pop(), /^Error: demands not met/);
});
});
2 changes: 1 addition & 1 deletion src/tests/assert.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ describe('The assertion function must support assertion signatures from TypeScri
*/
describe('Assertions must support throwing custom error types', () => {
nodeAssert.throws(() => assert(false, 'BOOM!'), AssertionError);
nodeAssert.throws(() => assert(false, 'BOOM!'), 'BOOM!');
nodeAssert.throws(() => assert(false, 'BOOM!'), { message: 'BOOM!' });
nodeAssert.throws(() => assert(false, 'BOOM!', TypeError), TypeError);
});
4 changes: 2 additions & 2 deletions src/tests/invariant.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ describe('Public properties must be forbidden', () => {
}

return new Foo();
}, Messages.MsgNoProperties);
}, { message: Messages.MsgNoProperties });
});
});

Expand Down Expand Up @@ -738,7 +738,7 @@ describe('Third-party features applied to a contracted class are subject to its
const foo = new Foo(),
bar = new Bar();
bar.dec.apply(foo);
}, /^Invariant violated/);
}, /^Error: Invariant violated/);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/tests/public-properties.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ describe('Public properties must be forbidden', () => {
) { }
}

nodeAssert.throws(() => new Point2D(12, 5), Messages.MsgNoProperties);
nodeAssert.throws(() => new Point2D(12, 5), { message: Messages.MsgNoProperties });
});
});
48 changes: 26 additions & 22 deletions src/tests/rescue.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ describe('The `rescue` declaration must preserve the invariant after execution',

test('test', () => {
const base = new Base();
nodeAssert.throws(() => base.method1(), 'I am error');
nodeAssert.throws(() => base.method1(), { message: 'I am error' });
nodeAssert.strictEqual(base.value, 5);
nodeAssert.throws(() => {
base.method2();
}, /^Invariant violated/);
nodeAssert.throws(() => base.value, /^Invariant violated/);
}, /^Error: Invariant violated/);
nodeAssert.throws(() => base.value, /^Error: Invariant violated/);
});
});

Expand Down Expand Up @@ -225,7 +225,7 @@ describe('Any error thrown by a class feature must be captured by its @rescue',
set value(_: number) { throw new Error('Setter fail'); }
}
const base = new Base();
nodeAssert.throws(() => base.value = 12, 'Rescue fail');
nodeAssert.throws(() => base.value = 12, { message: 'Rescue fail' });
});
});

Expand All @@ -250,7 +250,7 @@ describe('The rescue declarations are enabled in checkedMode and disabled otherw

const base = new Base();
base.throws('I am Error');
}, 'I am still an Error');
}, { message: 'I am still an Error' });
});

test('disabled', () => {
Expand All @@ -271,7 +271,7 @@ describe('The rescue declarations are enabled in checkedMode and disabled otherw

const base = new Base();
base.throws('I am Error');
}, 'I am Error');
}, { message: 'I am Error' });
});
});

Expand Down Expand Up @@ -361,7 +361,7 @@ describe('If a `rescue` is executed and the `retry` argument is not called then
});

test('Un-rescued error', () => {
nodeAssert.throws(() => base.throwFail(), 'I am error');
nodeAssert.throws(() => base.throwFail(), { message: 'I am error' });
});
});

Expand Down Expand Up @@ -407,7 +407,7 @@ describe('If an exception is thrown in a class feature without a `rescue` define
nodeAssert.throws(() => new B().method1(), { message: 'I am error' });
nodeAssert.throws(() => {
new B().method2();
}, /^Invariant violated/);
}, /^Error: Invariant violated/);
});
});

Expand All @@ -416,12 +416,16 @@ describe('If an exception is thrown in a class feature without a `rescue` define
*/
describe('If an error is thrown in `demands` the error is raised to the caller', () => {
const contractA = new Contract<A>({
[invariant](self) { return self.value > 0; },
[invariant](self) {
return self.value > 0;
},
method: {
rescue(_self, _error, args, _retry) {
if (args[0] === -2) throw new Error('Rescue Error');
},
demands(_self, value) { return value >= 0; }
demands(_self, value) {
return value >= 0;
}
},
methodEmpty: {
demands() { return false; },
Expand All @@ -437,19 +441,20 @@ describe('If an error is thrown in `demands` the error is raised to the caller',
class A {
accessor value = 1;

method(value: number): void { this.value = value; }
method(value: number): void {
this.value = value;
}
methodEmpty(): void { }
methodError(): void { throw new Error('Feature Error'); }
}

test('Error pathways', () => {
nodeAssert.doesNotThrow(() => new A().method(1));
nodeAssert.throws(() => new A().method(0), /^Invariant violated/);
nodeAssert.throws(() => new A().method(-1), /^demands not met/);
nodeAssert.doesNotThrow(() => new A().method(-2), /^Rescue Error/);

nodeAssert.throws(() => new A().methodEmpty(), /^demands not met/);
nodeAssert.throws(() => new A().methodError(), /^Rescue Error/);
nodeAssert.throws(() => new A().method(0), /^Error: Invariant violated/);
nodeAssert.throws(() => new A().method(-1), /^Error: demands not met/);
nodeAssert.throws(() => new A().method(-2), /^Error: demands not met/);
nodeAssert.throws(() => new A().methodEmpty(), /^Error: demands not met/);
nodeAssert.throws(() => new A().methodError(), /^Error: Rescue Error/);
});
});

Expand Down Expand Up @@ -485,10 +490,9 @@ describe('If an error is raised in an `ensures` then the associated rescue is ex

test('Error pathways', () => {
nodeAssert.doesNotThrow(() => new A().method(1));
nodeAssert.throws(() => new A().method(0), /^ensures not met/);
nodeAssert.throws(() => new A().method(-1), /^Invariant violated/);
nodeAssert.doesNotThrow(() => new A().method(-2), /^Rescue Error/);
nodeAssert.throws(() => new A().methodEmpty(), /^ensures not met/);
nodeAssert.throws(() => new A().methodError(), /^Rescue Error/);
nodeAssert.throws(() => new A().method(0), /^Error: ensures not met/);
nodeAssert.throws(() => new A().method(-1), /^Error: Invariant violated/);
nodeAssert.throws(() => new A().methodEmpty(), /^Error: ensures not met/);
nodeAssert.throws(() => new A().methodError(), /^Error: Rescue Error/);
});
});
2 changes: 1 addition & 1 deletion src/tests/within.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ describe('Features should support a time constraint declaration', () => {
}
}
nodeAssert.strictEqual(new Spinner().spinLock(50), 'Okay');
nodeAssert.throws(() => new Spinner().spinLock(500), /^Timing constraint violated/);
nodeAssert.throws(() => new Spinner().spinLock(500), /^Error: Timing constraint violated/);
});
});

0 comments on commit 02146f6

Please sign in to comment.