|
1 | 1 | // Implement a function repeatStr |
2 | 2 | const repeatStr = require("./repeat-str"); |
| 3 | + |
3 | 4 | // Given a target string `str` and a positive integer `count`, |
4 | 5 | // When the repeatStr function is called with these inputs, |
5 | 6 | // Then it should: |
6 | 7 |
|
7 | | -// Case: handle multiple repetitions: |
| 8 | +// Case: handle multiple repetitions |
8 | 9 | // Given a target string `str` and a positive integer `count` greater than 1, |
9 | | -// When the repeatStr function is called with these inputs, |
10 | 10 | // Then it should return a string that contains the original `str` repeated `count` times. |
11 | 11 |
|
12 | 12 | test("should repeat the string count times", () => { |
13 | 13 | const str = "hello"; |
14 | 14 | const count = 3; |
15 | 15 | const repeatedStr = repeatStr(str, count); |
| 16 | + |
16 | 17 | expect(repeatedStr).toEqual("hellohellohello"); |
17 | 18 | }); |
18 | 19 |
|
19 | | -// Case: handle count of 1: |
20 | | -// Given a target string `str` and a `count` equal to 1, |
21 | | -// When the repeatStr function is called with these inputs, |
22 | | -// Then it should return the original `str` without repetition. |
| 20 | +// Case: handle count of 1 |
| 21 | +// Given a target string `str` and a count equal to 1, |
| 22 | +// Then it should return the original string. |
23 | 23 |
|
24 | | -// Case: Handle count of 0: |
25 | | -// Given a target string `str` and a `count` equal to 0, |
26 | | -// When the repeatStr function is called with these inputs, |
| 24 | +test("should return the original string when count is 1", () => { |
| 25 | + const str = "hello"; |
| 26 | + const count = 1; |
| 27 | + const repeatedStr = repeatStr(str, count); |
| 28 | + |
| 29 | + expect(repeatedStr).toEqual("hello"); |
| 30 | +}); |
| 31 | + |
| 32 | +// Case: handle count of 0 |
| 33 | +// Given a target string `str` and a count equal to 0, |
27 | 34 | // Then it should return an empty string. |
28 | 35 |
|
29 | | -// Case: Handle negative count: |
30 | | -// Given a target string `str` and a negative integer `count`, |
31 | | -// When the repeatStr function is called with these inputs, |
32 | | -// Then it should throw an error, as negative counts are not valid. |
| 36 | +test("should return an empty string when count is 0", () => { |
| 37 | + const str = "hello"; |
| 38 | + const count = 0; |
| 39 | + const repeatedStr = repeatStr(str, count); |
| 40 | + |
| 41 | + expect(repeatedStr).toEqual(""); |
| 42 | +}); |
| 43 | + |
| 44 | +// Case: handle negative count |
| 45 | +// Given a negative integer count, |
| 46 | +// Then the function should throw an error. |
| 47 | + |
| 48 | +test("should throw an error when count is negative", () => { |
| 49 | + const str = "hello"; |
| 50 | + const count = -1; |
| 51 | + |
| 52 | + expect(() => repeatStr(str, count)).toThrow(); |
| 53 | +}); |
0 commit comments