-
-
Notifications
You must be signed in to change notification settings - Fork 337
Cape Town | 2026-ITP-Jan | Isaac Abodunrin | Sprint 3 | Implement and Rewrite Tests Coursework #956
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
bytesandroses
wants to merge
14
commits into
CodeYourFuture:main
Choose a base branch
from
bytesandroses: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 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ed9ee26
Sprint 3 Coursework: Implement and rewrite tests 1
bytesandroses dc4671c
Sprint 3 Coursework: Implement and rewrite tests 2
bytesandroses 3d71af0
Sprint 3 Coursework: Implement and rewrite tests 3
bytesandroses a0b25cc
Refactor: Sprint 3 Coursework: Implement and rewrite tests 3
bytesandroses 6e06535
Sprint 3 Coursework: rewrite-with-tests 1
bytesandroses 5bf402c
Add jest package
bytesandroses 9057f8f
Refactor for zero numerators and denominators: implement 2
bytesandroses 3618fa4
Refactor: allow isProperFunction to handle non numeric values
bytesandroses e31dece
Jest testing for is-proper-fractions
bytesandroses c156ae9
Test: get card value testing
bytesandroses 1e197ea
Correction: allow proper fractions to include numerator of zero
bytesandroses 6d48ff2
Correction: allow proper fractions to include numerator of zero
bytesandroses 288a17d
Refactor: group similar test cases under a single category to reduce …
bytesandroses 665ba54
Refactor: group similar test cases under a single category to reduce …
bytesandroses 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 | ||
| // Ensure non-numbers are treated as an invalid angles | ||
| if (typeof angle != "number") return "Invalid angle"; | ||
|
|
||
| if (angle > 0 && angle < 90) return "Acute angle"; | ||
| if (angle === 90) return "Right angle"; | ||
| if (angle > 90 && angle < 180) return "Obtuse angle"; | ||
| if (angle === 180) return "Straight angle"; | ||
| if (angle > 180 && angle < 360) return "Reflex angle"; | ||
|
|
||
| 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 | ||
| const right = getAngleType(90); | ||
| // Acute Angles Test | ||
| let acute = getAngleType(45); | ||
| assertEquals(acute, "Acute angle"); | ||
|
|
||
| // Right Angles Test | ||
| let right = getAngleType(90); | ||
| assertEquals(right, "Right angle"); | ||
|
|
||
| // Obtuse Angles Test | ||
| let obtuse = getAngleType(120); | ||
| assertEquals(obtuse, "Obtuse angle"); | ||
|
|
||
| // Straight Angles Test | ||
| let straight = getAngleType(180); | ||
| assertEquals(straight, "Straight angle"); | ||
|
|
||
| // Reflex Angles Test | ||
| let reflex = getAngleType(200); | ||
| assertEquals(reflex, "Reflex angle"); | ||
|
|
||
| // Invalid Angles Test | ||
| let invalid = getAngleType(0); | ||
| assertEquals(invalid, "Invalid angle"); | ||
|
|
||
| // Invalid Angles Test | ||
| invalid = getAngleType(360); | ||
| assertEquals(invalid, "Invalid angle"); | ||
|
|
||
| // Invalid Angles Test | ||
| invalid = getAngleType(500); | ||
| assertEquals(invalid, "Invalid angle"); | ||
|
|
||
| // Invalid Angles Test | ||
| invalid = getAngleType(-200); | ||
| assertEquals(invalid, "Invalid angle"); | ||
|
|
||
| // Test for float inputs | ||
| acute = getAngleType(34.333); | ||
| assertEquals(acute, "Acute angle"); |
35 changes: 16 additions & 19 deletions
35
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,30 @@ | ||
| // 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. | ||
| function isProperFraction(numerator, denominator) { | ||
| // Return non numeric numerators and denominators as false | ||
| if (typeof numerator != "number" || typeof denominator != "number") | ||
| return false; | ||
|
|
||
| // Acceptance criteria: | ||
| // After you have implemented the function, write tests to cover all the cases, and | ||
| // execute the code to ensure all tests pass. | ||
| if (numerator === 0 || denominator === 0) return false; | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| // TODO: Implement this function | ||
| return Math.abs(numerator) < Math.abs(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}` | ||
| ); | ||
| } | ||
|
|
||
| // TODO: Write tests to cover all cases. | ||
| // What combinations of numerators and denominators should you test? | ||
|
|
||
| // Example: 1/2 is a proper fraction | ||
| // Positive proper fraction test | ||
| assertEquals(isProperFraction(1, 2), true); | ||
|
|
||
| // Improper proper fraction test | ||
| assertEquals(isProperFraction(3, 2), false); | ||
|
|
||
| // Negative proper fraction test | ||
| assertEquals(isProperFraction(-1, 2), true); | ||
|
|
||
| // Zero numerator test | ||
| assertEquals(isProperFraction(0, 2), false); | ||
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
35 changes: 29 additions & 6 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,43 @@ | ||
| // 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 | ||
| test(`should return "Right angle" when (angle === 90)`, () => { | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }); | ||
|
|
||
| // Case 3: Obtuse angles | ||
| test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { | ||
| expect(getAngleType(91.0)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(100)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(179)).toEqual("Obtuse angle"); | ||
| }); | ||
|
|
||
| // Case 4: Straight angle | ||
| test(`should return "Straight angle" when (angle === 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(200)).toEqual("Reflex angle"); | ||
| expect(getAngleType(359)).toEqual("Reflex angle"); | ||
| }); | ||
|
|
||
| // Case 6: Invalid angles | ||
| test(`should return "Invalid angle" when angle doesn't lie between 0-360 exclusive`, () => { | ||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(500)).toEqual("Invalid angle"); | ||
| expect(getAngleType(-200)).toEqual("Invalid angle"); | ||
| expect(getAngleType("")).toEqual("Invalid angle"); | ||
| expect(getAngleType("ten degrees")).toEqual("Invalid angle"); | ||
| expect(getAngleType(true)).toEqual("Invalid angle"); | ||
| }); |
60 changes: 54 additions & 6 deletions
60
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,58 @@ | ||
| // 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`, () => { | ||
| // Case 1: numerator/denominator is zero | ||
| test(`should return false when either numerator or denominator is zero`, () => { | ||
| expect(isProperFraction(0, 1)).toEqual(false); | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| expect(isProperFraction(0, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 2: numerator and denominator are positive integers and form proper fractions | ||
| test(`should return true when either numerator is smaller than denominator`, () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| expect(isProperFraction(1, 10)).toEqual(true); | ||
| expect(isProperFraction(9, 100)).toEqual(true); | ||
| }); | ||
|
|
||
| // Case 3: numerator and denominator are negative and make proper fractions | ||
| test(`should return true when absolute of numerator is smaller than absolute of denominator`, () => { | ||
| expect(isProperFraction(-1, 2)).toEqual(true); | ||
| expect(isProperFraction(1, -10)).toEqual(true); | ||
| expect(isProperFraction(-9, -100)).toEqual(true); | ||
| }); | ||
|
|
||
| // Case 4: numerator/denominator is decimal and make proper fractions | ||
| test(`should return true when decimal numerator is smaller than decimal denominator`, () => { | ||
| expect(isProperFraction(0.5, 2)).toEqual(true); | ||
| expect(isProperFraction(1.2, -10)).toEqual(true); | ||
| expect(isProperFraction(-9, -9.5)).toEqual(true); | ||
| }); | ||
|
|
||
| // Case 5: numerator and denominator are positive integers and not proper fractions | ||
| test(`should return false when numerator is greater than denominator`, () => { | ||
| expect(isProperFraction(2, 1)).toEqual(false); | ||
| expect(isProperFraction(10, 1)).toEqual(false); | ||
| expect(isProperFraction(100, 9)).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 6: numerator and denominator are negative and not proper fractions | ||
| test(`should return false when absolute of numerator is greater than absolute of denominator`, () => { | ||
| expect(isProperFraction(-2, 1)).toEqual(false); | ||
| expect(isProperFraction(10, -1)).toEqual(false); | ||
| expect(isProperFraction(-100, -9)).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 7: numerator/denominator is decimal and not proper fractions | ||
| test(`should return false when numerator is greater than denominator`, () => { | ||
| expect(isProperFraction(2.5, 1)).toEqual(false); | ||
| expect(isProperFraction(10.2, 1)).toEqual(false); | ||
| expect(isProperFraction(100.5, 9.001)).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 8: numerator/denominator is not numeric | ||
| test(`should return false when either numerator or denominator is not numeric`, () => { | ||
| expect(isProperFraction(false, 1)).toEqual(false); | ||
| expect(isProperFraction(true, 1)).toEqual(false); | ||
| expect(isProperFraction("10.2", 1)).toEqual(false); | ||
| expect(isProperFraction("", 9.001)).toEqual(false); | ||
| }); |
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.
Isn't 0/1 a proper fraction?
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.
Good catch -- you're absolutely right since the definition of a proper fraction is where the magnitude of the denominator is great than that of the numerator. It doesn't say the numerator can't be zero, so I've updated the code to correct this