Skip to content

Commit e18a262

Browse files
Sprint 3 implement and rewrite tests
1 parent 49fe3af commit e18a262

File tree

8 files changed

+187
-31
lines changed

8 files changed

+187
-31
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ 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.
9-
For each function, you also have a number of different cases you'll need to check for your function.
8+
Here is a recommended order:
109

11-
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.
10+
1. `1-get-angle-type.js`In the `implement` directory you've got a number of functions you'll need to implement.
11+
For each function, you also have a number of different cases you'll need to check for your function.
1212

13-
Here is a recommended order:
13+
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.
1414

15-
1. `1-get-angle-type.js`
1615
2. `2-is-proper-fraction.js`
1716
3. `3-get-card-value.js`
1817

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
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+
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";
30+
}
1931
}
2032

2133
// The line below allows us to load the getAngleType function into tests in other files.
@@ -27,11 +39,29 @@ module.exports = getAngleType;
2739
function assertEquals(actualOutput, targetOutput) {
2840
console.assert(
2941
actualOutput === targetOutput,
30-
`Expected ${actualOutput} to equal ${targetOutput}`
42+
`Expected ${actualOutput} to equal ${targetOutput}`,
3143
);
3244
}
3345

3446
// TODO: Write tests to cover all cases, including boundary and invalid cases.
3547
// Example: Identify Right Angles
36-
const right = getAngleType(90);
48+
let invalid = getAngleType(0);
49+
assertEquals(invalid, "Invalid angle");
50+
let acute = getAngleType(1);
51+
assertEquals(acute, "Acute angle");
52+
acute = getAngleType(89);
53+
assertEquals(acute, "Acute angle");
54+
let right = getAngleType(90);
3755
assertEquals(right, "Right angle");
56+
let obtuse = getAngleType(91);
57+
assertEquals(obtuse, "Obtuse angle");
58+
obtuse = getAngleType(179);
59+
assertEquals(obtuse, "Obtuse angle");
60+
const straight = getAngleType(180);
61+
assertEquals(straight, "Straight angle");
62+
let reflex = getAngleType(181);
63+
assertEquals(reflex, "Reflex angle");
64+
reflex = getAngleType(359);
65+
assertEquals(reflex, "Reflex angle");
66+
invalid = getAngleType(400);
67+
assertEquals(invalid, "Invalid angle");

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
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+
return Math.abs(numerator) < Math.abs(denominator);
18+
1519
}
1620

1721
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -22,7 +26,7 @@ module.exports = isProperFraction;
2226
function assertEquals(actualOutput, targetOutput) {
2327
console.assert(
2428
actualOutput === targetOutput,
25-
`Expected ${actualOutput} to equal ${targetOutput}`
29+
`Expected ${actualOutput} to equal ${targetOutput}`,
2630
);
2731
}
2832

@@ -31,3 +35,10 @@ function assertEquals(actualOutput, targetOutput) {
3135

3236
// Example: 1/2 is a proper fraction
3337
assertEquals(isProperFraction(1, 2), true);
38+
assertEquals(isProperFraction(5, 4), false);
39+
assertEquals(isProperFraction(3, 3), false);
40+
assertEquals(isProperFraction(0, 5), true);
41+
assertEquals(isProperFraction(1, 0), false);
42+
assertEquals(isProperFraction(-1, 2), true);
43+
assertEquals(isProperFraction(1, -2), true);
44+
assertEquals(isProperFraction(-1, -2), true);

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

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,86 @@
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+
throw new Error("Invalid card");
27+
}
28+
const suit = card.slice(-1);
29+
const rank = card.slice(0, -1);
30+
31+
const validSuits = ["♠", "♥", "♦", "♣"];
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+
];
47+
48+
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
49+
throw new Error("Invalid card");
50+
} else if (rank == "A") {
51+
return 11;
52+
} else if (["J", "Q", "K"].includes(rank)) {
53+
return 10;
54+
} else {
55+
return Number(rank);
56+
}
2657
}
2758

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.
61+
//module.exports = getCardValue;
3062
module.exports = getCardValue;
31-
3263
// Helper functions to make our assertions easier to read.
3364
function assertEquals(actualOutput, targetOutput) {
3465
console.assert(
3566
actualOutput === targetOutput,
36-
`Expected ${actualOutput} to equal ${targetOutput}`
67+
`Expected ${actualOutput} to equal ${targetOutput}`,
3768
);
3869
}
3970

4071
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4172
// Examples:
4273
assertEquals(getCardValue("9♠"), 9);
74+
assertEquals(getCardValue("A♠"), 11);
75+
assertEquals(getCardValue("K♦"), 10);
76+
assertEquals(getCardValue("10♥"), 10);
4377

4478
// Handling invalid cards
4579
try {
46-
getCardValue("invalid");
80+
getCardValue("11♠");
81+
console.error("Error was not thrown for invalid card");
82+
} catch (e) {}
83+
try {
84+
getCardValue("A?");
85+
console.error("Error was not thrown for invalid card");
86+
} catch (e) {}
87+
try {
88+
getCardValue("A");
89+
90+
console.error("Error was not thrown for invalid card");
91+
} catch (e) {}
92+
try {
93+
getCardValue(10);
4794

48-
// This line will not be reached if an error is thrown as expected
4995
console.error("Error was not thrown for invalid card");
5096
} catch (e) {}
5197

