-
-
Notifications
You must be signed in to change notification settings - Fork 390
London | 26-ITP-May | Khaliun Baatarkhuu | Sprint 3 | Implement and Rewrite Tests #1440
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
khaliun-dev
wants to merge
8
commits into
CodeYourFuture:main
Choose a base branch
from
khaliun-dev:coursework/sprint-3-implement-and-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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5301d86
Implement getAngleType function with tests
khaliun-dev cc9fd10
Enhance angle type tests in 1-get-angle-type.js
khaliun-dev 97e20be
Implement isProperFraction function with tests
khaliun-dev 4b1e96a
Implement getCardValue function and add tests
khaliun-dev a5c8e7d
Enhance tests for invalid card values
khaliun-dev 5671348
Rewrite tests for getAngleType using Jest
khaliun-dev 4ab6f5c
Implement Jest tests for isProperFraction function
khaliun-dev 390b390
Rewrite tests for getCardValue using Jest
khaliun-dev 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
72 changes: 48 additions & 24 deletions
72
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,37 +1,61 @@ | ||
| // Implement a function getAngleType | ||
| // | ||
| // 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) { | ||
| // TODO: Implement this function | ||
| if (angle > 0 && angle < 90) { | ||
| return "Acute angle"; | ||
| } else if (angle === 90) { | ||
| return "Right angle"; | ||
| } else if (angle > 90 && angle < 180) { | ||
| return "Obtuse angle"; | ||
| } else if (angle === 180) { | ||
| return "Straight angle"; | ||
| } else if (angle > 180 && angle < 360) { | ||
| return "Reflex angle"; | ||
| } else { | ||
| return "Invalid angle"; | ||
| } | ||
| } | ||
|
|
||
| // 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; | ||
|
|
||
| // 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}` | ||
| ); | ||
| } | ||
|
|
||
| // TODO: Write tests to cover all cases, including boundary and invalid cases. | ||
| // Example: Identify Right Angles | ||
| assertEquals(getAngleType(45), "Acute angle"); | ||
| assertEquals(getAngleType(90), "Right angle"); | ||
| assertEquals(getAngleType(120), "Obtuse angle"); | ||
| assertEquals(getAngleType(180), "Straight angle"); | ||
| assertEquals(getAngleType(270), "Reflex angle"); | ||
| assertEquals(getAngleType(360), "Invalid angle"); | ||
|
|
||
| console.log("All tests finished"); | ||
|
|
||
| //I have written some tests, passed them all on Devtools. Wrote some more below. | ||
|
|
||
|
|
||
| const acute = getAngleType(45); | ||
| assertEquals(acute, "Acute angle"); | ||
| const right = getAngleType(90); | ||
| assertEquals(right, "Right angle"); | ||
| const obtuse = getAngleType(120); | ||
| assertEquals(obtuse, "Obtuse angle"); | ||
| const straight = getAngleType(180); | ||
| assertEquals(straight, "Straight angle"); | ||
| const reflex = getAngleType(270); | ||
| assertEquals(reflex, "Reflex angle"); | ||
| const zero = getAngleType(0); | ||
| assertEquals(zero, "Invalid angle"); | ||
| const negative = getAngleType(-10); | ||
| assertEquals(negative, "Invalid angle"); | ||
| const fullTurn = getAngleType(360); | ||
| assertEquals(fullTurn, "Invalid angle"); | ||
| const tooLarge = getAngleType(400); | ||
| assertEquals(tooLarge, "Invalid angle"); | ||
|
|
||
| console.log("All tests finished"); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
30 changes: 9 additions & 21 deletions
30
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,33 +1,21 @@ | ||
| // 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) { | ||
| // TODO: Implement this function | ||
| return numerator < denominator; | ||
| } | ||
|
|
||
| // 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 | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
| assertEquals(isProperFraction(1, 2), true); | ||
| assertEquals(isProperFraction(3, 4), true); | ||
| assertEquals(isProperFraction(0, 5), true); | ||
|
|
||
| // TODO: Write tests to cover all cases. | ||
| // What combinations of numerators and denominators should you test? | ||
| assertEquals(isProperFraction(5, 5), false); | ||
|
|
||
| // Example: 1/2 is a proper fraction | ||
| assertEquals(isProperFraction(1, 2), true); | ||
| assertEquals(isProperFraction(7, 4), false); | ||
| assertEquals(isProperFraction(10, 3), false); | ||
|
|
||
| console.log("All tests finished"); | ||
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 |
|---|---|---|
|
|
@@ -22,33 +22,90 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| } | ||
| const suits = ["♠", "♥", "♦", "♣"]; | ||
|
|
||
| const values = { | ||
|
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. This is an interesting approach, good idea |
||
| A: 11, | ||
| J: 10, | ||
| Q: 10, | ||
| K: 10, | ||
| 2: 2, | ||
| 3: 3, | ||
| 4: 4, | ||
| 5: 5, | ||
| 6: 6, | ||
| 7: 7, | ||
| 8: 8, | ||
| 9: 9, | ||
| 10: 10, | ||
| }; | ||
|
|
||
| // 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; | ||
| const suit = card.slice(-1); | ||
| const rank = card.slice(0, -1); | ||
|
|
||
| if (!suits.includes(suit) || !(rank in values)) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| return values[rank]; | ||
| } | ||
|
|
||
| // Helper functions to make our assertions easier to read. | ||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
| assertEquals(getCardValue("A♠"), 11); | ||
| assertEquals(getCardValue("2♥"), 2); | ||
| assertEquals(getCardValue("9♣"), 9); | ||
| assertEquals(getCardValue("10♦"), 10); | ||
| assertEquals(getCardValue("J♠"), 10); | ||
| assertEquals(getCardValue("Q♥"), 10); | ||
| assertEquals(getCardValue("K♣"), 10); | ||
|
|
||
| console.log("All valid card tests passed"); | ||
|
|
||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| // What other invalid card cases can you think of? | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| getCardValue("1♠"); | ||
| console.error("Error was not thrown for 1♠ 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for 1♠ 🎉"); | ||
| } | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| try { | ||
| getCardValue("11♠"); | ||
| console.error("Error was not thrown for 11♠ 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| console.log("Error thrown for 11♠ 🎉"); | ||
| } | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| try { | ||
| getCardValue("A"); | ||
| console.error("Error was not thrown for A 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for A 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("♠"); | ||
| console.error("Error was not thrown for ♠ 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for ♠ 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue("KH"); | ||
| console.error("Error was not thrown for KH 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for KH 🎉"); | ||
| } | ||
|
|
||
| try { | ||
| getCardValue(""); | ||
| console.error("Error was not thrown for empty string 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for empty string 🎉"); | ||
| } | ||
35 changes: 27 additions & 8 deletions
35
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,39 @@ | ||
| // 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 | ||
| test('should return "Acute angle" when (0 < angle < 90)', () => { | ||
| expect(getAngleType(1)).toEqual("Acute angle"); | ||
| expect(getAngleType(45)).toEqual("Acute angle"); | ||
| expect(getAngleType(89)).toEqual("Acute angle"); | ||
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| test('should return "Right angle" when angle is 90', () => { | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }); | ||
|
|
||
| // Case 3: Obtuse angles | ||
| 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"); | ||
| }); | ||
|
|
||
| // Case 4: Straight angle | ||
| test('should return "Straight angle" when angle is 180', () => { | ||
| expect(getAngleType(180)).toEqual("Straight angle"); | ||
| }); | ||
|
|
||
| // Case 5: Reflex angles | ||
| 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"); | ||
| }); | ||
|
|
||
| // Case 6: Invalid angles | ||
| test('should return "Invalid angle" for invalid values', () => { | ||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(-1)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(361)).toEqual("Invalid angle"); | ||
| }); |
40 changes: 33 additions & 7 deletions
40
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,36 @@ | ||
| // 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: denominator is zero | ||
|
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. Be careful here in your tests - have you tried running them with |
||
| test("should return false when denominator is zero", () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| // 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 is zero and denominator is not zero", () => { | ||
| expect(isProperFraction(0, 5)).toEqual(true); | ||
| expect(isProperFraction(0, -5)).toEqual(true); | ||
| }); | ||
|
|
||
| // Positive proper fractions | ||
| test("should return true for positive proper fractions", () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(2, 5)).toEqual(true); | ||
| }); | ||
|
|
||
| // Positive improper fractions | ||
| test("should return false for positive improper fractions", () => { | ||
| expect(isProperFraction(5, 2)).toEqual(false); | ||
| expect(isProperFraction(2, 2)).toEqual(false); | ||
| }); | ||
|
|
||
| // Negative proper fractions | ||
| test("should return true for negative proper fractions", () => { | ||
| expect(isProperFraction(-1, 2)).toEqual(true); | ||
| expect(isProperFraction(1, -2)).toEqual(true); | ||
| expect(isProperFraction(-2, -5)).toEqual(true); | ||
| }); | ||
|
|
||
| // Negative improper fractions | ||
| test("should return false for negative improper fractions", () => { | ||
| expect(isProperFraction(-5, 2)).toEqual(false); | ||
| expect(isProperFraction(5, -2)).toEqual(false); | ||
| expect(isProperFraction(-5, -2)).toEqual(false); | ||
| }); | ||
38 changes: 25 additions & 13 deletions
38
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,32 @@ | ||
| // 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`, () => { | ||
| test("should return 11 when given an ace card", () => { | ||
| expect(getCardValue("A♠")).toEqual(11); | ||
| }); | ||
|
|
||
| // Suggestion: Group the remaining test data into these categories: | ||
| // Number Cards (2-10) | ||
| // Face Cards (J, Q, K) | ||
| // Invalid Cards | ||
| // Case 2: Number cards (2-10) | ||
| test("should return the numeric value for number cards", () => { | ||
| expect(getCardValue("2♥")).toEqual(2); | ||
| expect(getCardValue("5♣")).toEqual(5); | ||
| expect(getCardValue("9♦")).toEqual(9); | ||
| expect(getCardValue("10♠")).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 | ||
| // Case 3: Face cards (J, Q, K) | ||
| test("should return 10 for face cards", () => { | ||
| expect(getCardValue("J♠")).toEqual(10); | ||
| expect(getCardValue("Q♥")).toEqual(10); | ||
| expect(getCardValue("K♦")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 4: Invalid cards | ||
| test("should throw an error for invalid cards", () => { | ||
| expect(() => getCardValue("invalid")).toThrow(); | ||
| expect(() => getCardValue("1♠")).toThrow(); | ||
| expect(() => getCardValue("11♠")).toThrow(); | ||
| expect(() => getCardValue("A")).toThrow(); | ||
| expect(() => getCardValue("♠")).toThrow(); | ||
| expect(() => getCardValue("KH")).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.
Does your testing consider valid proper fractions with negatives?