Skip to content

Commit c64bb45

Browse files
authored
Added a Testing Guide and updated the specification in some JS files (#907)
1 parent 24151e5 commit c64bb45

File tree

11 files changed

+234
-191
lines changed

11 files changed

+234
-191
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Implement solutions and rewrite tests with Jest
22

3+
Before writing any code, please read the [Testing Function Guide](testing-guide.md) to learn how
4+
to choose test values that thoroughly test a function.
5+
36
## 1 Implement solutions
47

58
In the `implement` directory you've got a number of functions you'll need to implement.
Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,37 @@
11
// Implement a function getAngleType
2-
// Build up your function case by case, writing tests as you go
3-
// The first test and case is written for you. The next case has a test, but no code.
4-
// Execute this script in your terminal
5-
// node 1-get-angle-type.js
6-
// The assertion error will tell you what the expected output is
7-
// Write the code to pass the test
8-
// Then, write the next test! :) Go through this process until all the cases are implemented
2+
//
3+
// When given an angle in degrees, it should return a string indicating the type of angle:
4+
// - "Acute angle" for angles greater than 0° and less than 90°
5+
// - "Right angle" for exactly 90°
6+
// - "Obtuse angle" for angles greater than 90° and less than 180°
7+
// - "Straight angle" for exactly 180°
8+
// - "Reflex angle" for angles greater than 180° and less than 360°
9+
// - "Invalid angle" for angles outside the valid range.
10+
11+
// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)
12+
13+
// Acceptance criteria:
14+
// After you have implemented the function, write tests to cover all the cases, and
15+
// execute the code to ensure all tests pass.
916

1017
function getAngleType(angle) {
11-
if (angle === 90) {
12-
return "Right angle";
13-
}
14-
// Run the tests, work out what Case 2 is testing, and implement the required code here.
15-
// Then keep going for the other cases, one at a time.
18+
// TODO: Implement this function
1619
}
1720

1821
// The line below allows us to load the getAngleType function into tests in other files.
1922
// This will be useful in the "rewrite tests with jest" step.
2023
module.exports = getAngleType;
2124

22-
// we're going to use this helper function to make our assertions easier to read
23-
// if the actual output matches the target output, the test will pass
25+
// This helper function is written to make our assertions easier to read.
26+
// If the actual output matches the target output, the test will pass
2427
function assertEquals(actualOutput, targetOutput) {
2528
console.assert(
2629
actualOutput === targetOutput,
2730
`Expected ${actualOutput} to equal ${targetOutput}`
2831
);
2932
}
3033

31-
// Acceptance criteria:
32-
33-
// Given an angle in degrees,
34-
// When the function getAngleType is called with this angle,
35-
// Then it should:
36-
37-
// Case 1: Identify Right Angles:
38-
// When the angle is exactly 90 degrees,
39-
// Then the function should return "Right angle"
34+
// TODO: Write tests to cover all cases, including boundary and invalid cases.
35+
// Example: Identify Right Angles
4036
const right = getAngleType(90);
4137
assertEquals(right, "Right angle");
42-
43-
// Case 2: Identify Acute Angles:
44-
// When the angle is less than 90 degrees,
45-
// Then the function should return "Acute angle"
46-
const acute = getAngleType(45);
47-
assertEquals(acute, "Acute angle");
48-
49-
// Case 3: Identify Obtuse Angles:
50-
// When the angle is greater than 90 degrees and less than 180 degrees,
51-
// Then the function should return "Obtuse angle"
52-
const obtuse = getAngleType(120);
53-
// ====> write your test here, and then add a line to pass the test in the function above
54-
55-
// Case 4: Identify Straight Angles:
56-
// When the angle is exactly 180 degrees,
57-
// Then the function should return "Straight angle"
58-
// ====> write your test here, and then add a line to pass the test in the function above
59-
60-
// Case 5: Identify Reflex Angles:
61-
// When the angle is greater than 180 degrees and less than 360 degrees,
62-
// Then the function should return "Reflex angle"
63-
// ====> write your test here, and then add a line to pass the test in the function above
Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,33 @@
1-
// Implement a function isProperFraction
2-
// Write assertions for your function to check it works in different cases
3-
// Terms:
4-
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j
5-
// Written here like this: 1/2 == Numerator/Denominator
6-
// the first test and first case is written for you
7-
// complete the rest of the tests and cases
8-
// write one test at a time, and make it pass, build your solution up methodically
1+
// Implement a function isProperFraction,
2+
// when given two numbers, a numerator and a denominator, it should return true if
3+
// the given numbers form a proper fraction, and false otherwise.
4+
5+
// Assumption: The parameters are valid numbers (not NaN or Infinity).
6+
7+
// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
8+
9+
// Acceptance criteria:
10+
// After you have implemented the function, write tests to cover all the cases, and
11+
// execute the code to ensure all tests pass.
912

1013
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) {
12-
return true;
13-
}
14+
// TODO: Implement this function
1415
}
1516