5298
// What other invalid card cases can you think of?
99+
//try {
100+
//getCardValue("AA");
101+
//console.error("Error was not thrown for invalid card");
102+
//} catch (e) {}
103+
//try {
104+
//getCardValue("1♠");
105+
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,27 @@ 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");
1312
expect(getAngleType(89)).toEqual("Acute angle");
1413
});
14+
test(`should return "Right angle" when (angle = 90)`, () => {
15+
expect(getAngleType(90)).toEqual("Right angle");
16+
});
17+
test(`should return"Obtuse angle" when (90 < angle< 180)`, () => {
18+
expect(getAngleType(91)).toEqual("Obtuse angle");
19+
expect(getAngleType(179)).toEqual("Obtuse angle");
20+
});
21+
test(`should return "Straight angle" when ( angle = 180)`, () => {
22+
expect(getAngleType(180)).toEqual("Straight angle");
23+
});
24+
test(`should return "Reflex angle" when (180 < angle <360)`, () => {
25+
expect(getAngleType(181)).toEqual("Reflex angle");
26+
expect(getAngleType(359)).toEqual("Reflex angle");
27+
});
28+
test(`should return "invalid angle when angles outside the valid range`, () => {
29+
expect(getAngleType(0)).toEqual("Invalid angle");
30+
expect(getAngleType(360)).toEqual("Invalid angle");
31+
expect(getAngleType(-10)).toEqual("Invalid angle");
32+
});
1533

1634
// Case 2: Right angle
1735
// Case 3: Obtuse angles

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@ 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.
66

7-
// Special case: numerator is zero
7+
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 abs(numerator) < abs(denominator)", () => {
24+
expect(isProperFraction(-1, 2)).toEqual(true);
25+
expect(isProperFraction(1, -2)).toEqual(true);
26+
expect(isProperFraction(-1, -2)).toEqual(true);
27+
});

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,37 @@ 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-
12-
// Suggestion: Group the remaining test data into these categories:
1311
// Number Cards (2-10)
12+
test("should return the value of number cards (2–10)", () => {
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+
});
1418
// 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+
});
1524
// Invalid Cards
25+
test("should throw error for invalid ranks", () => {
26+
expect(() => getCardValue("11♠")).toThrow();
27+
});
28+
29+
test("should throw error for invalid suits", () => {
30+
expect(() => getCardValue("A?")).toThrow();
31+
});
32+
33+
test("should throw error for missing suits", () => {
34+
expect(() => getCardValue("A")).toThrow();
35+
});
36+
37+
test("should throw error for non-string input", () => {
38+
expect(() => getCardValue(10)).toThrow();
39+
});
1640

1741
// To learn how to test whether a function throws an error as expected in Jest,
1842
// please refer to the Jest documentation:
1943
// https://jestjs.io/docs/expect#tothrowerror
20-
44+

Sprint-3/1-implement-and-rewrite-tests/testing-guide.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ Input ──▶ Function ──▶ Output
77
```
88

99
A function
10+
1011
- Takes **input** (via **arguments**)
1112
- Does some work
1213
- Produces **one output** (via a **return value**)
13-
14+
1415
Example:
1516

1617
```
@@ -19,17 +20,18 @@ sum(2, 3) → 5
1920

2021
Important idea: the same input should produce the same output.
2122

22-
2323
## 2. Testing Means Predicting
2424

25-
Testing means:
26-
> If I give this input, what output should I get?
25+
Testing means:
2726

27+
> If I give this input, what output should I get?
2828
2929
## 3. Choosing Good Test Values
3030

3131
### Step 1: Determining the space of possible inputs
32+
3233
Ask:
34+
3335
- What type of value is expected?
3436
- What values make sense?
3537
- If they are numbers:
@@ -39,7 +41,7 @@ Ask:
3941
- What are their length and patterns?
4042
- What values would not make sense?
4143

42-
### Step 2: Choosing Good Test Values
44+
### Step 2: Choosing Good Test Values
4345

4446
#### Normal Cases
4547

@@ -48,10 +50,9 @@ These confirm that the function works in normal use.
4850
- What does a typical, ordinary input look like?
4951
- Are there multiple ordinary groups of inputs? e.g. for an age checking function, maybe there are "adults" and "children" as expected ordinary groups of inputs.
5052

51-
5253
#### Boundary Cases
5354

54-
Test values exactly at, just inside, and just outside defined ranges.
55+
Test values exactly at, just inside, and just outside defined ranges.
5556
These values are where logic breaks most often.
5657

5758
#### Consider All Outcomes
@@ -64,6 +65,7 @@ Every outcome must be reached by at least one test.
6465
#### Crossing the Edges and Invalid Values
6566

6667
This tests how the function behaves when assumptions are violated.
68+
6769
- What happens when input is outside of the expected range?
6870
- What happens when input is not of the expected type?
6971
- What happens when input is not in the expected format?
@@ -73,18 +75,18 @@ This tests how the function behaves when assumptions are violated.
7375
### 1. Using `console.assert()`
7476

7577
```javascript
76-
// Report a failure only when the first argument is false
77-
console.assert( sum(4, 6) === 10, "Expected 4 + 6 to equal 10" );
78+
// Report a failure only when the first argument is false
79+
console.assert(sum(4, 6) === 10, "Expected 4 + 6 to equal 10");
7880
```
7981

8082
It is simpler than using `if-else` and requires no setup.
81-
83+
8284
### 2. Jest Testing Framework
8385

8486
```javascript
8587
test("Should correctly return the sum of two positive numbers", () => {
8688
expect( sum(4, 6) ).toEqual(10);
87-
... // Can test multiple samples
89+
... // Can test multiple samples
8890
});
8991

9092
```

0 commit comments

Comments
 (0)