Skip to content

Commit 21ded3e

Browse files
feat: implement and rewrite tests for CYF-1059
1 parent 29ef74a commit 21ded3e

9 files changed

Lines changed: 283 additions & 17 deletions

File tree

‎implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,48 @@ import test from "node:test";
33

44
import { getAngleType } from "../implement/1-get-angle-type.js";
55

6-
// TODO: Write tests to cover all cases, including boundary and invalid cases.
76
// Example: Identify Right Angles
87

98
test("Classifies right angles", () => {
109
const right = getAngleType(90);
1110
assert.equal(right, "Right angle");
1211
});
12+
13+
// Acute Angles (0° < angle < 90°)
14+
test("Classifies acute angles", () => {
15+
assert.equal(getAngleType(1), "Acute angle");
16+
assert.equal(getAngleType(45), "Acute angle");
17+
assert.equal(getAngleType(89.9), "Acute angle");
18+
});
19+
20+
// Right Angles (angle === 90°)
21+
test("Classifies right angles", () => {
22+
assert.equal(getAngleType(90), "Right angle");
23+
});
24+
25+
// Obtuse Angles (90° < angle < 180°)
26+
test("Classifies obtuse angles", () => {
27+
assert.equal(getAngleType(90.1), "Obtuse angle");
28+
assert.equal(getAngleType(135), "Obtuse angle");
29+
assert.equal(getAngleType(179.9), "Obtuse angle");
30+
});
31+
32+
// Straight Angles (angle === 180°)
33+
test("Classifies straight angles", () => {
34+
assert.equal(getAngleType(180), "Straight angle");
35+
});
36+
37+
// Reflex Angles (180° < angle < 360°)
38+
test("Classifies reflex angles", () => {
39+
assert.equal(getAngleType(180.1), "Reflex angle");
40+
assert.equal(getAngleType(270), "Reflex angle");
41+
assert.equal(getAngleType(359.9), "Reflex angle");
42+
});
43+
44+
// Invalid & Boundary Cases (<= 0° or >= 360°)
45+
test("Classifies invalid angles and exact boundaries", () => {
46+
assert.equal(getAngleType(0), "Invalid angle");
47+
assert.equal(getAngleType(-45), "Invalid angle");
48+
assert.equal(getAngleType(360), "Invalid angle");
49+
assert.equal(getAngleType(400), "Invalid angle");
50+
});

‎implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,33 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
66
// TODO: Write tests to cover all cases.
77
// What combinations of numerators and denominators should you test?
88

9+
910
test("Basic proper fraction", () => {
1011
// Example: 1/2 is a proper fraction
1112
assert.equal(isProperFraction(1, 2), true);
13+
assert.equal(isProperFraction(3, 4), true);
14+
});
15+
16+
test("Improper fraction where numerator is larger than denominator", () => {
17+
assert.equal(isProperFraction(5, 2), false);
18+
assert.equal(isProperFraction(4, 3), false);
19+
});
20+
21+
test("Equal numerator and denominator (equals 1)", () => {
22+
assert.equal(isProperFraction(3, 3), false);
1223
});
24+
25+
test("Denominator is zero", () => {
26+
assert.equal(isProperFraction(1, 0), false);
27+
assert.equal(isProperFraction(0, 0), false);
28+
});
29+
30+
test("Zero numerator with non-zero denominator", () => {
31+
assert.equal(isProperFraction(0, 5), true);
32+
});
33+
34+
test("Negative numbers", () => {
35+
assert.equal(isProperFraction(-1, 2), true);
36+
assert.equal(isProperFraction(1, -2), true);
37+
assert.equal(isProperFraction(-3, -2), false);
38+
});

‎implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,73 @@ import { getCardValue } from "../implement/3-get-card-value.js";
55

66
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
77

