|
1 | | -// Implement a function repeat |
2 | 1 | const repeat = require("./repeat"); |
3 | | -// Given a target string str and a positive integer count, |
4 | | -// When the repeat function is called with these inputs, |
5 | | -// Then it should: |
6 | | - |
7 | | -// case: repeat String: |
8 | | -// Given a target string str and a positive integer count, |
9 | | -// When the repeat function is called with these inputs, |
10 | | -// Then it should repeat the str count times and return a new string containing the repeated str values. |
11 | 2 |
|
12 | 3 | test("should repeat the string count times", () => { |
13 | | - const str = "hello"; |
14 | | - const count = 3; |
15 | | - const repeatedStr = repeat(str, count); |
16 | | - expect(repeatedStr).toEqual("hellohellohello"); |
| 4 | + expect(repeat("hello", 3)).toEqual("hellohellohello"); |
| 5 | +}); |
| 6 | + |
| 7 | +test("should return the original string when count is 1", () => { |
| 8 | + expect(repeat("hello", 1)).toEqual("hello"); |
| 9 | +}); |
| 10 | + |
| 11 | +test("should return an empty string when count is 0", () => { |
| 12 | + expect(repeat("hello", 0)).toEqual(""); |
17 | 13 | }); |
18 | 14 |
|
19 | | -// case: handle Count of 1: |
20 | | -// Given a target string str and a count equal to 1, |
21 | | -// When the repeat function is called with these inputs, |
22 | | -// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. |
| 15 | +test("should throw an error when count is negative", () => { |
| 16 | + expect(() => repeat("hello", -1)).toThrow("Count cannot be negative"); |
| 17 | +}); |
23 | 18 |
|
24 | | -// case: Handle Count of 0: |
25 | | -// Given a target string str and a count equal to 0, |
26 | | -// When the repeat function is called with these inputs, |
27 | | -// Then it should return an empty string, ensuring that a count of 0 results in an empty output. |
| 19 | +test("should repeat string multiple times", () => { |
| 20 | + expect(repeat("abc", 4)).toEqual("abcabcabcabc"); |
| 21 | +}); |
28 | 22 |
|
29 | | -// case: Negative Count: |
30 | | -// Given a target string str and a negative integer count, |
31 | | -// When the repeat function is called with these inputs, |
32 | | -// Then it should throw an error or return an appropriate error message, as negative counts are not valid. |
| 23 | +test("should handle empty string", () => { |
| 24 | + expect(repeat("", 5)).toEqual(""); |
| 25 | +}); |
| 26 | + |
| 27 | +test("should handle single character", () => { |
| 28 | + expect(repeat("x", 5)).toEqual("xxxxx"); |
| 29 | +}); |
0 commit comments