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 @@ -10,3 +10,41 @@ 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");
assert.equal(getAngleType(36), "Acute angle");
assert.equal(getAngleType(20), "Acute angle");

});

test("classifies obtuse angles", () => {
assert.equal(getAngleType(91), "Obtuse angle");
assert.equal(getAngleType(105), "Obtuse angle");
assert.equal(getAngleType(179), "Obtuse angle");
assert.equal(getAngleType(125), "Obtuse angle");
assert.equal(getAngleType(110), "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(220), "Reflex angle");
assert.equal(getAngleType(289), "Reflex angle");
assert.equal(getAngleType(300), "Reflex angle");
assert.equal(getAngleType(349), "Reflex angle");
});

test("classifies invalid angles", () => {
assert.equal(getAngleType(-5), "Invalid angle");
assert.equal(getAngleType(0), "Invalid angle");
assert.equal(getAngleType(360), "Invalid angle");
assert.equal(getAngleType(400), "Invalid angle");
assert.equal(getAngleType(420), "Invalid angle");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It seems to me that there are redundant and missing test cases:

  • About zero cases, you have tested (0, 1) (1, 0) and (-1, 0) but missed a case.
  • About non-zero (numerator≠denominator) case, you have included (-1, -2) (1, -2) (-1, 2) and (1, 2) but they are all numerator less than denominator (if not considering the negative sign). How about numerator greater than denominator with negative numbers? Since (9, 8) (4, 2) (5, 3) (6, 4) (7, 1) (15, 14) are all testing the same scenario, there would be 3 other combinations missing.
  • About non-zero (numerator=denominator) case, you have tested positive number (4, 4) but missed the negative number.

So please add the missing cases (and optionally remove redundant cases as well). Thank you.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,63 @@ 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", () => {
test("Returns true for a basic proper fraction", () => {
// Example: 1/2 is a proper fraction
assert.equal(isProperFraction(1, 2), true);
});

test("Returns true when the numerator is zero and denominator is positive", () => {
assert.equal(isProperFraction(0, 1), true);
});

test("Returns true when both numerator and denominator is negative", () => {
assert.equal(isProperFraction(-1, -2), true);
});

test("Returns true when the numerator is positive denominator is negative", () => {
assert.equal(isProperFraction(1, -2), true);
});

test("Returns true when the numerator is negative and the denominator is positive", () => {
assert.equal(isProperFraction(-1, 2), true);
});

test("Returns false when numerator is negative and denominator is zero", () => {
assert.equal(isProperFraction(-1, 0), false);
});

test("Returns false for 15/14", () => {
assert.equal(isProperFraction(15, 14), false);
});

test("Returns false when numerator equals denominator", () => {
assert.equal(isProperFraction(4, 4), false);
});

test("Returns false when numerator is positive and denominator is zero", () => {
assert.equal(isProperFraction(1, 0), false);
});

test("Returns false when both numerator and denominator are zero", () => {
assert.equal(isProperFraction(0, 0), false);
});

test("Returns true when numerator is zero and denominator is negative", () => {
assert.equal(isProperFraction(0, -2), true);
});

test("Returns false when positive numerator has greater magnitude than negative denominator", () => {
assert.equal(isProperFraction(3, -2), false);
});

test("Returns false when negative numerator has greater magnitude than positive denominator", () => {
assert.equal(isProperFraction(-3, 2), false);
});

test("Returns false when both numbers are negative and numerator has greater magnitude", () => {
assert.equal(isProperFraction(-3, -2), false);
});

test("Returns false when numerator and denominator are equal negative numbers", () => {
assert.equal(isProperFraction(-4, -4), false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,53 @@ test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
});

test("Ace has a value of 11", () => {
assert.equal(getCardValue("A♠"), 11);
});

test("Two has a value of 2", () => {
assert.equal(getCardValue("2♥"), 2);
});

test("Ten has a value of 10", () => {
assert.equal(getCardValue("10♥"), 10);
});

test("Jack has a value of 10", () => {
assert.equal(getCardValue("J♣"), 10);
});

test("Queen has a value of 10", () => {
assert.equal(getCardValue("Q♦"), 10);
});

test("King has a value of 10", () => {
assert.equal(getCardValue("K♦"), 10);
});

test("Arbitrary non-card string", () => {
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("Invalid rank throws an error", () => {
assert.throws(() => getCardValue("Z♠"), /Expected a number followed by a suit, but got "Z♠"/, "Expected clear error");
});

test("Invalid suit throws an error", () => {
assert.throws(() => getCardValue("9X"), /Expected a number followed by a suit, but got "9X"/, "Expected clear error");
});

test("Card without a suit throws an error", () => {
assert.throws(() => getCardValue("9"), /Expected a number followed by a suit, but got "9"/, "Expected clear error");
});

test("Card without a rank throws an error", () => {
assert.throws(() => getCardValue("♠"), /Expected a number followed by a suit, but got "♠"/, "Expected clear error");
});

test("Card without a number greater than 10 throws an error", () => {
assert.throws(() => getCardValue("15♠"), /Expected a number followed by a suit, but got "15♠"/, "Expected clear error");
});


// TODO: What other invalid card cases can you think of?
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 Math.abs(numerator) < Math.abs(denominator)
}
26 changes: 24 additions & 2 deletions 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,28 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.


export function getCardValue(card) {
// TODO: Implement this function
}
const rank = card.slice(0, -1);
const suit = card.slice(-1);
const validSuits = ["♠", "♥", "♦", "♣"];
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

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

if (!validRanks.includes(rank)) {
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
}

if (rank === "A") {
return 11;
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

return Number(rank);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,38 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle === 90)`, () => {
// Test the exact right-angle boundary
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(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when (angle === 180)`, () => {
// Test the exact straight-angle boundary
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(220)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when (angles outside of valid range)`, () => {
// Test various invalid angles, including boundary cases
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-5)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same comment as https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/pull/1685/changes#r4308748253
Please add the missing cases (and optionally remove redundant cases as well). Thank you.

Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
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 false when numerator equals the denominator`, () => {
expect(isProperFraction(4, 4)).toEqual(false);
});

test(`should return false if 15/14`, () => {
expect(isProperFraction(15, 14)).toEqual(false);
});

test(`should return false if numerator is negative and denominator is zero`, () => {
expect(isProperFraction(-1, 0)).toEqual(false);
});

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

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

test(`should return true if both numerator and denominator is negative`, () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});

test(`should return true if numerator is zero and the denominator is positive`, () => {
expect(isProperFraction(0, 1)).toEqual(true);
});

test(`should return true for a basic proper fraction`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

test("Returns false when both numerator and denominator are zero", () => {
expect(isProperFraction(0, 0)).toEqual(false);
});

test("Return true when numerator is zero and denominator is negative", () => {
expect(isProperFraction(0, -2)).toEqual(true);
});

test("Returns false when positive numerator has greater magnitude than negative denominator", () => {
expect(isProperFraction(3, -2)).toEqual(false);
});

test("Returns false when both numbers are negative and numerator has greater magnitude", () => {
expect(isProperFraction(-3, -2)).toEqual(false);
});

test("Returns false when negative numerator has greater magnitude than positive denominator", () => {
expect(isProperFraction(-3, 2)).toEqual(false);
});

test("Returns false when numerator and denominator are equal negative numbers", () => {
expect(isProperFraction(-4, -4)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,56 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: Ranks (Number Cards)
test("Two has a value of 2", () => {
expect(getCardValue("2♥")).toEqual (2);
});

test("Five has a value of 2", () => {
expect(getCardValue("5♦")).toEqual (5);
});

test("Ten has a value of 10", () => {
expect(getCardValue("10♥")).toEqual(10);
});


//Case 3: Face cards (J, Q, K)
test("Jack has a value of 10", () => {
expect(getCardValue("J♣")).toEqual(10);
});

test("Queen has a value of 10", () => {
expect(getCardValue("Q♦")).toEqual(10);
});

test("King has a value of 10", () => {
expect(getCardValue("K♦")).toEqual(10);
});


//Case 4: Invalid cards
test("Card without a suit throws an error", () => {
expect(() => getCardValue("9")).toThrow();
});

test("Card without a number throws an error", () => {
expect(() => getCardValue("♣")).toThrow();
});

test("Card with invalid suit throws an error", () => {
expect(() => getCardValue("9X")).toThrow();
});

test("Card with invalid rank throws an error", () => {
expect(() => getCardValue("X♣")).toThrow();
});

test("Card with rank higher than 10 throws an error", () => {
expect(() => getCardValue("15♣")).toThrow();
});


// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -15,4 +65,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