Skip to content

Instantly share code, notes, and snippets.

@WhiskeyTuesday
Created May 7, 2021 04:37
Show Gist options
  • Save WhiskeyTuesday/439625f0b62478dce1dcbd359099e63b to your computer and use it in GitHub Desktop.
Save WhiskeyTuesday/439625f0b62478dce1dcbd359099e63b to your computer and use it in GitHub Desktop.
Congener js test framework
/*
CONGENER - a really simple js testing framework with a weird
name that I wrote for no real reason. As presented here it's
set up to test your next crazy implementation of FizzBuzz.
Written by @WhiskeyTuesday
This project is libre, and licenced under the terms of the
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENCE, version 3.1,
as published by dtf, July 2019.
See https://ph.dtf.wtf/u/wtfplv31 for more details.
*/
// supply the function to test
const fizzbuzz = require('./fbz');
// this is the part you'd have as a 'setup phase' or something
// like a beforeAll() in jest kind of
const range = [...Array(100)].map((_, idx) => idx + 1);
const output = range.map(n => fizzbuzz(n));
// tests is an array of test functions and descriptions, provided by parameter
// each function returns true if its description proves true
const tests = [
[
'this test fails no matter what lol',
() => false,
],
[
'this test passes no matter what lmao',
() => true,
],
[
'every result is "Fizz", "Buzz"", "FizzBuzz", or a string of idx + 1',
(results) => !results.map((result, idx) => (
result === 'Fizz'
|| result === 'Buzz'
|| result === 'FizzBuzz'
|| result === (idx + 1).toString()
)).includes(false),
],
[
'numbers which divide evenly by 3 but not 5 return "Fizz"',
(results) => !results.map((result, idx) => (
!((idx + 1) % 3)
&& (idx + 1) % 5
&& result !== 'Fizz'
)).includes(true),
],
[
'numbers which divide evenly by 5 but not 3 return "Buzz"',
(results) => !results.map((result, idx) => (
!((idx + 1) % 5)
&& (idx + 1) % 3
&& result !== 'Buzz'
)).includes(true),
],
[
'numbers which divide evenly by both 3 and 5 return "FizzBuzz"',
(results) => !results.map((result, idx) => (
!((idx + 1) % 3)
&& !((idx + 1) % 5)
&& result !== 'FizzBuzz'
)).includes(true),
],
[
'numbers which do not divide evenly into 3 or 5 return string of number',
(results) => !results.map((result, idx) => (
(idx + 1) % 3
&& (idx + 1) % 5
&& result === (idx + 1).toString()
)).includes(false),
],
];
const testResults = tests.map(([description, fn]) => {
const testResult = fn(output);
return [description, Boolean(testResult)];
});
const REDSTRING = '\x1b[31m%s\x1b[0m'
const GREENSTRING = '\x1b[32m%s\x1b[0m';
const printGreen = (str) => console.log(GREENSTRING, str);
const printRed = (str) => console.log(REDSTRING, str);
const passedTests = testResults.filter(r => r[1] === true).length;
const failedTests = testResults.filter(r => r[1] === false).length;
if(!(passedTests + failedTests === tests.length)) {
throw new Error('something terrible has happened');
}
const consoleNewline = () => console.log();
if (passedTests) { printGreen(`passed tests: ${passedTests}`); }
if (failedTests) { printRed(`failed tests: ${failedTests}`); }
consoleNewline();
testResults.forEach(([description, passed]) => passed
? console.log(GREENSTRING, description)
: console.error(REDSTRING, description));
@WhiskeyTuesday
Copy link
Author

WhiskeyTuesday commented Aug 24, 2023

const fb = (n) => ([
  !(n % 3) && 'fizz',
  !(n % 5) && 'buzz',
].filter(x => x).join('') || n);

@WhiskeyTuesday
Copy link
Author

const fb = n => `${({ true: 'Fizz', false: '' })[n % 3 === 0]}`
  + `${({ true: 'Buzz', false: '' })[n % 5 === 0]}` || n;

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