Skip to content

Commit 801288b

Browse files
committed
Final attempt on 1-implement-and-rewrite-tests 
1 parent 0e99abc commit 801288b

File tree

8 files changed

+4641
-13
lines changed

8 files changed

+4641
-13
lines changed

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

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

1717
function getAngleType(angle) {
1818
// TODO: Implement this function
19+
if (angle <= 0 || angle > 360) {
20+
return "Invalid angle";
21+
}
22+
23+
if (angle < 90) {
24+
return "Acute angle";
25+
} else if (angle === 90) {
26+
return "Right angle";
27+
} else if (angle < 180) {
28+
return "Obtuse angle";
29+
} else if (angle === 180) {
30+
return "Straight angle";
31+
} else if (angle < 360) {
32+
return "Reflex angle";
33+
} else if (angle === 360) {
34+
35+
return "Full rotation";
36+
}
37+
38+
return "Invalid angle";
1939
}
2040

2141
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +55,12 @@ function assertEquals(actualOutput, targetOutput) {
3555
// Example: Identify Right Angles
3656
const right = getAngleType(90);
3757
assertEquals(right, "Right angle");
58+
59+
// Test cases
60+
assertEquals(getAngleType(45), "Acute angle");
61+
assertEquals(getAngleType(90), "Right angle");
62+
assertEquals(getAngleType(120), "Obtuse angle");
63+
assertEquals(getAngleType(180), "Straight angle");
64+
assertEquals(getAngleType(270), "Reflex angle");
65+
assertEquals(getAngleType(0), "Invalid angle");
66+
assertEquals(getAngleType(400), "Invalid angle");

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if (denominator === 0) {
16+
return false;
17+
}
18+
// Proper fractions must be positive
19+
return Math.abs(numerator) < Math.abs(denominator);
1520
}
1621

1722
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +36,19 @@ function assertEquals(actualOutput, targetOutput) {
3136

3237
// Example: 1/2 is a proper fraction
3338
assertEquals(isProperFraction(1, 2), true);
39+
40+
41+
assertEquals(isProperFraction(-1, 3), true); // Negative numerator
42+
assertEquals(isProperFraction(1, -3), true); // Negative denominator
43+
assertEquals(isProperFraction(0, 5), true); // Zero numerator
44+
45+
// Improper Fractions / Invalid
46+
assertEquals(isProperFraction(2, 1), false); // Numerator > Denominator
47+
assertEquals(isProperFraction(1, 1), false); // Numerator == Denominator
48+
assertEquals(isProperFraction(1, 0), false); // Zero denominator
49+
assertEquals(isProperFraction(5, 5), false); // Equal
50+
// --- ERROR CASES ---
51+
try {
52+
isProperFraction(1, 0);
53+
console.error("Error was not thrown for denominator = 0");
54+
} catch (e) { }

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

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,43 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
if (typeof card !== "string") {
27+
throw new Error("Invalid card: must be a string");
28+
}
29+
if (typeof card !== "string") {
30+
throw new Error("Invalid card: must be a string");
31+
}
32+
33+
// Valid suits
34+
const suits = ["♠", "♥", "♦", "♣"];
35+
36+
// Extract suit (last character)
37+
const suit = card.slice(-1);
38+
if (!suits.includes(suit)) {
39+
throw new Error("Invalid card: unknown suit");
40+
}
41+
42+
// Extract rank (everything except last character)
43+
const rank = card.slice(0, -1);
44+
45+
const rankValues = {
46+
A: 11,
47+
J: 10,
48+
Q: 10,
49+
K: 10
50+
};
51+
52+
if (rankValues[rank]) {
53+
return rankValues[rank];
54+
}
55+
56+
// Check numeric ranks 2–10
57+
const num = Number(rank);
58+
if (num >= 2 && num <= 10) {
59+
return num;
60+
}
61+
62+
throw new Error("Invalid card: unknown rank");
2663
}
2764

2865
// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,12 +78,69 @@ function assertEquals(actualOutput, targetOutput) {
4178
// Examples:
4279
assertEquals(getCardValue("9♠"), 9);
4380

