Skip to content

Commit b1f57bf

Browse files
committed
implement and rewriting tests complete
1 parent 3372770 commit b1f57bf

File tree

6 files changed

+118
-10
lines changed

6 files changed

+118
-10
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

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

2127
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +41,31 @@ function assertEquals(actualOutput, targetOutput) {
3541
// Example: Identify Right Angles
3642
const right = getAngleType(90);
3743
assertEquals(right, "Right angle");
44+
let acute = getAngleType(1);
45+
assertEquals(acute, "Acute angle");
46+
acute = getAngleType(45);
47+
assertEquals(acute, "Acute angle");
48+
acute = getAngleType(89);
49+
assertEquals(acute, "Acute angle");
50+
let obtuse = getAngleType(91);
51+
assertEquals(obtuse, "Obtuse angle");
52+
obtuse = getAngleType(100);
53+
assertEquals(obtuse, "Obtuse angle");
54+
obtuse = getAngleType(179);
55+
assertEquals(obtuse, "Obtuse angle");
56+
const straight = getAngleType(180);
57+
assertEquals(straight, "Straight angle");
58+
let reflex = getAngleType(200);
59+
assertEquals(reflex, "Reflex angle");
60+
reflex = getAngleType(243);
61+
assertEquals(reflex, "Reflex angle");
62+
reflex = getAngleType(359);
63+
assertEquals(reflex, "Reflex angle");
64+
let invalid = getAngleType(-11);
65+
assertEquals(invalid, "Invalid angle");
66+
invalid = getAngleType(0);
67+
assertEquals(invalid, "Invalid angle");
68+
invalid = getAngleType(360);
69+
assertEquals(invalid, "Invalid angle");
70+
invalid = getAngleType(371);
71+
assertEquals(invalid, "Invalid angle");

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if (Math.abs(numerator) < Math.abs(denominator)) return true;
16+
else return false;
1517
}
16-
1718
// The line below allows us to load the isProperFraction function into tests in other files.
1819
// This will be useful in the "rewrite tests with jest" step.
1920
module.exports = isProperFraction;
@@ -31,3 +32,9 @@ function assertEquals(actualOutput, targetOutput) {
3132

3233
// Example: 1/2 is a proper fraction
3334
assertEquals(isProperFraction(1, 2), true);
35+
assertEquals(isProperFraction(7, 4), false);
36+
assertEquals(isProperFraction(-8, 5), false);
37+
assertEquals(isProperFraction(9, 10), true);
38+
assertEquals(isProperFraction(-3, -6), true);
39+
assertEquals(isProperFraction(15, 11), false);
40+
assertEquals(isProperFraction(-17, -24), true);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,25 @@
2020
// Acceptance criteria:
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
23-
2423
function getCardValue(card) {
2524
// TODO: Implement this function
25+
if (typeof card !== "string") {
26+
throw new Error("Invalid card");
27+
}
28+
const validSuits = ["♠", "♥", "♦", "♣"];
29+
const suit = card.slice(-1);
30+
const rank = card.slice(0, -1);
31+
if (!validSuits.includes(suit)) {
32+
throw new Error("Invalid card");
33+
} else if (rank === "A") return 11;
34+
else if (["J", "Q", "K"].includes(rank)) return 10;
35+
const number = Number(rank);
36+
if (number >= 2 && number <= 10) return number;
37+
throw new Error("Invalid card");
2638
}
2739

40+
module.exports = getCardValue;
41+
2842
// The line below allows us to load the getCardValue function into tests in other files.
2943
// This will be useful in the "rewrite tests with jest" step.
3044
module.exports = getCardValue;

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,40 @@ const getAngleType = require("../implement/1-get-angle-type");
66
// including boundary and invalid cases.
77

88
// Case 1: Acute angles
9-
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
9+
test(`should return "Acute angles" when (0 < angle < 90)`, () => {
1010
// Test various acute angles, including boundary cases
11-
expect(getAngleType(1)).toEqual("Acute angle");
12-
expect(getAngleType(45)).toEqual("Acute angle");
13-
expect(getAngleType(89)).toEqual("Acute angle");
11+
expect(getAngleType(1)).toBe("Acute angle");
12+
expect(getAngleType(45)).toBe("Acute angle");
13+
expect(getAngleType(89)).toBe("Acute angle");
1414
});
15-
1615
// Case 2: Right angle
16+
test(`should return "Right angle" when (angle === 90)`, () => {
17+
// Test various acute angles, including boundary cases
18+
expect(getAngleType(90)).toBe("Right angle");
19+
});
1720
// Case 3: Obtuse angles
21+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
22+
// Test various acute angles, including boundary cases
23+
expect(getAngleType(97)).toBe("Obtuse angle");
24+
expect(getAngleType(129)).toBe("Obtuse angle");
25+
expect(getAngleType(165)).toBe("Obtuse angle");
26+
});
1827
// Case 4: Straight angle
28+
test(`should return "Straight angle" when (angle ==180)`, () => {
29+
// Test various acute angles, including boundary cases
30+
expect(getAngleType(180)).toBe("Straight angle");
31+
});
1932
// Case 5: Reflex angles
33+
test(`should return "Reflex angles" when (180 < angle < 360)`, () => {
34+
// Test various acute angles, including boundary cases
35+
expect(getAngleType(191)).toBe("Reflex angle");
36+
expect(getAngleType(250)).toBe("Reflex angle");
37+
expect(getAngleType(317)).toBe("Reflex angle");
38+
});
2039
// Case 6: Invalid angles
40+
test(`should return "Invalid angles" when (angle >= 360 or angle<= 0 )`, () => {
41+
// Test various acute angles, including boundary cases
42+
expect(getAngleType(0)).toBe("Invalid angle");
43+
expect(getAngleType(-45)).toBe("Invalid angle");
44+
expect(getAngleType(370)).toBe("Invalid angle");
45+
});

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
const isProperFraction = require("../implement/2-is-proper-fraction");
44

