Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});
18 changes: 17 additions & 1 deletion implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
25 changes: 24 additions & 1 deletion implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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

Loading