From 60880b1cea79922a4fd7b0e8413233c6b431ea59 Mon Sep 17 00:00:00 2001 From: Kratik Paliwal <128970811+KratikPaliwal@users.noreply.github.com> Date: Tue, 31 Dec 2024 23:46:24 +0530 Subject: [PATCH] Update all-your-base.spec.js Refactor converter tests to use dynamic test cases 1. Replaced individual test cases with a dynamic approach using a testCases array. 2. Improved test maintainability and scalability by automating test case execution. 3. Each test case is now defined with description, input, output, and expected error codes. --- .../all-your-base/all-your-base.spec.js | 142 ++++-------------- 1 file changed, 30 insertions(+), 112 deletions(-) diff --git a/exercises/practice/all-your-base/all-your-base.spec.js b/exercises/practice/all-your-base/all-your-base.spec.js index 35393ac..855bd22 100644 --- a/exercises/practice/all-your-base/all-your-base.spec.js +++ b/exercises/practice/all-your-base/all-your-base.spec.js @@ -58,117 +58,35 @@ describe("Converter", () => { } }); - test("single bit one to decimal", () => { - let [results, rc] = convert([1], 2, 10); - expect(rc).toEqual(0); - expect(results).toEqual([1]); - }); - - xtest("binary to single decimal", () => { - let [results, rc] = convert([1, 0, 1], 2, 10); - expect(rc).toEqual(0); - expect(results).toEqual([5]); - }); - - xtest("single decimal to binary", () => { - let [results, rc] = convert([5], 10, 2); - expect(rc).toEqual(0); - expect(results).toEqual([1, 0, 1]); - }); - - xtest("binary to multiple decimal", () => { - let [results, rc] = convert([1, 0, 1, 0, 1, 0], 2, 10); - expect(rc).toEqual(0); - expect(results).toEqual([4, 2]); - }); - - xtest("decimal to binary", () => { - let [results, rc] = convert([4, 2], 10, 2); - expect(rc).toEqual(0); - expect(results).toEqual([1, 0, 1, 0, 1, 0]); - }); - - xtest("trinary to hexadecimal", () => { - let [results, rc] = convert([1, 1, 2, 0], 3, 16); - expect(rc).toEqual(0); - expect(results).toEqual([2, 10]); - }); - - xtest("hexadecimal to trinary", () => { - let [results, rc] = convert([2, 10], 16, 3); - expect(rc).toEqual(0); - expect(results).toEqual([1, 1, 2, 0]); - }); - - xtest("15-bit integer", () => { - let [results, rc] = convert([3, 46, 60], 97, 73); - expect(rc).toEqual(0); - expect(results).toEqual([6, 10, 45]); - }); - - xtest("empty list", () => { - let [_, rc] = convert([], 2, 10); - expect(rc).toEqual(-1); - }); - - xtest("single zero", () => { - let [results, rc] = convert([0], 10, 2); - expect(rc).toEqual(0); - expect(results).toEqual([0]); - }); - - xtest("multiple zeros", () => { - let [_, rc] = convert([0, 0, 0], 10, 2); - expect(rc).toEqual(-1); - }); - - xtest("leading zeros", () => { - let [_, rc] = convert([0, 6, 0], 7, 10); - expect(rc).toEqual(-1); - }); - - xtest("negative digit", () => { - let [_, rc] = convert([1, -1, 1, 0, 1, 0], 2, 10); - expect(rc).toEqual(-1); - }); - - xtest("invalid positive digit", () => { - let [_, rc] = convert([1, 2, 1, 0, 1, 0], 2, 10); - expect(rc).toEqual(-1); - }); - - xtest("first base is one", () => { - let [_, rc] = convert([], 1, 10); - expect(rc).toEqual(-2); - }); - - xtest("second base is one", () => { - let [_, rc] = convert([1, 0, 1, 0, 1, 0], 2, 1); - expect(rc).toEqual(-3); - }); - - xtest("first base is zero", () => { - let [_, rc] = convert([], 0, 10); - expect(rc).toEqual(-2); - }); - - xtest("second base is zero", () => { - let [_, rc] = convert([7], 10, 0); - expect(rc).toEqual(-3); - }); - - xtest("first base is negative", () => { - let [_, rc] = convert([1], -2, 10); - expect(rc).toEqual(-2); - }); - - xtest("second base is negative", () => { - let [_, rc] = convert([1], 2, -7); - expect(rc).toEqual(-3); - }); - - xtest("both bases are negative", () => { - let [_, rc] = convert([1], -2, -7); - expect(rc).toEqual(-2); +const testCases = [ + { description: "single bit one to decimal", digits: [1], inputBase: 2, outputBase: 10, expected: [1] }, + { description: "binary to single decimal", digits: [1, 0, 1], inputBase: 2, outputBase: 10, expected: [5] }, + { description: "single decimal to binary", digits: [5], inputBase: 10, outputBase: 2, expected: [1, 0, 1] }, + { description: "binary to multiple decimal", digits: [1, 0, 1, 0, 1, 0], inputBase: 2, outputBase: 10, expected: [4, 2] }, + { description: "decimal to binary", digits: [4, 2], inputBase: 10, outputBase: 2, expected: [1, 0, 1, 0, 1, 0] }, + { description: "trinary to hexadecimal", digits: [1, 1, 2, 0], inputBase: 3, outputBase: 16, expected: [2, 10] }, + { description: "hexadecimal to trinary", digits: [2, 10], inputBase: 16, outputBase: 3, expected: [1, 1, 2, 0] }, + { description: "15-bit integer", digits: [3, 46, 60], inputBase: 97, outputBase: 73, expected: [6, 10, 45] }, + { description: "empty list", digits: [], inputBase: 2, outputBase: 10, expected: [], expectedErrorCode: -1 }, + { description: "single zero", digits: [0], inputBase: 10, outputBase: 2, expected: [0] }, + { description: "multiple zeros", digits: [0, 0, 0], inputBase: 10, outputBase: 2, expected: [], expectedErrorCode: -1 }, + { description: "leading zeros", digits: [0, 6, 0], inputBase: 7, outputBase: 10, expected: [], expectedErrorCode: -1 }, + { description: "negative digit", digits: [1, -1, 1, 0, 1, 0], inputBase: 2, outputBase: 10, expected: [], expectedErrorCode: -1 }, + { description: "invalid positive digit", digits: [1, 2, 1, 0, 1, 0], inputBase: 2, outputBase: 10, expected: [], expectedErrorCode: -1 }, + { description: "first base is one", digits: [], inputBase: 1, outputBase: 10, expected: [], expectedErrorCode: -2 }, + { description: "second base is one", digits: [1, 0, 1, 0, 1, 0], inputBase: 2, outputBase: 1, expected: [], expectedErrorCode: -3 }, + { description: "first base is zero", digits: [], inputBase: 0, outputBase: 10, expected: [], expectedErrorCode: -2 }, + { description: "second base is zero", digits: [7], inputBase: 10, outputBase: 0, expected: [], expectedErrorCode: -3 }, + { description: "first base is negative", digits: [1], inputBase: -2, outputBase: 10, expected: [], expectedErrorCode: -2 }, + { description: "second base is negative", digits: [1], inputBase: 2, outputBase: -7, expected: [], expectedErrorCode: -3 }, + { description: "both bases are negative", digits: [1], inputBase: -2, outputBase: -7, expected: [], expectedErrorCode: -2 } + ]; + + testCases.forEach(({ description, digits, inputBase, outputBase, expected, expectedErrorCode }) => { + test(description, () => { + let [results, rc] = convert(digits, inputBase, outputBase); + expect(rc).toEqual(expectedErrorCode || 0); + expect(results).toEqual(expected); + }); }); });