Skip to content

Commit 01ca25c

Browse files
Add Implement and Rewrite Test files only
1 parent 32407f9 commit 01ca25c

File tree

6 files changed

+216
-22
lines changed

6 files changed

+216
-22
lines changed

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
// execute the code to ensure all tests pass.
1616

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

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

29+
module.exports = getAngleType;
2530
// This helper function is written to make our assertions easier to read.
2631
// If the actual output matches the target output, the test will pass
2732
function assertEquals(actualOutput, targetOutput) {
@@ -31,7 +36,31 @@ function assertEquals(actualOutput, targetOutput) {
3136
);
3237
}
3338

34-
// TODO: Write tests to cover all cases, including boundary and invalid cases.
35-
// Example: Identify Right Angles
3639
const right = getAngleType(90);
3740
assertEquals(right, "Right angle");
41+
42+
const acute = getAngleType(45);
43+
assertEquals(acute, "Acute angle");
44+
45+
const obtuse = getAngleType(120);
46+
assertEquals(obtuse, "Obtuse angle");
47+
48+
const straight = getAngleType(180);
49+
assertEquals(straight, "Straight angle");
50+
51+
const reflex = getAngleType(270);
52+
assertEquals(reflex, "Reflex angle");
53+
54+
const invalid = getAngleType(-10);
55+
assertEquals(invalid, "Invalid angle");
56+
57+
const invalid2 = getAngleType(360);
58+
assertEquals(invalid2, "Invalid angle");
59+
60+
console.log(getAngleType(20));
61+
console.log(getAngleType(90));
62+
console.log(getAngleType(120));
63+
console.log(getAngleType(180));
64+
console.log(getAngleType(270));
65+
console.log(getAngleType(-10));
66+
console.log(getAngleType(360));

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
if (denominator === 0) return false; // A fraction with a zero denominator is not valid
15+
if (Math.abs(numerator) >= Math.abs(denominator)) return false; // Not a proper fraction
16+
return true; // Is a proper fraction
1517
}
1618

1719
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +33,15 @@ function assertEquals(actualOutput, targetOutput) {
3133

3234
// Example: 1/2 is a proper fraction
3335
assertEquals(isProperFraction(1, 2), true);
36+
assertEquals(isProperFraction(2, 2), false);
37+
assertEquals(isProperFraction(2, 1), false);
38+
assertEquals(isProperFraction(0, 2), true);
39+
assertEquals(isProperFraction(1, 0), false);
40+
assertEquals(isProperFraction(0, 0), false);
41+
42+
console.log(isProperFraction(1, 2));
43+
console.log(isProperFraction(2, 2));
44+
console.log(isProperFraction(2, 1));
45+
console.log(isProperFraction(0, 2));
46+
console.log(isProperFraction(1, 0));
47+
console.log(isProperFraction(0, 0));

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

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,40 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
26-
}
25+
if (typeof card !== "string" || card.length < 2) {
26+
throw new Error("Invalid card");
27+
}
28+
// Handle "10" which is like "10♥"
29+
if (card.startsWith("10")) {
30+
return 10;
31+
}
32+
//if more than 2 characters and not starting with a 10 card
33+
if (card.length > 2) {
34+
throw new Error("Invalid card");
35+
}
36+
37+
// remaining cards must be 2 characters long
38+
39+
const validSuits = ["♠", "♥", "♦", "♣"];
40+
const suit = card[card.length - 1];
41+
42+
const firstChar = card[0];
43+
44+
// check if picture cards
45+
if (firstChar === "A") return 11;
46+
if (firstChar === "J" || firstChar === "Q" || firstChar === "K") return 10;
47+
48+
// check if number is between 2 and 9, 10 has already been checked for and there should be no other valid cards
2749

