Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,27 @@ 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("returns Acute angle for angles between 0 and 90", () => {
assert.strictEqual(getAngleType(45), "Acute angle");
});

test("returns Right angle for 90", () => {
assert.strictEqual(getAngleType(90), "Right angle");
});

test("returns Obtuse angle for angles between 90 and 180", () => {
assert.strictEqual(getAngleType(120), "Obtuse angle");
});

test("returns Straight angle for 180", () => {
assert.strictEqual(getAngleType(180), "Straight angle");
});

test("returns Reflex angle for angles between 180 and 360", () => {
assert.strictEqual(getAngleType(270), "Reflex angle");
});

test("returns Invalid angle for angles outside the valid range", () => {
assert.strictEqual(getAngleType(0), "Invalid angle");
assert.strictEqual(getAngleType(360), "Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,38 @@ 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
// Case 1: Proper fraction
test("should return true when numerator is smaller than denominator", () => {
assert.equal(isProperFraction(1, 2), true);
assert.equal(isProperFraction(3, 5), true);
});

// Case 2: Numerator equals denominator
test("should return false when numerator equals denominator", () => {
assert.equal(isProperFraction(2, 2), false);
});

// Case 3: Numerator is greater than denominator
test("should return false when numerator is greater than denominator", () => {
assert.equal(isProperFraction(3, 2), false);
});

// Case 4: Zero numerator
test("should return true when numerator is zero", () => {
assert.equal(isProperFraction(0, 2), true);
});

// Case 5: Negative numerator
test("should return true when numerator is negative and denominator is positive", () => {
assert.equal(isProperFraction(-1, 2), true);
});

// Case 6: Negative denominator
test("should return false when denominator is negative", () => {
assert.equal(isProperFraction(1, -2), false);
});
Comment on lines +30 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Although there are different definitions for negative proper fractions (one definition returns true and another returns false) and I would treat both definitions as correct solution, these two cases should be consistently returned the same result regardless of which definition to follow. So please fix the test cases (and the program if necessary).

Also, how about both numerator and denominator are negative numbers (e.g. -1/-2 and -2/-1) ? Please add them to the test suite as well. Thank you.


// Case 7: Zero denominator
test("should return false when denominator is zero", () => {
assert.equal(isProperFraction(1, 0), false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,36 @@ 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?
test("Ace should return 11", () => {
assert.equal(getCardValue("A♠"), 11);
});

test("Face cards should return 10", () => {
assert.equal(getCardValue("J♠"), 10);
assert.equal(getCardValue("Q♠"), 10);
assert.equal(getCardValue("K♠"), 10);
});

test("Number cards should return their numeric value", () => {
assert.equal(getCardValue("2♠"), 2);
assert.equal(getCardValue("10♠"), 10);
});

test("Should throw an error for a missing suit", () => {
assert.throws(() => getCardValue("A"), Error);
});

test("Should throw an error for an invalid rank", () => {
assert.throws(() => getCardValue("X♠"), Error);
});

test("Should throw an error for an empty string", () => {
assert.throws(() => getCardValue(""), Error);
});
14 changes: 13 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,17 @@
// execute the code to ensure all tests pass.

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

export function getCardValue(card) {
// TODO: Implement this function
if (typeof card !== "string" || !/^(A|[2-9]|10|J|Q|K)[♠♥♦♣]$/.test(card)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your program is correct, and no need to change anything. Just I would like to know more about your program design process:

  1. Can you explain the breakdown of !/^(A|[2-9]|10|J|Q|K)[♠♥♦♣]$/.test(card) expression?
  2. What is the advantages of this approach over other implementations?

Thank you for adding a reply to this comment.

throw new Error(`Expected a number followed by a suit, but got "${card}"`);
}

if (card.startsWith("A")) {
return 11;
}
if (card.startsWith("J") || card.startsWith("Q") || card.startsWith("K")) {
return 10;
}

const value = parseInt(card);
if (!isNaN(value)) {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,37 @@ import { getAngleType } from "../implement/1-get-angle-type.js";

// 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");
});

// Case 2: Right angle
test(`should return "Right angle" for 90 degrees`, () => {
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" for 180 degrees`, () => {
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" when ( angle <= 0 or angle >= 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
Comment on lines +37 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This seems like the duplicate of the Case 5 tests above. Please fix it, thank you.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ 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 return true for a proper fraction", () => {
expect(isProperFraction(1, 2)).toBe(true);
expect(isProperFraction(3, 5)).toBe(true);
});

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

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

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

test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toBe(false);
Comment on lines +18 to +23

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If only considering positive proper fraction, these two cases should both return false. Depends on how you fix https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/pull/1766/changes#r4423374448 , one of these test cases should be fixed as well. Thank you.

});
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,45 @@ 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`, () => {
expect(getCardValue("A♠")).toEqual(11);

describe("Ace cards", () => {
test("Should return 11 when given and ace card", () => {
expect(getCardValue("A♠")).toEqual(11);
});
});

describe("Number cards (2-10)", () => {
test("Should return the number for a number card", () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♠")).toEqual(5);
expect(getCardValue("10♠")).toEqual(10);
});
});

describe("Face cards (J, Q, K)", () => {
test("Should return 10 for face cards", () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});
});

describe("Invalid cards", () => {
test("Should throw an error for an invalid card", () => {
expect(() => getCardValue("invalid")).toThrow();
});

test("Should throw an error when the suit is missing", () => {
expect(() => getCardValue("A")).toThrow();
});

test("Should throw an error for an invalid rank", () => {
expect(() => getCardValue("X♠")).toThrow();
});

test("Should throw an error for an empty string", () => {
expect(() => getCardValue("")).toThrow();
});
});

// Suggestion: Group the remaining test data into these categories:
Expand All @@ -15,4 +52,3 @@ test(`Should return 11 when given an ace card`, () => {
// 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