Last active
June 27, 2022 17:54
-
-
Save DarrenSem/401fe6326e3889dc3ed6f742a88b4b31 to your computer and use it in GitHub Desktop.
Test Suite in less than 25 lines
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
"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