diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index 9e05a871e2..b6ea5c375a 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -15,7 +15,19 @@ // 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. @@ -31,7 +43,10 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles -const right = getAngleType(90); -assertEquals(right, "Right angle"); +assertEquals(getAngleType(90), "Right angle"); +assertEquals(getAngleType(45), "Acute angle"); +assertEquals(getAngleType(91), "Obtuse angle"); +assertEquals(getAngleType(180), "Straight angle"); +assertEquals(getAngleType(185), "Reflex angle"); +assertEquals(getAngleType(365), "Invalid angle"); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index 970cb9b641..342b3ba67a 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -11,7 +11,14 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (isNaN(numerator) && isNaN(denominator)) { + return false; + } + + if (numerator < denominator) { + return true; + } + return false; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -26,8 +33,11 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(2, 2), false); +assertEquals(isProperFraction(3, 2), false); +assertEquals(isProperFraction(4312, 5000), true); +assertEquals(isProperFraction("Invalid", "data"), false); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index ff5c532e1d..20222ad288 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -22,7 +22,31 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const lastChar = card.length - 1; + const rank = card.slice(0, lastChar); + const suit = card.slice(lastChar); + + if (rank === "10") { + return 10; + } + + if (card.length > 2) { + throw new Error("Invalid input length"); + } + + if (!(suit === "♠" || suit === "♥" || suit === "♦" || suit === "♣")) { + throw new Error("Invalid suit ", suit); + } + + if (rank == "A") { + return 11; + } else if (rank == "J" || rank == "Q" || rank == "K") { + return 10; + } else if (!isNaN(rank)) { + return Number(rank); + } else { + throw new Error("Invalid card"); + } } // The line below allows us to load the getCardValue function into tests in other files. diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index d777f348d3..2da7b6d62b 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -14,7 +14,36 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when (angle == 90)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test(`should return "Obtuse" when (90 < angle < 180)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(140)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when (angle == 180)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(220)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle"`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(361)).toEqual("Invalid angle"); + expect(getAngleType("Test data")).toEqual("Invalid angle"); +}); diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index 7f087b2ba1..9e922c4456 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -7,4 +7,9 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); // Special case: numerator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(2, 2)).toEqual(false); + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(3, 4)).toEqual(true); + expect(isProperFraction(4312, 5000)).toEqual(true); + expect(isProperFraction("invalid", "data")).toEqual(false); }); diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index cf7f9dae2e..bd82f9a679 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -7,6 +7,30 @@ const getCardValue = require("../implement/3-get-card-value"); // Case 1: Ace (A) test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); + expect(() => getCardValue("AA♠")).toThrow(Error); +}); + +// Case 2 +test(`Should return 10 when given an jack, queen, king`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♥")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); + expect(() => getCardValue("K🤣")).toThrow(Error); + expect(() => getCardValue("KKW🤣")).toThrow(Error); +}); + +// Case 3 +test(`Should return number when given a number`, () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("10♥")).toEqual(10); + expect(() => getCardValue("423🤣")).toThrow(Error); +}); + +test("Should return error on invalid input", () => { + expect(() => getCardValue(null)).toThrow(Error); + expect(() => getCardValue(undefined)).toThrow(Error); + expect(() => getCardValue(12398)).toThrow(Error); + expect(() => getCardValue(NaN)).toThrow(Error); }); // Suggestion: Group the remaining test data into these categories: @@ -17,4 +41,3 @@ test(`Should return 11 when given an ace card`, () => { // 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 - diff --git a/Sprint-3/1-implement-and-rewrite-tests/testing-guide.md b/Sprint-3/1-implement-and-rewrite-tests/testing-guide.md index 917194e7a9..062fe50af9 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/testing-guide.md +++ b/Sprint-3/1-implement-and-rewrite-tests/testing-guide.md @@ -7,10 +7,11 @@ Input ──▶ Function ──▶ Output ``` A function + - Takes **input** (via **arguments**) - Does some work - Produces **one output** (via a **return value**) - + Example: ``` @@ -19,17 +20,18 @@ sum(2, 3) → 5 Important idea: the same input should produce the same output. - ## 2. Testing Means Predicting -Testing means: -> If I give this input, what output should I get? +Testing means: +> If I give this input, what output should I get? ## 3. Choosing Good Test Values ### Step 1: Determining the space of possible inputs + Ask: + - What type of value is expected? - What values make sense? - If they are numbers: @@ -39,7 +41,7 @@ Ask: - What are their length and patterns? - What values would not make sense? -### Step 2: Choosing Good Test Values +### Step 2: Choosing Good Test Values #### Normal Cases @@ -48,10 +50,9 @@ These confirm that the function works in normal use. - What does a typical, ordinary input look like? - Are there multiple ordinary groups of inputs? e.g. for an age checking function, maybe there are "adults" and "children" as expected ordinary groups of inputs. - #### Boundary Cases -Test values exactly at, just inside, and just outside defined ranges. +Test values exactly at, just inside, and just outside defined ranges. These values are where logic breaks most often. #### Consider All Outcomes @@ -64,6 +65,7 @@ Every outcome must be reached by at least one test. #### Crossing the Edges and Invalid Values This tests how the function behaves when assumptions are violated. + - What happens when input is outside of the expected range? - What happens when input is not of the expected type? - What happens when input is not in the expected format? @@ -73,18 +75,19 @@ This tests how the function behaves when assumptions are violated. ### 1. Using `console.assert()` ```javascript - // Report a failure only when the first argument is false - console.assert( sum(4, 6) === 10, "Expected 4 + 6 to equal 10" ); +// Report a failure only when the first argument is false +console.assert(sum(4, 6) === 10, "Expected 4 + 6 to equal 10"); ``` It is simpler than using `if-else` and requires no setup. - + ### 2. Jest Testing Framework ```javascript + describe("Description for this case used for grouping") test("Should correctly return the sum of two positive numbers", () => { expect( sum(4, 6) ).toEqual(10); - ... // Can test multiple samples + ... // Can test multiple samples }); ```