Skip to content

Commit d15f64e

Browse files
Sprint 3 Implement and Rewrite Tests
1 parent 3372770 commit d15f64e

File tree

7 files changed

+184
-13
lines changed

7 files changed

+184
-13
lines changed

Sprint-3/1-implement-and-rewrite-tests/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ to choose test values that thoroughly test a function.
55

66
## 1 Implement solutions
77

8-
In the `implement` directory you've got a number of functions you'll need to implement.
8+
9+
Here is a recommended order:
10+
11+
1. `1-get-angle-type.js`In the `implement` directory you've got a number of functions you'll need to implement.
912
For each function, you also have a number of different cases you'll need to check for your function.
1013

1114
Write your assertions and build up your program case by case. Don't rush to a solution. The point of these assignments is to learn how to write assertions and build up a program step by step.
1215

13-
Here is a recommended order:
14-
15-
1. `1-get-angle-type.js`
1616
2. `2-is-proper-fraction.js`
1717
3. `3-get-card-value.js`
1818

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

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

1717
function getAngleType(angle) {
18-
// TODO: Implement this function
18+
if(angle > 0 && angle <90)
19+
{
20+
return 'Acute angle';
21+
}
22+
else if(angle === 90){
23+
return 'Right angle'
24+
}
25+
else if(angle > 90 && angle < 180)
26+
{
27+
return "Obtuse angle"
28+
}
29+
else if(angle === 180)
30+
{
31+
return "Straight angle" ;
32+
}
33+
else if( angle > 180 && angle < 360)
34+
{
35+
return "Reflex angle" ;
36+
}
37+
else
38+
39+
{
40+
return "Invalid angle"
41+
}
1942
}
2043

2144
// The line below allows us to load the getAngleType function into tests in other files.
@@ -33,5 +56,24 @@ function assertEquals(actualOutput, targetOutput) {
3356

3457
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3558
// Example: Identify Right Angles
36-
const right = getAngleType(90);
59+
let invalid = getAngleType(0);
60+
assertEquals(invalid, "Invalid angle");
61+
let acute = getAngleType(1);
62+
assertEquals(acute, "Acute angle");
63+
acute = getAngleType(89);
64+
assertEquals(acute, "Acute angle");
65+
let right= getAngleType(90);
3766
assertEquals(right, "Right angle");
67+
let obtuse = getAngleType(91);
68+
assertEquals(obtuse, "Obtuse angle");
69+
obtuse = getAngleType(179);
70+
assertEquals(obtuse, "Obtuse angle");
71+
const straight = getAngleType(180);
72+
assertEquals(straight, "Straight angle");
73+
let reflex = getAngleType(181);
74+
assertEquals(reflex, "Reflex angle");
75+
reflex = getAngleType(359);
76+
assertEquals(reflex, "Reflex angle");
77+
invalid = getAngleType(400);
78+
assertEquals(invalid, "Invalid angle");
79+

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

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

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
if (denominator === 0){
15+
return false;
16+
}
17+
else if (numerator < denominator)
18+
{
19+
return true;
20+
21+
}
22+
else
23+
{
24+
return false;
25+
}
1526
}
1627

1728
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +42,10 @@ function assertEquals(actualOutput, targetOutput) {
3142

3243
// Example: 1/2 is a proper fraction
3344
assertEquals(isProperFraction(1, 2), true);
45+
assertEquals(isProperFraction(5, 4), false);
46+
assertEquals(isProperFraction(3, 3), false);
47+
assertEquals(isProperFraction(0, 5), true);
48+
assertEquals(isProperFraction(1, 0), false);
49+
assertEquals(isProperFraction(-3, 2), true);
50+
assertEquals(isProperFraction(2, -5), false);
51+
assertEquals(isProperFraction(-2, -3), false);

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,37 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
if(typeof card !== "string" || card.length<2)
26+
{
27+
throw new Error("Invalid card");
28+
}
29+
const suit = card.slice(-1);
30+
const rank = card.slice(0, -1);
31+
32+
const validSuits = ["♠", "♥", "♦", "♣"];
33+
const validRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"];
34+
35+
if(!validSuits.includes(suit) || !validRanks.includes(rank))
36+
{
37+
throw new Error("Invalid card");
38+
}
39+
else if (rank == "A")
40+
{
41+
return 11;
42+
}
43+
else if (["J","Q","K"].includes(rank))
44+
{
45+
return 10
46+
}
47+
else {
48+
return Number(rank);
49+
}
2650
}
2751

