-
-
Notifications
You must be signed in to change notification settings - Fork 339
Added a Testing Guide and updated the specification in some JS files #907
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3635e58
Add Testing Guide
3e9bea5
Updated 1-implement-and-rewrite-test
7c92cdc
Updated 1-implement/rewrite-tests-with-jest
6a89a27
Updated 2-practice-tdd
b9cda01
Fixed typo in 1-implement
16bb366
Fixed typo in 2-practice-tdd
e72944f
Fixed code format in rewrite-test-with-jest
6f4ca5b
Moved testing guide to 1-implement
1ab6e34
Apply suggestions from code review
cjyuan 0b2a745
Update testing-guide.md
cjyuan 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
64 changes: 19 additions & 45 deletions
64
Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.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,63 +1,37 @@ | ||
| // Implement a function getAngleType | ||
| // Build up your function case by case, writing tests as you go | ||
| // The first test and case is written for you. The next case has a test, but no code. | ||
| // Execute this script in your terminal | ||
| // node 1-get-angle-type.js | ||
| // The assertion error will tell you what the expected output is | ||
| // Write the code to pass the test | ||
| // Then, write the next test! :) Go through this process until all the cases are implemented | ||
| // | ||
| // When given an angle in degrees, it should return a string indicating the type of angle: | ||
| // - "Acute angle" for angles greater than 0° and less than 90° | ||
| // - "Right angle" for exactly 90° | ||
| // - "Obtuse angle" for angles greater than 90° and less than 180° | ||
| // - "Straight angle" for exactly 180° | ||
| // - "Reflex angle" for angles greater than 180° and less than 360° | ||
| // - "Invalid angle" for angles outside the valid range. | ||
|
|
||
| // Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) | ||
|
|
||
| // 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 getAngleType(angle) { | ||
| if (angle === 90) { | ||
| return "Right angle"; | ||
| } | ||
| // Run the tests, work out what Case 2 is testing, and implement the required code here. | ||
| // Then keep going for the other cases, one at a time. | ||
| // TODO: Implement this function | ||
| } | ||
|
|
||
| // The line below allows us to load the getAngleType function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = getAngleType; | ||
|
|
||
| // we're going to use this helper function to make our assertions easier to read | ||
| // if the actual output matches the target output, the test will pass | ||
| // This helper function is written to make our assertions easier to read. | ||
| // If the actual output matches the target output, the test will pass | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // Given an angle in degrees, | ||
| // When the function getAngleType is called with this angle, | ||
| // Then it should: | ||
|
|
||
| // Case 1: Identify Right Angles: | ||
| // When the angle is exactly 90 degrees, | ||
| // Then the function should return "Right angle" | ||
| // TODO: Write tests to cover all cases, including boundary and invalid cases. | ||
| // Example: Identify Right Angles | ||
| const right = getAngleType(90); | ||
| assertEquals(right, "Right angle"); | ||
|
|
||
| // Case 2: Identify Acute Angles: | ||
| // When the angle is less than 90 degrees, | ||
| // Then the function should return "Acute angle" | ||
| const acute = getAngleType(45); | ||
| assertEquals(acute, "Acute angle"); | ||
|
|
||
| // Case 3: Identify Obtuse Angles: | ||
| // When the angle is greater than 90 degrees and less than 180 degrees, | ||
| // Then the function should return "Obtuse angle" | ||
| const obtuse = getAngleType(120); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
|
|
||
| // Case 4: Identify Straight Angles: | ||
| // When the angle is exactly 180 degrees, | ||
| // Then the function should return "Straight angle" | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
|
|
||
| // Case 5: Identify Reflex Angles: | ||
| // When the angle is greater than 180 degrees and less than 360 degrees, | ||
| // Then the function should return "Reflex angle" | ||
| // ====> write your test here, and then add a line to pass the test in the function above |
60 changes: 17 additions & 43 deletions
60
Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.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,59 +1,33 @@ | ||
| // Implement a function isProperFraction | ||
| // Write assertions for your function to check it works in different cases | ||
| // Terms: | ||
| // Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j | ||
| // Written here like this: 1/2 == Numerator/Denominator | ||
| // the first test and first case is written for you | ||
| // complete the rest of the tests and cases | ||
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // Implement a function isProperFraction, | ||
| // when given two numbers, a numerator and a denominator, it should return true if | ||
| // the given numbers form a proper fraction, and false otherwise. | ||
|
|
||
| // Assumption: The parameters are valid numbers (not NaN or Infinity). | ||
|
|
||
| // Note: If you are unfamiliar with proper fractions, please look up its mathematical definition. | ||
|
|
||
| // 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 isProperFraction(numerator, denominator) { | ||
| if (numerator < denominator) { | ||
| return true; | ||
| } | ||
| // TODO: Implement this function | ||
| } | ||
|
|
||
| // The line below allows us to load the isProperFraction function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = isProperFraction; | ||
|
|
||
| // here's our helper again | ||
| // Here's our helper again | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
|
|
||
| // Acceptance criteria: | ||
| // TODO: Write tests to cover all cases. | ||
| // What combinations of numerators and denominators should you test? | ||
|
|
||
| // Proper Fraction check: | ||
| // Input: numerator = 2, denominator = 3 | ||
| // target output: true | ||
| // Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true. | ||
| const properFraction = isProperFraction(2, 3); | ||
| assertEquals(properFraction, true); | ||
|
|
||
| // Improper Fraction check: | ||
| // Input: numerator = 5, denominator = 2 | ||
| // target output: false | ||
| // Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false. | ||
| const improperFraction = isProperFraction(5, 2); | ||
| assertEquals(improperFraction, false); | ||
|
|
||
| // Negative Fraction check: | ||
| // Input: numerator = -4, denominator = 7 | ||
| // target output: true | ||
| // 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. | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| // ====> complete with your assertion | ||
|
|
||
| // Equal Numerator and Denominator check: | ||
| // Input: numerator = 3, denominator = 3 | ||
| // target output: false | ||
| // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. | ||
| const equalFraction = isProperFraction(3, 3); | ||
| // ====> complete with your assertion | ||
|
|
||
| // Stretch: | ||
| // What other scenarios could you test for? | ||
| // Example: 1/2 is a proper fraction | ||
| assertEquals(isProperFraction(1, 2), true); |
77 changes: 36 additions & 41 deletions
77
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,57 +1,52 @@ | ||
| // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
|
|
||
| // You will need to implement a function getCardValue | ||
| // the function takes a single parameter, a string representing a playing card | ||
| // the function should return the numerical value of the card | ||
| // the first test and first case is written for you | ||
| // complete the rest of the tests and cases | ||
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
| // 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) { | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
| // TODO: Implement this function | ||
| } | ||
|
|
||
| // 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; | ||
|
|
||
| // You need to write assertions for your function to check it works in different cases | ||
| // we're going to use this helper function to make our assertions easier to read | ||
| // if the actual output matches the target output, the test will pass | ||
| // Helper functions to make our assertions easier to read. | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
| // Acceptance criteria: | ||
|
|
||
| // 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), | ||
| // When the function getCardValue is called with this card string as input, | ||
| // Then it should return the numerical card value | ||
| const aceofSpades = getCardValue("A♠"); | ||
| assertEquals(aceofSpades, 11); | ||
|
|
||
| // Handle Number Cards (2-10): | ||
| // Given a card with a rank between "2" and "9", | ||
| // When the function is called with such a card, | ||
| // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
|
|
||
| // Handle Face Cards (J, Q, K): | ||
| // Given a card with a rank of "10," "J," "Q," or "K", | ||
| // When the function is called with such a card, | ||
| // Then it should return the value 10, as these cards are worth 10 points each in blackjack. | ||
|
|
||
| // Handle Ace (A): | ||
| // Given a card with a rank of "A", | ||
| // When the function is called with an Ace, | ||
| // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. | ||
|
|
||
| // Handle Invalid Cards: | ||
| // Given a card with an invalid rank (neither a number nor a recognized face card), | ||
| // When the function is called with such a card, | ||
| // Then it should throw an error indicating "Invalid card rank." | ||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| // 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"); | ||
| } catch (e) {} | ||
|
|
||
| // What other invalid card cases can you think of? |
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
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.
Uh oh!
There was an error while loading. Please reload this page.