Skip to content

Commit 2a6c5ad

Browse files
committed
Test: complete jest tests for all functions
1 parent 8f3d6cf commit 2a6c5ad

File tree

12 files changed

+306
-25
lines changed

12 files changed

+306
-25
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ assertEquals(acute, "Acute angle");
5050
// When the angle is greater than 90 degrees and less than 180 degrees,
5151
// Then the function should return "Obtuse angle"
5252
const obtuse = getAngleType(120);
53-
// ====> write your test here, and then add a line to pass the test in the function above
53+
assertEquals(obtuse, "Obtuse angle");
5454

5555
// Case 4: Identify Straight Angles:
5656
// When the angle is exactly 180 degrees,
5757
// 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
58+
const straight = getAngleType(180);
59+
assertEquals(straight, "Straight angle");
5960

6061
// Case 5: Identify Reflex Angles:
6162
// When the angle is greater than 180 degrees and less than 360 degrees,
6263
// 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
64+
const reflex = getAngleType(270);
65+
assertEquals(reflex, "Reflex angle");

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,31 @@ assertEquals(improperFraction, false);
4646
// target output: true
4747
// 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.
4848
const negativeFraction = isProperFraction(-4, 7);
49-
// ====> complete with your assertion
49+
assertEquals(negativeFraction, true);
5050

5151
// Equal Numerator and Denominator check:
5252
// Input: numerator = 3, denominator = 3
5353
// target output: false
5454
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
5555
const equalFraction = isProperFraction(3, 3);
56-
// ====> complete with your assertion
56+
assertEquals(equalFraction, false);
5757

5858
// Stretch:
5959
// What other scenarios could you test for?
60+
61+
62+
// // Stretch Scenarios:
63+
64+
// Stretch 1: Zero in numerator
65+
// Input: numerator = 0, denominator = 5
66+
// target output: true
67+
// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). Any fraction with zero numerator is proper (except when denominator is also zero).
68+
const zeroNumerator = isProperFraction(0, 5);
69+
assertEquals(zeroNumerator, true);
70+
71+
// Stretch 2: Negative denominator
72+
// Input: numerator = 2, denominator = -5
73+
// target output: false
74+
// Explanation: The fraction 2/-5 equals -0.4, but when comparing 2 < -5, this is false. The function compares the actual values, not absolute values.
75+
const negativeDenominator = isProperFraction(2, -5);
76+
assertEquals(negativeDenominator, false);

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,23 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11+
// Handle "10" case first since it has two characters
12+
if (card.startsWith("10")) {
13+
return 10;
14+
}
15+
16+
const rank = card[0]; // Extract the rank from the card string
1117
if (rank === "A") {
1218
return 11;
1319
}
20+
if (rank >= "2" && rank <= "9") {
21+
return parseInt(rank);
22+
}
23+
if (rank === "J" || rank === "Q" || rank === "K") {
24+
return 10;
25+
}
26+
27+
throw new Error("Invalid card rank.");
1428
}
1529

1630
// The line below allows us to load the getCardValue function into tests in other files.
@@ -26,6 +40,7 @@ function assertEquals(actualOutput, targetOutput) {
2640
`Expected ${actualOutput} to equal ${targetOutput}`
2741
);
2842
}
43+
2944
// Acceptance criteria:
3045

3146
// 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),
@@ -39,19 +54,38 @@ assertEquals(aceofSpades, 11);
3954
// When the function is called with such a card,
4055
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4156
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
57+
assertEquals(fiveofHearts, 5);
4358

4459
// Handle Face Cards (J, Q, K):
4560
// Given a card with a rank of "10," "J," "Q," or "K",
4661
// When the function is called with such a card,
4762
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
63+
const jackOfDiamonds = getCardValue("J♦");
64+
assertEquals(jackOfDiamonds, 10);
65+
66+
const queenOfHearts = getCardValue("Q♥");
67+
assertEquals(queenOfHearts, 10);
68+
69+
const kingOfClubs = getCardValue("K♣");
70+
assertEquals(kingOfClubs, 10);
71+
72+
const tenOfClubs = getCardValue("10♣");
73+
assertEquals(tenOfClubs, 10);
4874

4975
// Handle Ace (A):
5076
// Given a card with a rank of "A",
5177
// When the function is called with an Ace,
5278
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
79+
const aceOfDiamonds = getCardValue("A♦");
80+
assertEquals(aceOfDiamonds, 11);
5381

5482
// Handle Invalid Cards:
5583
// Given a card with an invalid rank (neither a number nor a recognized face card),
5684
// When the function is called with such a card,
5785
// Then it should throw an error indicating "Invalid card rank."
86+
try {
87+
getCardValue("X♠");
88+
console.assert(false, "Expected an error for invalid card");
89+
} catch (error) {
90+
console.assert(error.message === "Invalid card rank.", "Expected invalid card rank error");
91+
}
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
// This statement loads the getAngleType 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 getAngleType = require("../implement/1-get-angle-type");
3+
const getAngleType = require('../implement/1-get-angle-type');
44

