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,37 @@ 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");
});

test("Classifies acute angles", () => {
assert.equal(getAngleType(1), "Acute angle");
assert.equal(getAngleType(45), "Acute angle");
assert.equal(getAngleType(89), "Acute angle");
});

test("Classifies obtuse angles", () => {
assert.equal(getAngleType(91), "Obtuse angle");
assert.equal(getAngleType(120), "Obtuse angle");
assert.equal(getAngleType(179), "Obtuse angle");
});

test("Classifies straight angles", () => {
const straight = getAngleType(180);
assert.equal(straight, "Straight angle");
});

test("Classifies reflex angles", () => {
assert.equal(getAngleType(181), "Reflex angle");
assert.equal(getAngleType(270), "Reflex angle");
assert.equal(getAngleType(359), "Reflex angle");
});

test("Classifies angles outside the valid range as invalid", () => {
assert.equal(getAngleType(-10), "Invalid angle");
assert.equal(getAngleType(0), "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 @@ -3,14 +3,37 @@ import test from "node:test";

import { getCardValue } from "../implement/3-get-card-value.js";

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
test("Returns the correct value for every valid rank and suit", () => {
const expectedValues = {
A: 11,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
J: 10,
Q: 10,
K: 10,
};
const suits = ["♠", "♥", "♦", "♣"];

test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
for (const [rank, value] of Object.entries(expectedValues)) {
for (const suit of suits) {
assert.equal(getCardValue(`${rank}${suit}`), value);
}
}
});

test("Arbitrary non-card string", () => {
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
});
test("Throws an error for invalid card formats", () => {
const invalidCards = ["A", "invalid", "1♠", "11♠", "02♠", "2.5♠", " 2♠", "A?", ""];

// TODO: What other invalid card cases can you think of?
for (const card of invalidCards) {
assert.throws(() => getCardValue(card), {
message: `Expected a number followed by a suit, but got "${card}"`,
});
}
});
21 changes: 20 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,24 @@
// execute the code to ensure all tests pass.

export function getAngleType(angle) {
// TODO: Implement this function
if (angle === 90) {
return "Right angle";
}
if (angle > 0 && angle < 90) {
return "Acute 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,5 @@
// execute the code to ensure all tests pass.

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return numerator < denominator;
}
29 changes: 28 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 @@ -23,6 +23,33 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

const cardValues = new Map([
["A", 11],
["2", 2],
["3", 3],
["4", 4],
["5", 5],
["6", 6],
["7", 7],
["8", 8],
["9", 9],
["10", 10],
["J", 10],
["Q", 10],
["K", 10],
]);

export function getCardValue(card) {
// TODO: Implement this function
const suit = card.slice(-1);
const rank = card.slice(0, -1);

if (!["♠", "♥", "♦", "♣"].includes(suit)) {
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
}

if (cardValues.has(rank)) {
return cardValues.get(rank);
}

throw new Error(`Expected a number followed by a suit, but got "${card}"`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,30 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test('should return "right angle" when angle is 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(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test('should return "Straight angle" when angle is 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" for invalid angles', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An exact range of invalid angles could probably make the test description more informative.

expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(0)).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 @@ -6,3 +6,30 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
test("should return true for a proper fraction", () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

test("should return false when numerator equals denominator", () => {
expect(isProperFraction(5, 5)).toEqual(false);
});

test("should return false when numerator is greater than denominator", () => {
expect(isProperFraction(7, 3)).toEqual(false);
});

test("should return true when numerator is zero", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

test("should return true when both numerator and denominator are negative and form a proper fraction", () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});

test("should return true for negative numerator and positive denominator", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
});

test("should return false for positive numerator and negative denominator", () => {
expect(isProperFraction(1, -2)).toEqual(false);
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import { getCardValue } from "../implement/3-get-card-value.js";

// TODO: Write tests in Jest syntax to cover all possible outcomes.
test("Returns the correct value for every valid rank and suit", () => {
const expectedValues = {
A: 11,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
J: 10,
Q: 10,
K: 10,
};
const suits = ["♠", "♥", "♦", "♣"];

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
for (const [rank, value] of Object.entries(expectedValues)) {
for (const suit of suits) {
expect(getCardValue(`${rank}${suit}`)).toEqual(value);
}
}
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards

// 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("Throws an error for invalid card formats", () => {
const invalidCards = ["A", "invalid", "1♠", "11♠", "02♠", "2.5♠", " 2♠", "A?", ""];

for (const card of invalidCards) {
expect(() => getCardValue(card)).toThrow(
`Expected a number followed by a suit, but got "${card}"`
);
}
});
Loading