Skip to content

Commit 516640d

Browse files
Sprint 3 implement and rewrite tests
1 parent d15f64e commit 516640d

File tree

8 files changed

+90
-104
lines changed

8 files changed

+90
-104
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ to choose test values that thoroughly test a function.
55

66
## 1 Implement solutions
77

8-
98
Here is a recommended order:
109

1110
1. `1-get-angle-type.js`In the `implement` directory you've got a number of functions you'll need to implement.
12-
For each function, you also have a number of different cases you'll need to check for your function.
11+
For each function, you also have a number of different cases you'll need to check for your function.
1312

1413
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.
1514

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

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

1717
function getAngleType(angle) {
18-
if(angle > 0 && angle <90)
19-
{
20-
return 'Acute angle';
18+
if (angle > 0 && angle < 90) {
19+
return "Acute angle";
20+
} else if (angle === 90) {
21+
return "Right angle";
22+
} else if (angle > 90 && angle < 180) {
23+
return "Obtuse angle";
24+
} else if (angle === 180) {
25+
return "Straight angle";
26+
} else if (angle > 180 && angle < 360) {
27+
return "Reflex angle";
28+
} else {
29+
return "Invalid angle";
2130
}
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-
}
4231
}
4332

4433
// The line below allows us to load the getAngleType function into tests in other files.
@@ -50,7 +39,7 @@ module.exports = getAngleType;
5039
function assertEquals(actualOutput, targetOutput) {
5140
console.assert(
5241
actualOutput === targetOutput,
53-
`Expected ${actualOutput} to equal ${targetOutput}`
42+
`Expected ${actualOutput} to equal ${targetOutput}`,
5443
);
5544
}
5645

@@ -60,9 +49,9 @@ let invalid = getAngleType(0);
6049
assertEquals(invalid, "Invalid angle");
6150
let acute = getAngleType(1);
6251
assertEquals(acute, "Acute angle");
63-
acute = getAngleType(89);
52+
acute = getAngleType(89);
6453
assertEquals(acute, "Acute angle");
65-
let right= getAngleType(90);
54+
let right = getAngleType(90);
6655
assertEquals(right, "Right angle");
6756
let obtuse = getAngleType(91);
6857
assertEquals(obtuse, "Obtuse angle");
@@ -74,6 +63,5 @@ let reflex = getAngleType(181);
7463
assertEquals(reflex, "Reflex angle");
7564
reflex = getAngleType(359);
7665
assertEquals(reflex, "Reflex angle");
77-
invalid = getAngleType(400);
66+
invalid = getAngleType(400);
7867
assertEquals(invalid, "Invalid angle");
79-

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

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