8-
test("Valid single-digit card", () => {
9-
assert.equal(getCardValue("9♠"), 9);
8+
test("Valid single-digit number cards return correct values", () => {
9+
assert.equal(getCardValue("2♠"), 2);
10+
assert.equal(getCardValue("5♥"), 5);
11+
assert.equal(getCardValue("9♦"), 9);
1012
});
1113

12-
test("Arbitrary non-card string", () => {
13-
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
14+
test("Valid double-digit card (10) returns 10", () => {
15+
assert.equal(getCardValue("10♥"), 10);
16+
assert.equal(getCardValue("10♣"), 10);
1417
});
1518

16-
// TODO: What other invalid card cases can you think of?
19+
test("Ace cards return 11", () => {
20+
assert.equal(getCardValue("A♠"), 11);
21+
assert.equal(getCardValue("A♦"), 11);
22+
});
23+
24+
test("Face cards (J, Q, K) return 10", () => {
25+
assert.equal(getCardValue("J♣"), 10);
26+
assert.equal(getCardValue("Q♦"), 10);
27+
assert.equal(getCardValue("K♠"), 10);
28+
});
29+
30+
// Invalid Input Cases
31+
test("Throws error for arbitrary non-card string", () => {
32+
assert.throws(
33+
() => getCardValue("invalid"),
34+
/Expected a card rank followed by a suit/
35+
);
36+
});
37+
38+
test("Throws error for invalid ranks or suit-like characters", () => {
39+
assert.throws(
40+
() => getCardValue("1♠"),
41+
/Expected a card rank followed by a suit/
42+
);
43+
assert.throws(
44+
() => getCardValue("11♥"),
45+
/Expected a card rank followed by a suit/
46+
);
47+
assert.throws(
48+
() => getCardValue("Z♠"),
49+
/Expected a card rank followed by a suit/
50+
);
51+
});
52+
53+
test("Throws error for invalid or missing suit", () => {
54+
assert.throws(
55+
() => getCardValue("10"),
56+
/Expected a card rank followed by a suit/
57+
);
58+
assert.throws(
59+
() => getCardValue("AX"),
60+
/Expected a card rank followed by a suit/
61+
);
62+
});
63+
64+
test("Throws error for reversed order (suit before rank)", () => {
65+
assert.throws(
66+
() => getCardValue("♠A"),
67+
/Expected a card rank followed by a suit/
68+
);
69+
});
70+
71+
test("Throws error for empty string or non-string input", () => {
72+
assert.throws(
73+
() => getCardValue(""),
74+
/Expected a card rank followed by a suit/
75+
);
76+
assert.throws(() => getCardValue(10), /Expected a string/);
77+
});

‎implement-and-rewrite-tests/implement/1-get-angle-type.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,21 @@
1717
// execute the code to ensure all tests pass.
1818

1919
export function getAngleType(angle) {
20-
// TODO: Implement this function
20+
if (angle > 0 && angle < 90) {
21+
return "Acute angle";
22+
}
23+
if (angle === 90) {
24+
return "Right angle";
25+
}
26+
if (angle > 90 && angle < 180) {
27+
return "Obtuse angle";
28+
}
29+
if (angle === 180) {
30+
return "Straight angle";
31+
}
32+
if (angle > 180 && angle < 360) {
33+
return "Reflex angle";
34+
}
35+
36+
return "Invalid angle";
2137
}

‎implement-and-rewrite-tests/implement/2-is-proper-fraction.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
// execute the code to ensure all tests pass.
1515

1616
export function isProperFraction(numerator, denominator) {
17-
// TODO: Implement this function
17+
if (denominator === 0) {
18+
return false;
19+
}
20+
21+
return Math.abs(numerator) < Math.abs(denominator);
1822
}

‎implement-and-rewrite-tests/implement/3-get-card-value.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,28 @@
2424
// execute the code to ensure all tests pass.
2525

2626
export function getCardValue(card) {
27-
// TODO: Implement this function
27+
if (typeof card !== "string") {
28+
throw new Error(`Expected a string, but got ${typeof card}`);
29+
}
30+
31+
const cardRegex = /^(10|[2-9]|[AJQK])([♠♥♦♣])$/;
32+
const match = card.match(cardRegex);
33+
34+
if (!match) {
35+
throw new Error(
36+
`Expected a card rank followed by a suit, but got "${card}"`
37+
);
38+
}
39+
40+
const rank = match[1];
41+
42+
if (rank === "A") {
43+
return 11;
44+
}
45+
46+
if (["J", "Q", "K"].includes(rank)) {
47+
return 10;
48+
}
49+
50+
return Number(rank);
2851
}

‎implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,43 @@ import { getAngleType } from "../implement/1-get-angle-type.js";
33
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
44
// including boundary and invalid cases.
55

6+
67
// Case 1: Acute angles
78
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
89
// Test various acute angles, including boundary cases
910
expect(getAngleType(1)).toEqual("Acute angle");
1011
expect(getAngleType(45)).toEqual("Acute angle");
11-
expect(getAngleType(89)).toEqual("Acute angle");
12+
expect(getAngleType(89.9)).toEqual("Acute angle");
1213
});
1314

1415
// Case 2: Right angle
16+
test(`should return "Right angle" when angle is exactly 90`, () => {
17+
expect(getAngleType(90)).toEqual("Right angle");
18+
});
19+
1520
// Case 3: Obtuse angles
21+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
22+
expect(getAngleType(90.1)).toEqual("Obtuse angle");
23+
expect(getAngleType(135)).toEqual("Obtuse angle");
24+
expect(getAngleType(179.9)).toEqual("Obtuse angle");
25+
});
26+
1627
// Case 4: Straight angle
28+
test(`should return "Straight angle" when angle is exactly 180`, () => {
29+
expect(getAngleType(180)).toEqual("Straight angle");
30+
});
31+
1732
// Case 5: Reflex angles
33+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
34+
expect(getAngleType(180.1)).toEqual("Reflex angle");
35+
expect(getAngleType(270)).toEqual("Reflex angle");
36+
expect(getAngleType(359.9)).toEqual("Reflex angle");
37+
});
38+
1839
// Case 6: Invalid angles
40+
test(`should return "Invalid angle" when angle is <= 0 or >= 360`, () => {
41+
expect(getAngleType(0)).toEqual("Invalid angle");
42+
expect(getAngleType(-45)).toEqual("Invalid angle");
43+
expect(getAngleType(360)).toEqual("Invalid angle");
44+
expect(getAngleType(400)).toEqual("Invalid angle");
45+
});

