|
1 | | - |
2 | 1 | // Implement a function repeatStr |
3 | 2 | const repeatStr = require("./repeat-str"); |
4 | 3 | // Given a target string str and a positive integer count, |
5 | 4 | // When the repeatStr function is called with these inputs, |
6 | 5 | // Then it should: |
7 | 6 |
|
8 | | -// case: repeat String: |
| 7 | +// case 1: repeat String: |
9 | 8 | // Given a target string str and a positive integer count, |
10 | 9 | // When the repeatStr function is called with these inputs, |
11 | 10 | // Then it should repeat the str count times and return a new string containing the repeated str values. |
12 | 11 |
|
13 | 12 | test("should repeat the string count times", () => { |
14 | 13 | const str = "hello"; |
15 | 14 | const count = 3; |
16 | | - const repeatedStr = repeatStr(str, count); |
17 | | - expect(repeatedStr).toEqual("hellohellohello"); |
| 15 | + expect(repeatStr(str, count)).toEqual("hellohellohello"); |
18 | 16 | }); |
19 | 17 |
|
20 | | -// case: handle Count of 1: |
| 18 | +// case 2: handle Count of 1: |
21 | 19 | // Given a target string str and a count equal to 1, |
22 | 20 | // When the repeatStr function is called with these inputs, |
23 | 21 | // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. |
24 | 22 | test("should return the original string when count is 1", () => { |
25 | 23 | const str = "hello"; |
26 | 24 | const count = 1; |
27 | | - const repeatedStr = repeat(str, count); |
28 | | - expect(repeatedStr).toEqual("hello"); |
| 25 | + expect(repeatStr(str, count)).toEqual("hello"); |
29 | 26 | }); |
30 | 27 |
|
31 | | -// case: Handle Count of 0: |
| 28 | +// case 3: Handle Count of 0: |
32 | 29 | // Given a target string str and a count equal to 0, |
33 | 30 | // When the repeatStr function is called with these inputs, |
34 | 31 | // Then it should return an empty string, ensuring that a count of 0 results in an empty output. |
35 | 32 | test("should return an empty string when count is 0", () => { |
36 | 33 | const str = "hello"; |
37 | 34 | const count = 0; |
38 | | - const repeatedStr = repeat(str, count); |
39 | | - expect(repeatedStr).toEqual(""); |
| 35 | + expect(repeatStr(str, count)).toEqual(""); |
40 | 36 | }); |
41 | 37 |
|
42 | | -// case: Negative Count: |
| 38 | +// case 4: Negative Count: |
43 | 39 | // Given a target string str and a negative integer count, |
44 | 40 | // When the repeatStr function is called with these inputs, |
45 | 41 | // Then it should throw an error or return an appropriate error message, as negative counts are not valid. |
46 | 42 | test("should throw an error for negative count", () => { |
47 | 43 | const str = "hello"; |
48 | 44 | const count = -2; |
49 | | - expect(() => { |
50 | | - repeat(str, count); |
51 | | - }).toThrow("Invalid count value"); |
| 45 | + expect(() => repeatStr(str, count)).toThrow("Invalid count value"); |
| 46 | +}); |
| 47 | + |
| 48 | +// case 5: Non-integer Count: |
| 49 | +// Given a target string str and a non-integer count (e.g., a float or a string), |
| 50 | +// When the repeatStr function is called with these inputs, |
| 51 | +// Then it should throw an error or return an appropriate error message, as non-integer counts are not valid. |
| 52 | +test("should throw an error for non-integer count", () => { |
| 53 | + const str = "hello"; |
| 54 | + const count = 2.5; |
| 55 | + expect(() => repeatStr(str, count)).toThrow("Invalid count value"); |
52 | 56 | }); |
0 commit comments