1313
function isProperFraction(numerator, denominator) {
14-
if (denominator === 0){
14+
if (denominator === 0) {
1515
return false;
16-
}
17-
else if (numerator < denominator)
18-
{
16+
} else if (numerator < denominator) {
1917
return true;
20-
21-
}
22-
else
23-
{
18+
} else {
2419
return false;
2520
}
2621
}
@@ -33,7 +28,7 @@ module.exports = isProperFraction;
3328
function assertEquals(actualOutput, targetOutput) {
3429
console.assert(
3530
actualOutput === targetOutput,
36-
`Expected ${actualOutput} to equal ${targetOutput}`
31+
`Expected ${actualOutput} to equal ${targetOutput}`,
3732
);
3833
}
3934

@@ -48,4 +43,4 @@ assertEquals(isProperFraction(0, 5), true);
4843
assertEquals(isProperFraction(1, 0), false);
4944
assertEquals(isProperFraction(-3, 2), true);
5045
assertEquals(isProperFraction(2, -5), false);
51-
assertEquals(isProperFraction(-2, -3), false);
46+
assertEquals(isProperFraction(-2, -3), false);

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

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

2424
function getCardValue(card) {
25-
if(typeof card !== "string" || card.length<2)
26-
{
25+
if (typeof card !== "string" || card.length < 2) {
2726
throw new Error("Invalid card");
2827
}
2928
const suit = card.slice(-1);
3029
const rank = card.slice(0, -1);
3130

3231
const validSuits = ["♠", "♥", "♦", "♣"];
33-
const validRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"];
32+
const validRanks = [
33+
"A",
34+
"2",
35+
"3",
36+
"4",
37+
"5",
38+
"6",
39+
"7",
40+
"8",
41+
"9",
42+
"10",
43+
"J",
44+
"Q",
45+
"K",
46+
];
3447

35-
if(!validSuits.includes(suit) || !validRanks.includes(rank))
36-
{
48+
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
3749
throw new Error("Invalid card");
38-
}
39-
else if (rank == "A")
40-
{
50+
} else if (rank == "A") {
4151
return 11;
52+
} else if (["J", "Q", "K"].includes(rank)) {
53+
return 10;
54+
} else {
55+
return Number(rank);
4256
}
43-
else if (["J","Q","K"].includes(rank))
44-
{
45-
return 10
46-
}
47-
else {
48-
return Number(rank);
49-
}
5057
}
5158

5259
// The line below allows us to load the getCardValue function into tests in other files.
@@ -57,15 +64,15 @@ module.exports = getCardValue;
5764
function assertEquals(actualOutput, targetOutput) {
5865
console.assert(
5966
actualOutput === targetOutput,
60-
`Expected ${actualOutput} to equal ${targetOutput}`
67+
`Expected ${actualOutput} to equal ${targetOutput}`,
6168
);
6269
}
6370

6471
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
6572
// Examples:
6673
assertEquals(getCardValue("9♠"), 9);
6774
assertEquals(getCardValue("A♠"), 11);
68-
assertEquals(getCardValue("K♦"), 10);
75+
assertEquals(getCardValue("K♦"), 10);
6976
assertEquals(getCardValue("10♥"), 10);
7077

7178
// Handling invalid cards
@@ -75,29 +82,26 @@ try {
7582
} catch (e) {}
7683
try {
7784
getCardValue("A?");
78-
console.error("Error was not thrown for invalid card");
85+
console.error("Error was not thrown for invalid card");
7986
} catch (e) {}
8087
try {
8188
getCardValue("A");
8289

83-
8490
console.error("Error was not thrown for invalid card");
8591
} catch (e) {}
8692
try {
8793
getCardValue("10");
8894

89-
9095
console.error("Error was not thrown for invalid card");
9196
} catch (e) {}
9297

9398
// What other invalid card cases can you think of?
9499
//try {
95-
//getCardValue("AA");
100+
//getCardValue("AA");
96101
//console.error("Error was not thrown for invalid card");
97102
//} catch (e) {}
98103
//try {
99-
//getCardValue("1♠");
104+
//getCardValue("1♠");
100105

101-
102-
//console.error("Error was not thrown for invalid card");
103-
//} catch (e) {}
106+
//console.error("Error was not thrown for invalid card");
107+
//} catch (e) {}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@ test(`should return "Right angle" when (angle = 90)`, () => {
1616
// Test various acute angles, including boundary cases
1717
expect(getAngleType(90)).toEqual("Right angle");
1818
});
19-
test(`should return"Obtuse angle" when (90 < angle< 180)`, ()=>{
19+
test(`should return"Obtuse angle" when (90 < angle< 180)`, () => {
2020
expect(getAngleType(91)).toEqual("Obtuse angle");
2121
expect(getAngleType(179)).toEqual("Obtuse angle");
2222
});
23-
test(`should return "Straight angle" when ( angle = 180)`,()=>{
23+
test(`should return "Straight angle" when ( angle = 180)`, () => {
2424
expect(getAngleType(180)).toEqual("Straight angle");
2525
});
26-
test(`should return "Reflex angle" when (180 < angle <360)`, ()=>{
26+
test(`should return "Reflex angle" when (180 < angle <360)`, () => {
2727
expect(getAngleType(181)).toEqual("Reflex angle");
2828
expect(getAngleType(359)).toEqual("Reflex angle");
29-
3029
});
31-
test(`should return "invalid angle when angles outside the valid range`, ()=>{
32-
expect(getAngleType(0)).toEqual("Invalid angle")
33-
})
30+
test(`should return "invalid angle when angles outside the valid range`, () => {
31+
expect(getAngleType(0)).toEqual("Invalid angle");
32+
});
3433

3534
// Case 2: Right angle
3635
// Case 3: Obtuse angles

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +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`, ()=>{
11+
test(`Should return true when numerator is zero`, () => {
1212
expect(isProperFraction(0, 5)).toEqual(true);
1313
});
14-
test(`should return true when denominator is greater than numerator`, () =>{
15-
expect(isProperFraction(1, 2)).toEqual(true);
14+
test(`should return true when denominator is greater than numerator`, () => {
15+
expect(isProperFraction(1, 2)).toEqual(true);
1616
});
17-
test(`should return false when numerator is greater than denominator`, ()=> {
17+
test(`should return false when numerator is greater than denominator`, () => {
1818
expect(isProperFraction(5, 4)).toEqual(false);
1919
});
20-
test(`should return false when numerator is equals to denominator`, ()=>{
21-
expect(isProperFraction(3, 3)).toEqual(false);
20+
test(`should return false when numerator is equals to denominator`, () => {
21+
expect(isProperFraction(3, 3)).toEqual(false);
2222
});
23-
test(`should return true when denominator is greater than negative numerator`, ()=>{
23+
test(`should return true when denominator is greater than negative numerator`, () => {
2424
expect(isProperFraction(-3, 2)).toEqual(true);
2525
});
26-
test(`should return false when numerator is greater than denominator`, ()=> {
26+
test(`should return false when numerator is greater than denominator`, () => {
2727
expect(isProperFraction(2, -5)).toEqual(false);
2828
});
29-
test(`should return false when negative numerator is greater than negative denominator`, ()=> {
29+
test(`should return false when negative numerator is greater than negative denominator`, () => {
3030
expect(isProperFraction(-2, -3)).toEqual(false);
3131
});

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +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-
test(`should return 9 when given a 9♠ card`,()=>{
11+
test(`should return 9 when given a 9♠ card`, () => {
1212
expect(getCardValue("9♠")).toEqual(9);
1313
});
14-
test (`should return 10 when given a face card K♦`, ()=>{
14+
test(`should return 10 when given a face card K♦`, () => {
1515
expect(getCardValue("K♦")).toEqual(10);
1616
});
17-
test(`should return 10 when given a 10`, ()=>{
17+
test(`should return 10 when given a 10`, () => {
1818
expect(getCardValue("10♥")).toEqual(10);
1919
});
20-
test(`should throw error for invalid Ranks`, ()=>{
21-
expect(()=> getCardValue("11♠")).toThrow();
20+
test(`should throw error for invalid Ranks`, () => {
21+
expect(() => getCardValue("11♠")).toThrow();
2222
});
23-
test(`should throw error for invalid Suits`, ()=>{
24-
expect(()=> getCardValue("A?")).toThrow();
23+
test(`should throw error for invalid Suits`, () => {
24+
expect(() => getCardValue("A?")).toThrow();
2525
});
26-
test(`should throw error for missing Suits`,()=>{
27-
expect(()=> getCardValue("A")).toThrow();
26+
test(`should throw error for missing Suits`, () => {
27+
expect(() => getCardValue("A")).toThrow();
2828
});
29-
test(`should throw error for non String input`, ()=>{
30-
expect(()=> getCardValue("10")).toThrow();
29+
test(`should throw error for non String input`, () => {
30+
expect(() => getCardValue("10")).toThrow();
3131
});
3232
// Suggestion: Group the remaining test data into these categories:
3333
// Number Cards (2-10)
@@ -37,4 +37,3 @@ test(`should throw error for non String input`, ()=>{
3737
// To learn how to test whether a function throws an error as expected in Jest,
3838
// please refer to the Jest documentation:
3939
// https://jestjs.io/docs/expect#tothrowerror
40-

0 commit comments

Comments
 (0)