50+
const num = Number(firstChar);
51+
52+
if (!isNaN(num) && num >= 2 && num <= 9) {
53+
return num;
54+
// for everything else
55+
} else {
56+
throw new Error("Invalid card");
57+
}
58+
}
2859
// The line below allows us to load the getCardValue function into tests in other files.
2960
// This will be useful in the "rewrite tests with jest" step.
3061
module.exports = getCardValue;
@@ -40,13 +71,52 @@ function assertEquals(actualOutput, targetOutput) {
4071
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4172
// Examples:
4273
assertEquals(getCardValue("9♠"), 9);
74+
assertEquals(getCardValue("10♥"), 10);
75+
assertEquals(getCardValue("J♥"), 10);
76+
assertEquals(getCardValue("A♠"), 11);
77+
assertEquals(getCardValue("Q♦"), 10);
78+
assertEquals(getCardValue("K♣"), 10);
4379

4480
// Handling invalid cards
81+
try {
82+
getCardValue("♠J");
83+
console.error("Error was not thrown for invalid card");
84+
} catch (e) {}
85+
4586
try {
4687
getCardValue("invalid");
88+
console.error("Error was not thrown for invalid card");
89+
} catch (e) {}
90+
91+
try {
92+
getCardValue("22");
93+
console.error("Error was not thrown for invalid card");
94+
} catch (e) {}
4795

48-
// This line will not be reached if an error is thrown as expected
96+
console.log(getCardValue("9♠"));
97+
console.log(getCardValue("10♥"));
98+
console.log(getCardValue("J♥"));
99+
console.log(getCardValue("A♠"));
100+
console.log(getCardValue("Q♦"));
101+
console.log(getCardValue("K♣"));
102+
103+
// This line will not be reached if an error is thrown as expected
104+
try {
49105
console.error("Error was not thrown for invalid card");
50106
} catch (e) {}
51107

52108
// What other invalid card cases can you think of?
109+
110+
// There could be cards with special characters.
111+
112+
// There could be cards with two numbers rather than a number and a suite
113+
// These will not be picked up because the code only checks for if starts with 10 or if the first character
114+
// is a number between 2 and 9, so cards like "22" would be valid because the first number
115+
// is 2, but the second character is not checked for validity. It is also 2 characters
116+
// so will not cause an error when the length is checked.
117+
118+
// Since the second character is not checked it could be 2D which is not a valid card but
119+
// would be accepted because the first character is 2 and the second character is not checked for validity
120+
121+
// When the card is checked if it begins with 10 it does check if it has a valid suite
122+
// as only the first 2 characters are checked so it could be 10DEVON or 10♥♥.

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,48 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getAngleType = require("../implement/1-get-angle-type");
44

5-
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
6-
// including boundary and invalid cases.
7-
85
// Case 1: Acute angles
96
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
107
// Test various acute angles, including boundary cases
118
expect(getAngleType(1)).toEqual("Acute angle");
129
expect(getAngleType(45)).toEqual("Acute angle");
1310
expect(getAngleType(89)).toEqual("Acute angle");
11+
expect(getAngleType(91)).toEqual("Obtuse angle");
1412
});
1513

1614
// Case 2: Right angle
15+
test(`should return "Right angle" when (angle === 90)`, () => {
16+
// Test various right angles, including boundary cases
17+
expect(getAngleType(90)).toEqual("Right angle");
18+
expect(getAngleType(89)).toEqual("Acute angle");
19+
expect(getAngleType(91)).toEqual("Obtuse angle");
20+
});
21+
1722
// Case 3: Obtuse angles
23+
test(`should return "Obtuse angle" when (angle > 90) && when (angle < 180)`, () => {
24+
// Test various obtuse angles, including boundary cases
25+
expect(getAngleType(91)).toEqual("Obtuse angle");
26+
expect(getAngleType(150)).toEqual("Obtuse angle");
27+
expect(getAngleType(189)).toEqual("Reflex angle");
28+
});
29+
1830
// Case 4: Straight angle
31+
test(`should return "Straight angle" when (angle === 180)`, () => {
32+
// Test various straight angles, including boundary cases
33+
expect(getAngleType(180)).toEqual("Straight angle");
34+
expect(getAngleType(179)).toEqual("Obtuse angle");
35+
expect(getAngleType(181)).toEqual("Reflex angle");
36+
});
37+
1938
// Case 5: Reflex angles
39+
test(`should return "Reflex angle" when (angle > 180)`, () => {
40+
expect(getAngleType(181)).toEqual("Reflex angle");
41+
expect(getAngleType(270)).toEqual("Reflex angle");
42+
expect(getAngleType(359)).toEqual("Reflex angle");
43+
});
44+
2045
// Case 6: Invalid angles
46+
test(`should return "Invalid angle" when (angle < 0 || angle >= 360)`, () => {
47+
expect(getAngleType(-1)).toEqual("Invalid angle");
48+
expect(getAngleType(360)).toEqual("Invalid angle");
49+
});

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,27 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const isProperFraction = require("../implement/2-is-proper-fraction");
44