81+
assertEquals(getCardValue("9♠"), 9);
82+
assertEquals(getCardValue("A♥"), 11);
83+
assertEquals(getCardValue("10♦"), 10);
84+
assertEquals(getCardValue("K♣"), 10);
4485
// Handling invalid cards
4586
try {
4687
getCardValue("invalid");
4788

4889
// This line will not be reached if an error is thrown as expected
4990
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
91+
} catch (e) { }
5192

5293
// What other invalid card cases can you think of?
94+
95+
try {
96+
getCardValue("A?");
97+
console.error("Error was not thrown for invalid suit");
98+
} catch (e) { }
99+
100+
// Invalid rank: too small
101+
try {
102+
getCardValue("1♠");
103+
console.error("Error was not thrown for invalid rank 1");
104+
} catch (e) { }
105+
106+
// Invalid rank: too large
107+
try {
108+
getCardValue("11♣");
109+
console.error("Error was not thrown for invalid rank 11");
110+
} catch (e) { }
111+
112+
// Missing suit
113+
try {
114+
getCardValue("A");
115+
console.error("Error was not thrown for missing suit");
116+
} catch (e) { }
117+
118+
// Missing rank
119+
try {
120+
getCardValue("♠");
121+
console.error("Error was not thrown for missing rank");
122+
} catch (e) { }
123+
124+
// Empty string
125+
try {
126+
getCardValue("");
127+
console.error("Error was not thrown for empty string");
128+
} catch (e) { }
129+
130+
// Non-string input
131+
try {
132+
getCardValue(42);
133+
console.error("Error was not thrown for non-string input");
134+
} catch (e) { }
135+
136+
// Rank with extra characters
137+
try {
138+
getCardValue("AAA♠");
139+
console.error("Error was not thrown for malformed rank");
140+
} catch (e) { }
141+
142+
// Suit in the wrong position
143+
try {
144+
getCardValue("♠A");
145+
console.error("Error was not thrown for reversed card format");
146+
} catch (e) { }

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

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,42 @@ const getAngleType = require("../implement/1-get-angle-type");
55
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
66
// including boundary and invalid cases.
77

