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..136c59ff9 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,40 @@ test("Classifies right angles", () => { const right = getAngleType(90); assert.equal(right, "Right angle"); }); +// "Acute angle" for angles greater than 0° and less than 90° +test("Classifies Acute angles", () => { + const right = getAngleType(45); + assert.equal(right, "Acute angle"); +}); +// - "Obtuse angle" for angles greater than 90° and less than 180° +test("Classifies Obtuse angles", () => { + const right = getAngleType(135); + assert.equal(right, "Obtuse angle"); +}); +// - "Straight angle" for exactly 180° +test("Classifies Straight angles", () => { + const right = getAngleType(180); + assert.equal(right, "Straight angle"); +}); +// - "Reflex angle" for angles greater than 180° and less than 360° +test("Classifies Reflex angles", () => { + const right = getAngleType(225); + assert.equal(right, "Reflex angle"); +}); +// - "Invalid angle" for angles outside the valid range. +test("Classifies Invalid angles", () => { + const right = getAngleType(361); + assert.equal(right, "Invalid angle"); + const right = getAngleType(-361); + assert.equal(right, "Invalid angle"); +}); +// - "Invalid angle" for angles that are at 0° +test("Classifies Invalid angles for 0", () => { + const right = getAngleType(0); + assert.equal(right, "Invalid angle"); +}); +// - "Invalid angle" for angles that are at 360° +test("Classifies Invalid angles for 360", () => { + const right = getAngleType(360); + assert.equal(right, "Invalid 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..2677a5c15 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 @@ -6,7 +6,71 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; // TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? -test("Basic proper fraction", () => { +test("Basic proper fraction: Numerator < Denominator", () => { // Example: 1/2 is a proper fraction assert.equal(isProperFraction(1, 2), true); }); + +test("Proper fraction with negative number: -Numerator < Denominator", () => { + assert.equal(isProperFraction(-1, 2), true); +}); + +test("Proper fraction with negative number: Numerator < -Denominator", () => { + assert.equal(isProperFraction(1, -2), true); +}); + +test("Proper fraction with negative numbers: -Numerator < -Denominator", () => { + assert.equal(isProperFraction(-1, -2), true); +}); + +test("Improper fraction: Numerator = Denominator", () => { + assert.equal(isProperFraction(2, 2), false); +}); + +test("Improper fraction with same value and negative number: -Numerator > Denominator", () => { + assert.equal(isProperFraction(-2, 2), false); +}); + +test("Improper fraction with same value and negative number: Numerator > -Denominator", () => { + assert.equal(isProperFraction(2, -2), false); +}); + +test("Improper fraction same value and both negative: Numerator = Denominator", () => { + assert.equal(isProperFraction(-2, -2), false); +}); + +test("improper fraction: Numerator > Denominator", () => { + assert.equal(isProperFraction(2, 1), false); +}); + +test("Improper fraction with negative number: -Numerator > Denominator", () => { + assert.equal(isProperFraction(-2, 1), false); +}); + +test("Improper fraction with negative number: Numerator > -Denominator", () => { + assert.equal(isProperFraction(2, -1), false); +}); + +test("Improper fraction with negative numbers: -Numerator > -Denominator", () => { + assert.equal(isProperFraction(-2, -1), false); +}); + +test("Improper fraction: Denominator = 0", () => { + assert.equal(isProperFraction(1, 0), false); +}); + +test("Proper fraction with negative Numerator and Zero: -Numerator < 0", () => { + assert.equal(isProperFraction(-1, 0), false); +}); + +test("Improper fraction: Nominator = 0", () => { + assert.equal(isProperFraction(0, 2), false); +}); + +test("Proper fraction with Zero and negative Denominator: 0 < -Denominator", () => { + assert.equal(isProperFraction(0, -2), false); +}); + +test("Proper fraction with both being Zero", () => { + assert.equal(isProperFraction(0, 0), false); +}); 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..176cc8e81 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 @@ -10,7 +10,79 @@ test("Valid single-digit card", () => { }); test("Arbitrary non-card string", () => { - assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error"); + assert.throws( + () => getCardValue("invalid"), + /Expected a number followed by a suit, but got "invalid"/, + "Expected clear error" + ); }); // TODO: What other invalid card cases can you think of? +//Incorrect suit as symbol +test("Incorrect suit", () => { + assert.throws( + () => getCardValue("2☺"), + /Incorrect suit given/, + "Incorrect suit" + ); +}); + +//Incorrect suit as letter +test("Incorrect suit", () => { + assert.throws( + () => getCardValue("7l"), + /Incorrect suit given/, + "Incorrect suit" + ); +}); + +//Incorrect card value to high +test("Incorrect card number: High value", () => { + assert.throws( + () => getCardValue("20♦"), + /Incorrect card number given/, + "Incorrect card number given" + ); +}); +//Incorrect card value when negative +test("Incorrect card number: Negative Number", () => { + assert.throws( + () => getCardValue("-2♦"), + /Incorrect card number given/, + "Incorrect card number given" + ); +}); +//Incorrect card value when symbol +test("Incorrect card number: Symbol", () => { + assert.throws( + () => getCardValue("🐱♦"), + /Incorrect card number given/, + "Incorrect card number given" + ); +}); +//Incorrect card value when letter +test("Incorrect card number: Letter", () => { + assert.throws( + () => getCardValue("H♦"), + /Incorrect card number given/, + "Incorrect card number given" + ); +}); + +//Incorrect card value when lower case Royal +test("Incorrect card number: lower case Royal", () => { + assert.throws( + () => getCardValue("k♦"), + /Incorrect card number given/, + "Incorrect card number given" + ); +}); + +//Incorrect card value when lower case Ace +test("Incorrect card number: lower case Ace", () => { + assert.throws( + () => getCardValue("a♦"), + /Incorrect card number given/, + "Incorrect card number given" + ); +}); 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..94e4e0f0e 100644 --- a/implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -18,4 +18,21 @@ export function getAngleType(angle) { // TODO: Implement this function + if (angle === 90) { + return "Right angle"; + } else if (angle > 0 && angle < 90) { + return "Acute angle"; + } else if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } else if (angle === 180) { + return "Straight angle"; + } else if (angle > 180 && angle < 360) { + return "Reflex angle"; + } else if (angle === 0) { + return "Invalid angle"; + } else if (angle === 360) { + return "Invalid angle"; + } else (angle < 0 || angle > 360) { + return "Invalid 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..95a54d3be 100644 --- a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -15,4 +15,12 @@ export function isProperFraction(numerator, denominator) { // TODO: Implement this function + if (denominator === 0) { + return false; + } else if (numerator === 0) { + return false; + } else if (Math.abs(numerator) >= Math.abs(denominator)) { + return false; + } + return true; } 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..011796558 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -25,4 +25,26 @@ export function getCardValue(card) { // TODO: Implement this function + if (card.length >= 4) { + throw new Error(`Expected a number followed by a suit, but got "invalid"`); + } else if (card === "") { + throw new Error("Input needed"); + } + + let suit = card.slice(-1); + let cardNumber = card.slice(0, -1); + let value = Number(cardNumber); + if (!(suit === "♠" || suit === "♥" || suit === "♦" || suit === "♣")) { + throw new Error("Incorrect suit given"); + } else if (cardNumber === "J" || cardNumber === "Q" || cardNumber === "K") { + return 10; + } else if (cardNumber === "A") { + return 11; + } else if (Number.isNaN(value)) { + throw new Error("Incorrect card number given"); + } else if (value >= 2 && value <= 10) { + return value; + } else if (value < 2 || value > 10) { + throw new Error("Incorrect card number given"); + } } 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..409b95982 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 @@ -12,7 +12,36 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when (angle = 90)`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); // Case 3: Obtuse angles -// Case 4: Straight angle +test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(167)).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 "Right angle" when (180 < angle < 360)`, () => { + expect(getAngleType(186)).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 (0 > angle > 360)`, () => { + expect(getAngleType(361)).toEqual("Invalid angle"); + expect(getAngleType(396)).toEqual("Invalid angle"); + expect(getAngleType(-57)).toEqual("Invalid angle"); +}); +// Case 7: Zero angles +test(`should return "Invalid angle" when (angle = 0)`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); +}); +// Case 8: Full angles +test(`should return "Invalid angle" when (angle = 360)`, () => { + expect(getAngleType(360)).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..f28aeb5cc 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 @@ -2,7 +2,119 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. +// Special case: Basic proper fraction: Numerator < Denominator +test(`should return true when: Numerator < Denominator`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(3, 6)).toEqual(true); + expect(isProperFraction(45, 60)).toEqual(true); +}); + +// Special case: Basic proper negative fraction: -Numerator < Denominator +test(`should return true when: Numerator < Denominator`, () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(-3, 6)).toEqual(true); + expect(isProperFraction(-45, 60)).toEqual(true); +}); + +// Special case: Basic proper negative fraction: Numerator < -Denominator +test(`should return true when: Numerator < Denominator`, () => { + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(3, -6)).toEqual(true); + expect(isProperFraction(45, -60)).toEqual(true); +}); + +// Special case: Proper fraction with both negative number: -Numerator < -Denominator +test(`should return true when: -Numerator < -Denominator`, () => { + expect(isProperFraction(-1, -2)).toEqual(true); + expect(isProperFraction(-6, -8)).toEqual(true); + expect(isProperFraction(-10, -20)).toEqual(true); +}); + +// Special case: Improper fraction when same values: Numerator = Denominator +test(`should return false when: Numerator = Denominator`, () => { + expect(isProperFraction(1, 1)).toEqual(false); + expect(isProperFraction(12, 12)).toEqual(false); + expect(isProperFraction(56, 56)).toEqual(false); +}); + +// Special case: Improper negative fraction when same values: -Numerator = -Denominator +test(`should return false when: Numerator = Denominator`, () => { + expect(isProperFraction(-1, -1)).toEqual(false); + expect(isProperFraction(-12, -12)).toEqual(false); + expect(isProperFraction(-56, -56)).toEqual(false); +}); + +// Special case: Improper negative fraction when same values: -Numerator = Denominator +test(`should return false when: Numerator = Denominator`, () => { + expect(isProperFraction(-1, 1)).toEqual(false); + expect(isProperFraction(-12, 12)).toEqual(false); + expect(isProperFraction(-56, 56)).toEqual(false); +}); + +// Special case: Improper negative fraction when same values: Numerator = -Denominator +test(`should return false when: Numerator = Denominator`, () => { + expect(isProperFraction(1, -1)).toEqual(false); + expect(isProperFraction(12, -12)).toEqual(false); + expect(isProperFraction(56, -56)).toEqual(false); +}); + +// Special case: Improper fraction larger Numerator: Numerator > Denominator +test(`should return false when: Numerator > Denominator`, () => { + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(7, 4)).toEqual(false); + expect(isProperFraction(30, 20)).toEqual(false); +}); + +// Special case: Improper fraction with larger Numerator and negative Denominator: Numerator > -Denominator +test(`should return false when: -Numerator > Denominator`, () => { + expect(isProperFraction(2, -1)).toEqual(false); + expect(isProperFraction(5, -4)).toEqual(false); + expect(isProperFraction(20, -10)).toEqual(false); +}); + +// Special case: Improper fraction with negative Numerator and larger Denominator Numerator > -Denominator +test(`should return false when: -Numerator > Denominator`, () => { + expect(isProperFraction(-2, 1)).toEqual(false); + expect(isProperFraction(-5, 4)).toEqual(false); + expect(isProperFraction(-20, 10)).toEqual(false); +}); + +// Special case: Improper fraction with larger Numerator and both negative: -Numerator > -Denominator +test(`should return false when: -Numerator > Denominator`, () => { + expect(isProperFraction(-2, -1)).toEqual(false); + expect(isProperFraction(-5, -4)).toEqual(false); + expect(isProperFraction(-20, -10)).toEqual(false); +}); + // Special case: numerator is zero -test(`should return false when denominator is zero`, () => { +test(`should return false when numerator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(5, 0)).toEqual(false); + expect(isProperFraction(20, 0)).toEqual(false); +}); + +// Special case: Improper fraction with negative Numerator and 0: -Numerator > 0 +test(`should return false when: -Numerator > Denominator`, () => { + expect(isProperFraction(-2, 0)).toEqual(false); + expect(isProperFraction(-5, 0)).toEqual(false); + expect(isProperFraction(-20, 0)).toEqual(false); +}); + +// Special case: denominator is zero +test(`should return false when denominator is zero`, () => { + expect(isProperFraction(0, 1)).toEqual(false); + expect(isProperFraction(0, 4)).toEqual(false); + expect(isProperFraction(0, 10)).toEqual(false); +}); + +// Special case: Improper fraction with 0 and negative Denominator: 0 > -Denominator +test(`should return false when: -Numerator > Denominator`, () => { + expect(isProperFraction(0, -1)).toEqual(false); + expect(isProperFraction(0, -4)).toEqual(false); + expect(isProperFraction(0, -10)).toEqual(false); +}); + +// Special case: both are zero +test(`should return false when denominator is zero`, () => { + expect(isProperFraction(0, 0)).toEqual(false); }); 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..55492bb4b 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,79 @@ 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 number value when given an numbered card`, () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("7♥")).toEqual(7); + expect(getCardValue("9♦")).toEqual(9); + expect(getCardValue("5♠")).toEqual(5); +}); + // Face Cards (J, Q, K) +test(`Should return 10 when given a J, Q, K`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♥")).toEqual(10); + expect(getCardValue("K♦")).toEqual(10); + expect(getCardValue("J♠")).toEqual(10); +}); + // Invalid Cards +test(`Should throw error 'expected a number followed by a suit, but got "invalid"' with invalid card`, () => { + expect(() => getCardValue("120♥")).toThrow( + new Error('expected a number followed by a suit, but got "invalid"') + ); + expect(() => getCardValue("203♦")).toThrow( + new Error('expected a number followed by a suit, but got "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 +test(`Should throw error "Incorrect suit given" when given wrong symbol"`, () => { + expect(() => getCardValue("2☺")).toThrow(new Error("Incorrect suit given")); +}); + +test(`Should throw error "Incorrect suit given" when given wrong letter"`, () => { + expect(() => getCardValue("2K")).toThrow(new Error("Incorrect suit given")); +}); + +test(`Should throw error "Incorrect card number given" when given wrong 2 digit number"`, () => { + expect(() => getCardValue("25♠")).toThrow( + new Error("Incorrect card number given") + ); +}); + +test(`Should throw error "Incorrect card number given" when given negative number"`, () => { + expect(() => getCardValue("-2♠")).toThrow( + new Error("Incorrect card number given") + ); +}); + +test(`Should throw error "Incorrect card number given" when given a lower case Royal"`, () => { + expect(() => getCardValue("k♠")).toThrow( + new Error("Incorrect card number given") + ); +}); + +test(`Should throw error "Incorrect card number given" when given a lower case Ace"`, () => { + expect(() => getCardValue("a♠")).toThrow( + new Error("Incorrect card number given") + ); +}); + +test(`Should throw error "Incorrect card number given" when given a letter"`, () => { + expect(() => getCardValue("I♠")).toThrow( + new Error("Incorrect card number given") + ); +}); + +test(`Should throw error "Incorrect card number given" when given symbol"`, () => { + expect(() => getCardValue("🐱♠")).toThrow( + new Error("Incorrect card number given") + ); +}); + +test(`Should throw error "Input needed" if no imput given`, () => { + expect(() => getCardValue("")).toThrow(new Error("Input needed")); +});