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..7e223870a 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 @@ -3,10 +3,48 @@ import test from "node:test"; import { getAngleType } from "../implement/1-get-angle-type.js"; -// TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles test("Classifies right angles", () => { const right = getAngleType(90); assert.equal(right, "Right angle"); }); + +// Acute Angles (0° < angle < 90°) +test("Classifies acute angles", () => { + assert.equal(getAngleType(1), "Acute angle"); + assert.equal(getAngleType(45), "Acute angle"); + assert.equal(getAngleType(89.9), "Acute angle"); +}); + +// Right Angles (angle === 90°) +test("Classifies right angles", () => { + assert.equal(getAngleType(90), "Right angle"); +}); + +// Obtuse Angles (90° < angle < 180°) +test("Classifies obtuse angles", () => { + assert.equal(getAngleType(90.1), "Obtuse angle"); + assert.equal(getAngleType(135), "Obtuse angle"); + assert.equal(getAngleType(179.9), "Obtuse angle"); +}); + +// Straight Angles (angle === 180°) +test("Classifies straight angles", () => { + assert.equal(getAngleType(180), "Straight angle"); +}); + +// Reflex Angles (180° < angle < 360°) +test("Classifies reflex angles", () => { + assert.equal(getAngleType(180.1), "Reflex angle"); + assert.equal(getAngleType(270), "Reflex angle"); + assert.equal(getAngleType(359.9), "Reflex angle"); +}); + +// Invalid & Boundary Cases (<= 0° or >= 360°) +test("Classifies invalid angles and exact boundaries", () => { + assert.equal(getAngleType(0), "Invalid angle"); + assert.equal(getAngleType(-45), "Invalid angle"); + assert.equal(getAngleType(360), "Invalid angle"); + assert.equal(getAngleType(400), "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..d821ad498 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,33 @@ 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", () => { // Example: 1/2 is a proper fraction assert.equal(isProperFraction(1, 2), true); + assert.equal(isProperFraction(3, 4), true); +}); + +test("Improper fraction where numerator is larger than denominator", () => { + assert.equal(isProperFraction(5, 2), false); + assert.equal(isProperFraction(4, 3), false); +}); + +test("Equal numerator and denominator (equals 1)", () => { + assert.equal(isProperFraction(3, 3), false); }); + +test("Denominator is zero", () => { + assert.equal(isProperFraction(1, 0), false); + assert.equal(isProperFraction(0, 0), false); +}); + +test("Zero numerator with non-zero denominator", () => { + assert.equal(isProperFraction(0, 5), true); +}); + +test("Negative numbers", () => { + assert.equal(isProperFraction(-1, 2), true); + assert.equal(isProperFraction(1, -2), true); + assert.equal(isProperFraction(-3, -2), false); +}); \ 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..f590aac82 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 @@ -5,12 +5,73 @@ import { getCardValue } from "../implement/3-get-card-value.js"; // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -test("Valid single-digit card", () => { - assert.equal(getCardValue("9♠"), 9); +test("Valid single-digit number cards return correct values", () => { + assert.equal(getCardValue("2♠"), 2); + assert.equal(getCardValue("5♥"), 5); + 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("Valid double-digit card (10) returns 10", () => { + assert.equal(getCardValue("10♥"), 10); + assert.equal(getCardValue("10♣"), 10); }); -// TODO: What other invalid card cases can you think of? +test("Ace cards return 11", () => { + assert.equal(getCardValue("A♠"), 11); + assert.equal(getCardValue("A♦"), 11); +}); + +test("Face cards (J, Q, K) return 10", () => { + assert.equal(getCardValue("J♣"), 10); + assert.equal(getCardValue("Q♦"), 10); + assert.equal(getCardValue("K♠"), 10); +}); + +// Invalid Input Cases +test("Throws error for arbitrary non-card string", () => { + assert.throws( + () => getCardValue("invalid"), + /Expected a card rank followed by a suit/ + ); +}); + +test("Throws error for invalid ranks or suit-like characters", () => { + assert.throws( + () => getCardValue("1♠"), + /Expected a card rank followed by a suit/ + ); + assert.throws( + () => getCardValue("11♥"), + /Expected a card rank followed by a suit/ + ); + assert.throws( + () => getCardValue("Z♠"), + /Expected a card rank followed by a suit/ + ); +}); + +test("Throws error for invalid or missing suit", () => { + assert.throws( + () => getCardValue("10"), + /Expected a card rank followed by a suit/ + ); + assert.throws( + () => getCardValue("AX"), + /Expected a card rank followed by a suit/ + ); +}); + +test("Throws error for reversed order (suit before rank)", () => { + assert.throws( + () => getCardValue("♠A"), + /Expected a card rank followed by a suit/ + ); +}); + +test("Throws error for empty string or non-string input", () => { + assert.throws( + () => getCardValue(""), + /Expected a card rank followed by a suit/ + ); + assert.throws(() => getCardValue(10), /Expected a string/); +}); 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..00df61114 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,21 @@ // execute the code to ensure all tests pass. export function getAngleType(angle) { - // TODO: Implement this function + if (angle > 0 && angle < 90) { + return "Acute angle"; + } + if (angle === 90) { + return "Right angle"; + } + if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } + if (angle === 180) { + return "Straight angle"; + } + if (angle > 180 && angle < 360) { + return "Reflex angle"; + } + + 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..376a2cbc0 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,9 @@ // execute the code to ensure all tests pass. export function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (denominator === 0) { + return false; + } + + 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..b929b5bb3 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,28 @@ // execute the code to ensure all tests pass. export function getCardValue(card) { - // TODO: Implement this function + if (typeof card !== "string") { + throw new Error(`Expected a string, but got ${typeof card}`); + } + + const cardRegex = /^(10|[2-9]|[AJQK])([♠♥♦♣])$/; + const match = card.match(cardRegex); + + if (!match) { + throw new Error( + `Expected a card rank followed by a suit, but got "${card}"` + ); + } + + const rank = match[1]; + + if (rank === "A") { + return 11; + } + + if (["J", "Q", "K"].includes(rank)) { + return 10; + } + + return Number(rank); } 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..32dc72a58 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 @@ -3,16 +3,43 @@ 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"); + expect(getAngleType(89.9)).toEqual("Acute angle"); }); // Case 2: Right angle +test(`should return "Right angle" when angle is exactly 90`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { + expect(getAngleType(90.1)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179.9)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when angle is exactly 180`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(180.1)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359.9)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when angle is <= 0 or >= 360`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-45)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(400)).toEqual("Invalid angle"); +}); \ No newline at end of file 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..3ef0e69f5 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,38 @@ 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. +// Positive numbers +test("should return true when positive numerator is less than positive denominator", () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(3, 4)).toEqual(true); +}); + +test("should return false when positive numerator is equal to or greater than positive denominator", () => { + expect(isProperFraction(3, 3)).toEqual(false); + expect(isProperFraction(5, 2)).toEqual(false); +}); + // Special case: numerator is zero -test(`should return false when denominator is zero`, () => { + +test("should return false when denominator is zero", () => { expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(0, 0)).toEqual(false); +}); + +test("should return true when numerator is zero and denominator is non-zero", () => { + expect(isProperFraction(0, 5)).toEqual(true); + expect(isProperFraction(0, -5)).toEqual(true); +}); + +// Negative numbers +test("should return true when absolute value of numerator is less than denominator", () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(-1, -2)).toEqual(true); +}); + +test("should return false when absolute value of numerator is equal to or greater than denominator", () => { + expect(isProperFraction(-5, 2)).toEqual(false); + expect(isProperFraction(5, -2)).toEqual(false); + expect(isProperFraction(-3, -3)).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..74a8b581d 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 @@ -2,17 +2,57 @@ import { getCardValue } from "../implement/3-get-card-value.js"; // TODO: Write tests in Jest syntax to cover all possible outcomes. + // Case 1: Ace (A) -test(`Should return 11 when given an ace card`, () => { +test("Should return 11 when given an ace card", () => { expect(getCardValue("A♠")).toEqual(11); + expect(getCardValue("A♦")).toEqual(11); }); -// Suggestion: Group the remaining test data into these categories: -// Number Cards (2-10) -// Face Cards (J, Q, K) -// Invalid Cards +// Case 2: Number Cards (2-10) +test("Should return numeric value for single-digit number cards", () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("9♦")).toEqual(9); +}); + +test("Should return 10 for '10' cards across all suits", () => { + expect(getCardValue("10♥")).toEqual(10); + expect(getCardValue("10♣")).toEqual(10); +}); + +// Case 3: Face Cards (J, Q, K) +test("Should return 10 for face cards (J, Q, K)", () => { + expect(getCardValue("J♣")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); +}); + +// Case 4: Invalid Cards +test("Should throw an error for arbitrary non-card strings", () => { + expect(() => getCardValue("invalid")).toThrow(); +}); + +test("Should throw an error for invalid ranks or suit-like characters", () => { + expect(() => getCardValue("1♠")).toThrow(); + expect(() => getCardValue("11♥")).toThrow(); + expect(() => getCardValue("Z♠")).toThrow(); +}); + +test("Should throw an error for missing or invalid suit", () => { + expect(() => getCardValue("10")).toThrow(); + expect(() => getCardValue("AX")).toThrow(); +}); + +test("Should throw an error when suit is before rank", () => { + expect(() => getCardValue("♠A")).toThrow(); +}); + +test("Should throw an error for non-string or empty inputs", () => { + expect(() => getCardValue("")).toThrow(); + expect(() => getCardValue(10)).toThrow(); +}); // 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 - diff --git a/test-driven-development/fizzbuzz.js b/test-driven-development/fizzbuzz.js new file mode 100644 index 000000000..12198b29f --- /dev/null +++ b/test-driven-development/fizzbuzz.js @@ -0,0 +1,10 @@ +function fizzbuzz(number){ + if (number % 3 === 0 && number % 5 === 0){ + return "fizzbuzz"; + } else if (number % 5 === 0){ + return "buzz"; + } else if (number % 3 === 0){ + return "fizz"; + } + return number.toString(); +}; \ No newline at end of file diff --git a/test-driven-development/fizzbuzz.test.js b/test-driven-development/fizzbuzz.test.js new file mode 100644 index 000000000..e48f23149 --- /dev/null +++ b/test-driven-development/fizzbuzz.test.js @@ -0,0 +1,36 @@ +import {fizzbuzz} from './fizzbuzz.js'; +import assert from 'node:assert'; +import {test, describe} from 'node:test'; + +describe('division by 3 and 5', () => { + + test('15 returns fizzbuzz', () => { + assert.equal(fizzbuzz(15), "fizzbuzz"); + }); + + test('30 returns fizzbuzz', () => { + assert.equal(fizzbuzz(30), "fizzbuzz"); + }); + + test('90 returns fizzbuzz', () => { + assert.equal(fizzbuzz(90), "fizzbuzz"); + }); + +}); + + +describe('returning the number as a string', () => { + + test('1 returns "1"', () => { + assert.equal(fizzbuzz(1), "1"); + }); + + test('4 returns "4"', () => { + assert.equal(fizzbuzz(4), "4"); + }); + + test('91 returns "91"', () => { + assert.equal(fizzbuzz(91), "91"); + }); + +}); \ No newline at end of file