2852
// The line below allows us to load the getCardValue function into tests in other files.
2953
// This will be useful in the "rewrite tests with jest" step.
54+
//module.exports = getCardValue;
3055
module.exports = getCardValue;
31-
3256
// Helper functions to make our assertions easier to read.
3357
function assertEquals(actualOutput, targetOutput) {
3458
console.assert(
@@ -40,13 +64,40 @@ function assertEquals(actualOutput, targetOutput) {
4064
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4165
// Examples:
4266
assertEquals(getCardValue("9♠"), 9);
67+
assertEquals(getCardValue("A♠"), 11);
68+
assertEquals(getCardValue("K♦"), 10);
69+
assertEquals(getCardValue("10♥"), 10);
4370

4471
// Handling invalid cards
4572
try {
46-
getCardValue("invalid");
73+
getCardValue("11♠");
74+
console.error("Error was not thrown for invalid card");
75+
} catch (e) {}
76+
try {
77+
getCardValue("A?");
78+
console.error("Error was not thrown for invalid card");
79+
} catch (e) {}
80+
try {
81+
getCardValue("A");
4782

48-
// This line will not be reached if an error is thrown as expected
83+
84+
console.error("Error was not thrown for invalid card");
85+
} catch (e) {}
86+
try {
87+
getCardValue("10");
88+
89+
4990
console.error("Error was not thrown for invalid card");
5091
} catch (e) {}
5192

5293
// What other invalid card cases can you think of?
94+
//try {
95+
//getCardValue("AA");
96+
//console.error("Error was not thrown for invalid card");
97+
//} catch (e) {}
98+
//try {
99+
//getCardValue("1♠");
100+
101+
102+
//console.error("Error was not thrown for invalid card");
103+
//} catch (e) {}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,28 @@ const getAngleType = require("../implement/1-get-angle-type");
99
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1010
// Test various acute angles, including boundary cases
1111
expect(getAngleType(1)).toEqual("Acute angle");
12-
expect(getAngleType(45)).toEqual("Acute angle");
12+
//expect(getAngleType(45)).toEqual("Acute angle");
1313
expect(getAngleType(89)).toEqual("Acute angle");
1414
});
15+
test(`should return "Right angle" when (angle = 90)`, () => {
16+
// Test various acute angles, including boundary cases
17+
expect(getAngleType(90)).toEqual("Right angle");
18+
});
19+
test(`should return"Obtuse angle" when (90 < angle< 180)`, ()=>{
20+
expect(getAngleType(91)).toEqual("Obtuse angle");
21+
expect(getAngleType(179)).toEqual("Obtuse angle");
22+
});
23+
test(`should return "Straight angle" when ( angle = 180)`,()=>{
24+
expect(getAngleType(180)).toEqual("Straight angle");
25+
});
26+
test(`should return "Reflex angle" when (180 < angle <360)`, ()=>{
27+
expect(getAngleType(181)).toEqual("Reflex angle");
28+
expect(getAngleType(359)).toEqual("Reflex angle");
29+
30+
});
31+
test(`should return "invalid angle when angles outside the valid range`, ()=>{
32+
expect(getAngleType(0)).toEqual("Invalid angle")
33+
})
1534

1635
// Case 2: Right angle
1736
// Case 3: Obtuse angles

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,24 @@ 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+
test(`Should return true when numerator is zero`, ()=>{
12+
expect(isProperFraction(0, 5)).toEqual(true);
13+
});
14+
test(`should return true when denominator is greater than numerator`, () =>{
15+
expect(isProperFraction(1, 2)).toEqual(true);
16+
});
17+
test(`should return false when numerator is greater than denominator`, ()=> {
18+
expect(isProperFraction(5, 4)).toEqual(false);
19+
});
20+
test(`should return false when numerator is equals to denominator`, ()=>{
21+
expect(isProperFraction(3, 3)).toEqual(false);
22+
});
23+
test(`should return true when denominator is greater than negative numerator`, ()=>{
24+
expect(isProperFraction(-3, 2)).toEqual(true);
25+
});
26+
test(`should return false when numerator is greater than denominator`, ()=> {
27+
expect(isProperFraction(2, -5)).toEqual(false);
28+
});
29+
test(`should return false when negative numerator is greater than negative denominator`, ()=> {
30+
expect(isProperFraction(-2, -3)).toEqual(false);
31+
});

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,27 @@ 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+
test(`should return 9 when given a 9♠ card`,()=>{
12+
expect(getCardValue("9♠")).toEqual(9);
13+
});
14+
test (`should return 10 when given a face card K♦`, ()=>{
15+
expect(getCardValue("K♦")).toEqual(10);
16+
});
17+
test(`should return 10 when given a 10`, ()=>{
18+
expect(getCardValue("10♥")).toEqual(10);
19+
});
20+
test(`should throw error for invalid Ranks`, ()=>{
21+
expect(()=> getCardValue("11♠")).toThrow();
22+
});
23+
test(`should throw error for invalid Suits`, ()=>{
24+
expect(()=> getCardValue("A?")).toThrow();
25+
});
26+
test(`should throw error for missing Suits`,()=>{
27+
expect(()=> getCardValue("A")).toThrow();
28+
});
29+
test(`should throw error for non String input`, ()=>{
30+
expect(()=> getCardValue("10")).toThrow();
31+
});
1232
// Suggestion: Group the remaining test data into these categories:
1333
// Number Cards (2-10)
1434
// Face Cards (J, Q, K)

0 commit comments

Comments
 (0)