Skip to content

Commit

Permalink
Run all tests under folders
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacio-chiazzo committed Oct 30, 2023
1 parent 562c431 commit 21e9249
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ var kClosest = require("../../../LeetcodeProblems/Algorithms/medium/K_Closest_Po
function test1() {
var points = [[1,3],[-2,2]];
var output = [[-2,2]];
assert.strictEqual(kClosest(points,1), output);
assert.deepStrictEqual(kClosest(points,1), output);
}

function test2() {
var points = [[3,3],[5,-1],[-2,4]];
var output = [[-2,4],[3,3]];

assert.strictEqual(kClosest(points,2), output);
assert.deepStrictEqual(kClosest(points,2).sort, output.sort);
}

function test() {
Expand Down
84 changes: 46 additions & 38 deletions Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,65 @@
const fs = require("fs");

const TESTS_FOLDERS = [
// "./LeetcodeProblemsTests/Algorithms/easy/",
"./LeetcodeProblemsTests/Algorithms/medium/"
// "./LeetcodeProblemsTests/Algorithms/hard/"
"./LeetcodeProblemsTests/Algorithms/easy/",
"./LeetcodeProblemsTests/Algorithms/medium/",
"./LeetcodeProblemsTests/Algorithms/hard/"
]

const REGEX_PATTERN_HIDDEN_FILES = /(^|\/)\.[^\/\.]/g;

var test_all = async function () {
try {
console.log("problems");
const problems = await loadProblemsFiles();
// console.log(problems);
for (i in problems) {
console.log("Solving: " + problems[i]);

const tests = require(problems[i]);
console.log("*" * 100);
if (Object.keys(tests).length == 0) {
console.warn("🔴 The problem " + problems[i] + " doesn't have a test method implemented.\n");
continue;
}
for(testIdx in tests) {
tests[testIdx]();
}
console.log("✅ Tests for " + problems[i] + " run successfully \n");
}
var problems = [];
for(const i in TESTS_FOLDERS) {
var folder = TESTS_FOLDERS[i];
var new_problems = await loadProblemsFiles(folder); // await
problems = problems.concat(new_problems);
};
console.log(problems);

var solvePromises = problems.map(solve);

await Promise.all(solvePromises)
} catch (error) {
console.log(error);
throw new Error(error);
}
};

var loadProblemsFiles = () => {
return new Promise(function (resolve, reject) {
const problems = [];

for(i in TESTS_FOLDERS) {
folder = TESTS_FOLDERS[i];
fs.readdir(folder, (error, files) => {
if (error) {
console.log("........................................................................");
reject(error);
} else {
console.log(folder);
new_problems = files.filter((item) => !REGEX_PATTERN_HIDDEN_FILES.test(item));
new_problems = new_problems.map((item) => folder + item);
// console.log(new_problems);
resolve(new_problems);
}
});
var solve = (problem) => {
try {
console.log("Solving: " + problem);

const tests = require(problem);
console.log("*" * 100);
if (Object.keys(tests).length == 0) {
console.warn("🔴 The problem " + problem + " doesn't have a test method implemented.\n");
return;
}
for(testIdx in tests) {
tests[testIdx]();
}
console.log("✅ Tests for " + problem + " run successfully \n");
} catch (error) {
console.log(error);
throw new Error(error);
}
}

var loadProblemsFiles = (folder) => {
return new Promise(function (resolve, reject) {
fs.readdir(folder, (error, files) => {
if (error) {
reject(error);
} else {
console.log(folder);
new_problems = files.filter((item) => !REGEX_PATTERN_HIDDEN_FILES.test(item));
new_problems = new_problems.map((item) => folder + item);

resolve(new_problems);
}
});
});
};

Expand Down

0 comments on commit 21e9249

Please sign in to comment.