8-
// Case 1: Acute angles
9-
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
10-
// 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");
14-
});
8+
describe("getAngleType", () => {
9+
// Case 1: Acute
10+
test('should return "Acute angle" when (0 < angle < 90)', () => {
11+
expect(getAngleType(1)).toBe("Acute angle");
12+
expect(getAngleType(45)).toBe("Acute angle");
13+
expect(getAngleType(89)).toBe("Acute angle");
14+
});
1515

16-
// Case 2: Right angle
17-
// Case 3: Obtuse angles
18-
// Case 4: Straight angle
19-
// Case 5: Reflex angles
20-
// Case 6: Invalid angles
16+
// Case 2: Right angle
17+
test('should return "Right angle" for exactly 90', () => {
18+
expect(getAngleType(90)).toBe("Right angle");
19+
});
20+
21+
// Case 3: Obtuse angles
22+
test('should return "Obtuse angle" when (90 < angle < 180)', () => {
23+
expect(getAngleType(91)).toBe("Obtuse angle");
24+
expect(getAngleType(135)).toBe("Obtuse angle");
25+
expect(getAngleType(179)).toBe("Obtuse angle");
26+
});
27+
28+
// Case 4: Straight angle
29+
test('should return "Straight angle" for exactly 180', () => {
30+
expect(getAngleType(180)).toBe("Straight angle");
31+
});
32+
33+
// Case 5: Reflex angles
34+
test('should return "Reflex angle" when (180 < angle < 360)', () => {
35+
expect(getAngleType(181)).toBe("Reflex angle");
36+
expect(getAngleType(270)).toBe("Reflex angle");
37+
expect(getAngleType(359)).toBe("Reflex angle");
38+
});
39+
40+
// Case 6: Invalid angles
41+
test('should return "Invalid angle" for values out of range', () => {
42+
expect(getAngleType(0)).toBe("Invalid angle");
43+
expect(getAngleType(-10)).toBe("Invalid angle");
44+
expect(getAngleType(361)).toBe("Invalid angle");
45+
});
46+
});

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,32 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
12+
describe("isProperFraction", () => {
13+
test("returns true for standard proper fractions", () => {
14+
expect(isProperFraction(1, 2)).toBe(true);
15+
expect(isProperFraction(2, 5)).toBe(true);
16+
});
17+
18+
test("returns true for negative proper fractions", () => {
19+
expect(isProperFraction(-1, 3)).toBe(true);
20+
expect(isProperFraction(1, -3)).toBe(true);
21+
expect(isProperFraction(-2, -5)).toBe(true);
22+
});
23+
24+
test("returns false for improper fractions (n >= d)", () => {
25+
expect(isProperFraction(3, 2)).toBe(false);
26+
expect(isProperFraction(1, 1)).toBe(false);
27+
expect(isProperFraction(-5, 2)).toBe(false);
28+
});
29+
30+
test("returns true when numerator is zero (if denominator is non-zero)", () => {
31+
expect(isProperFraction(0, 5)).toBe(true);
32+
expect(isProperFraction(0, -1)).toBe(true);
33+
});
34+
35+
test("returns false when denominator is zero", () => {
36+
expect(isProperFraction(1, 0)).toBe(false);
37+
expect(isProperFraction(0, 0)).toBe(false);
38+
});
39+
});

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,69 @@ test(`Should return 11 when given an ace card`, () => {
1818
// please refer to the Jest documentation:
1919
// https://jestjs.io/docs/expect#tothrowerror
2020

21+
// Case 1: Ace (A)
22+
//
23+
describe("Ace cards", () => {
24+
test("returns 11 for any Ace", () => {
25+
expect(getCardValue("A♠")).toBe(11);
26+
expect(getCardValue("A♥")).toBe(11);
27+
expect(getCardValue("A♦")).toBe(11);
28+
expect(getCardValue("A♣")).toBe(11);
29+
});
30+
});
31+
32+
//
33+
// Case 2: Number Cards (2–10)
34+
//
35+
describe("Number cards", () => {
36+
test("returns correct numeric values for 2–10", () => {
37+
expect(getCardValue("2♠")).toBe(2);
38+
expect(getCardValue("5♥")).toBe(5);
39+
expect(getCardValue("9♦")).toBe(9);
40+
expect(getCardValue("10♣")).toBe(10);
41+
});
42+
});
43+
44+
//
45+
// Case 3: Face Cards (J, Q, K)
46+
//
47+
describe("Face cards", () => {
48+
test("returns 10 for J, Q, K", () => {
49+
expect(getCardValue("J♠")).toBe(10);
50+
expect(getCardValue("Q♥")).toBe(10);
51+
expect(getCardValue("K♦")).toBe(10);
52+
});
53+
});
54+
55+
//
56+
// Case 4: Invalid Cards
57+
//
58+
describe("Invalid cards", () => {
59+
test("throws error for invalid rank", () => {
60+
expect(() => getCardValue("1♠")).toThrow();
61+
expect(() => getCardValue("11♣")).toThrow();
62+
expect(() => getCardValue("Z♦")).toThrow();
63+
});
64+
65+
test("throws error for invalid suit", () => {
66+
expect(() => getCardValue("A?")).toThrow();
67+
expect(() => getCardValue("10X")).toThrow();
68+
});
69+
70+
test("throws error for missing suit or rank", () => {
71+
expect(() => getCardValue("A")).toThrow();
72+
expect(() => getCardValue("♠")).toThrow();
73+
expect(() => getCardValue("")).toThrow();
74+
});
75+
76+
test("throws error for non-string input", () => {
77+
expect(() => getCardValue(123)).toThrow();
78+
expect(() => getCardValue(null)).toThrow();
79+
expect(() => getCardValue(undefined)).toThrow();
80+
});
81+
82+
test("throws error for malformed multi-character ranks", () => {
83+
expect(() => getCardValue("AA♠")).toThrow();
84+
expect(() => getCardValue("A10♣")).toThrow();
85+
});
86+
});

0 commit comments

Comments
 (0)