|
1 | | -// implement a function countChar that counts the number of times a character occurs in a string |
2 | 1 | const countChar = require("./count"); |
3 | 2 |
|
4 | 3 | // Scenario 1: Multiple Occurrences |
5 | | -test("should count multiple occurrences of a character", () => { |
6 | | - expect(countChar("aaaaa", "a")).toEqual(5); |
7 | | - expect(countChar("sea saw", "s")).toEqual(2); |
8 | | - expect(countChar("Arrested Development", "d")).toEqual(2); |
9 | | - expect(countChar("* Star * TV *", "*")).toEqual(3); |
| 4 | +test("Should count multiple occurrences of a character", () => { |
| 5 | + const multipleOccurrences = [ |
| 6 | + { a: "aaaaa", b: "a", c: 5 }, |
| 7 | + { a: "sea saw", b: "s", c: 2 }, |
| 8 | + { a: "Arrested Development", b: "d", c: 2 }, |
| 9 | + { a: "* Star * TV *", b: "*", c: 3 }, |
| 10 | + ]; |
| 11 | + |
| 12 | + for (const { a, b, c } of multipleOccurrences) { |
| 13 | + expect(countChar(a, b)).toEqual(c); |
| 14 | + } |
10 | 15 | }); |
11 | 16 |
|
12 | 17 | // Scenario 2: Single Occurrence |
13 | | -test("should count single occurrence of a character", () => { |
14 | | - expect(countChar("a", "a")).toEqual(1); |
15 | | - expect(countChar("Star Light", "i")).toEqual(1); |
16 | | - expect(countChar("!@#$%^&*()", "#")).toEqual(1); |
17 | | - expect(countChar("onomatopoeia", "t")).toEqual(1); |
| 18 | +test("Should count single occurrence of a character", () => { |
| 19 | + const singleOccurrences = [ |
| 20 | + { a: "a", b: "a" }, |
| 21 | + { a: "Star Light", b: "i" }, |
| 22 | + { a: "!@#$%^&*()", b: "#" }, |
| 23 | + { a: "Why fly?", b: " " }, |
| 24 | + ]; |
| 25 | + |
| 26 | + for (const { a, b } of singleOccurrences) { |
| 27 | + expect(countChar(a, b)).toEqual(1); |
| 28 | + } |
18 | 29 | }); |
19 | 30 |
|
20 | 31 | // Scenario 3: No Occurrence |
21 | | -test("should count single occurrence of a character", () => { |
22 | | - expect(countChar("The Rookie", "S")).toEqual(0); |
23 | | - expect(countChar("Angela Bassett", "r")).toEqual(0); |
24 | | - expect(countChar("Angela Bassett", "r")).toEqual(0); |
25 | | - expect(countChar("Better Off Ted", "9")).toEqual(0); |
| 32 | +test("Should return 0 for no occurrence of a character", () => { |
| 33 | + const noOccurrences = [ |
| 34 | + { a: "The Rookie", b: "S" }, |
| 35 | + { a: "Angela Bassett", b: "r" }, |
| 36 | + { a: "Santa Clarita Diet", b: "*" }, |
| 37 | + { a: "Better Off Ted", b: "9" }, |
| 38 | + ]; |
| 39 | + |
| 40 | + for (const { a, b } of noOccurrences) { |
| 41 | + expect(countChar(a, b)).toEqual(0); |
| 42 | + } |
26 | 43 | }); |
27 | 44 |
|
28 | 45 | // Scenario 4: Invalid Entries |
29 | | -const invalidEntries = [ |
30 | | - { a: "Courage, the Cowardly Dog", b: "dog" }, |
31 | | - { a: "Sheep in the Big City", b: 1 }, |
32 | | - { a: -205, b: "a" }, |
33 | | - { a: -205, b: 55.5 }, |
34 | | - { a: true, b: "f" }, |
35 | | - { a: undefined, b: "f" }, |
36 | | - { a: null, b: true }, |
37 | | - { a: false, b: true }, |
38 | | - { a: [false], b: {} }, |
39 | | -]; |
40 | | - |
41 | | -describe.each(invalidEntries)("countChar($a, $b)", ({ a, b }) => { |
42 | | - test(`Should throw an error when given an invalid input (${a}, ${b})`, () => { |
| 46 | +test("Should throw an error when given an invalid input", () => { |
| 47 | + const invalidEntries = [ |
| 48 | + { a: "Courage, the Cowardly Dog", b: "dog" }, |
| 49 | + { a: "Sheep in the Big City", b: 1 }, |
| 50 | + { a: -205, b: "a" }, |
| 51 | + { a: -205, b: 55.5 }, |
| 52 | + { a: true, b: "f" }, |
| 53 | + { a: undefined, b: "f" }, |
| 54 | + { a: null, b: true }, |
| 55 | + { a: false, b: true }, |
| 56 | + { a: [false], b: {} }, |
| 57 | + ]; |
| 58 | + |
| 59 | + for (const { a, b } of invalidEntries) { |
43 | 60 | expect(() => { |
44 | 61 | countChar(a, b); |
45 | 62 | }).toThrow(); |
46 | | - }); |
| 63 | + } |
47 | 64 | }); |
0 commit comments