‎implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,38 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";
22

33
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
44

5+
// Positive numbers
6+
test("should return true when positive numerator is less than positive denominator", () => {
7+
expect(isProperFraction(1, 2)).toEqual(true);
8+
expect(isProperFraction(3, 4)).toEqual(true);
9+
});
10+
11+
test("should return false when positive numerator is equal to or greater than positive denominator", () => {
12+
expect(isProperFraction(3, 3)).toEqual(false);
13+
expect(isProperFraction(5, 2)).toEqual(false);
14+
});
15+
516
// Special case: numerator is zero
6-
test(`should return false when denominator is zero`, () => {
17+
18+
test("should return false when denominator is zero", () => {
719
expect(isProperFraction(1, 0)).toEqual(false);
20+
expect(isProperFraction(0, 0)).toEqual(false);
21+
});
22+
23+
test("should return true when numerator is zero and denominator is non-zero", () => {
24+
expect(isProperFraction(0, 5)).toEqual(true);
25+
expect(isProperFraction(0, -5)).toEqual(true);
26+
});
27+
28+
// Negative numbers
29+
test("should return true when absolute value of numerator is less than denominator", () => {
30+
expect(isProperFraction(-1, 2)).toEqual(true);
31+
expect(isProperFraction(1, -2)).toEqual(true);
32+
expect(isProperFraction(-1, -2)).toEqual(true);
33+
});
34+
35+
test("should return false when absolute value of numerator is equal to or greater than denominator", () => {
36+
expect(isProperFraction(-5, 2)).toEqual(false);
37+
expect(isProperFraction(5, -2)).toEqual(false);
38+
expect(isProperFraction(-3, -3)).toEqual(false);
839
});

‎implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,57 @@ import { getCardValue } from "../implement/3-get-card-value.js";
22

33
// TODO: Write tests in Jest syntax to cover all possible outcomes.
44

5+
56
// Case 1: Ace (A)
6-
test(`Should return 11 when given an ace card`, () => {
7+
test("Should return 11 when given an ace card", () => {
78
expect(getCardValue("A♠")).toEqual(11);
9+
expect(getCardValue("A♦")).toEqual(11);
810
});
911

10-
// Suggestion: Group the remaining test data into these categories:
11-
// Number Cards (2-10)
12-
// Face Cards (J, Q, K)
13-
// Invalid Cards
12+
// Case 2: Number Cards (2-10)
13+
test("Should return numeric value for single-digit number cards", () => {
14+
expect(getCardValue("2♠")).toEqual(2);
15+
expect(getCardValue("5♥")).toEqual(5);
16+
expect(getCardValue("9♦")).toEqual(9);
17+
});
18+
19+
test("Should return 10 for '10' cards across all suits", () => {
20+
expect(getCardValue("10♥")).toEqual(10);
21+
expect(getCardValue("10♣")).toEqual(10);
22+
});
23+
24+
// Case 3: Face Cards (J, Q, K)
25+
test("Should return 10 for face cards (J, Q, K)", () => {
26+
expect(getCardValue("J♣")).toEqual(10);
27+
expect(getCardValue("Q♦")).toEqual(10);
28+
expect(getCardValue("K♠")).toEqual(10);
29+
});
30+
31+
// Case 4: Invalid Cards
32+
test("Should throw an error for arbitrary non-card strings", () => {
33+
expect(() => getCardValue("invalid")).toThrow();
34+
});
35+
36+
test("Should throw an error for invalid ranks or suit-like characters", () => {
37+
expect(() => getCardValue("1♠")).toThrow();
38+
expect(() => getCardValue("11♥")).toThrow();
39+
expect(() => getCardValue("Z♠")).toThrow();
40+
});
41+
42+
test("Should throw an error for missing or invalid suit", () => {
43+
expect(() => getCardValue("10")).toThrow();
44+
expect(() => getCardValue("AX")).toThrow();
45+
});
46+
47+
test("Should throw an error when suit is before rank", () => {
48+
expect(() => getCardValue("♠A")).toThrow();
49+
});
50+
51+
test("Should throw an error for non-string or empty inputs", () => {
52+
expect(() => getCardValue("")).toThrow();
53+
expect(() => getCardValue(10)).toThrow();
54+
});
1455

1556
// To learn how to test whether a function throws an error as expected in Jest,
1657
// please refer to the Jest documentation:
1758
// https://jestjs.io/docs/expect#tothrowerror
18-

0 commit comments

Comments
 (0)