Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Last active November 13, 2022 16:34
Show Gist options
  • Save DarrenSem/dfa37be21c11d1f92848e10519e97ec8 to your computer and use it in GitHub Desktop.
Save DarrenSem/dfa37be21c11d1f92848e10519e97ec8 to your computer and use it in GitHub Desktop.
assert.js - truthy test function in less than 240 characters: assert(testVal1, testVal2, () => "testFunction3", ...argN) plus improvement on normal console.assert: assertMessage(test, ...messageAndSubstitutions)
// assert.js - truthy test function in less than 240 characters: assert(testVal1, testVal2, () => "testFunction3", ...argN)
// plus improvement on normal console.assert: assertMessage(test, ...messageAndSubstitutions)
// https://gist.github.com/DarrenSem (02Nov2022 141pm)
// plus (94 characters) assert_SIMPLEST_no_messages(testVal1, testVal2, () => "testFunction3")
// OR (138 characters) assert_SIMPLEST_with_messages_via_optional_array(testVal1, [testVal2, "msg2a", "msg2b"], [() => "testFunction3", "msg3"])
// 138 char let assert=(...t)=>!!t.reduce((p,f,i,a,m=Array.isArray(f)?f:[f])=>("function"==typeof m[0]?m[0]():m[0])?p:console.assert(0,...m),t.length)
let assert_SIMPLEST_with_messages_via_optional_array = (...tests) => !!tests.reduce(
(pass, func, i, ar, messages = Array.isArray(func) ? func : [func]) => (
typeof messages[0] === "function" ? messages[0]() : messages[0]
) ? pass : console.assert(0, ...messages)
, tests.length
);
// 94 char let assert=(...t)=>!!t.reduce((p,f)=>("function"==typeof f?f():f)?p:console.error(f),t.length)
let assert_SIMPLEST_no_messages = (...tests) => !!tests.reduce(
(pass, func) => (
typeof func === "function" ? func() : func
) ? pass : console.error(func) // OR... ) ? pass : console.assert(0, func)
, tests.length
);
let assert_SIMPLEST = assert_SIMPLEST_with_messages_via_optional_array;
// let assert_SIMPLEST = assert_SIMPLEST_no_messages;
console.clear();
assert_SIMPLEST();
// false
// assert_SIMPLEST(true);
// Uncaught TypeError: z[0] is not a function
assert_SIMPLEST(() => true);
// true
assert_SIMPLEST(1 - 1);
// Assertion failed: 0
// false
console.warn("---");
let with_messages_fail = [
() => null,
null,
"<< is it truthy?",
!!null
];
assert_SIMPLEST( // this is the ONLY different result versus "previous_gist_no_messages" version
() => 1,
2 - 2,
() => 3 - 3,
with_messages_fail
);
// Assertion failed: 0
// Assertion failed: () => 3 - 3
// Assertion failed: () => null null 'is not' 'truthy' 0
// false
console.warn("---");
console.assert(null,...with_messages_fail);
// Assertion failed: () => null null 'is not' 'truthy' 0
// undefined
console.assert(...with_messages_fail);
// undefined
console.warn("---");
let with_messages_pass = [
() => 0.1,
0.1,
"<< is it truthy?",
!!0.1
];
assert_SIMPLEST(
() => 1,
2 - 2,
() => 3 - 3,
with_messages_pass
);
// Assertion failed: 0
// Assertion failed: () => 3 - 3
// false
console.assert(0.1,...with_messages_pass);
// undefined
console.assert(...with_messages_pass);
// undefined
console.warn("---");
// (show every fail) 223 char let assert=(...a)=>!a.reduce((b,c,d,e,f)=>(f=("function"==typeof c?!c():!c)&&[2>a.length?c:`<arg #${d+1}> ${a.map((a,b)=>`#${b+1} = [ ${a} ]`).join(" , ")}`],f&&console.assert(0,...b[b.push(f)-1]),b),a.length?[]:[a]).length
// (abort after 1st) 236 char let assert=(...a)=>!a.reduce((b,c,d,e,f)=>(f=("function"==typeof c?!c():!c)&&(e.length=0,[2>a.length?c:`<arg #${d+1}> ${a.map((a,b)=>`#${b+1} = [ ${a} ]`).join(" , ")}`]),f&&console.assert(0,...b[b.push(f)-1]),b),a.length?[]:[a]).length
let assert = (...args) => {
return !args.reduce((acc, value, i, ar, failureThis) => {
failureThis = !(typeof value === "function" ? value() : value) && (
// ar.length = 0, // uncomment this line to ABORT_AFTER_1ST
[
args.length < 2 ? value
: `<arg #${i + 1}> ${args.map((arg, n) => `#${n + 1} = [ ${arg} ]`).join(" , ")}` // OLD: `\t<<< #${i + 1} of ${args.length} args: ${args}`)
]
);
if(failureThis) {
console.assert(0, ...acc[acc.push(failureThis) - 1]);
};
return acc;
}, args.length ? [] : [args]).length
};
// an improvement on the normal console.assert(test, message, ...substitutions)
// 62 char let assertMessage=(t,...m)=>assert(t)||console.error(...m)||!1
let assertMessage = (test, ...messageAndSubstitutions) => assert(test) || console.error(...messageAndSubstitutions) || false;
// console.clear();
assert(9)
// true
assert()
// false
assert(9 - 9)
// VM456:11 Assertion failed: 0
// (anonymous) @ VM456:11
// assert @ VM456:2
// (anonymous) @ VM123:1
// false
assert(() => 9 - 9)
// VM456:11 Assertion failed: () => 9 - 9
// (anonymous) @ VM456:11
// assert @ VM456:2
// (anonymous) @ VM123:1
// false
assert(9 - 1, () => 9 - 1)
// true
assert(9 - 1, () => 9 - 9, () => 9 - 1, 9 - 9)
// VM456:12 Assertion failed: <arg #2> #1 = [ 8 ] , #2 = [ () => 9 - 9 ] , #3 = [ () => 9 - 1 ] , #4 = [ 0 ]
// (anonymous) @ VM456:12
// assert @ VM456:3
// (anonymous) @ VM123:1
// VM456:12 Assertion failed: <arg #4> #1 = [ 8 ] , #2 = [ () => 9 - 9 ] , #3 = [ () => 9 - 1 ] , #4 = [ 0 ]
// (anonymous) @ VM456:12
// assert @ VM456:3
// (anonymous) @ VM123:1
// false
console.assert(() => 9 - 1, "is %c9 minus 1%c truthy%c1???", "color: blue;", "color: green", "???")
// undefined returned, but "truthy" AKA not failure because it did not execute as a FUNCTION: () => 9 - 9
// true
assertMessage(() => 9 - 1, "is %c9 minus 1%c truthy%c2???", "color: blue;", "color: green", "???")
// true
console.assert(9 - 9, "is %c9 minus 9%c truthy%c#3???", "color: blue;", "color: green", "???")
// VM123:1 Assertion failed: is 9 minus 9 truthy#3???
// (anonymous) @ VM123:1
// undefined
console.assert(() => 9 - 9, "is %c9 minus 9%c truthy%c#4???", "color: blue;", "color: green", "???")
// undefined returned, but "truthy" AKA not failure because it did not execute as a FUNCTION: () => 9 - 9
assertMessage(() => 9 - 9, "is %c9 minus 9%c truthy%c#5???", "color: blue;", "color: green", "???")
// VM456:12 Assertion failed: () => 9 - 9
// (anonymous) @ VM456:12
// assert @ VM456:3
// assertMessage @ VM456:19
// (anonymous) @ VM123:1
// VM456:19 is 9 minus 9 truthy#5???
// assertMessage @ VM456:19
// (anonymous) @ VM123:1
// false
@DarrenSem
Copy link
Author

let assert_SIMPLEST = (...tests) => !!tests.reduce((pass, func) => (typeof func === "function" ? func() : func) ? pass : console.error(func), tests.length);
could also be...
let assert_SIMPLEST = (...tests) => !!tests.reduce((pass, func) => (typeof func === "function" ? func() : func) ? pass : console.assert(false, func), tests.length);
...for a red error message saying "Assertion failed: () => 123 - 123" rather than just "() => 123 - 123"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment