|
1 | 1 | // implement a function countChar that counts the number of times a character occurs in a string |
2 | 2 | import { countChar } from "./count.js"; |
3 | | -// Given a string `str` and a single character `char` to search for, |
4 | | -// When the countChar function is called with these inputs, |
5 | | -// Then it should: |
6 | 3 |
|
7 | 4 | // Scenario: Multiple Occurrences |
8 | | -// Given the input string `str`, |
9 | | -// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'), |
10 | | -// When the function is called with these inputs, |
11 | | -// Then it should correctly count occurrences of `char`. |
12 | | - |
| 5 | +// Given a string `str` and a character `char` that appears several times, |
| 6 | +// When the function is called, |
| 7 | +// Then it should correctly count each occurrence. |
13 | 8 | test("should count multiple occurrences of a character", () => { |
14 | 9 | const str = "aaaaa"; |
15 | 10 | const char = "a"; |
16 | 11 | const count = countChar(str, char); |
17 | 12 | expect(count).toEqual(5); |
18 | 13 | }); |
19 | 14 |
|
| 15 | +// Scenario: Single Occurrence |
| 16 | +// Given a string containing exactly one match, |
| 17 | +// When the function is called, |
| 18 | +// Then it should return 1. |
| 19 | +test("should count a single occurrence of a character", () => { |
| 20 | + const str = "banana"; |
| 21 | + const char = "n"; |
| 22 | + const count = countChar(str, char); |
| 23 | + expect(count).toEqual(2); |
| 24 | +}); |
| 25 | + |
20 | 26 | // Scenario: No Occurrences |
21 | | -// Given the input string `str`, |
22 | | -// And a character `char` that does not exist within `str`. |
23 | | -// When the function is called with these inputs, |
24 | | -// Then it should return 0, indicating that no occurrences of `char` were found. |
| 27 | +// Given a string with no matching character, |
| 28 | +// When the function is called, |
| 29 | +// Then it should return 0. |
| 30 | +test("should return 0 when the character does not occur", () => { |
| 31 | + const str = "hello"; |
| 32 | + const char = "z"; |
| 33 | + const count = countChar(str, char); |
| 34 | + expect(count).toEqual(0); |
| 35 | +}); |
| 36 | + |
| 37 | +// Edge case: Empty String |
| 38 | +// Given an empty string, |
| 39 | +// When the function is called with any character, |
| 40 | +// Then it should return 0. |
| 41 | +test("should return 0 for an empty string", () => { |
| 42 | + const str = ""; |
| 43 | + const char = "a"; |
| 44 | + const count = countChar(str, char); |
| 45 | + expect(count).toEqual(0); |
| 46 | +}); |
| 47 | + |
| 48 | +// Edge case: Character at start and end |
| 49 | +// Given a string where the character appears at both ends, |
| 50 | +// When the function is called, |
| 51 | +// Then it should count both occurrences. |
| 52 | +test("should count characters at the start and end of the string", () => { |
| 53 | + const str = "ahelloa"; |
| 54 | + const char = "a"; |
| 55 | + const count = countChar(str, char); |
| 56 | + expect(count).toEqual(2); |
| 57 | +}); |
0 commit comments