-
-
Notifications
You must be signed in to change notification settings - Fork 275
Glasgow | 25-ITP-SEP | Alaa Tagi | Sprint 3 | Coursework/sprint 3 practice tdd #758
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
b5de08b
b5fa562
3898643
78d374a
d07dd99
a223e45
ef42f92
b02a77f
3f11ed4
d7507ff
2eaa92c
73c8fcf
52660c9
408f2e1
1f0b098
d276533
ce08a1f
874c5ca
ea944a1
d25945b
5e24460
d269acc
62bf3a9
6f9d56b
b1c0345
8ff02d3
2aafdb6
c491953
0f0c704
9e74422
099a76f
2db8252
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,5 +1,17 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| if ( | ||
| typeof stringOfCharacters !== "string" || | ||
| typeof findCharacter !== "string" || | ||
| findCharacter.length !== 1 | ||
| ) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (stringOfCharacters.length === 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return stringOfCharacters.split(findCharacter).length - 1; | ||
| } | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,8 @@ | |
| const countChar = require("./count"); | ||
| // Given a string str and a single character char to search for, | ||
| // When the countChar function is called with these inputs, | ||
| // Then it should: | ||
| // Then it should: correctly count the occurrences of char in str. | ||
|
|
||
|
|
||
| // Scenario: Multiple Occurrences | ||
| // Given the input string str, | ||
|
|
@@ -17,8 +18,89 @@ test("should count multiple occurrences of a character", () => { | |
| expect(count).toEqual(5); | ||
| }); | ||
|
|
||
|
|
||
| // Scenario: No Occurrences | ||
| // Given the input string str, | ||
| // And a character char that does not exist within the case-sensitive str, | ||
| // When the function is called with these inputs, | ||
| // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. | ||
|
|
||
| test("should return 0 for no occurrences", () => { | ||
| const str = "hello"; | ||
| const char = "z"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
|
|
||
| // Scenario: Multiple occurrences | ||
| test("should count multiple occurrences of a character", () => { | ||
| const str = "aaaaa"; | ||
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(5); | ||
| }); | ||
|
|
||
| // Scenario: No occurrences | ||
| test("should return 0 for no occurrences", () => { | ||
| const str = "hello"; | ||
| const char = "z"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
| // Scenario: Empty string | ||
| test("should return 0 when input string is empty", () => { | ||
| const str = ""; | ||
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
| // Scenario: Empty character | ||
| test("should return 0 when character input is empty", () => { | ||
| const str = "hello"; | ||
| const char = ""; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
| // Scenario: Character longer than 1 | ||
| test("should return 0 when character input is longer than one character", () => { | ||
| const str = "hello"; | ||
| const char = "ll"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
| // Scenario: char string longer than str string | ||
| test("should return 0 when char is longer than the input string", () => { | ||
| const str = "a"; | ||
| const char = "abc"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
| // Scenario: Non-string inputs | ||
| test("should return 0 when inputs are not strings", () => { | ||
| expect(countChar(12345, "1")).toEqual(0); | ||
| expect(countChar("12345", 1)).toEqual(0); | ||
| expect(countChar(null, "a")).toEqual(0); | ||
| expect(countChar("hello", undefined)).toEqual(0); | ||
| }); | ||
|
Comment on lines
+76
to
+90
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. This is fine but when you read this function's purpose, you might think that if char is longer than str, it should perhaps throw an error as there is no way to count char occurrence in this case. It's fine to leave Same for |
||
|
|
||
| // Scenario: Case sensitivity | ||
| test("should count only exact case matches", () => { | ||
|
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. Very thoughtful test case! |
||
| const str = "AaAaA"; | ||
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(2); | ||
| }); | ||
|
|
||
| // Scenario: Spaces and special characters | ||
| test("should correctly count spaces and symbols", () => { | ||
| const str = "a b c d e ! !"; | ||
| const char = " "; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(6); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,34 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| if (typeof num !== "number" || isNaN(num)) { | ||
| return "Invalid input"; | ||
| } | ||
| const sign = num < 0 ? "-" : ""; | ||
| num = Math.abs(num); | ||
|
|
||
| // Handle decimals | ||
| if (!Number.isInteger(num)) { | ||
| return sign + num + "th"; | ||
| } | ||
|
|
||
| const lastTwo = num % 100; | ||
| const lastOne = num % 10; | ||
|
|
||
| // Handle special cases 11th, 12th, 13th | ||
| if (lastTwo >= 11 && lastTwo <= 13) { | ||
| return sign + num + "th"; | ||
| } | ||
|
|
||
| // Normal ordinal rules | ||
| switch (lastOne) { | ||
| case 1: | ||
| return sign + num + "st"; | ||
| case 2: | ||
| return sign + num + "nd"; | ||
| case 3: | ||
| return sign + num + "rd"; | ||
| default: | ||
| return sign + num + "th"; | ||
| } | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
| module.exports = getOrdinalNumber; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,87 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| test("should return '1st' for 1", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| }); | ||
|
|
||
|
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. I like these test cases but again, what are other expected/unexpected inputs your function can receive and how will it behave? For example, what if num is undefined, a boolean, etc.? That's just two examples of num being not of a type you expect. |
||
| /// Case 2: Identify the ordinal number for 2 | ||
| // When the number is 2, | ||
| // Then the function should return "2nd" | ||
| test("should return '2nd' for 2", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| }); | ||
|
|
||
| // Case 3: Identify the ordinal number for 3 | ||
| // When the number is 3, | ||
| // Then the function should return "3rd" | ||
| test("should return '3rd' for 3", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| }); | ||
|
|
||
| // Case 4: Identify the ordinal number for 4 | ||
| // When the number is 4, | ||
| // Then the function should return "4th" | ||
| test("should return '4th' for 4", () => { | ||
| expect(getOrdinalNumber(4)).toEqual("4th"); | ||
| }); | ||
|
|
||
| // Case 5: Identify the ordinal number for 11 | ||
| // When the number is 11, | ||
| // Then the function should return "11th" | ||
| test("should return '11th' for 11", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| }); | ||
|
|
||
| // Case 6: Identify the ordinal number for 22 | ||
| // When the number is 22, | ||
| // Then the function should return "22nd" | ||
| test("should return '22nd' for 22", () => { | ||
| expect(getOrdinalNumber(22)).toEqual("22nd"); | ||
| }); | ||
|
|
||
| // Case 7: Identify the ordinal number for 33 | ||
| // When the number is 33, | ||
| // Then the function should return "33rd" | ||
| test("should return '33rd' for 33", () => { | ||
| expect(getOrdinalNumber(33)).toEqual("33rd"); | ||
| }); | ||
|
|
||
| // Case 8: Undefined input | ||
| // When the input is undefined, | ||
| // Then the function should return "Invalid input" | ||
| test("should handle undefined input gracefully", () => { | ||
| expect(getOrdinalNumber(undefined)).toEqual("Invalid input"); | ||
| }); | ||
|
|
||
| // Case 9: Null input | ||
| // When the input is null, | ||
| // Then the function should return "Invalid input" | ||
| test("should handle null input gracefully", () => { | ||
| expect(getOrdinalNumber(null)).toEqual("Invalid input"); | ||
| }); | ||
|
|
||
| // Case 10: Boolean input | ||
| // When the input is a boolean value, | ||
| // Then the function should return "Invalid input" | ||
| test("should handle boolean input gracefully", () => { | ||
| expect(getOrdinalNumber(true)).toEqual("Invalid input"); | ||
| }); | ||
|
|
||
| // Case 11: Non-numeric string input | ||
| // When the input is a non-numeric string, | ||
| // Then the function should return "Invalid input" | ||
| test("should handle non-numeric string input gracefully", () => { | ||
| expect(getOrdinalNumber("hello")).toEqual("Invalid input"); | ||
| }); | ||
|
|
||
| // Case 12: Negative number | ||
| // When the input is a negative number, | ||
| // Then the function should return the ordinal form with a negative sign | ||
| test("should return '-1st' for -1", () => { | ||
| expect(getOrdinalNumber(-1)).toEqual("-1st"); | ||
| }); | ||
|
|
||
| // Case 13: Floating number | ||
| // When the input is a floating number, | ||
| // Then the function should return the ordinal with decimal intact | ||
| test("should handle decimal numbers", () => { | ||
| expect(getOrdinalNumber(21.5)).toEqual("21.5th"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(str, count) { | ||
| if (str === undefined || count === undefined){ | ||
| throw new Error ("argument must be defined") | ||
| } | ||
| if (count < 0) { | ||
| throw new Error("Count must be a non-negative integer"); | ||
| } | ||
|
|
||
| let result = ""; | ||
| for (let i = 0; i < count; i++) { | ||
| result += str; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| module.exports = repeat; | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| const repeat = require("./repeat"); | ||
| // Given a target string str and a positive integer count, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should: | ||
| // Then it should: repeat the str count times and return a new string containing the repeated str values. | ||
|
|
||
| // case: repeat String: | ||
| // Given a target string str and a positive integer count, | ||
|
|
@@ -11,22 +11,80 @@ const repeat = require("./repeat"); | |
|
|
||
| test("should repeat the string count times", () => { | ||
| const str = "hello"; | ||
| const count = 3; | ||
| const count = 4; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("hellohellohello"); | ||
| expect(repeatedStr).toEqual("hellohellohellohello"); | ||
| }); | ||
|
|
||
| // case: handle Count of 1: | ||
|
|
||
| // case: Handle Count of 1 : | ||
| // Given a target string str and a count equal to 1, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. | ||
| // Then it should return the original str without repetition. | ||
| test("should return the original string when count is 1", () => { | ||
|
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. This test suit are more comprehensive but can you think of other test 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. Hi @jennethydyrova i have fixed all the errors that you have mentioned to me.
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. Have you tried running the tests you wrote?
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. If you try to run your unit tests, you will see that some of them are failing both for this function and getOrdinalNumber. Can you try to fix tests/functions by reading output produced when you run tests?
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. Yes, I have and it passed all the tests.
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. The unit tests in your screenshots do not match those in the PR. Take a look at and on the screenshot you sent the last test is
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. @jennethydyrova,
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. Please always double check if you pushed your local changes, deleted all scratch work (including console logs) and ran all unit tests before requesting another round of review. It will reduce time I spend on reviewing your code and you going back to the same PR over and over again. File changes tab is quite useful for checking if you code is up to standard. I don't see any new changes. I assume you made some local changes but you haven't pushed them.
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. @jennethydyrova, I am sorry and I appreciate your time. But I am so confused too I make the changes, do push as usual and in here every thing appears.
Like this pic here it shows that the changes has been push and I do not see any unstage files. Thanks for you patient with me
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. No worries. Okay, I can see these changes and I can see from the function implementation, that this particular unit test on the screenshot you sent will fail but you can understand this by just running unit tests. You have these changes locally, right? If you have same changes locally and you run unit tests, some of them will fail in |
||
| const str = "hello"; | ||
| const count = 1; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("hello"); | ||
| }); | ||
|
|
||
| // case: Handle Count of 0: | ||
| // Given a target string str and a count equal to 0, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should return an empty string, ensuring that a count of 0 results in an empty output. | ||
| // case: Handle Count of 0 : | ||
| // When count is 0, it should return an empty string. | ||
| test("should return an empty string when count is 0", () => { | ||
| const str = "hello"; | ||
| const count = 0; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual(""); | ||
| }); | ||
|
|
||
| // case: Negative Count: | ||
| // Given a target string str and a negative integer count, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should throw an error or return an appropriate error message, as negative counts are not valid. | ||
| // case: Negative Count : | ||
| // When count is negative, it should throw an error. | ||
| test("should throw an error when count is negative", () => { | ||
| const str = "hello"; | ||
| const count = -2; | ||
| expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); | ||
| }); | ||
|
|
||
|
|
||
| // case: Non-string input for str : | ||
| // When str is not a string (e.g., number), it should still repeat it as a string. | ||
| test("should convert non-string input to string before repeating", () => { | ||
| const str = 123; | ||
| const count = 2; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("123123"); | ||
| }); | ||
|
|
||
| // case: Non-integer count : | ||
| // When count is a non-integer (e.g., 2.5), it should repeat only the integer part. | ||
| test("should handle non-integer count by truncating it", () => { | ||
| const str = "hi"; | ||
| const count = 2.5; | ||
| const repeatedStr = repeat(str, Math.floor(count)); | ||
| expect(repeatedStr).toEqual("hihi"); | ||
| }); | ||
|
|
||
| // case: Empty string input : | ||
| // When str is empty, the result should always be empty, regardless of count. | ||
| test("should return an empty string when str is empty", () => { | ||
| const str = ""; | ||
| const count = 5; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual(""); | ||
| }); | ||
|
|
||
| // case: Undefined or missing arguments : | ||
| // When arguments are missing or undefined, it should throw an error. | ||
| test("should handle undefined inputs gracefully", () => { | ||
| expect(() => repeat(undefined, 3)).toThrow("argument must be defined"); | ||
|
|
||
| }); | ||
|
|
||
| // case: Boolean inputs : | ||
| // When str or count are booleans, it should convert str to string and handle count normally. | ||
| test("should handle boolean values as inputs", () => { | ||
| const str = true; | ||
| const count = 2; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("truetrue"); | ||
| }); | ||

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.
This test is good but it doesn't test different possible function inputs. What if the inputs' types are not strings, the
charstring is longer than thestrstring, or you have an empty string for either of the inputs, etc.? Testing different inputs will show whether your function accounts for different scenarios, which is what makes a function robust.