-
-
Notifications
You must be signed in to change notification settings - Fork 396
Birmingham | 26-ITP-May | Gabriel Pawuoi | Sprint 3 | Implement and rewrite tests #1577
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
Open
KhotKeys
wants to merge
6
commits into
CodeYourFuture:main
Choose a base branch
from
KhotKeys:sprint-3-implement-rewrite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−95
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
854d4b6
Implement getAngleType function
KhotKeys bf17942
Implement isProperFraction function
KhotKeys b59a933
Implement getCardValue function
KhotKeys 2f1b246
Add Jest tests for getAngleType
KhotKeys ec03f55
Add Jest tests for isProperFraction
KhotKeys 25c18e0
Add Jest tests for getCardValue
KhotKeys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 19 additions & 34 deletions
53
Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,39 @@ | ||
| // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
|
|
||
| // Implement a function getCardValue, when given a string representing a playing card, | ||
| // should return the numerical value of the card. | ||
|
|
||
| // A valid card string will contain a rank followed by the suit. | ||
| // The rank can be one of the following strings: | ||
| // "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" | ||
| // The suit can be one of the following emojis: | ||
| // "♠", "♥", "♦", "♣" | ||
| // For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". | ||
|
|
||
| // When the card is an ace ("A"), the function should return 11. | ||
| // When the card is a face card ("J", "Q", "K"), the function should return 10. | ||
| // When the card is a number card ("2" to "10"), the function should return its numeric value. | ||
|
|
||
| // When the card string is invalid (not following the above format), the function should | ||
| // throw an error. | ||
|
|
||
| // Acceptance criteria: | ||
| // After you have implemented the function, write tests to cover all the cases, and | ||
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const suit = card.slice(-1); | ||
| const rank = card.slice(0, -1); | ||
| const validSuits = ["\u2660", "\u2665", "\u2666", "\u2663"]; | ||
| const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; | ||
|
|
||
| if (!validSuits.includes(suit) || !validRanks.includes(rank)) { | ||
| throw new Error(`Invalid card: ${card}`); | ||
| } | ||
| if (rank === "A") return 11; | ||
| if (["J", "Q", "K"].includes(rank)) return 10; | ||
| return Number(rank); | ||
| } | ||
|
|
||
| // 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; | ||
|
|
||
| // Helper functions to make our assertions easier to read. | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
|
|
||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("9\u2660"), 9); | ||
| assertEquals(getCardValue("A\u2660"), 11); | ||
| assertEquals(getCardValue("10\u2665"), 10); | ||
| assertEquals(getCardValue("J\u2663"), 10); | ||
| assertEquals(getCardValue("Q\u2666"), 10); | ||
| assertEquals(getCardValue("K\u2666"), 10); | ||
|
|
||
| // Handling invalid cards | ||
| 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 😢"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| console.log("Error thrown for invalid card"); | ||
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
37 changes: 25 additions & 12 deletions
37
Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,33 @@ | ||
| // This statement loads the getAngleType function you wrote in the implement directory. | ||
| // We will use the same function, but write tests for it using Jest in this file. | ||
| const getAngleType = require("../implement/1-get-angle-type"); | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all cases/outcomes, | ||
| // including boundary and invalid cases. | ||
|
|
||
| // Case 1: Acute angles | ||
| test(`should return "Acute angle" when (0 < angle < 90)`, () => { | ||
| // Test various acute angles, including boundary cases | ||
| expect(getAngleType(1)).toEqual("Acute angle"); | ||
| expect(getAngleType(45)).toEqual("Acute angle"); | ||
| expect(getAngleType(89)).toEqual("Acute angle"); | ||
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| // Case 3: Obtuse angles | ||
| // Case 4: Straight angle | ||
| // Case 5: Reflex angles | ||
| // Case 6: Invalid angles | ||
| test(`should return "Right angle" for exactly 90`, () => { | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }); | ||
|
|
||
| test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { | ||
| expect(getAngleType(91)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(135)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(179)).toEqual("Obtuse angle"); | ||
| }); | ||
|
|
||
| test(`should return "Straight angle" for exactly 180`, () => { | ||
| expect(getAngleType(180)).toEqual("Straight angle"); | ||
| }); | ||
|
|
||
| test(`should return "Reflex angle" when (180 < angle < 360)`, () => { | ||
| expect(getAngleType(181)).toEqual("Reflex angle"); | ||
| expect(getAngleType(270)).toEqual("Reflex angle"); | ||
| expect(getAngleType(359)).toEqual("Reflex angle"); | ||
| }); | ||
|
|
||
| test(`should return "Invalid angle" for angles outside 0-360`, () => { | ||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(-10)).toEqual("Invalid angle"); | ||
| }); |
25 changes: 20 additions & 5 deletions
25
Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,25 @@ | ||
| // This statement loads the isProperFraction function you wrote in the implement directory. | ||
| // We will use the same function, but write tests for it using Jest in this file. | ||
| const isProperFraction = require("../implement/2-is-proper-fraction"); | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. | ||
|
|
||
| // Special case: numerator is zero | ||
| test(`should return false when denominator is zero`, () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return true when numerator < denominator (positive)`, () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(3, 7)).toEqual(true); | ||
| }); | ||
|
|
||
| test(`should return false when numerator >= denominator`, () => { | ||
| expect(isProperFraction(2, 1)).toEqual(false); | ||
| expect(isProperFraction(3, 3)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should handle negative numerator`, () => { | ||
| expect(isProperFraction(-1, 2)).toEqual(true); | ||
| expect(isProperFraction(-3, 2)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should handle negative denominator`, () => { | ||
| expect(isProperFraction(1, -2)).toEqual(true); | ||
| expect(isProperFraction(3, -2)).toEqual(false); | ||
| }); | ||
|
Comment on lines
+12
to
+25
Contributor
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. Could consider using pseudo-code and notations like |
||
29 changes: 16 additions & 13 deletions
29
Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,23 @@ | ||
| // This statement loads the getCardValue function you wrote in the implement directory. | ||
| // We will use the same function, but write tests for it using Jest in this file. | ||
| const getCardValue = require("../implement/3-get-card-value"); | ||
|
|
||
| // TODO: Write tests in Jest syntax to cover all possible outcomes. | ||
|
|
||
| // Case 1: Ace (A) | ||
| test(`Should return 11 when given an ace card`, () => { | ||
| expect(getCardValue("A♠")).toEqual(11); | ||
| expect(getCardValue("A\u2660")).toEqual(11); | ||
| expect(getCardValue("A\u2665")).toEqual(11); | ||
| }); | ||
|
|
||
| // Suggestion: Group the remaining test data into these categories: | ||
| // Number Cards (2-10) | ||
| // Face Cards (J, Q, K) | ||
| // Invalid Cards | ||
| test(`Should return numeric value for number cards 2-10`, () => { | ||
| expect(getCardValue("2\u2665")).toEqual(2); | ||
| expect(getCardValue("5\u2666")).toEqual(5); | ||
| expect(getCardValue("10\u2665")).toEqual(10); | ||
| }); | ||
|
|
||
| // To learn how to test whether a function throws an error as expected in Jest, | ||
| // please refer to the Jest documentation: | ||
| // https://jestjs.io/docs/expect#tothrowerror | ||
| test(`Should return 10 for face cards J, Q, K`, () => { | ||
| expect(getCardValue("J\u2663")).toEqual(10); | ||
| expect(getCardValue("Q\u2666")).toEqual(10); | ||
| expect(getCardValue("K\u2660")).toEqual(10); | ||
| }); | ||
|
|
||
| test(`Should throw an error for invalid cards`, () => { | ||
| expect(() => getCardValue("invalid")).toThrow(); | ||
| expect(() => getCardValue("")).toThrow(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just specify the suit characters as
"♠", "♥", "♦", "♣"?