Repository navigation
Cape Town | 26-ITP-Sept | Leigh Ross | Sprint 2 | Implement and rewrite tests #1683
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
leigh-ross
wants to merge
10
commits into
CodeYourFuture:main
Choose a base branch
from
leigh-ross:coursework/sprint-2-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
10 commits
Select commit
Hold shift + click to select a range
50a495c
Angle type: Function logic works, added test cases, all tests pass
leigh-ross b5a6757
Proper fraction: added function logic, added test cases, all tests pass
leigh-ross 93ff60f
Card value: added fucntion logic, added test cases, all tests pass
leigh-ross 6adba30
Copied over tests from node and rewrote as jest
leigh-ross 4457a78
Fixed testing inputs, boundary angles were used accidentally
leigh-ross 270090a
Fixed function logic to allow negative numerators as proper fractions…
leigh-ross 13306d7
Function logic fix: allows negative numerators but abs value must be …
leigh-ross 8fadb82
Function logic fix: allows negative numerators but abs value must be …
leigh-ross 6f8616a
Function logic: added safeguards for incorrect suit and card value, a…
leigh-ross 5647d78
Package and .gitignore for others to work on this task
leigh-ross 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules |
84 changes: 81 additions & 3 deletions
84
implement-and-rewrite-tests/implement-tests-with-node-test/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,12 +1,90 @@ | ||
| import assert from "node:assert"; | ||
| import test from "node:test"; | ||
| import test, { describe } from "node:test"; | ||
|
|
||
| import { getAngleType } from "../implement/1-get-angle-type.js"; | ||
|
|
||
| // TODO: Write tests to cover all cases, including boundary and invalid cases. | ||
| // Example: Identify Right Angles | ||
|
|
||
| test("Classifies right angles", () => { | ||
| const right = getAngleType(90); | ||
| assert.equal(right, "Right angle"); | ||
| const angleValue = getAngleType(90); | ||
| assert.equal(angleValue, "Right angle"); | ||
| }); | ||
|
|
||
| test("Classifies straight angles", () => { | ||
| const angleValue = getAngleType(180); | ||
| assert.equal(angleValue, "Straight angle"); | ||
| }); | ||
|
|
||
| describe ("Acute angles and boundaries", () => { | ||
| test("1 is acute", () => { | ||
| const angleValue = getAngleType(1); | ||
| assert.equal(angleValue, "Acute angle"); | ||
| }); | ||
|
|
||
| test("45 is acute", () => { | ||
| const angleValue = getAngleType(45); | ||
| assert.equal(angleValue, "Acute angle"); | ||
| }); | ||
|
|
||
| test("89 is acute", () => { | ||
| const angleValue = getAngleType(89); | ||
| assert.equal(angleValue, "Acute angle"); | ||
| }); | ||
| }); | ||
|
|
||
| describe ("Obtuse angles and boundaries", () => { | ||
| test("91 is obtuse", () => { | ||
| const angleValue = getAngleType(91); | ||
| assert.equal(angleValue, "Obtuse angle"); | ||
| }); | ||
|
|
||
| test("135 is obtuse", () => { | ||
| const angleValue = getAngleType(135); | ||
| assert.equal(angleValue, "Obtuse angle"); | ||
| }); | ||
|
|
||
| test("179 is obtuse", () => { | ||
| const angleValue = getAngleType(179); | ||
| assert.equal(angleValue, "Obtuse angle"); | ||
| }); | ||
| }); | ||
|
|
||
| describe ("Reflex angles and boundaries", () => { | ||
| test("181 is reflex", () => { | ||
| const angleValue = getAngleType(181); | ||
| assert.equal(angleValue, "Reflex angle"); | ||
| }); | ||
|
|
||
| test("270 is reflex", () => { | ||
| const angleValue = getAngleType(270); | ||
| assert.equal(angleValue, "Reflex angle"); | ||
| }); | ||
|
|
||
| test("359 is reflex", () => { | ||
| const angleValue = getAngleType(359); | ||
| assert.equal(angleValue, "Reflex angle"); | ||
| }); | ||
| }); | ||
|
|
||
| describe ("Invalid angles and boundaries", () => { | ||
| test("0 is invalid", () => { | ||
| const angleValue = getAngleType(0); | ||
| assert.equal(angleValue, "Invalid angle"); | ||
| }); | ||
|
|
||
| test("5879 is invalid", () => { | ||
| const angleValue = getAngleType(5879); | ||
| assert.equal(angleValue, "Invalid angle"); | ||
| }); | ||
|
|
||
| test("-1 is invalid", () => { | ||
| const angleValue = getAngleType(-1); | ||
| assert.equal(angleValue, "Invalid angle"); | ||
| }); | ||
|
|
||
| test("360 is invalid", () => { | ||
| const angleValue = getAngleType(360); | ||
| assert.equal(angleValue, "Invalid angle"); | ||
| }); | ||
| }); | ||
83 changes: 80 additions & 3 deletions
83
implement-and-rewrite-tests/implement-tests-with-node-test/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,12 +1,89 @@ | ||
| import assert from "node:assert"; | ||
| import test from "node:test"; | ||
| import test, {describe} from "node:test"; | ||
|
|
||
| import { isProperFraction } from "../implement/2-is-proper-fraction.js"; | ||
|
|
||
| // TODO: Write tests to cover all cases. | ||
| // What combinations of numerators and denominators should you test? | ||
|
|
||
| test("Basic proper fraction", () => { | ||
| // Example: 1/2 is a proper fraction | ||
| describe ("Proper fractions", () => { | ||
| test("Basic proper fraction", () => { | ||
| assert.equal(isProperFraction(1, 2), true); | ||
| }); | ||
|
|
||
| test("3/4 is proper", () => { | ||
| assert.equal(isProperFraction(3, 4), true); | ||
| }); | ||
|
|
||
| test("2345678/98765234567 is proper", () => { | ||
| assert.equal(isProperFraction(2345678, 98765234567), true); | ||
| }); | ||
|
|
||
| test("0/5 is proper", () => { | ||
| assert.equal(isProperFraction(0, 5), true); | ||
| }); | ||
|
|
||
| test("99/100 is proper", () => { | ||
| assert.equal(isProperFraction(99, 100), true); | ||
| }); | ||
|
|
||
| test("1/900 is proper", () => { | ||
| assert.equal(isProperFraction(1, 900), true); | ||
| }); | ||
| }); | ||
|
|
||
| describe ("Division by 0", () => { | ||
| test("1/0 is invalid", () => { | ||
| assert.equal(isProperFraction(1, 0), false); | ||
| }); | ||
|
|
||
| test("19768/0 is invalid", () => { | ||
| assert.equal(isProperFraction(19768, 0), false); | ||
| }); | ||
|
|
||
| test("0/0 is invalid", () => { | ||
| assert.equal(isProperFraction(0, 0), false); | ||
| }); | ||
| }); | ||
|
|
||
| describe ("Improper fractions", () => { | ||
| test("1/1 is improper", () => { | ||
| assert.equal(isProperFraction(1, 1), false); | ||
| }); | ||
|
|
||
| test("19768/9 is improper", () => { | ||
| assert.equal(isProperFraction(19768, 9), false); | ||
| }); | ||
|
|
||
| test("8/5 is improper", () => { | ||
| assert.equal(isProperFraction(8, 5), false); | ||
| }); | ||
| }); | ||
|
|
||
| describe ("Negative proper fractions", () => { | ||
| test("-98/100 is proper", () => { | ||
| assert.equal(isProperFraction(-98, 100), true); | ||
| }); | ||
|
|
||
| test("-1/9 is proper", () => { | ||
| assert.equal(isProperFraction(-1, 9), true); | ||
| }); | ||
|
|
||
| test("-8/15 is proper", () => { | ||
| assert.equal(isProperFraction(-8, 15), true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Negative improper fractions", () => { | ||
| test("-980/100", () => { | ||
| assert.equal(isProperFraction(-980, 100), false); | ||
| }); | ||
|
|
||
| test("-19768/9 is improper", () => { | ||
| assert.equal(isProperFraction(-19768, 9), false); | ||
| }); | ||
|
|
||
| test("-8/5 is improper", () => { | ||
| assert.equal(isProperFraction(-8, 5), false); | ||
| }); | ||
| }); |
65 changes: 59 additions & 6 deletions
65
implement-and-rewrite-tests/implement-tests-with-node-test/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,16 +1,69 @@ | ||
| import assert from "node:assert"; | ||
| import test from "node:test"; | ||
| import test, {describe} from "node:test"; | ||
|
|
||
| import { getCardValue } from "../implement/3-get-card-value.js"; | ||
|
|
||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| describe ("Valid Face cards", () => { | ||
| test("Check Ace", () => { | ||
| assert.equal(getCardValue("A♠"), 11); | ||
| }); | ||
| test("Check King", () => { | ||
| assert.equal(getCardValue("K♠"), 10); | ||
| }) | ||
| test("Check Queen", () => { | ||
| assert.equal(getCardValue("Q♠"), 10); | ||
| }) | ||
| test("Check Jack", () => { | ||
| assert.equal(getCardValue("J♠"), 10); | ||
| }) | ||
| }); | ||
|
|
||
| test("Valid single-digit card", () => { | ||
| assert.equal(getCardValue("9♠"), 9); | ||
| describe ("Valid number cards", () => { | ||
| test("Check 2", () => { | ||
| assert.equal(getCardValue("2♠"), 2); | ||
| }); | ||
| test("Check 5", () => { | ||
| assert.equal(getCardValue("5♠"), 5); | ||
| }) | ||
| test("Check 7", () => { | ||
| assert.equal(getCardValue("7♠"), 7); | ||
| }) | ||
| test("Check 10", () => { | ||
| assert.equal(getCardValue("10♠"), 10); | ||
| }); | ||
| }); | ||
|
|
||
| test("Arbitrary non-card string", () => { | ||
| describe ("Invalid cards", () => { | ||
| test("Arbitrary non-card string", () => { | ||
| assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error"); | ||
| }); | ||
| test("Out of bounds: 1♥", () => { | ||
| assert.throws(() => getCardValue("1♥"), /Expected a number followed by a suit, but got "1♥"/); | ||
| }); | ||
| test("Out of bound: 11♥", () => { | ||
| assert.throws(() => getCardValue("11♥"), /Expected a number followed by a suit, but got "11♥"/); | ||
| }); | ||
| }); | ||
|
|
||
| // TODO: What other invalid card cases can you think of? | ||
| describe("Invalid card suits", () => { | ||
| test("Arbitrary non-card string", () => { | ||
| assert.throws( | ||
| () => getCardValue("2O"), | ||
| /Expected card suit to be: "♠", "♥", "♦", "♣" but got "2O"/, | ||
| "Expected clear error" | ||
| ); | ||
| }); | ||
| test("Out of bounds: 1d", () => { | ||
| assert.throws( | ||
| () => getCardValue("8d"), | ||
| /Expected card suit to be: "♠", "♥", "♦", "♣" but got "8d"/ | ||
| ); | ||
| }); | ||
| test("Out of bound: 11X", () => { | ||
| assert.throws( | ||
| () => getCardValue("9X"), | ||
| //`Expected card suit to be: "♠", "♥", "♦", "♣" but got ${cardSuit}` | ||
| /Expected card suit to be: "♠", "♥", "♦", "♣" but got "9X"/ | ||
| ); | ||
| }); | ||
| }); |
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
63 changes: 38 additions & 25 deletions
63
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,28 +1,41 @@ | ||
| // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
| /* | ||
| This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
|
|
||
| // Don't forget to write tests in implement-tests-with-node-test. | ||
|
|
||
| // 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. | ||
| 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. | ||
| */ | ||
|
|
||
| export function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| } | ||
| const cardValue = card.slice(0,-1); | ||
| const cardSuit = card.slice(-1); | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
| const validNumberCards = ["2", "3", "4", "5", "6", "7", "8", "9", "10"] | ||
| const validFaceCards = ["A", "K", "Q", "J"]; | ||
| const cardValueNumber = Number(cardValue) | ||
| if (!validNumberCards.includes(cardValue) && !validFaceCards.includes(cardValue)){ | ||
| throw new Error(`Expected a number followed by a suit, but got "${card}"`); | ||
| } else if (!validSuits.includes(cardSuit)) { | ||
| throw new Error(`Expected card suit to be: "♠", "♥", "♦", "♣" but got "${card}"`); | ||
| } | ||
| if (cardValue === "A") { | ||
| return 11; | ||
| } else if (validFaceCards.includes(cardValue)) { | ||
| return 10; | ||
| } else { | ||
| return cardValueNumber; | ||
| } | ||
| } |
Oops, something went wrong.
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.