55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
6-
76
// Special case: numerator is zero
87
test(`should return false when denominator is zero`, () => {
98
expect(isProperFraction(1, 0)).toEqual(false);
9+
expect(isProperFraction(0, 1)).toEqual(true);
10+
expect(isProperFraction(0, -1)).toEqual(true);
11+
expect(isProperFraction(18, 1)).toEqual(false);
12+
expect(isProperFraction(7, 3)).toEqual(false);
13+
expect(isProperFraction(1, -2)).toEqual(true);
14+
expect(isProperFraction(-15, -9)).toEqual(false);
15+
expect(isProperFraction(-2, -6)).toEqual(true);
16+
expect(isProperFraction(-137, -71)).toEqual(false);
17+
expect(isProperFraction(-100, -189)).toEqual(true);
18+
expect(isProperFraction(27, 5)).toEqual(false);
19+
expect(isProperFraction(-29, 17)).toEqual(false);
1020
});

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,26 @@ const getCardValue = require("../implement/3-get-card-value");
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
11-
11+
// Case 2: Number Cards (2-10)
12+
test(`Should return the numeric value for number cards`, () => {
13+
expect(getCardValue("2♠")).toEqual(2);
14+
expect(getCardValue("5♥")).toEqual(5);
15+
expect(getCardValue("9♦")).toEqual(9);
16+
expect(getCardValue("10♣")).toEqual(10);
17+
});
18+
// Case 3: Face Cards (J, Q, K)
19+
test(`Should return 10 for face cards`, () => {
20+
expect(getCardValue("J♠")).toEqual(10);
21+
expect(getCardValue("Q♥")).toEqual(10);
22+
expect(getCardValue("K♦")).toEqual(10);
23+
});
24+
// Case 4: Invalid Cards
25+
test(`Should throw "Invalid card" for invalid cards`, () => {
26+
expect(() => getCardValue("1♠")).toThrow("Invalid card");
27+
expect(() => getCardValue("B♠")).toThrow("Invalid card");
28+
expect(() => getCardValue("A$")).toThrow("Invalid card");
29+
expect(() => getCardValue("10X")).toThrow("Invalid card");
30+
});
1231
// Suggestion: Group the remaining test data into these categories:
1332
// Number Cards (2-10)
1433
// Face Cards (J, Q, K)
@@ -17,4 +36,3 @@ test(`Should return 11 when given an ace card`, () => {
1736
// To learn how to test whether a function throws an error as expected in Jest,
1837
// please refer to the Jest documentation:
1938
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)