Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Last active June 27, 2022 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenSem/401fe6326e3889dc3ed6f742a88b4b31 to your computer and use it in GitHub Desktop.
Save DarrenSem/401fe6326e3889dc3ed6f742a88b4b31 to your computer and use it in GitHub Desktop.
Test Suite in less than 25 lines
"use strict"; let undef = {}.z, str = (obj, def = "") => String(obj == undef ? def : obj), stringify = value => JSON.stringify(value, null, "\t")
, assert = (condition, msgOrActualExpectedArray, msgPrefix, shouldStringify, z) => {
if(condition)return true;
const pair = Array.isArray(msgOrActualExpectedArray) ? msgOrActualExpectedArray : undef
, [actualValue, expectedValue] = pair || []
, actual = shouldStringify ? stringify(actualValue) : actualValue
, expected = shouldStringify ? stringify(expectedValue) : expectedValue;
throw { name: "Assertion" , message: message = [
str(msgPrefix)
, pair ? `Actual=[${actual}], Expected=[${expected}]` : str(msgOrActualExpectedArray)
].map(el => el.trim()).join(" ").trim() }
}
, passes = funcToTry => { try { funcToTry(); return [true]; } catch(e) { return [false, e.message]; }; }
, report = (name, passed, message, shouldTrace, z) => { console.log(`%c${name}`, ["color:red; font-weight:bold; background-color:yellow;", "color:limegreen"][+!!passed], z = str(message)); z.length && shouldTrace && console.trace(); }
, test = (tests, shouldTrace) => {
let pass, failed = 0, attempted = 0; for(let name in tests) {
if(str(name).length && tests.hasOwnProperty(name)) {
pass = passes(tests[name]);
report(name, pass[0], pass[1], shouldTrace);
!pass[0] && (failed ++);
attempted ++;
}
}; return failed ? {failed, attempted} : false; // false if successful, so can easily check if(test({...}))
}; // via https://gist.github.com/DarrenSem [https://archive.is/Pm2jk = https://gist.github.com/DarrenSem/401fe6326e3889dc3ed6f742a88b4b31]
console.clear();
let myTests = { // assert = (condition, msgOrActualExpectedArray, msgPrefix, shouldStringify, z) => {
"#CurrentYear is 2030":
(actual, expected, z) => (
z = new Date()
, actual = z.getFullYear()
, expected = 2030
, assert(actual == expected, [actual, expected], "there's still time")
, z
)
, "Current date's year is 2022":
z => (z = new Date(), assert(z.getFullYear() === 2022, z))
, "Current date's month is Jun":
z => (z = new Date(), assert(z.toDateString().match(/^\w{3} Jun \d{2} \d{4}$/), z))
, "Current date's month is Mar":
z => (z = new Date(), assert(z.toDateString().match(/^\w{3} Mar \d{2} \d{4}$/), z))
, "Intentional fail when checking current date's month is Mar":
z => (z = new Date(), assert(z.toDacteString().match(/^\w{3} Mar \d{2} \d{4}$/), z))
}
;
let {failed, attempted} = test(myTests) || {};
if(failed)console.log("\n***ERROR: ^ failed " + failed + " of " + attempted + " ^ \n\n");
if(test(myTests))console.log("oops with date tests");
console.log("DONE.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment