diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js index cf50f6cad..9a8b2a805 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js @@ -10,3 +10,43 @@ test("Classifies right angles", () => { const right = getAngleType(90); assert.equal(right, "Right angle"); }); + +test("Returns invalid angle for less than 0", () => { + const negative = getAngleType(-10); + assert.equal(negative, "Invalid angle"); +}); + +test("Returns invalid angle for 0", () => { + const angle0 = getAngleType(-10); + assert.equal(angle0, "Invalid angle"); +}); + +test("Returns invalid angle for greater than 360", () => { + const greater360 = getAngleType(450); + assert.equal(greater360, "Invalid angle"); +}); + +test("Returns invalid angle for 360", () => { + const angle360 = getAngleType(360); + assert.equal(angle360, "Invalid angle"); +}); + +test("Classifies acute angles", () => { + const acute = getAngleType(34); + assert.equal(acute, "Acute angle"); +}); + +test("Classifies obtuse angles", () => { + const obtuse = getAngleType(125); + assert.equal(obtuse, "Obtuse angle"); +}); + +test("Classifies straight angles", () => { + const straight = getAngleType(180); + assert.equal(straight, "Straight angle"); +}); + +test("Classifies reflex angles", () => { + const reflex = getAngleType(260); + assert.equal(reflex, "Reflex angle"); +}); diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js index a29084605..4593e8de8 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js @@ -10,3 +10,43 @@ test("Basic proper fraction", () => { // Example: 1/2 is a proper fraction assert.equal(isProperFraction(1, 2), true); }); + +test("Basic improper fraction", () => { + assert.equal(isProperFraction(3, 2), false); +}); + +test("equal numerator and denominator", () => { + assert.equal(isProperFraction(5, 5), false); +}); + +test("negative numerator proper fraction", () => { + assert.equal(isProperFraction(-1, 2), true); +}); + +test("negative denominator proper fraction", () => { + assert.equal(isProperFraction(1, -2), true); +}); + +test("negative denominator and negative numerator proper fraction", () => { + assert.equal(isProperFraction(-1, -2), true); +}); + +test("negative numerator improper fraction", () => { + assert.equal(isProperFraction(-3, 2), false); +}); + +test("negative denominator improper fraction", () => { + assert.equal(isProperFraction(3, -2), false); +}); + +test("negative denominator and negative numerator improper fraction", () => { + assert.equal(isProperFraction(-3, -2), false); +}); + +test("zero denominator", () => { + assert.equal(isProperFraction(3, 0), false); +}); + +test("zero numerator", () =>{ + assert.equal(isProperFraction(0,2), true); +}) \ No newline at end of file diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js index f5092d9b1..3f3f8e66c 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js @@ -9,8 +9,223 @@ test("Valid single-digit card", () => { assert.equal(getCardValue("9♠"), 9); }); +test("Valid double-digit card", () => { + assert.equal(getCardValue("10♠"), 10); +}); + +test("Valid face card", () => { + assert.equal(getCardValue("J♣"), 10); +}); + +test("Valid ace card", () => { + assert.equal(getCardValue("A♦"), 11); +}); + test("Arbitrary non-card string", () => { - assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error"); + const card = "invalid"; + assert.throws( + () => getCardValue("invalid"), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); }); -// TODO: What other invalid card cases can you think of? +test("Three digit card string", () => { + const card = "125♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with leading 0", () => { + const card = "02♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with face followed by number", () => { + const card = "J2♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with number followed by face", () => { + const card = "2J♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with multiple same suit", () => { + const card = "2♦♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with multiple different suit", () => { + const card = "2♣♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with decimal number", () => { + const card = "2.5♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with negative number", () => { + const card = "-2♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with leading whitespace", () => { + const card = " 2♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with trailing whitespace", () => { + const card = "2♦ "; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with middle whitespace", () => { + const card = "2 ♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with invalid face", () => { + const card = "M♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with invalid Ace", () => { + const card = "A10♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with no suit", () => { + const card = "10"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with no rank", () => { + const card = "♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Empty String", () => { + const card = ""; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with invalid number one above max", () => { + const card = "12♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); + +test("Card with invalid number one below min", () => { + const card = "1♦"; + assert.throws( + () => getCardValue(card), + new RegExp( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ), + "Expected clear error" + ); +}); diff --git a/implement-and-rewrite-tests/implement/1-get-angle-type.js b/implement-and-rewrite-tests/implement/1-get-angle-type.js index 8a6fba050..a2e2cb7d2 100644 --- a/implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -17,5 +17,25 @@ // execute the code to ensure all tests pass. export function getAngleType(angle) { - // TODO: Implement this function + if (angle <= 0 || angle >= 360) { + return "Invalid angle"; + } + + if (angle < 90) { + return "Acute angle"; + } + + if (angle === 90) { + return "Right angle"; + } + + if (angle < 180) { + return "Obtuse angle"; + } + + if (angle === 180) { + return "Straight angle"; + } + + return "Reflex angle"; } diff --git a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js index f983800f0..91194267d 100644 --- a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -14,5 +14,5 @@ // execute the code to ensure all tests pass. export function isProperFraction(numerator, denominator) { - // TODO: Implement this function + return Math.abs(numerator) < Math.abs(denominator); } diff --git a/implement-and-rewrite-tests/implement/3-get-card-value.js b/implement-and-rewrite-tests/implement/3-get-card-value.js index 9614ab1ee..ce29ee666 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -24,5 +24,31 @@ // execute the code to ensure all tests pass. export function getCardValue(card) { - // TODO: Implement this function + const validSuits = ["♠", "♥", "♦", "♣"]; + const validNumberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]; + const validFace = ["J", "Q", "K"]; + + const suit = card.at(-1); + const rank = card.slice(0, -1); + + if (!validSuits.includes(suit)) { + throw new Error( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ); + } + if (validNumberRanks.includes(rank)) { + return Number(rank); + } + + if (validFace.includes(rank)) { + return 10; + } + + if (rank === "A") { + return 11; + } + + throw new Error( + `Invalid input. Expected a valid rank followed by a suit, but got "${card}"` + ); } diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index e0ba786ab..e888d4ae5 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -1,18 +1,44 @@ -import { getAngleType } from "../implement/1-get-angle-type.js"; - -// TODO: Write tests in Jest syntax to cover all cases/outcomes, -// including boundary and invalid cases. - -// Case 1: Acute angles -test(`should return "Acute angle" when (0 < angle < 90)`, () => { - // Test various acute angles, including boundary cases - expect(getAngleType(1)).toEqual("Acute angle"); - expect(getAngleType(45)).toEqual("Acute angle"); - expect(getAngleType(89)).toEqual("Acute angle"); -}); - -// Case 2: Right angle -// Case 3: Obtuse angles -// Case 4: Straight angle -// Case 5: Reflex angles -// Case 6: Invalid angles +import { getAngleType } from "../implement/1-get-angle-type.js"; + +// TODO: Write tests in Jest syntax to cover all cases/outcomes, +// including boundary and invalid cases. + +// Case 1: Acute angles +test(`should return "Acute angle" when (0 < angle < 90)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(1)).toEqual("Acute angle"); + expect(getAngleType(45)).toEqual("Acute angle"); + expect(getAngleType(89)).toEqual("Acute angle"); +}); + +// Case 2: Right angle +test(`should return "Right angle" when angle = 90`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + +// Case 3: Obtuse angles +test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + +// Case 4: Straight angle +test(`should return "Straight angle" when angle = 180`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + +// Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + +// Case 6: Invalid angles +test(`Should return "Invalid angle when angle <= 0, angle >= 360`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-45)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(540)).toEqual("Invalid angle"); +}); diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index 5d49e2d8b..061b21c65 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -6,3 +6,80 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +//Case 1: Basic proper fraction +test(`should return true for numerator < denominator`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(3, 5)).toEqual(true); + expect(isProperFraction(7, 9)).toEqual(true); + expect(isProperFraction(12, 26)).toEqual(true); +}); + +//Case 2: Basic improper fraction +test(`should return false for numerator > denominator`, () => { + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(5, 3)).toEqual(false); + expect(isProperFraction(10, 5)).toEqual(false); + expect(isProperFraction(25, 6)).toEqual(false); +}); + +//Case 3: Equal numbers +test(`should return false for numerator = denominator`, () => { + expect(isProperFraction(2, 2)).toEqual(false); + expect(isProperFraction(5, 5)).toEqual(false); + expect(isProperFraction(27, 27)).toEqual(false); + expect(isProperFraction(36, 36)).toEqual(false); +}); + +//Case 4: proper fraction negative numerator +test(`should return true for abs(numerator) < denominator`, () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(-3, 5)).toEqual(true); + expect(isProperFraction(-7, 9)).toEqual(true); + expect(isProperFraction(-12, 26)).toEqual(true); +}); + +//Case 5: improper fraction negative numerator +test(`should return false for abs(numerator) > denominator`, () => { + expect(isProperFraction(-3, 2)).toEqual(false); + expect(isProperFraction(-5, 3)).toEqual(false); + expect(isProperFraction(-10, 5)).toEqual(false); + expect(isProperFraction(-25, 6)).toEqual(false); +}); + +//Case 6: proper fraction negative denominator +test(`should return true for numerator < abs(denominator)`, () => { + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(3, -5)).toEqual(true); + expect(isProperFraction(7, -9)).toEqual(true); + expect(isProperFraction(12, -26)).toEqual(true); +}); + +//Case 7: improper fraction negative denominator +test(`should return false for numerator > abs(denominator)`, () => { + expect(isProperFraction(3, -2)).toEqual(false); + expect(isProperFraction(5, -3)).toEqual(false); + expect(isProperFraction(10, -5)).toEqual(false); + expect(isProperFraction(25, -6)).toEqual(false); +}); + +//Case 8: proper fraction negative numerator and denominator +test(`should return true for abs(numerator) < abs(denominator)`, () => { + expect(isProperFraction(-1, -2)).toEqual(true); + expect(isProperFraction(-3, -5)).toEqual(true); + expect(isProperFraction(-7, -9)).toEqual(true); + expect(isProperFraction(-12, -26)).toEqual(true); +}); + +//Case 9: improper fraction negative numerator and denominator +test(`should return false for abs(numerator) > abs(denominator)`, () => { + expect(isProperFraction(-3, -2)).toEqual(false); + expect(isProperFraction(-5, -3)).toEqual(false); + expect(isProperFraction(-10, -5)).toEqual(false); + expect(isProperFraction(-25, -6)).toEqual(false); +}); + +//Case 10: zero numerator +test(`should return true for numerator = 0`, () => { + expect(isProperFraction(0, 2)).toEqual(true); +}); diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index a6de5a82a..3318b263e 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -9,10 +9,104 @@ test(`Should return 11 when given an ace card`, () => { // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) +test(`Should return 2 when given 2♠`, () => { + expect(getCardValue("2♠")).toEqual(2); +}); +test(`Should return 3 when given 3♠`, () => { + expect(getCardValue("3♠")).toEqual(3); +}); +test(`Should return 4 when given 4♠`, () => { + expect(getCardValue("4♠")).toEqual(4); +}); +test(`Should return 5 when given 5♠`, () => { + expect(getCardValue("5♠")).toEqual(5); +}); +test(`Should return 6 when given 6♠`, () => { + expect(getCardValue("6♠")).toEqual(6); +}); +test(`Should return 7 when given 7♠`, () => { + expect(getCardValue("7♠")).toEqual(7); +}); +test(`Should return 8 when given 8♠`, () => { + expect(getCardValue("8♠")).toEqual(8); +}); +test(`Should return 9 when given 9♠`, () => { + expect(getCardValue("9♠")).toEqual(9); +}); +test(`Should return 10 when given 10♠`, () => { + expect(getCardValue("10♠")).toEqual(10); +}); + // Face Cards (J, Q, K) +test(`Should return 10 when given J♣`, () => { + expect(getCardValue("J♣")).toEqual(10); +}); +test(`Should return 10 when given Q♣`, () => { + expect(getCardValue("Q♣")).toEqual(10); +}); +test(`Should return 10 when given K♣`, () => { + expect(getCardValue("K♣")).toEqual(10); +}); + // Invalid Cards +test(`Should get Invalid when given "Arbitrary non-card string"`, () => { + expect(() => getCardValue("Arbitrary non-card string")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "125♦" three digit card`, () => { + expect(() => getCardValue("125♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "02♦" card with leading 0`, () => { + expect(() => getCardValue("02♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "J2♦" card with face followed by number`, () => { + expect(() => getCardValue("J2♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "2J♦" card with number followed by a face`, () => { + expect(() => getCardValue("2J♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "2♦♦" card with two of same suits`, () => { + expect(() => getCardValue("2♦♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "2♣♦" card with two different suits`, () => { + expect(() => getCardValue("2♣♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "2.5♦" card with decimal number`, () => { + expect(() => getCardValue("2.5♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "-2♦" card with negative number`, () => { + expect(() => getCardValue("-2♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given " 2♦" card with leading whitespace`, () => { + expect(() => getCardValue(" 2♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "2♦ " card with trailing whitespace`, () => { + expect(() => getCardValue("2♦ ")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "2 ♦" card with trailing whitespace`, () => { + expect(() => getCardValue("2 ♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "M♦" card with invalid face`, () => { + expect(() => getCardValue("M♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "A10♦" card with invalid Ace`, () => { + expect(() => getCardValue("A10♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "10" card with no suit`, () => { + expect(() => getCardValue("10")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "♦" card with no rank`, () => { + expect(() => getCardValue("♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "" empty string`, () => { + expect(() => getCardValue("")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "12♦" one above max number`, () => { + expect(() => getCardValue("12♦")).toThrow(/Invalid/); +}); +test(`Should get Invalid when given "1♦" one below min number`, () => { + expect(() => getCardValue("1♦")).toThrow(/Invalid/); +}); // To learn how to test whether a function throws an error as expected in Jest, // please refer to the Jest documentation: // https://jestjs.io/docs/expect#tothrowerror -