1617
// The line below allows us to load the isProperFraction function into tests in other files.
1718
// This will be useful in the "rewrite tests with jest" step.
1819
module.exports = isProperFraction;
1920

20-
// here's our helper again
21+
// Here's our helper again
2122
function assertEquals(actualOutput, targetOutput) {
2223
console.assert(
2324
actualOutput === targetOutput,
2425
`Expected ${actualOutput} to equal ${targetOutput}`
2526
);
2627
}
2728

28-
// Acceptance criteria:
29+
// TODO: Write tests to cover all cases.
30+
// What combinations of numerators and denominators should you test?
2931

30-
// Proper Fraction check:
31-
// Input: numerator = 2, denominator = 3
32-
// target output: true
33-
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
34-
const properFraction = isProperFraction(2, 3);
35-
assertEquals(properFraction, true);
36-
37-
// Improper Fraction check:
38-
// Input: numerator = 5, denominator = 2
39-
// target output: false
40-
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
41-
const improperFraction = isProperFraction(5, 2);
42-
assertEquals(improperFraction, false);
43-
44-
// Negative Fraction check:
45-
// Input: numerator = -4, denominator = 7
46-
// target output: true
47-
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
48-
const negativeFraction = isProperFraction(-4, 7);
49-
// ====> complete with your assertion
50-
51-
// Equal Numerator and Denominator check:
52-
// Input: numerator = 3, denominator = 3
53-
// target output: false
54-
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
55-
const equalFraction = isProperFraction(3, 3);
56-
// ====> complete with your assertion
57-
58-
// Stretch:
59-
// What other scenarios could you test for?
32+
// Example: 1/2 is a proper fraction
33+
assertEquals(isProperFraction(1, 2), true);
Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,52 @@
11
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
22

3-
// You will need to implement a function getCardValue
4-
// the function takes a single parameter, a string representing a playing card
5-
// the function should return the numerical value of the card
6-
// the first test and first case is written for you
7-
// complete the rest of the tests and cases
8-
// write one test at a time, and make it pass, build your solution up methodically
9-
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
3+
// Implement a function getCardValue, when given a string representing a playing card,
4+
// should return the numerical value of the card.
5+
6+
// A valid card string will contain a rank followed by the suit.
7+
// The rank can be one of the following strings:
8+
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
9+
// The suit can be one of the following emojis:
10+
// "♠", "♥", "♦", "♣"
11+
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
12+
13+
// When the card is an ace ("A"), the function should return 11.
14+
// When the card is a face card ("J", "Q", "K"), the function should return 10.
15+
// When the card is a number card ("2" to "10"), the function should return its numeric value.
16+
17+
// When the card string is invalid (not following the above format), the function should
18+
// throw an error.
19+
20+
// Acceptance criteria:
21+
// After you have implemented the function, write tests to cover all the cases, and
22+
// execute the code to ensure all tests pass.
23+
1024
function getCardValue(card) {
11-
if (rank === "A") {
12-
return 11;
13-
}
25+
// TODO: Implement this function
1426
}
1527

1628
// The line below allows us to load the getCardValue function into tests in other files.
1729
// This will be useful in the "rewrite tests with jest" step.
1830
module.exports = getCardValue;
1931