5-
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
6-
75
// Special case: numerator is zero
86
test(`should return false when denominator is zero`, () => {
97
expect(isProperFraction(1, 0)).toEqual(false);
108
});
9+
10+
test(`should return false when denominator is zero`, () => {
11+
expect(isProperFraction(1, 2)).toEqual(true);
12+
});
13+
14+
test(`should return false when denominator is zero`, () => {
15+
expect(isProperFraction(2, 2)).toEqual(false);
16+
});
17+
18+
test(`should return false when denominator is zero`, () => {
19+
expect(isProperFraction(2, 1)).toEqual(false);
20+
});
21+
22+
test(`should return false when denominator is zero`, () => {
23+
expect(isProperFraction(0, 0)).toEqual(false);
24+
});
25+
26+
test(`should return false when denominator is zero`, () => {
27+
expect(isProperFraction(0, 1)).toEqual(true);
28+
});
Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,54 @@
11
// This statement loads the getCardValue function you wrote in the implement directory.
22
// We will use the same function, but write tests for it using Jest in this file.
3+
const { createTestScheduler } = require("jest");
34
const getCardValue = require("../implement/3-get-card-value");
45

5-
// TODO: Write tests in Jest syntax to cover all possible outcomes.
6-
76
// Case 1: Ace (A)
87
test(`Should return 11 when given an ace card`, () => {
98
expect(getCardValue("A♠")).toEqual(11);
109
});
1110

12-
// Suggestion: Group the remaining test data into these categories:
13-
// Number Cards (2-10)
14-
// Face Cards (J, Q, K)
15-
// Invalid Cards
11+
// Case 2: Face Cards (J, Q, K)
12+
test(`Should return 10 when given a Jack card`, () => {
13+
expect(getCardValue("J♥")).toEqual(10);
14+
});
1615

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
16+
test(`Should return 10 when given a Queen card`, () => {
17+
expect(getCardValue("Q♦")).toEqual(10);
18+
});
19+
20+
test(`Should return 10 when given a King card`, () => {
21+
expect(getCardValue("K♣")).toEqual(10);
22+
});
23+
24+
// Case 3: Number Cards (2-10)
25+
test(`Should return 2 when given a 2 card`, () => {
26+
expect(getCardValue("2♠")).toEqual(2);
27+
});
28+
29+
test(`Should return 10 when given a 10 card`, () => {
30+
expect(getCardValue("10♥")).toEqual(10);
31+
});
32+
33+
// Case 4: Invalid Cards
34+
test(`Should throw an error when given an invalid card`, () => {
35+
expect(() => getCardValue("♠J")).toThrow();
36+
});
37+
38+
test(`Should throw an error when given an invalid card`, () => {
39+
expect(() => getCardValue("invalid")).toThrow();
40+
});
41+
42+
test(`Should throw an error when given an invalid card`, () => {
43+
expect(() => getCardValue("12♠")).toThrow();
44+
});
45+
46+
test(`Should throw an error when given an invalid card`, () => {
47+
expect(() => getCardValue("1")).toThrow();
48+
});
2049

50+
// when I tested with test(`Should throw an error when given an invalid card`, () => {
51+
// expect(() => getCardValue("22")).toThrow(); it did not throw an error because 22 passes the
52+
// test of first number between and 9 and being 2 characters long,
53+
// but it is not a valid card because the second character is not a valid suite.
54+
// The second character is not checked.

0 commit comments

Comments
 (0)