-
-
Notifications
You must be signed in to change notification settings - Fork 335
Sheffield | ITP-Jan-26 | Hayriye Saricicek | Sprint 3 | Implement and rewrite tests #1266
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
32407f9
01ca25c
77ebe17
1af10d6
2deb27e
01b8b9c
eaaccca
23b24d4
39e1bfc
126aa36
767fe59
2a7ca83
9bf2f4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| node_modules | ||
| .DS_Store | ||
| .vscode | ||
| **/.DS_Store | ||
| .vscode/ | ||
| **/.DS_Store |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,40 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| } | ||
| if (typeof card !== "string" || card.length < 2) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
| // Handle "10" which is like "10♥" | ||
| if (card.startsWith("10")) { | ||
| return 10; | ||
| } | ||
|
Comment on lines
+40
to
+46
Contributor
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. Can you test your function with these invalid cases?
Author
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. I have amended it to include these as invalid cases too. |
||
| //if more than 2 characters and not starting with a 10 card | ||
| if (card.length > 2) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| // remaining cards must be 2 characters long | ||
|
|
||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const suit = card[card.length - 1]; | ||
|
|
||
| const firstChar = card[0]; | ||
|
|
||
| // check if picture cards | ||
| if (firstChar === "A") return 11; | ||
| if (firstChar === "J" || firstChar === "Q" || firstChar === "K") return 10; | ||
|
|
||
| // check if number is between 2 and 9, 10 has already been checked for and there should be no other valid cards | ||
|
|
||
| const num = Number(firstChar); | ||
|
|
||
| if (!isNaN(num) && num >= 2 && num <= 9) { | ||
| return num; | ||
| // for everything else | ||
| } else { | ||
| throw new Error("Invalid card"); | ||
| } | ||
| } | ||
| // 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; | ||
|
|
@@ -40,13 +71,52 @@ function assertEquals(actualOutput, targetOutput) { | |
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("10♥"), 10); | ||
| assertEquals(getCardValue("J♥"), 10); | ||
| assertEquals(getCardValue("A♠"), 11); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| assertEquals(getCardValue("K♣"), 10); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("♠J"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("invalid"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("22"); | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.log(getCardValue("9♠")); | ||
| console.log(getCardValue("10♥")); | ||
| console.log(getCardValue("J♥")); | ||
| console.log(getCardValue("A♠")); | ||
| console.log(getCardValue("Q♦")); | ||
| console.log(getCardValue("K♣")); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| try { | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| // What other invalid card cases can you think of? | ||
|
|
||
| // There could be cards with special characters. | ||
|
|
||
| // There could be cards with two numbers rather than a number and a suite | ||
| // These will not be picked up because the code only checks for if starts with 10 or if the first character | ||
| // is a number between 2 and 9, so cards like "22" would be valid because the first number | ||
| // is 2, but the second character is not checked for validity. It is also 2 characters | ||
| // so will not cause an error when the length is checked. | ||
|
|
||
| // Since the second character is not checked it could be 2D which is not a valid card but | ||
| // would be accepted because the first character is 2 and the second character is not checked for validity | ||
|
|
||
| // When the card is checked if it begins with 10 it does check if it has a valid suite | ||
| // as only the first 2 characters are checked so it could be 10DEVON or 10♥♥. | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,50 @@ | |
| // 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"); | ||
| expect(getAngleType(91)).toEqual("Obtuse angle"); | ||
|
||
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| test(`should return "Right angle" when (angle === 90)`, () => { | ||
| // Test various right angles, including boundary cases | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| expect(getAngleType(89)).toEqual("Acute angle"); | ||
| expect(getAngleType(91)).toEqual("Obtuse angle"); | ||
| }); | ||
|
|
||
| // Case 3: Obtuse angles | ||
| test(`should return "Obtuse angle" when (angle > 90) && when (angle < 180)`, () => { | ||
| // Test various obtuse angles, including boundary cases | ||
| expect(getAngleType(91)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(150)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(189)).toEqual("Reflex angle"); | ||
| }); | ||
|
|
||
| // Case 4: Straight angle | ||
| test(`should return "Straight angle" when (angle === 180)`, () => { | ||
| // Test various straight angles, including boundary cases | ||
| expect(getAngleType(180)).toEqual("Straight angle"); | ||
| expect(getAngleType(179)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(181)).toEqual("Reflex angle"); | ||
| }); | ||
|
|
||
| // Case 5: Reflex angles | ||
| test(`should return "Reflex angle" when (angle > 180)`, () => { | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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" when (angle < 0 || angle >= 360)`, () => { | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expect(getAngleType(-1)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(361)).toEqual("Invalid angle"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,37 @@ | ||
| // 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 { createTestScheduler } = require("jest"); | ||
| 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`, () => { | ||
| 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: Face Cards (J, Q, K) | ||
| test(`Should return 10 when given a Jack, Queen or King card`, () => { | ||
| expect(getCardValue("J♥")).toEqual(10); | ||
| expect(getCardValue("Q♦")).toEqual(10); | ||
| expect(getCardValue("K♣")).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: Number Cards (2-10) | ||
| test(`Should return the value of number cards (2-10)`, () => { | ||
| expect(getCardValue("2♠")).toEqual(2); | ||
| expect(getCardValue("5♥")).toEqual(5); | ||
| expect(getCardValue("10♥")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 4: Invalid Cards | ||
| test(`Should throw an error when given an invalid card`, () => { | ||
| expect(() => getCardValue("♠J")).toThrow(); | ||
| expect(() => getCardValue("invalid")).toThrow(); | ||
| expect(() => getCardValue("12♠")).toThrow(); | ||
| expect(() => getCardValue("1")).toThrow(); | ||
| }); | ||
|
|
||
| // when I tested with test(`Should throw an error when given an invalid card`, () => { | ||
| // expect(() => getCardValue("22")).toThrow(); it did not throw an error because 22 passes the | ||
| // test of first number between and 9 and being 2 characters long, | ||
| // but it is not a valid card because the second character is not a valid suite. | ||
| // The second character is not checked. |
Uh oh!
There was an error while loading. Please reload this page.