20-
// You need to write assertions for your function to check it works in different cases
21-
// we're going to use this helper function to make our assertions easier to read
22-
// if the actual output matches the target output, the test will pass
32+
// Helper functions to make our assertions easier to read.
2333
function assertEquals(actualOutput, targetOutput) {
2434
console.assert(
2535
actualOutput === targetOutput,
2636
`Expected ${actualOutput} to equal ${targetOutput}`
2737
);
2838
}
29-
// Acceptance criteria:
3039

31-
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
32-
// When the function getCardValue is called with this card string as input,
33-
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
36-
37-
// Handle Number Cards (2-10):
38-
// Given a card with a rank between "2" and "9",
39-
// When the function is called with such a card,
40-
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
41-
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
43-
44-
// Handle Face Cards (J, Q, K):
45-
// Given a card with a rank of "10," "J," "Q," or "K",
46-
// When the function is called with such a card,
47-
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
48-
49-
// Handle Ace (A):
50-
// Given a card with a rank of "A",
51-
// When the function is called with an Ace,
52-
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
53-
54-
// Handle Invalid Cards:
55-
// Given a card with an invalid rank (neither a number nor a recognized face card),
56-
// When the function is called with such a card,
57-
// Then it should throw an error indicating "Invalid card rank."
40+
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41+
// Examples:
42+
assertEquals(getCardValue("9♠"), 9);
43+
44+
// Handling invalid cards
45+
try {
46+
getCardValue("invalid");
47+
48+
// This line will not be reached if an error is thrown as expected
49+
console.error("Error was not thrown for invalid card");
50+
} catch (e) {}
51+
52+
// What other invalid card cases can you think of?

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@
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-
test("should identify right angle (90°)", () => {
6-
expect(getAngleType(90)).toEqual("Right angle");
7-
});
8-
9-
// REPLACE the comments with the tests
10-
// make your test descriptions as clear and readable as possible
11-
12-
// Case 2: Identify Acute Angles:
13-
// When the angle is less than 90 degrees,
14-
// Then the function should return "Acute angle"
5+
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
6+
// including boundary and invalid cases.
157

16-
// Case 3: Identify Obtuse Angles:
17-
// When the angle is greater than 90 degrees and less than 180 degrees,
18-
// Then the function should return "Obtuse angle"
19-
20-
// Case 4: Identify Straight Angles:
21-
// When the angle is exactly 180 degrees,
22-
// Then the function should return "Straight angle"
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+
});
2315

24-
// Case 5: Identify Reflex Angles:
25-
// When the angle is greater than 180 degrees and less than 360 degrees,
26-
// Then the function should return "Reflex angle"
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

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
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-
test("should return true for a proper fraction", () => {
6-
expect(isProperFraction(2, 3)).toEqual(true);
7-
});
8-
9-
// Case 2: Identify Improper Fractions:
5+
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
106

11-
// Case 3: Identify Negative Fractions:
12-
13-
// Case 4: Identify Equal Numerator and Denominator:
7+
// Special case: numerator is zero
8+
test(`should return false when denominator is zero`, () => {
9+
expect(isProperFraction(1, 0)).toEqual(false);
10+
});

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getCardValue = require("../implement/3-get-card-value");
44

5-
test("should return 11 for Ace of Spades", () => {
6-
const aceofSpades = getCardValue("A♠");
7-
expect(aceofSpades).toEqual(11);
5+
// TODO: Write tests in Jest syntax to cover all possible outcomes.
6+
7+
// Case 1: Ace (A)
8+
test(`Should return 11 when given an ace card`, () => {
9+
expect(getCardValue("A♠")).toEqual(11);
810
});
911

10-
// Case 2: Handle Number Cards (2-10):
11-
// Case 3: Handle Face Cards (J, Q, K):
12-
// Case 4: Handle Ace (A):
13-
// Case 5: Handle Invalid Cards:
12+
// Suggestion: Group the remaining test data into these categories:
13+
// Number Cards (2-10)
14+
// Face Cards (J, Q, K)
15+
// Invalid Cards
16+
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
20+

0 commit comments

Comments
 (0)