-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | 26-ITP-Jan | Ebrahim Moqbel | Sprint 3 | Implement and Rewrite Tests #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
9eed6df
49b88ad
3c92b48
bfb2769
34ae0f2
5237b54
e75c8ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,8 +23,27 @@ | |
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| let rank = card.slice(0,-1); | ||
| let cardFace = card[card.length - 1]; | ||
|
|
||
| if (!["♠", "♥", "♦", "♣"].includes(cardFace)) { | ||
| throw new Error(`Invalid card face: ${cardFace}`); | ||
| } | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
| if (+rank >= 2 && +rank <= 9) { | ||
| return +rank; | ||
| } | ||
| if (["K", "10", "Q", "J"].includes(rank)) { | ||
| return 10; | ||
| } | ||
| if (!["♠", "♥", "♦", "♣"].includes(cardFace)) { | ||
|
Ebrahim-Moqbel marked this conversation as resolved.
|
||
| } | ||
| throw new Error(`Invalid card rank: ${rank}`); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you confirm that the error messages in the implementation match what the tests expect?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you favourO. I have matched the function message thron error to the tests expectation. |
||
| } | ||
|
|
||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = getCardValue; | ||
|
|
@@ -39,14 +58,61 @@ function assertEquals(actualOutput, targetOutput) { | |
|
|
||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| // (Ace All suits) | ||
| const aceOfSpades = getCardValue("A♠"); | ||
| assertEquals(aceOfSpades, 11); | ||
|
|
||
| const aceOfHearts = getCardValue("A♥"); | ||
| assertEquals(aceOfHearts, 11); | ||
|
|
||
| const aceOfDiamonds = getCardValue("A♦"); | ||
| assertEquals(aceOfDiamonds, 11); | ||
|
|
||
| const aceOfClubs = getCardValue("A♣"); | ||
| assertEquals(aceOfClubs, 11); | ||
|
|
||
| // (Face cards) | ||
| const jackOfHearts = getCardValue("J♥"); | ||
| assertEquals(jackOfHearts, 10); | ||
|
|
||
| const queenOfClubs = getCardValue("Q♣"); | ||
| assertEquals(queenOfClubs, 10); | ||
|
|
||
| const kingOfSpades = getCardValue("K♠"); | ||
| assertEquals(kingOfSpades, 10); | ||
|
|
||
| // (Number cards) | ||
| const threeOfDiamonds = getCardValue("3♦"); | ||
| assertEquals(threeOfDiamonds, 3); | ||
|
|
||
| const sevenOfClubs = getCardValue("7♣"); | ||
| assertEquals(sevenOfClubs, 7); | ||
|
|
||
|
|
||
|
|
||
| // Handling invalid cards | ||
| // giving an invalid rank (a number or an recognized face card) | ||
| // When the function is called with such a card, | ||
| // Then it should throw an error indicating "Invalid card rank." | ||
| try { | ||
| getCardValue("invalid"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
| } catch (error) {} | ||
|
|
||
| // What other invalid card cases can you think of? | ||
|
|
||
| try { | ||
| getCardValue("1♠"); | ||
| console.error("Error was not thrown for invalid card rank"); | ||
| } catch (error) { | ||
| assertEquals(error.message, "Invalid card rank"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("5X"); | ||
| console.error("Error was not thrown for invalid card suit"); | ||
| } catch (error) { | ||
| assertEquals(error.message, "Invalid card suit"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,27 @@ test(`Should return 11 when given an ace card`, () => { | |
|
|
||
| // Suggestion: Group the remaining test data into these categories: | ||
| // Number Cards (2-10) | ||
| test(`Should return the correct value for number cards (2-10)`), () => { | ||
| expect(getCardValue("2♠")).toEqual(2); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test call here looks slightly mis-structured, the callback function appears to be outside the test(...) call. |
||
| expect(getCardValue("5♠")).toEqual(5); | ||
| expect(getCardValue("10♠")).toEqual(10); | ||
| } | ||
| // Face Cards (J, Q, K) | ||
| // Invalid Cards | ||
| test(`Should return 10 when given a face card (J, Q, K)`), () => { | ||
| expect(getCardValue("J♠")).toEqual(10); | ||
| expect(getCardValue("Q♠")).toEqual(10); | ||
| expect(getCardValue("K♠")).toEqual(10); | ||
| } | ||
| // Invalid Card suits | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice test cases. also can you check if you properly closed the test block for this test( |
||
| test(`Should throw an error when given an invalid card suit`), () => { | ||
| expect(() => getCardValue("3-")).toThrow("Invalid card rank"); | ||
| expect(() => getCardValue("5X")).toThrow("Invalid card suit"); | ||
|
|
||
| // Invalid Card ranks | ||
| test(`Should throw an error when given an invalid card rank`), () => { | ||
| expect(() => getCardValue("1♠")).toThrow("Invalid card rank"); | ||
| expect(() => getCardValue("11♠")).toThrow("Invalid card rank"); | ||
| } | ||
|
|
||
| // To learn how to test whether a function throws an error as expected in Jest, | ||
| // please refer to the Jest documentation: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.