summaryrefslogtreecommitdiff
path: root/tests/js/utils.mjs
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-02-25 06:46:52 -0300
committerEuAndreh <eu@euandre.org>2024-02-25 06:46:52 -0300
commit1f0c90b9a34872c3a33ea12c6e5e329a1f98b213 (patch)
treeb4522548f89a2abe28053d47748b0f9adee8c164 /tests/js/utils.mjs
parentExplicit import from "node:process"; move log() to hero.mjs (diff)
downloadpapod-1f0c90b9a34872c3a33ea12c6e5e329a1f98b213.tar.gz
papod-1f0c90b9a34872c3a33ea12c6e5e329a1f98b213.tar.xz
src/hero.mjs: Promote log() to fancy logger object
Diffstat (limited to 'tests/js/utils.mjs')
-rw-r--r--tests/js/utils.mjs70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/js/utils.mjs b/tests/js/utils.mjs
index 5a1507c..004af95 100644
--- a/tests/js/utils.mjs
+++ b/tests/js/utils.mjs
@@ -9,6 +9,7 @@ import {
getIn,
first,
promisify,
+ partial,
} from "../../src/utils.mjs";
const test_eq = t => {
@@ -275,6 +276,74 @@ const test_promisify = t => {
});
};
+const test_partial = t => {
+ t.start("partial()");
+
+ t.test("empty values", () => {
+ const adder = (a, b, c) => a + b + c;
+
+ const adder1 = partial(adder);
+ assert.equal(adder1(1, 2, 3), 6);
+
+ const adder2 = partial(adder, 4, 5, 6);
+ assert.equal(adder2(), 15);
+
+ const noargs = () => "static";
+ assert.equal(partial(noargs)(), noargs());
+ });
+
+ t.test("too few arguments", () => {
+ const threeArgs = (a, b, c) => {
+ assert.notEqual(c, undefined);
+ return a + b + c
+ };
+
+ const add1 = partial(threeArgs, 1);
+ assert.throws(
+ () => add1(2),
+ assert.AssertionError,
+ );
+
+ const add1And2 = partial(threeArgs, 1, 2);
+ assert.throws(
+ () => add1And2(),
+ assert.AssertionError,
+ );
+
+ const addNothing = partial(threeArgs);
+ assert.throws(
+ () => addNothing(),
+ assert.AssertionError,
+ );
+ });
+
+ t.test("too many arguments", () => {
+ const twoArgs = (a, b) => a + b;
+
+ assert.equal(partial(twoArgs, 1)(2, 3), 3);
+ assert.equal(partial(twoArgs, 1, 2)(3), 3);
+ });
+
+ t.test("daily usage", () => {
+ const twoArg = (a, b) => a + b;
+
+ const numbers = [ 1, 2, 3, 4, 5 ];
+ assert.deepEqual(
+ numbers.map(partial(twoArg, 2)),
+ [ 3, 4, 5, 6, 7 ],
+ );
+ });
+
+ t.test("nested partials", () => {
+ const threeArgs = (a, b, c) => a + b + c;
+
+ const add1 = partial(threeArgs, 1);
+ const add1And2 = partial(add1, 2);
+
+ assert.equal(add1And2(3), 6);
+ });
+};
+
await runner.runTests([
test_eq,
@@ -284,4 +353,5 @@ await runner.runTests([
test_getIn,
test_first,
test_promisify,
+ test_partial,
]);