-
-
Notifications
You must be signed in to change notification settings - Fork 239
NW | 25-ITP-Sep |Ahmad Hmedan | Sprint 3 | Sprint 3 Practice TDD coursework #831
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?
NW | 25-ITP-Sep |Ahmad Hmedan | Sprint 3 | Sprint 3 Practice TDD coursework #831
Conversation
…t tests to check my function
…ne more test and verify all tests with the new function
| test("should return 0 occurrences of b characters ", () => { | ||
| const str = "Ahmad Hmedan"; | ||
| const char = "b"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
| test("should return 1 occurrence of A characters", () => { | ||
| const str = "Ahmad Hmedan"; | ||
| const char = "A"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(1); | ||
| }); | ||
| test("should return 2 occurrence of m characters", () => { | ||
| const str = "Ahmad Hmedan"; | ||
| const char = "m"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(2); | ||
| }); |
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.
The test description usually describe a general case, like the one on line 13. And then for each case we can test multiple samples that belong to that case.
A specific description like this "should return 0 occurrences of b characters" could confuse the the person implementing the function into thinking "what is so special about checking 0 occurrences of 'b' in a string?"
| test("should return 1 occurrence of @ characters in empty string", () => { | ||
| const str = "Ahmadhm@gamil.com"; | ||
| const char = "@"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(1); | ||
| }); |
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.
- The test description does not quite match the test.
- Could consider also generalise this test or combine it with others.
| test("should return '22nd' for 22", () => { | ||
| expect(getOrdinalNumber(22)).toEqual("22nd"); | ||
| }); | ||
| // Case 3: Identify the ordinal number for 73 | ||
| // When the number is 73, | ||
| // Then the function should return 73rd" | ||
|
|
||
| test("should return '73rd' for 73", () => { | ||
| expect(getOrdinalNumber(73)).toEqual("73rd"); | ||
| }); | ||
|
|
||
| // Case 4: Identify the ordinal number for 99 | ||
| // When the number is 99, | ||
| // Then the function should return 99th" | ||
|
|
||
| test("should return '99th' for 99", () => { | ||
| expect(getOrdinalNumber(99)).toEqual("99th"); | ||
| }); | ||
|
|
||
| // Case 5: Identify the ordinal number for number ends with 11,12, or 13 | ||
| // When the number is 111, | ||
| // Then the function should return 111th" | ||
| // When the number is 212, | ||
| // Then the function should return 212th" | ||
| // When the number is 413, | ||
| // Then the function should return 413th" | ||
|
|
||
| test("should return '111th' for 111", () => { | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| }); | ||
| test("should return '212th' for 212", () => { | ||
| expect(getOrdinalNumber(212)).toEqual("212th"); | ||
| }); | ||
| test("should return '413th' for 413", () => { | ||
| expect(getOrdinalNumber(413)).toEqual("413th"); | ||
| }); |
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.
To ensure thorough testing, we need broad scenarios that cover all possible cases.
Listing individual values, however, can quickly lead to an unmanageable number of test cases.
Instead of writing tests for individual numbers, consider grouping all possible input values into meaningful categories.
Then, select representative samples from each category to test. This approach improves coverage and makes our tests easier to maintain.
For example, we can prepare a test for numbers 2, 22, 132, etc. as
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect( getOrdinalNumber(2) ).toEqual("2nd");
expect( getOrdinalNumber(22) ).toEqual("22nd");
expect( getOrdinalNumber(132) ).toEqual("132nd");
});
Sprint-3/2-practice-tdd/repeat.js
Outdated
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(myString, repeatNumber) { | ||
| if (repeatNumber < 0) return "Invalid Input must be a positive number"; |
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.
How would the caller distinguish the result of the following two function calls?
repeat("Invalid Input must be a positive number", 1)repeat("", -1)
Both function calls return the same value.
| // function repeat(myString, repeatNumber) { | ||
| // if (repeatNumber < 0) return "Invalid Input must be a positive number"; | ||
| // let str = ""; | ||
| // for (let i = 0; i < repeatNumber; i++) { | ||
| // str += myString; | ||
| // } | ||
| // return str; | ||
| // } |
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 works too.
While knowing how to use built-in functions can improve productivity and performance, it is equally important to know how to implement such a function without relying on any built-in function.
| test("should return empty when the count is negative", () => { | ||
| const str = "CYF"; | ||
| const count = -2; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("Invalid Input must be a positive number"); | ||
| }); |
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.
If you modified repeat() to throw an error when count is negative, and you wanted to test if the function can throw an error as expected, you can use .toThrow(). You can find out more about how to use .toThrow() here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)
|
Changes look good. I wished I have the super power to interpret what your emoji means. |
Learners, PR Template
Self checklist
Changelist
I’ve implemented all the required functions along with comprehensive Jest tests that aim to cover all possible cases. I’m open to any feedback that could help me improve my skills.
Questions
There are two questions in the repeat.js file. Thanks in advance.