Skip to content

Instantly share code, notes, and snippets.

@gavinsykes
Created November 30, 2021 22:05
Show Gist options
  • Save gavinsykes/a92af408b5e7ac36a36384376fb31126 to your computer and use it in GitHub Desktop.
Save gavinsykes/a92af408b5e7ac36a36384376fb31126 to your computer and use it in GitHub Desktop.
Testing script in JavaScript
import { readFileSync } from 'fs';
const expectedAnswers = JSON.parse(readFileSync('problem_1/problem_1_expected_answers.json', 'utf8'));
import { euler_1 } from './problem_1.js';
let successfulTests = 0;
const failedTests = [];
expectedAnswers.forEach((expectedAnswer) => {
console.log(`Input of ${expectedAnswer.input} should yield ${expectedAnswer.expected_answer}`);
let success = false;
let formatCharacter = "\x1b[1;31m";
const closingFormatCharacter = "\x1b[0m";
const answer = euler_1(expectedAnswer.input);
if (answer === expectedAnswer.expected_answer) {
successfulTests++;
success = true;
formatCharacter = "\x1b[1;32m";
} else {
failedTests.push({
'input': expectedAnswer.input,
'expectedAnswer': expectedAnswer.expected_answer,
'actualAnswer': answer
});
}
console.log(`${formatCharacter}Input of ${expectedAnswer.input} yields ${answer}${closingFormatCharacter}`);
});
const totalTests = successfulTests + failedTests.length;
console.log(`\x1b[1mTotal tests: ${totalTests}\x1b[0m`);
console.log(`\x1b[1;32mSuccessful tests: ${successfulTests} (${successfulTests * 100 / totalTests}%)\x1b[0m`);
if (failedTests.length > 0) {
console.log(`\x1b[1;31mFailed tests: ${failedTests.length} (${failedTests.length * 100 / totalTests}%)\x1b[0m`);
failedTests.forEach((test) => {
console.log(`\x1b[1;31mInput of ${test.input} should have yielded ${test.expectedAnswer} but yields ${test.actualAnswer}\x1b[0m`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment