Skip to content

Commit 18b7aff

Browse files
committed
Complete implement and Jest rewrite tests for angle, fraction, and cards
1 parent 3372770 commit 18b7aff

File tree

6 files changed

+153
-33
lines changed

6 files changed

+153
-33
lines changed

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,44 @@
1515
// execute the code to ensure all tests pass.
1616

1717
function getAngleType(angle) {
18-
// TODO: Implement this function
18+
if (angle === 90) return "Right angle";
19+
if (angle === 180) return "Straight angle";
20+
if (angle > 0 && angle < 90) return "Acute angle";
21+
if (angle > 90 && angle < 180) return "Obtuse angle";
22+
if (angle > 180 && angle < 360) return "Reflex angle";
23+
24+
return "Invalid angle";
1925
}
2026

21-
// The line below allows us to load the getAngleType function into tests in other files.
22-
// This will be useful in the "rewrite tests with jest" step.
2327
module.exports = getAngleType;
2428

25-
// This helper function is written to make our assertions easier to read.
26-
// If the actual output matches the target output, the test will pass
2729
function assertEquals(actualOutput, targetOutput) {
2830
console.assert(
2931
actualOutput === targetOutput,
3032
`Expected ${actualOutput} to equal ${targetOutput}`
3133
);
3234
}
3335

34-
// TODO: Write tests to cover all cases, including boundary and invalid cases.
35-
// Example: Identify Right Angles
3636
const right = getAngleType(90);
3737
assertEquals(right, "Right angle");
38+
39+
const acute = getAngleType(45);
40+
assertEquals(acute, "Acute angle");
41+
42+
const obtuse = getAngleType(120);
43+
assertEquals(obtuse, "Obtuse angle");
44+
45+
const straight = getAngleType(180);
46+
assertEquals(straight, "Straight angle");
47+
48+
const reflex = getAngleType(270);
49+
assertEquals(reflex, "Reflex angle");
50+
51+
const invalidZero = getAngleType(0);
52+
assertEquals(invalidZero, "Invalid angle");
53+
54+
const invalidNegative = getAngleType(-45);
55+
assertEquals(invalidNegative, "Invalid angle");
56+
57+
const invalidOver = getAngleType(361);
58+
assertEquals(invalidOver, "Invalid angle");

Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
return Math.abs(numerator) < Math.abs(denominator);
1515
}
1616

17-
// The line below allows us to load the isProperFraction function into tests in other files.
18-
// This will be useful in the "rewrite tests with jest" step.
1917
module.exports = isProperFraction;
2018

21-
// Here's our helper again
2219
function assertEquals(actualOutput, targetOutput) {
2320
console.assert(
2421
actualOutput === targetOutput,
2522
`Expected ${actualOutput} to equal ${targetOutput}`
2623
);
2724
}
2825

29-
// TODO: Write tests to cover all cases.
30-
// What combinations of numerators and denominators should you test?
26+
const properFraction = isProperFraction(2, 3);
27+
assertEquals(properFraction, true);
3128

32-
// Example: 1/2 is a proper fraction
33-
assertEquals(isProperFraction(1, 2), true);
29+
const improperFraction = isProperFraction(5, 2);
30+
assertEquals(improperFraction, false);
31+
32+
const negativeFraction = isProperFraction(-4, 7);
33+
assertEquals(negativeFraction, true);
34+
35+
const equalFraction = isProperFraction(3, 3);
36+
assertEquals(equalFraction, false);
37+
38+
const zeroNumerator = isProperFraction(0, 5);
39+
assertEquals(zeroNumerator, true);
40+
41+
const zeroDenominator = isProperFraction(5, 0);
42+
assertEquals(zeroDenominator, false);

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,53 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const rank = card.slice(0, -1);
26+
const suit = card.slice(-1);
27+
28+
const validSuits = ["♠", "♥", "♦", "♣"];
29+
const faceCards = ["10", "J", "Q", "K"];
30+
const numberCards = ["2", "3", "4", "5", "6", "7", "8", "9"];
31+
32+
if (!validSuits.includes(suit)) {
33+
throw new Error("Invalid card suit");
34+
}
35+
36+
if (rank === "A") return 11;
37+
if (faceCards.includes(rank)) return 10;
38+
if (numberCards.includes(rank)) return Number(rank);
39+
40+
throw new Error("Invalid card rank");
2641
}
2742

28-
// The line below allows us to load the getCardValue function into tests in other files.
29-
// This will be useful in the "rewrite tests with jest" step.
3043
module.exports = getCardValue;
3144

32-
// Helper functions to make our assertions easier to read.
3345
function assertEquals(actualOutput, targetOutput) {
3446
console.assert(
3547
actualOutput === targetOutput,
3648
`Expected ${actualOutput} to equal ${targetOutput}`
3749
);
3850
}
3951

40-
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
4252
assertEquals(getCardValue("9♠"), 9);
53+
assertEquals(getCardValue("5♥"), 5);
54+
assertEquals(getCardValue("K♦"), 10);
55+
assertEquals(getCardValue("A♣"), 11);
4356

44-
// Handling invalid cards
4557
try {
4658
getCardValue("invalid");
59+
console.error("Error was not thrown for completely invalid card");
60+
} catch (error) {}
4761

48-
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
62+
try {
63+
getCardValue("1♠");
64+
console.error("Error was not thrown for invalid card rank");
65+
} catch (error) {
66+
assertEquals(error.message, "Invalid card rank");
67+
}
5168

52-
// What other invalid card cases can you think of?
69+
try {
70+
getCardValue("5X");
71+
console.error("Error was not thrown for invalid card suit");
72+
} catch (error) {
73+
assertEquals(error.message, "Invalid card suit");
74+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1414
});
1515

1616
// Case 2: Right angle
17+
test(`should return "Right angle" when angle is 90`, () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
});
20+
1721
// Case 3: Obtuse angles
22+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(135)).toEqual("Obtuse angle");
25+
expect(getAngleType(179)).toEqual("Obtuse angle");
26+
});
27+
1828
// Case 4: Straight angle
29+
test(`should return "Straight angle" when angle is 180`, () => {
30+
expect(getAngleType(180)).toEqual("Straight angle");
31+
});
32+
1933
// Case 5: Reflex angles
34+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
35+
expect(getAngleType(181)).toEqual("Reflex angle");
36+
expect(getAngleType(270)).toEqual("Reflex angle");
37+
expect(getAngleType(359)).toEqual("Reflex angle");
38+
});
39+
2040
// Case 6: Invalid angles
41+
test(`should return "Invalid angle" when angle is <= 0 or >= 360`, () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType(-10)).toEqual("Invalid angle");
44+
expect(getAngleType(360)).toEqual("Invalid angle");
45+
expect(getAngleType(400)).toEqual("Invalid angle");
46+
});

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,28 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
77
// Special case: numerator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
10+
expect(isProperFraction(-5, 0)).toEqual(false);
1011
});
12+
13+
test(`should return true for positive proper fractions`, () => {
14+
expect(isProperFraction(1, 2)).toEqual(true);
15+
expect(isProperFraction(2, 5)).toEqual(true);
16+
});
17+
18+
test(`should return false for positive improper fractions or equal values`, () => {
19+
expect(isProperFraction(3, 2)).toEqual(false);
20+
expect(isProperFraction(5, 5)).toEqual(false);
21+
});
22+
23+
test(`should return true when numerator is zero`, () => {
24+
expect(isProperFraction(0, 5)).toEqual(true);
25+
expect(isProperFraction(0, -5)).toEqual(true);
26+
});
27+
28+
test(`should evaluate correctly with various negative number combinations`, () => {
29+
expect(isProperFraction(-1, 2)).toEqual(true);
30+
expect(isProperFraction(1, -2)).toEqual(true);
31+
expect(isProperFraction(-1, -2)).toEqual(true);
32+
expect(isProperFraction(-5, 2)).toEqual(false);
33+
expect(isProperFraction(5, -2)).toEqual(false);
34+
});

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,32 @@ const getCardValue = require("../implement/3-get-card-value");
77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
10+
expect(getCardValue("A♥")).toEqual(11);
1011
});
1112

12-
// Suggestion: Group the remaining test data into these categories:
13-
// Number Cards (2-10)
14-
// Face Cards (J, Q, K)
15-
// Invalid Cards
13+
test(`Should return the correct numeric value for number cards`, () => {
14+
expect(getCardValue("2♣")).toEqual(2);
15+
expect(getCardValue("7♥")).toEqual(7);
16+
expect(getCardValue("10♦")).toEqual(10);
17+
});
18+
19+
test(`Should return 10 for face cards (J, Q, K)`, () => {
20+
expect(getCardValue("J♠")).toEqual(10);
21+
expect(getCardValue("Q♥")).toEqual(10);
22+
expect(getCardValue("K♦")).toEqual(10);
23+
});
1624

17-
// To learn how to test whether a function throws an error as expected in Jest,
18-
// please refer to the Jest documentation:
19-
// https://jestjs.io/docs/expect#tothrowerror
25+
test(`Should throw an error for invalid card rank`, () => {
26+
expect(() => getCardValue("1♠")).toThrow("Invalid card rank");
27+
expect(() => getCardValue("11♥")).toThrow("Invalid card rank");
28+
expect(() => getCardValue("Z♦")).toThrow("Invalid card rank");
29+
expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank");
30+
expect(() => getCardValue("3.1416♠")).toThrow("Invalid card rank");
31+
});
32+
33+
test(`Should throw an error for invalid card suit`, () => {
34+
expect(() => getCardValue("5X")).toThrow("Invalid card suit");
35+
expect(() => getCardValue("10-")).toThrow("Invalid card suit");
36+
expect(() => getCardValue("A/")).toThrow("Invalid card suit");
37+
});
2038

0 commit comments

Comments
 (0)