From cb984c5c22685766a604f99fa39a5dec957bc1d2 Mon Sep 17 00:00:00 2001 From: hot9cups Date: Sun, 23 Oct 2022 13:37:56 +0530 Subject: [PATCH] Enhancing Tests.js logic - Modified Tests.js to get rid of hardcoding and work for files having variable number of test methods. Now any number of tests can be exported and can also be named anything(As opposed to being 'test' from earlier) --- LeetcodeProblemsTests/Algorithms/2Sum_Test.js | 9 ++++++--- Test.js | 20 ++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js index 132b3db..19f8ce0 100644 --- a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js +++ b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js @@ -3,15 +3,18 @@ const twoSum = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum; const twoSum2 = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum2; -var test = function () { +var testFirstSolution = function () { assert.deepEqual([0,1], twoSum([2,7,11,15], 9)); assert.deepEqual([1,2], twoSum([3,2,4], 6)); assert.deepEqual([0,1], twoSum([3,3], 6)); - +}; + +var testSecondSolution = function() { assert.deepEqual([0,1], twoSum2([2,7,11,15], 9)); assert.deepEqual([1,2], twoSum2([3,2,4], 6)); assert.deepEqual([0,1], twoSum2([3,3], 6)); }; -module.exports.test = test; +module.exports.testFirstSolution = testFirstSolution; +module.exports.testSecondSolution = testSecondSolution; diff --git a/Test.js b/Test.js index 185ebe3..27850e4 100644 --- a/Test.js +++ b/Test.js @@ -10,19 +10,15 @@ var test_all = async function () { const problems = await loadProblems(); for (i in problems) { console.log("Solving: " + problems[i]); - const problem = require(TESTS_FOLDER + problems[i]); - - if (typeof problem.test !== "undefined") { - problem.test(); - console.log("✅ Tests for " + problems[i] + " run successfully \n"); - } else { - console.warn( - problem, - "🔴 The problem " + - problems[i] + - " doesn't have a test method implemented.\n" - ); + const tests = require(TESTS_FOLDER + problems[i]); + 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"); } } catch (error) { throw new Error(error);