-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathcount.test.js
More file actions
45 lines (39 loc) · 1.55 KB
/
count.test.js
File metadata and controls
45 lines (39 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// implement a function countChar that counts the number of times a character occurs in a string
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:
// Scenario: Multiple Occurrences
// Given the input string str,
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').
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
// 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("returns 0 if the character is not in the string", () => {
const str = "bread";
const char = "z";
const count = countChar(str, char);
expect(count).toBe(0);
});
test("counts a character that appears once", () => {
const str = "bread";
const char = "d";
const count = countChar(str, char);
expect(count).toBe(1);
});
test("counts how many times a character appears", () => {
const str = "breadboard";
const char = "b";
const count = countChar(str, char);
expect(count).toBe(2);
});