From fdea2ddb62f284d814d229db45426cd97d610c1e Mon Sep 17 00:00:00 2001 From: FarancisGH Date: Sun, 8 Mar 2026 00:48:59 +0000 Subject: [PATCH] Ajustments made on Sprint-3/2-practice-tdd to make the tests pass. The functions were implemented in a way that they return the expected output for the given test cases. --- Sprint-3/2-practice-tdd/count.test.js | 38 ++++++++++++++-- .../2-practice-tdd/get-ordinal-number.test.js | 43 +++++++++++++++++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 25 +++++++++-- 3 files changed, 95 insertions(+), 11 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..05121108d 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -11,10 +11,10 @@ const countChar = require("./count"); // Then it should correctly count occurrences of `char`. test("should count multiple occurrences of a character", () => { - const str = "aaaaa"; - const char = "a"; - const count = countChar(str, char); - expect(count).toEqual(5); +const str = "aaaaa"; +const char = "a"; +const count = countChar(str, char); +expect(count).toEqual(5); }); // Scenario: No Occurrences @@ -22,3 +22,33 @@ test("should count multiple occurrences of a character", () => { // And a character `char` that does not exist within `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. +test("should return 0 when character does not occur in the string", () => { +const str = "hello world"; +const char = "x"; +const count = countChar(str, char); +expect(count).toEqual(0); +}); + +// Scenario: Empty String +// Given an empty string `str`, +// And any character `char` (e.g., 'a'), +// When the function is called with these inputs, +// Then it should return 0, as there are no characters to count in an empty string. +test("should return 0 when the input string is empty", () => { +const str = ""; +const char = "a"; +const count = countChar(str, char); +expect(count).toEqual(0); +}); + +// Scenario: Case Sensitivity +// Given the input string `str`, +// And a character `char` that exists in `str` but with different cases (e.g., 'a' and 'A'), +// When the function is called with these inputs, +// Then it should count occurrences of `char` in a case-sensitive manner, treating uppercase and lowercase characters as distinct. +test("should count characters in a case-sensitive manner", () => { +const str = "AaAaA"; +const char = "a"; +const count = countChar(str, char); +expect(count).toEqual(3); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index adfa58560..118fb0cb3 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -14,7 +14,44 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number ends with 1, except those ending with 11, // Then the function should return a string by appending "st" to the number. test("should append 'st' for numbers ending with 1, except those ending with 11", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); - expect(getOrdinalNumber(21)).toEqual("21st"); - expect(getOrdinalNumber(131)).toEqual("131st"); +expect(getOrdinalNumber(1)).toEqual("1st"); +expect(getOrdinalNumber(21)).toEqual("21st"); +expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Case 2: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, +// Then the function should return a string by appending "nd" to the number. +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { +expect(getOrdinalNumber(2)).toEqual("2nd"); +expect(getOrdinalNumber(22)).toEqual("22nd"); +expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +// Case 3: Numbers ending with 3 (but not 13) +// When the number ends with 3, except those ending with 13, +// Then the function should return a string by appending "rd" to the number. +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { +expect(getOrdinalNumber(3)).toEqual("3rd"); +expect(getOrdinalNumber(23)).toEqual("23rd"); +expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +// Case 4: Numbers ending with 11, 12, or 13 +// When the number ends with 11, 12, or 13, +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for numbers ending with 11, 12, or 13", () => { +expect(getOrdinalNumber(11)).toEqual("11th"); +expect(getOrdinalNumber(12)).toEqual("12th"); +expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// Case 5: All other numbers +// When the number does not end with 1, 2, or 3 (except those ending with 11, 12, or 13), +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for all other numbers", () => { +expect(getOrdinalNumber(4)).toEqual("4th"); +expect(getOrdinalNumber(10)).toEqual("10th"); +expect(getOrdinalNumber(14)).toEqual("14th"); +expect(getOrdinalNumber(100)).toEqual("100th"); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..d43b51773 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -10,23 +10,40 @@ const repeatStr = require("./repeat-str"); // Then it should return a string that contains the original `str` repeated `count` times. test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); +const str = "hello"; +const count = 3; +const repeatedStr = repeatStr(str, count); +expect(repeatedStr).toEqual("hellohellohello"); }); // Case: handle count of 1: // Given a target string `str` and a `count` equal to 1, // When the repeatStr function is called with these inputs, // Then it should return the original `str` without repetition. +test("should return the original string when count is 1", () => { +const str = "world"; +const count = 1; +const repeatedStr = repeatStr(str, count); +expect(repeatedStr).toEqual("world"); +}); // Case: Handle count of 0: // Given a target string `str` and a `count` equal to 0, // When the repeatStr function is called with these inputs, // Then it should return an empty string. +test("should return an empty string when count is 0", () => { +const str = "test"; +const count = 0; +const repeatedStr = repeatStr(str, count); +expect(repeatedStr).toEqual(""); +}); // Case: Handle negative count: // Given a target string `str` and a negative integer `count`, // When the repeatStr function is called with these inputs, // Then it should throw an error, as negative counts are not valid. +test("should throw an error when count is negative", () => { +const str = "error"; +const count = -1; +expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer"); +}); \ No newline at end of file