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..4d01ab105 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,58 @@ test("Classifies right angles", () => { const right = getAngleType(90); assert.equal(right, "Right angle"); }); + +test("Classifies obtuse angles", () => { + const right = getAngleType(91); + assert.equal(right, "Obtuse angle"); +}); + +test("Classifies obtuse angles", () => { + const right = getAngleType(179); + assert.equal(right, "Obtuse angle"); +}); + +test("Classifies Straight angles", () => { + const right = getAngleType(180); + assert.equal(right, "Straight angle"); +}); + +test("Classifies Reflex angles", () => { + const right = getAngleType(181); + assert.equal(right, "Reflex angle"); +}); + +test("Classifies Reflex angles", () => { + const right = getAngleType(359); + assert.equal(right, "Reflex angle"); +}); + +test("Classifies Reflex angles", () => { + const right = getAngleType(360); + assert.equal(right, "Reflex angle"); +}); + +test("Classifies Invalid angles", () => { + const right = getAngleType(361); + assert.equal(right, "Invalid angle"); +}); + +test("Classifies Invalid angles", () => { + const right = getAngleType(361); + assert.equal(right, "Invalid angle"); +}); + +test("Classifies Invalid angles", () => { + const right = getAngleType(-1); + assert.equal(right, "Invalid angle"); +}); + + + + +// - "Acute angle" for angles greater than 0° and less than 90° +// - "Right angle" for exactly 90° +// - "Obtuse angle" for angles greater than 90° and less than 180° +// - "Straight angle" for exactly 180° +// - "Reflex angle" for angles greater than 180° and less than 360° +// - "Invalid angle" for angles outside the valid range. 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..077eebe8f 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,35 @@ test("Basic proper fraction", () => { // Example: 1/2 is a proper fraction assert.equal(isProperFraction(1, 2), true); }); + +test("Equal numerator and denominator is not a proper fraction", () => { + assert.equal(isProperFraction(2, 2), false); +}); + +test("Improper fraction is false", () => { + assert.equal(isProperFraction(3, 2), false); +}); + +test("Zero numerator with positive denominator is a proper fraction", () => { + assert.equal(isProperFraction(0, 2), true); +}); + +test("Non-numeric numerator throws TypeError", () => { + assert.throws(() => isProperFraction("1", 2), TypeError); +}); + +test("Non-numeric denominator throws TypeError", () => { + assert.throws(() => isProperFraction(1, "2"), TypeError); +}); + +test("NaN numerator throws TypeError", () => { + assert.throws(() => isProperFraction(Number.NaN, 2), TypeError); +}); + +test("Infinity denominator throws TypeError", () => { + assert.throws(() => isProperFraction(1, Number.POSITIVE_INFINITY), TypeError); +}); + +test("Denominator is 0 throws TypeError", () => { + assert.throws(() => isProperFraction(1, 0), TypeError); +}); 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..34cae540f 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,40 @@ test("Valid single-digit card", () => { assert.equal(getCardValue("9♠"), 9); }); -test("Arbitrary non-card string", () => { - assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error"); +test("Invalid Rank", () => { + assert.throws( + () => getCardValue("1♥"), + /Invalid Rank/, + "Expected clear error" + ); }); +test("Invalid Suit", () => { + assert.throws( + () => getCardValue("9x"), + /Invalid Suit/, + "Expected clear error" + ); +}); + +test("Non-string input", () => { + assert.throws( + () => getCardValue(9), + /Input not a string/, + "Expected clear error" + ); +}); // TODO: What other invalid card cases can you think of? +//Invalid Rank or +//Invalid String +////Out of range input +//Input: (Rank) "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" and (Suit), also invalid input +//Input type: String +//Process Turn input into Num : "A" to 11 ; ("J", "Q", "K") to 10 +//Output : a valid Num , or invalid throw error +//const rank +//const suit +//Edge cases 0: not string +//Edge cases 1: ANy first letter which is not a Rank; +//Edge cases 2: Any second character which is not a Suit +//Edge cases 3: Both Rank plus Suit input invalid; 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..a43f5fb6a 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,33 @@ 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 `Straight angle`; + } + if (angle < 180) { + return `Obtuse angle`; + } + return `Reflex angle`; } + +//Pseudo codes +//First define type: Num (whole number) +//Then range: 0 - 360 +//Cases: +//1. 0-89 : Actute angle +//2. 90 : Right angle +//3. 91-179: Obtuse angle +//4. 180: Straight angle +//5. 181-359 : Reflex angle +//6. 360 and beyond Invalid angle +//Key word to use : If? 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..ce10d7168 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,28 @@ export function isProperFraction(numerator, denominator) { // TODO: Implement this function + if (typeof numerator !== "number" || typeof denominator !== "number") { + throw new TypeError("Both arguments must be numbers"); + } + if (!Number.isFinite(numerator) || !Number.isFinite(denominator)) { + throw new TypeError("Both arguments must be numbers"); + } + + if (denominator === 0) { + throw new TypeError("Denominator cannot be zero"); + } + + if (Math.abs(numerator) < Math.abs(denominator)) { + return true; + } + + return false; } + +//Cases show cases +//Proper function ; improper function +//Input type: Num +//Process : number to logic test +//Output type: boolean or error +//Edge case : negative number +//Edge case: decimal 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..255933aab 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,55 @@ export function getCardValue(card) { // TODO: Implement this function + if (typeof card !== "string") { + throw new TypeError("Input not a string"); + } + const Rank = card.slice(0, -1); + const Suit = card.slice(-1); + const validRanks = [ + "A", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "J", + "Q", + "K", + ]; + const validSuits = ["♠", "♥", "♦", "♣"]; + + if (!validRanks.includes(Rank)) { + throw new Error("Invalid Rank"); + } + + if (!validSuits.includes(Suit)) { + throw new Error("Invalid Suit"); + } + + if (Rank === "A") { + return 11; + } + + if (["J", "Q", "K"].includes(Rank)) { + return 10; + } + + return Number(Rank); } + +//Case showcases +//Input: (Rank) "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" and (Suit), also invalid input +//Input type: String +//Process Turn input into Num : "A" to 11 ; ("J", "Q", "K") to 10 +//Output : a valid Num , or invalid throw error +//const rank +//const suit +//Edge cases 0: not string +//Edge cases 1: ANy first letter which is not a Rank; +//Edge cases 2: Not a Rank plus Suit input; +//Edge cases 3: Suit mistyped ; or second character is not a suit 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..c90e0fc74 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,61 @@ 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 +test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { + // Test various obtuse angles, including boundary cases + 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)`, () => { + // Test various reflex angles, including boundary cases + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(225)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); // Case 6: Invalid angles +test(`should return "Invalid angle" when (angle < 0 or angle > 360)`, () => { + // Test various invalid angles, including boundary cases + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(361)).toEqual("Invalid angle"); + expect(getAngleType(-0.5)).toEqual("Invalid angle"); +}); + +//test("Classifies right angles", () => { +// const right = getAngleType(90); + +//test("Classifies obtuse angles", () => { +// const right = getAngleType(91); + +//test("Classifies obtuse angles", () => { +// const right = getAngleType(179); + +//test("Classifies Straight angles", () => { +// const right = getAngleType(180); + +//test("Classifies Reflex angles", () => { +// const right = getAngleType(181); + +//test("Classifies Reflex angles", () => { +// const right = getAngleType(359); + +//test("Classifies Reflex angles", () => { +// const right = getAngleType(360); + +//test("Classifies Invalid angles", () => { +// const right = getAngleType(361); + +//test("Classifies Invalid angles", () => { +// const right = getAngleType(361); + +//test("Classifies Invalid angles", () => { +// const right = getAngleType(-1); 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..ef0d731a5 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 @@ -3,6 +3,92 @@ 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: numerator is zero -test(`should return false when denominator is zero`, () => { - expect(isProperFraction(1, 0)).toEqual(false); +test('should throw error when denominator is zero', () => { + expect(() => isProperFraction(1, 0)).toThrow(Error); }); +// Case1: numerator is zero +test('should return true when numerator is zero', () => { + expect(isProperFraction(0, 2)).toEqual(true); +}); +// Case2: denominator is Infinity +test('should throw TypeError when denominator is infinity', () => { + expect(() => isProperFraction(1, Infinity)).toThrow(TypeError); +}); +// Case3: numerator is Infinity +test('should throw TypeError when numerator is Infinity', () => { + expect(() => isProperFraction(Number.POSITIVE_INFINITY, 2)).toThrow(TypeError); +}); +// Case4: denominator is not a number +test('should throw TypeError when denominator is not a number', () => { + expect(() => isProperFraction(1, "2")).toThrow(TypeError); + expect(() => isProperFraction(2, "3")).toThrow(TypeError); + expect(() => isProperFraction(3, "1")).toThrow(TypeError); +}); +// Case5: numerator is not a number +test('should throw TypeError when numerator is not a number', () => { + expect(() => isProperFraction("1", 2)).toThrow(TypeError); + expect(() => isProperFraction("2", 3)).toThrow(TypeError); + expect(() => isProperFraction("3", 2)).toThrow(TypeError); +}); +// Case6: improper fraction with positive numerator and denominator +test('should return false for improper fraction', () => { + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(5, 3)).toEqual(false); + expect(isProperFraction(10, 5)).toEqual(false); +}); +// Case7: equal numerator and denominator +test('should return false for equal numerator and denominator', () => { + expect(isProperFraction(2, 2)).toEqual(false); + expect(isProperFraction(5, 5)).toEqual(false); + expect(isProperFraction(10, 10)).toEqual(false); +}); +// Case8: equal numerator and negative denominator +test('should return false for equal numerator and negative denominator', () => { + expect(isProperFraction(2, -2)).toEqual(false); + expect(isProperFraction(5, -5)).toEqual(false); + expect(isProperFraction(10, -10)).toEqual(false); +}); +// Case9: negative improper fraction with positive denominator +test('should return false for negative improper fraction', () => { + expect(isProperFraction(-3, 2)).toEqual(false); + expect(isProperFraction(-5, 3)).toEqual(false); + expect(isProperFraction(-10, 5)).toEqual(false); +}); +// Case10: negative improper fraction with negative numerator +test('should return true for negative proper fraction', () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(-2, 3)).toEqual(true); + expect(isProperFraction(-3, 4)).toEqual(true); +}); +// Case11: proper fraction with negative denominator +test('should return true for proper fraction with negative denominator', () => { + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(2, -3)).toEqual(true); + expect(isProperFraction(3, -4)).toEqual(true); +}); +// Case12: improper fraction with negative denominator +test('should return false for improper fraction with negative denominator', () => { + expect(isProperFraction(3, -2)).toEqual(false); + expect(isProperFraction(5, -3)).toEqual(false); + expect(isProperFraction(10, -5)).toEqual(false); +}); +// Case13: proper fraction with both negative numerator and denominator +test('should return true for proper fraction with both negative numerator and denominator', () => { + expect(isProperFraction(-1, -2)).toEqual(true); + expect(isProperFraction(-2, -3)).toEqual(true); + expect(isProperFraction(-3, -4)).toEqual(true); +}); +// Case14: improper fraction with both negative numerator and denominator +test('should return false for improper fraction with both negative numerator and denominator', () => { + expect(isProperFraction(-3, -2)).toEqual(false); + expect(isProperFraction(-5, -3)).toEqual(false); + expect(isProperFraction(-10, -5)).toEqual(false); +}); + +// Case15: proper fraction with positive numerator and denominator +test('should return true for proper fraction', () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(2, 3)).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..06c8d1531 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 @@ -7,6 +7,59 @@ test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); +// Case 2: Face cards (J, Q, K) +test(`Should return 10 when given a face card`, () => { + expect(getCardValue("J♣")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♥")).toEqual(10); +}); + +// Case 3: Number cards (2-10) +test(`Should return the correct value for number cards`, () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("3♣")).toEqual(3); + expect(getCardValue("4♦")).toEqual(4); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("6♠")).toEqual(6); + expect(getCardValue("7♣")).toEqual(7); + expect(getCardValue("8♦")).toEqual(8); + expect(getCardValue("9♥")).toEqual(9); + expect(getCardValue("10♠")).toEqual(10); +}); + +// Case 4: Invalid ranks +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("11♠")).toThrow(Error); + expect(() => getCardValue("1♣")).toThrow(Error); + expect(() => getCardValue("H♠")).toThrow(Error); +}); + +// Case 5: Invalid suits +test(`Should throw an error when given an invalid suit`, () => { + expect(() => getCardValue("A♤")).toThrow(Error); + expect(() => getCardValue("10X")).toThrow(Error); + expect(() => getCardValue("KZ")).toThrow(Error); +}); + +// Case 6: Invalid input types +test(`Should throw an error when given a non-string input`, () => { + expect(() => getCardValue(10)).toThrow(Error); + expect(() => getCardValue(null)).toThrow(Error); + expect(() => getCardValue(undefined)).toThrow(Error); + expect(() => getCardValue({})).toThrow(Error); + expect(() => getCardValue([])).toThrow(Error); +}); + +// Case 7: Edge cases +test(`Should throw an error for edge cases`, () => { + expect(() => getCardValue("")).toThrow(Error); + expect(() => getCardValue("A")).toThrow(Error); + expect(() => getCardValue("♠")).toThrow(Error); + expect(() => getCardValue("A♠♣")).toThrow(Error); + expect(() => getCardValue("10♠♣")).toThrow(Error); +}); + + // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) // Face Cards (J, Q, K)