-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
overhaul: make unit tests dependency-less
- Loading branch information
Showing
10 changed files
with
175 additions
and
1,204 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import "./suites/after.mjs"; | ||
import "./suites/before.mjs"; | ||
import "./suites/instead.mjs"; | ||
import "./suites/unpatches.mjs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { notEqual as isNotEqual } from "node:assert/strict"; | ||
import { beforeEach } from "node:test"; | ||
|
||
beforeEach(() => { | ||
globalThis.testFuncs = {}; | ||
|
||
// for testing basic single patch-unpatch | ||
testFuncs.simple = (a, b) => a + b; | ||
|
||
// now we know that works, for testing context | ||
testFuncs.contextual = function (a) { | ||
isNotEqual(this?.x, undefined); | ||
isNotEqual(this.y, undefined); | ||
isNotEqual(this.z, undefined); | ||
return (this.x - a) / this.y + this.z; | ||
}; | ||
|
||
// now we know patching works, for testing patch composing | ||
testFuncs.passthru = (a) => a; | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { equal as isEqual } from "node:assert/strict"; | ||
import { describe, it } from "node:test"; | ||
import { after } from "../../dist/index.mjs"; | ||
|
||
describe("spitroast after patches", () => { | ||
it("should patch a simple func", () => { | ||
after("simple", testFuncs, ([, b], ret) => ret * b); | ||
|
||
isEqual(testFuncs.simple(1, 2), 6); | ||
}); | ||
|
||
it("should be unpatchable", () => { | ||
const unpatch = after("simple", testFuncs, () => 0); | ||
|
||
unpatch(); | ||
|
||
isEqual(testFuncs.simple(1, 2), 3); | ||
}); | ||
|
||
it("should maintain context", () => { | ||
after("contextual", testFuncs, function () { | ||
isEqual(this?.x, 17); | ||
isEqual(this.y, 5); | ||
isEqual(this.z, "test"); | ||
}); | ||
|
||
const ctx = { x: 17, y: 5, z: "test" }; | ||
|
||
isEqual(testFuncs.contextual.call(ctx, 1), "3.2test"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { equal as isEqual } from "node:assert/strict"; | ||
import { describe, it } from "node:test"; | ||
import { before } from "../../dist/index.mjs"; | ||
|
||
describe("spitroast before patches", () => { | ||
it("should patch a simple func", () => { | ||
before("simple", testFuncs, ([a, b]) => [a + b, a * b]); | ||
|
||
isEqual(testFuncs.simple(1, 2), 5); | ||
}); | ||
|
||
it("should be unpatchable", () => { | ||
const unpatch = before("simple", testFuncs, () => [0, 0]); | ||
|
||
unpatch(); | ||
|
||
isEqual(testFuncs.simple(1, 2), 3); | ||
}); | ||
|
||
it("should maintain context", () => { | ||
before("contextual", testFuncs, function () { | ||
isEqual(this?.x, 17); | ||
isEqual(this.y, 5); | ||
isEqual(this.z, "test"); | ||
}); | ||
|
||
const ctx = { x: 17, y: 5, z: "test" }; | ||
|
||
isEqual(testFuncs.contextual.call(ctx, 1), "3.2test"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { equal as isEqual } from "node:assert/strict"; | ||
import { describe, it } from "node:test"; | ||
import { instead } from "../../dist/index.mjs"; | ||
|
||
describe("spitroast instead patches", () => { | ||
it("should patch a simple func", () => { | ||
instead("simple", testFuncs, ([a, b], orig) => orig(a + b, b - a) * b); | ||
|
||
isEqual(testFuncs.simple(1, 2), 8); | ||
}); | ||
|
||
it("should be unpatchable", () => { | ||
const unpatch = instead("simple", testFuncs, () => 0); | ||
|
||
unpatch(); | ||
|
||
isEqual(testFuncs.simple(1, 2), 3); | ||
}); | ||
|
||
it("should maintain context", () => { | ||
instead("contextual", testFuncs, function (args, orig) { | ||
isEqual(this?.x, 17); | ||
isEqual(this.y, 5); | ||
isEqual(this.z, "test"); | ||
|
||
return orig.apply(this, args); | ||
}); | ||
|
||
const ctx = { x: 17, y: 5, z: "test" }; | ||
|
||
isEqual(testFuncs.contextual.call(ctx, 1), "3.2test"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { equal as isEqual } from "node:assert/strict"; | ||
import { describe, it } from "node:test"; | ||
import { after, before, instead, unpatchAll } from "../../dist/index.mjs"; | ||
|
||
describe("spitroast unpatches", () => { | ||
it("should be able to unpatch the most recent on a given func", () => { | ||
after("passthru", testFuncs, ([], ret) => ret + "a"); | ||
const unpatch = after("passthru", testFuncs, ([], ret) => ret + "b"); | ||
|
||
unpatch(); | ||
isEqual(testFuncs.passthru("x_"), "x_a"); | ||
}); | ||
|
||
it("should be able to unpatch the first on a given func", () => { | ||
const unpatch = after("passthru", testFuncs, ([], ret) => ret + "a"); | ||
after("passthru", testFuncs, ([], ret) => ret + "b"); | ||
|
||
unpatch(); | ||
isEqual(testFuncs.passthru("x_"), "x_b"); | ||
}); | ||
|
||
it("should be able to unpatch an in-between on a given func", () => { | ||
after("passthru", testFuncs, ([], ret) => ret + "a"); | ||
const unpatch = after("passthru", testFuncs, ([], ret) => ret + "b"); | ||
after("passthru", testFuncs, ([], ret) => ret + "c"); | ||
|
||
unpatch(); | ||
isEqual(testFuncs.passthru("x_"), "x_ac"); | ||
}); | ||
|
||
it("should be able to completely unpatch", () => { | ||
before("simple", testFuncs, ([a, b]) => [a + 1, b + 1]); | ||
after("simple", testFuncs, ([], ret) => ret / 2); | ||
|
||
after("passthru", testFuncs, ([], ret) => ret + "_patched"); | ||
|
||
instead("contextual", testFuncs, ([a], orig) => | ||
orig.call({ x: 1, y: 1, z: "a" }, a - 4), | ||
); | ||
|
||
unpatchAll(); | ||
|
||
const ctx = { x: 17, y: 5, z: "test" }; | ||
|
||
isEqual(testFuncs.simple(1, 2), 3); | ||
isEqual(testFuncs.contextual.call(ctx, 1), "3.2test"); | ||
isEqual(testFuncs.passthru("x"), "x"); | ||
}); | ||
}); |