Repository navigation
Manchester | 26-ITP-Sept | Rahana Suleiman | Sprint 2 | Implement and rewrite tests #1702
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 all commits
21ded3e
922023b
4bf1349
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 |
|---|---|---|
|
|
@@ -24,5 +24,28 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| export function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| if (typeof card !== "string") { | ||
| throw new Error(`Expected a string, but got ${typeof card}`); | ||
| } | ||
|
|
||
| const cardRegex = /^(10|[2-9]|[AJQK])([♠♥♦♣])$/; | ||
|
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. Nice! Very clean approach. |
||
| const match = card.match(cardRegex); | ||
|
|
||
| if (!match) { | ||
| throw new Error( | ||
| `Expected a card rank followed by a suit, but got "${card}"` | ||
| ); | ||
| } | ||
|
|
||
| const rank = match[1]; | ||
|
|
||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
|
|
||
| if (["J", "Q", "K"].includes(rank)) { | ||
| return 10; | ||
| } | ||
|
|
||
| return Number(rank); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,16 +3,43 @@ import { getAngleType } from "../implement/1-get-angle-type.js"; | |
| // 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(89.9)).toEqual("Acute angle"); | ||
|
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. Nice idea to test decimals close to the boundary! Is there a particular reason to replace 89 rather than keep both values? |
||
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| test(`should return "Right angle" when angle is exactly 90`, () => { | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }); | ||
|
|
||
| // Case 3: Obtuse angles | ||
| test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { | ||
| expect(getAngleType(90.1)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(135)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(179.9)).toEqual("Obtuse angle"); | ||
| }); | ||
|
|
||
| // Case 4: Straight angle | ||
| test(`should return "Straight angle" when angle is exactly 180`, () => { | ||
| expect(getAngleType(180)).toEqual("Straight angle"); | ||
| }); | ||
|
|
||
| // Case 5: Reflex angles | ||
| test(`should return "Reflex angle" when (180 < angle < 360)`, () => { | ||
| expect(getAngleType(180.1)).toEqual("Reflex angle"); | ||
| expect(getAngleType(270)).toEqual("Reflex angle"); | ||
| expect(getAngleType(359.9)).toEqual("Reflex angle"); | ||
| }); | ||
|
|
||
| // Case 6: Invalid angles | ||
| test(`should return "Invalid angle" when angle is <= 0 or >= 360`, () => { | ||
| expect(getAngleType(0)).toEqual("Invalid angle"); | ||
| expect(getAngleType(-45)).toEqual("Invalid angle"); | ||
| expect(getAngleType(360)).toEqual("Invalid angle"); | ||
| expect(getAngleType(400)).toEqual("Invalid angle"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,57 @@ import { getCardValue } from "../implement/3-get-card-value.js"; | |
|
|
||
| // 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); | ||
| 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 numeric value for single-digit number cards", () => { | ||
| expect(getCardValue("2♠")).toEqual(2); | ||
| expect(getCardValue("5♥")).toEqual(5); | ||
| expect(getCardValue("9♦")).toEqual(9); | ||
| }); | ||
|
|
||
| test("Should return 10 for '10' cards across all suits", () => { | ||
| expect(getCardValue("10♥")).toEqual(10); | ||
| expect(getCardValue("10♣")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 3: Face Cards (J, Q, K) | ||
| test("Should return 10 for face cards (J, Q, K)", () => { | ||
| 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 arbitrary non-card strings", () => { | ||
| expect(() => getCardValue("invalid")).toThrow(); | ||
|
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 think of how these test that expect throw can be made more robust? |
||
| }); | ||
|
|
||
| test("Should throw an error for invalid ranks or suit-like characters", () => { | ||
| expect(() => getCardValue("1♠")).toThrow(); | ||
| expect(() => getCardValue("11♥")).toThrow(); | ||
| expect(() => getCardValue("Z♠")).toThrow(); | ||
| }); | ||
|
|
||
| test("Should throw an error for missing or invalid suit", () => { | ||
| expect(() => getCardValue("10")).toThrow(); | ||
| expect(() => getCardValue("AX")).toThrow(); | ||
| }); | ||
|
|
||
| test("Should throw an error when suit is before rank", () => { | ||
| expect(() => getCardValue("♠A")).toThrow(); | ||
| }); | ||
|
|
||
| test("Should throw an error for non-string or empty inputs", () => { | ||
| expect(() => getCardValue("")).toThrow(); | ||
| expect(() => getCardValue(10)).toThrow(); | ||
| }); | ||
|
|
||
| // 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 | ||
|
|
||
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.
Is there a particular reason why you updated the provided test here?