5-
test("should identify right angle (90°)", () => {
5+
test("should identify right angle (90)", () => {
66
expect(getAngleType(90)).toEqual("Right angle");
77
});
88

9-
// REPLACE the comments with the tests
10-
// make your test descriptions as clear and readable as possible
11-
129
// Case 2: Identify Acute Angles:
1310
// When the angle is less than 90 degrees,
1411
// Then the function should return "Acute angle"
12+
test("should identify acute angles (less than 90 degrees)", () => {
13+
expect(getAngleType(45)).toEqual("Acute angle");
14+
expect(getAngleType(30)).toEqual("Acute angle");
15+
expect(getAngleType(89)).toEqual("Acute angle");
16+
});
1517

1618
// Case 3: Identify Obtuse Angles:
1719
// When the angle is greater than 90 degrees and less than 180 degrees,
1820
// Then the function should return "Obtuse angle"
21+
test("should identify obtuse angles (between 90 and 180 degrees)", () => {
22+
expect(getAngleType(120)).toEqual("Obtuse angle");
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(179)).toEqual("Obtuse angle");
25+
});
1926

2027
// Case 4: Identify Straight Angles:
2128
// When the angle is exactly 180 degrees,
2229
// Then the function should return "Straight angle"
30+
test("should identify straight angle (180 degrees)", () => {
31+
expect(getAngleType(180)).toEqual("Straight angle");
32+
});
2333

2434
// Case 5: Identify Reflex Angles:
2535
// When the angle is greater than 180 degrees and less than 360 degrees,
2636
// Then the function should return "Reflex angle"
37+
test("should identify reflex angles (between 180 and 360 degrees)", () => {
38+
expect(getAngleType(200)).toEqual("Reflex angle");
39+
expect(getAngleType(181)).toEqual("Reflex angle");
40+
expect(getAngleType(359)).toEqual("Reflex angle");
41+
});

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ test("should return true for a proper fraction", () => {
77
});
88

99
// Case 2: Identify Improper Fractions:
10+
test("should return false for improper fractions", () => {
11+
expect(isProperFraction(5, 2)).toEqual(false);
12+
expect(isProperFraction(10, 5)).toEqual(false);
13+
});
1014

1115
// Case 3: Identify Negative Fractions:
16+
test("should handle negative fractions correctly", () => {
17+
expect(isProperFraction(-4, 7)).toEqual(true);
18+
expect(isProperFraction(2, -5)).toEqual(false);
19+
expect(isProperFraction(-2, -5)).toEqual(false);
20+
});
1221

1322
// Case 4: Identify Equal Numerator and Denominator:
23+
test("should return false when numerator equals denominator", () => {
24+
expect(isProperFraction(3, 3)).toEqual(false);
25+
expect(isProperFraction(7, 7)).toEqual(false);
26+
});

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ test("should return 11 for Ace of Spades", () => {
88
});
99

1010
// Case 2: Handle Number Cards (2-10):
11+
test("should return numeric value for number cards (2-9)", () => {
12+
expect(getCardValue('5♥')).toEqual(5);
13+
expect(getCardValue('2♦')).toEqual(2);
14+
expect(getCardValue('9♣')).toEqual(9);
15+
});
16+
1117
// Case 3: Handle Face Cards (J, Q, K):
18+
test("should return 10 for face cards (J, Q, K)", () => {
19+
expect(getCardValue('J♦')).toEqual(10);
20+
expect(getCardValue('Q♥')).toEqual(10);
21+
expect(getCardValue('K♣')).toEqual(10);
22+
});
23+
1224
// Case 4: Handle Ace (A):
25+
test("should return 11 for all Aces", () => {
26+
expect(getCardValue('A♦')).toEqual(11);
27+
expect(getCardValue('A♥')).toEqual(11);
28+
expect(getCardValue('A♣')).toEqual(11);
29+
});
30+
1331
// Case 5: Handle Invalid Cards:
32+
test("should throw error for invalid card ranks", () => {
33+
expect(() => getCardValue('X♠')).toThrow('Invalid card rank.');
34+
expect(() => getCardValue('1♥')).toThrow('Invalid card rank.');
35+
});
Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,66 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
2-
const countChar = require("./count");
2+
const countChar = require('./count');
3+
34
// Given a string str and a single character char to search for,
45
// When the countChar function is called with these inputs,
56
// Then it should:
67

7-
// Scenario: Multiple Occurrences
8+
// Scenario: Multiple occurrences
89
// Given the input string str,
910
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
1011
// When the function is called with these inputs,
1112
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').
12-
1313
test("should count multiple occurrences of a character", () => {
1414
const str = "aaaaa";
1515
const char = "a";
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
1818
});
1919

20-
// Scenario: No Occurrences
20+
// Scenario: No occurrences
2121
// Given the input string str,
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
test("should return 0 when character is not found", () => {
26+
const str = "hello world";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
32+
// Scenario: Case sensitivity
33+
// Given the input string str with mixed case,
34+
// And a character char with specific case,
35+
// When the function is called with these inputs,
36+
// Then it should only count occurrences that match the case exactly.
37+
test("should be case sensitive when counting characters", () => {
38+
const str = "Hello World";
39+
const char = "h";
40+
const count = countChar(str, char);
41+
expect(count).toEqual(0); // 'h' is not the same as 'H'
42+
});
43+
44+
// Scenario: Single occurrence
45+
// Given the input string str,
46+
// And a character char that appears exactly once,
47+
// When the function is called with these inputs,
48+
// Then it should return 1.
49+
test("should count single occurrence correctly", () => {
50+
const str = "hello";
51+
const char = "e";
52+
const count = countChar(str, char);
53+
expect(count).toEqual(1);
54+
});
55+
56+
// Scenario: Empty string
57+
// Given an empty input string str,
58+
// And any character char,
59+
// When the function is called with these inputs,
60+
// Then it should return 0.
61+
test("should return 0 for empty string", () => {
62+
const str = "";
63+
const char = "a";
64+
const count = countChar(str, char);
65+
expect(count).toEqual(0);
66+
});
Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,74 @@
1-
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber
1+
const getOrdinalNumber = require('./get-ordinal-number');
32

3+
// In this week's prep, we started implementing getOrdinalNumber
44
// continue testing and implementing getOrdinalNumber for additional cases
55
// Write your tests using Jest - remember to run your tests often for continual feedback
66

77
// Case 1: Identify the ordinal number for 1
88
// When the number is 1,
99
// Then the function should return "1st"
10-
1110
test("should return '1st' for 1", () => {
1211
expect(getOrdinalNumber(1)).toEqual("1st");
1312
});
13+
14+
// Case 2: Identify the ordinal number for 2
15+
// When the number is 2,
16+
// Then the function should return "2nd"
17+
test("should return '2nd' for 2", () => {
18+
expect(getOrdinalNumber(2)).toEqual("2nd");
19+
});
20+
21+
// Case 3: Identify the ordinal number for 3
22+
// When the number is 3,
23+
// Then the function should return "3rd"
24+
test("should return '3rd' for 3", () => {
25+
expect(getOrdinalNumber(3)).toEqual("3rd");
26+
});
27+
28+
// Case 4: Identify the ordinal number for numbers ending with 1 (except 11)
29+
// When the number ends with 1 but is not 11,
30+
// Then the function should return "[number]st"
31+
test("should return correct ordinal for numbers ending with 1 (except 11)", () => {
32+
expect(getOrdinalNumber(21)).toEqual("21st");
33+
expect(getOrdinalNumber(31)).toEqual("31st");
34+
expect(getOrdinalNumber(41)).toEqual("41st");
35+
});
36+
37+
// Case 5: Identify the ordinal number for numbers ending with 2 (except 12)
38+
// When the number ends with 2 but is not 12,
39+
// Then the function should return "[number]nd"
40+
test("should return correct ordinal for numbers ending with 2 (except 12)", () => {
41+
expect(getOrdinalNumber(22)).toEqual("22nd");
42+
expect(getOrdinalNumber(32)).toEqual("32nd");
43+
expect(getOrdinalNumber(42)).toEqual("42nd");
44+
});
45+
46+
// Case 6: Identify the ordinal number for numbers ending with 3 (except 13)
47+
// When the number ends with 3 but is not 13,
48+
// Then the function should return "[number]rd"
49+
test("should return correct ordinal for numbers ending with 3 (except 13)", () => {
50+
expect(getOrdinalNumber(23)).toEqual("23rd");
51+
expect(getOrdinalNumber(33)).toEqual("33rd");
52+
expect(getOrdinalNumber(43)).toEqual("43rd");
53+
});
54+
55+
// Case 7: Identify the ordinal number for numbers ending with 11, 12, 13
56+
// When the number is 11, 12, or 13,
57+
// Then the function should return "[number]th"
58+
test("should return 'th' for numbers 11, 12, 13", () => {
59+
expect(getOrdinalNumber(11)).toEqual("11th");
60+
expect(getOrdinalNumber(12)).toEqual("12th");
61+
expect(getOrdinalNumber(13)).toEqual("13th");
62+
});
63+
64+
// Case 8: Identify the ordinal number for other numbers
65+
// When the number doesn't end with 1, 2, or 3 (or is 11, 12, 13),
66+
// Then the function should return "[number]th"
67+
test("should return 'th' for other numbers", () => {
68+
expect(getOrdinalNumber(4)).toEqual("4th");
69+
expect(getOrdinalNumber(5)).toEqual("5th");
70+
expect(getOrdinalNumber(10)).toEqual("10th");
71+
expect(getOrdinalNumber(15)).toEqual("15th");
72+
expect(getOrdinalNumber(20)).toEqual("20th");
73+
expect(getOrdinalNumber(100)).toEqual("100th");
74+
});

0 commit comments

Comments
 (0)