Skip to content

Commit d8aad19

Browse files
committed
Add countChar edge-case tests and validation
1 parent affdd56 commit d8aad19

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

‎practice-tdd/count.test.js‎

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
22
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:
63

74
// 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.
138
test("should count multiple occurrences of a character", () => {
149
const str = "aaaaa";
1510
const char = "a";
1611
const count = countChar(str, char);
1712
expect(count).toEqual(5);
1813
});
1914

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+
2026
// 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.
2530
test("should return 0 when the character does not occur", () => {
2631
const str = "hello";
2732
const char = "z";
2833
const count = countChar(str, char);
2934
expect(count).toEqual(0);
3035
});
3136

32-
test("should count multiple occurrences of a character", () => {
33-
const str = "aaaaa";
34-
const char = "d";
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";
3544
const count = countChar(str, char);
3645
expect(count).toEqual(0);
3746
});
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

Comments
 (0)