generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 301
NW | ITP-May-25 | Geraldine Edwards | Module-Structuring-and-Testing-Data | Sprint-3 #541
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
Closed
Geraldine-Edwards
wants to merge
14
commits into
CodeYourFuture:main
from
Geraldine-Edwards:coursework/sprint3
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f464538
add code to getAngleType function for testing, add additional test cases
Gerbil1511 5a452a0
add additional acceptance criteria for the tests, complete the stretc…
Gerbil1511 5724979
update original function and add test cases to check the rank of a pa…
Gerbil1511 fe3ff3c
add jest tests to 1-key-implement functions
Gerbil1511 5167a24
update functions in files and add tests for certain conditions.
Gerbil1511 f2fe186
create function to validate a credit card number and add test file
Gerbil1511 826e1db
add answers to questions about the find function
Gerbil1511 e621b0b
add tests (using an array for the inputs) and complete function for p…
Gerbil1511 be02da1
adjust return statement in isProperFraction function
Gerbil1511 becf97a
correct faceCards array and following if statement to handle card num…
Gerbil1511 f7d09da
replace hand-rolled parameterised test with Jest's .each() version
Gerbil1511 6bb7c11
extract the common values into well named variables so code is more r…
Gerbil1511 32d6f56
adjust function repeat(str,count) for clarity
Gerbil1511 5fe320b
switch invalid inputs from returning messages to 'throw error' messages
Gerbil1511 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,13 @@ | ||
| function getAngleType(angle) { | ||
| if (angle === 90) return "Right angle"; | ||
| // replace with your completed function from key-implement | ||
|
|
||
| if (angle > 0 && angle < 90) return "Acute angle"; | ||
| if (angle > 90 && angle < 180) return "Obtuse angle"; | ||
| if (angle === 180) return "Straight angle"; | ||
| if (angle > 180 && angle < 360) return "Reflex angle"; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // Don't get bogged down in this detail | ||
| // Jest uses CommonJS module syntax by default as it's quite old | ||
| // We will upgrade our approach to ES6 modules in the next course module, so for now | ||
| // We will upgrade our approach to ES6 modules in the next course module, so for now | ||
| // we have just written the CommonJS module.exports syntax for you | ||
| module.exports = getAngleType; | ||
| module.exports = getAngleType; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| function isProperFraction(numerator, denominator) { | ||
| if (numerator < denominator) return true; | ||
| // add your completed function from key-implement here | ||
| // ensure that the both inputs are evaluated to positive numbers by using Math.abs(), eg -4 returns 4, 4 returns 4, and 0 returns 0 | ||
| if (Math.abs(numerator) < Math.abs(denominator)) return true; | ||
| return false; //if the inputs don't satisfy the expression it returns false | ||
| } | ||
|
|
||
| module.exports = isProperFraction; | ||
| module.exports = isProperFraction; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| function getCardValue(card) { | ||
| // replace with your code from key-implement | ||
| return 11; | ||
| // use an array to hold the values of the face cards. | ||
| const faceCards = ["10", "J", "Q", "K"]; | ||
|
|
||
| /* | ||
| use slice to extract the rank (the numeric or face card value) from the "card" value. Start at the beginning index of the string (0) and go up to but not including the last character (-1 means one from the end) | ||
| */ | ||
| const rank = card.slice(0, -1); | ||
|
|
||
| if (rank === "A") return 11; //handles Ace cards | ||
| if (Number(rank) >= 2 && Number(rank) < 10) return Number(rank); //handles cards 2-9 | ||
|
|
||
| // use the includes() method to check if the value of the variable rank is one of the faceCards array values. | ||
| if (faceCards.includes(rank)) return 10; //handles face cards | ||
|
|
||
| // use the built-in JS Error() object to throw an error with a message if the value of "card" is invalid (with the message as the argument) | ||
| throw new Error("Invalid card rank"); //handles all invalid "card" values | ||
| } | ||
| module.exports = getCardValue; | ||
|
|
||
| module.exports = getCardValue; |
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,11 +1,56 @@ | ||
| const getCardValue = require("./3-get-card-value"); | ||
|
|
||
| test("should return 11 for Ace of Spades", () => { | ||
| const aceofSpades = getCardValue("A♠"); | ||
| expect(aceofSpades).toEqual(11); | ||
| }); | ||
| const aceofSpades = getCardValue("A♠"); | ||
| expect(aceofSpades).toEqual(11); | ||
| }); | ||
|
|
||
| // Case 2: Handle Number Cards (2-10): | ||
| test("should return correlating number for cards with ranks between 2-10", () => { | ||
| const numberCard = getCardValue("5♥"); | ||
| expect(numberCard).toEqual(5); | ||
| }); | ||
| // Case 3: Handle Face Cards (J, Q, K): | ||
| test("should return 10 for face cards of J, Q, K", () => { | ||
| const faceCard = getCardValue("J♣"); | ||
| expect(faceCard).toEqual(10); | ||
| }); | ||
| // Case 4: Handle Ace (A): | ||
| // Case 5: Handle Invalid Cards: | ||
| test("should return 11 for Ace of Spades", () => { | ||
| const aceCard = getCardValue("A♦"); | ||
| expect(aceCard).toEqual(11); | ||
| }); | ||
| // Case 5: Handle Invalid Cards - using an array for the inputs : | ||
| // test("should throw an error for invalid card ranks", () => { | ||
| // const invalidCardInput = [ | ||
| // "G♥", // random rank letter | ||
| // "1♠", // 1 is not a valid card | ||
| // "11♦", // 11 is not a valid card | ||
| // "62♣", //random rank number | ||
| // "", //empty string | ||
| // "NaN♠", //NaN as rank | ||
| // "undefined♣", //undefined as rank | ||
| // "null♦", // null as rank | ||
| // ]; | ||
|
|
||
| // I have learned that we can use Jest's .each() for clearer test output, better reporting, parallel execution (improves test performance) and less boilerplate code. in the description of the test we can use placeholders such as %s (string value) and %d (number , or digit, value) as placeholders for the actual test cases inputs, meaning that when the error message appears we can immediately know which case it refers to. E.g if the current input is "G♥", the test name will be: should throw an error for invalid card rank: G♥" | ||
| test.each([ | ||
| "G♥", // random rank letter | ||
| "1♠", // 1 is not a valid card | ||
| "11♦", // 11 is not a valid card | ||
| "62♣", // random rank number | ||
| "", // empty string | ||
| "NaN♠", // NaN as rank | ||
| "undefined♣", // undefined as rank | ||
| "null♦", // null as rank | ||
| ])("should throw an error for invalid card rank: %s", (input) => { | ||
| expect(() => getCardValue(input)).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| for (const input of invalidCardInput) { | ||
| expect(() => getCardValue(input)).toThrow("Invalid card rank"); | ||
| /* | ||
| Jest doesn;t test the value itself, it tests whether the function is throwing an error. If getCardValue(input) is called directly (i.e. using: expect(getCardValue(input)).toThrow("Invalid card rank") instead), the error happens immediately and Jest never gets a chance to check it, therefore the test fails. Wrapping getCardValue(input) in an anonymous function ensures that it delays the function execution allowing Jest to observe the error instead of triggering it too soon. | ||
| */ | ||
| } | ||
| }); |
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,5 +1,12 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let count = 0; | ||
| for (const char of stringOfCharacters) { | ||
| //loops through each character in the string input | ||
| if (char === findCharacter) | ||
| //checks if the character matches the findCharacter input | ||
| count++; //increases the count when a match is found in the input | ||
| } | ||
| return count; //return the actual count of the characters that match | ||
| } | ||
|
|
||
| module.exports = countChar; | ||
| module.exports = countChar; |
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
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.
Jest also has
.toBeTruthy(): https://jestjs.io/docs/expect#tobetruthyWhy might that not be a good thing to use here?
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.
I imagine it's not great to use at this test probably because if something is "truthy" it is less strict.. or something to that effect? I would need to make sure that an improper fraction was handled strictly to get the exact correct values.
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.
That is exactly it -
truthyvalues are a lot more thantruewhich means it is less strict than.toEqual(true). It's any value that would be coerced intotruewhen in a boolean contexthttps://developer.mozilla.org/en-US/docs/Glossary/Truthy