Skip to content

Commit 2a5a3e9

Browse files
committed
test: add assertions and edge case tests for getCardValue function
1 parent 8ebeba7 commit 2a5a3e9

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

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

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,24 @@ function getCardValue(card) {
2525
//check the length and if does not match the valid length throw an error
2626
if (card.length < 2 || card.length > 3) {
2727
throw new Error("Invalid card");
28-
} "♠";
28+
}
2929
const suits = ["♠", "♥", "♦", "♣"];
3030
const suit = card[card.length - 1];
3131

3232
let suitValid = false;
3333

34-
for(let i = 0; i < suits.length; i++){
35-
if(suit === suits[i]){
34+
for (let i = 0; i < suits.length; i++) {
35+
if (suit === suits[i]) {
3636
suitValid = true;
3737
break;
3838
}
3939
}
4040
// after comparing each suit in our array and does not match throw an error
41-
if(suitValid === false){
41+
if (suitValid === false) {
4242
throw new Error("Invalid card suit");
4343
}
4444

45-
46-
let rank = card.substring(0, card.length -1);
47-
console.log(rank);
45+
let rank = card.substring(0, card.length - 1);
4846

4947
if (rank === "A") {
5048
return 11;
@@ -54,47 +52,65 @@ function getCardValue(card) {
5452
}
5553

5654
let number = Number(rank);
57-
55+
5856
if (isNaN(number) || number < 2 || number > 10) {
5957
throw new Error("Invalid card rank");
6058
}
6159

6260
return number;
6361
}
6462

65-
66-
67-
68-
6963
// The line below allows us to load the getCardValue function into tests in other files.
7064
// This will be useful in the "rewrite tests with jest" step.
7165
module.exports = getCardValue;
7266

7367
// Helper functions to make our assertions easier to read.
7468
function assertEquals(actualOutput, targetOutput) {
75-
console.assert(
76-
actualOutput === targetOutput,
77-
`Expected ${actualOutput} to equal ${targetOutput}`
78-
);
69+
if (actualOutput !== targetOutput) {
70+
console.error(
71+
`❌ FAILED: Expected ${targetOutput} but received ${actualOutput}`
72+
);
73+
} else {
74+
console.log(`✅ PASSED: Value is ${actualOutput} as expected`);
75+
}
76+
}
77+
78+
function assertThrows(invalidCard) {
79+
try {
80+
getCardValue(invalidCard);
81+
console.error(
82+
`❌ FAILED: "${invalidCard}" should have thrown an error, but it didn't`
83+
);
84+
} catch (e) {
85+
console.log(`✅ PASSED: Error catch para "${invalidCard}" [${e.message}]`);
86+
}
7987
}
8088

8189
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
8290
// Examples:
83-
assertEquals(getCardValue("2♠"), 2), true;
84-
assertEquals(getCardValue("d"), d), false;
85-
// assertEquals(getCardValue("10♥"), 10);
86-
87-
88-
89-
91+
console.log("--- Running Success Tests ---");
92+
assertEquals(getCardValue("2♠"), 2);
93+
assertEquals(getCardValue("10♥"), 10);
94+
assertEquals(getCardValue("A♣"), 11);
95+
assertEquals(getCardValue("K♦"), 10);
96+
assertEquals(getCardValue("J♠"), 10);
97+
assertEquals(getCardValue("Q♥"), 10);
98+
assertEquals(getCardValue("7♦"), 7);
99+
100+
console.log("\n--- Running Edge Case Tests ---");
101+
assertThrows("d");
102+
assertThrows("15♥");
103+
assertThrows("A");
104+
assertThrows("JPP");
105+
assertThrows("Q🌸");
90106

91107
// Handling invalid cards
92108
try {
93109
getCardValue("invalid");
94110

95111
// This line will not be reached if an error is thrown as expected
96112
console.error("Error was not thrown for invalid card");
97-
} catch (e) {
113+
} catch (e) {
98114
console.log("✓ Error thrown as expected for invalid card");
99115
}
100116

0 commit comments

Comments
 (0)