|
1 | 1 | // Implement a function repeatStr |
2 | 2 | const repeatStr = require("./repeat-str"); |
| 3 | + |
| 4 | +// Case: repeat string multiple times |
3 | 5 | test("should repeat the string count times", () => { |
4 | 6 | const str = "hello"; |
5 | 7 | const count = 3; |
6 | 8 | const repeatedStr = repeatStr(count, str); |
7 | 9 | expect(repeatedStr).toEqual("hellohellohello"); |
8 | 10 | }); |
9 | | -// test("should return the original string when count is 1", () => { |
10 | | -// const str = "hello"; |
11 | | -// const count = 1; |
12 | | -// const repeatedStr = repeatStr(str, count); |
13 | | -// expect(repeatedStr).toEqual("hello"); |
14 | | -// }); |
15 | | -// test("should return an empty string when count is 0", () => { |
16 | | -// const str = "hello"; |
17 | | -// const count = 0; |
18 | | -// const repeatedStr = repeatStr(str, count); |
19 | | -// expect(repeatedStr).toEqual(""); |
20 | | -// }); |
21 | | -// // Given a target string `str` and a positive integer `count`, |
22 | | -// // When the repeatStr function is called with these inputs, |
23 | | -// // Then it should: |
24 | | -// // - Validate that `count` is a non-negative integer. |
25 | | -// // - If `count` is 0, return an empty string. |
26 | | -// // - If `count` is 1, return the original `str`. |
27 | | -// // - If `count` is greater than 1, concatenate `str` to itself `count` times and return the resulting string. |
28 | 11 |
|
29 | | -// // Case: handle multiple repetitions: |
30 | | -// // Given a target string `str` and a positive integer `count` greater than 1, |
31 | | -// // When the repeatStr function is called with these inputs, |
32 | | -// // Then it should return a string that contains the original `str` repeated `count` times. |
33 | | -// test("should repeat the string count times", () => { |
34 | | -// const str = "hello"; |
35 | | -// const count = 3; |
36 | | -// const repeatedStr = repeatStr(str, count); |
37 | | -// expect(repeatedStr).toEqual("hellohellohello"); |
38 | | -// }); |
39 | | -// // Case: handle count of 1: |
40 | | -// // Given a target string `str` and a `count` equal to 1, |
41 | | -// // When the repeatStr function is called with these inputs, |
42 | | -// test("should repeat the string count times", () => { |
43 | | -// const str = "hello"; |
44 | | -// const count = 3; |
45 | | -// const repeatedStr = repeatStr(str, count); |
46 | | -// expect(repeatedStr).toEqual("hellohellohello"); |
47 | | -// }); |
| 12 | +// Case: count = 1 |
| 13 | +test("should return the original string when count is 1", () => { |
| 14 | + const str = "hello"; |
| 15 | + const count = 1; |
| 16 | + const repeatedStr = repeatStr(count, str); |
| 17 | + expect(repeatedStr).toEqual("hello"); |
| 18 | +}); |
48 | 19 |
|
49 | | -// // Case: handle count of 1: |
50 | | -// // Given a target string `str` and a `count` equal to 1, |
51 | | -// // When the repeatStr function is called with these inputs, |
52 | | -// // Then it should return the original `str` without repetition. |
53 | | -// test("should return the original string when count is 1", () => { |
54 | | -// const str = "hello"; |
55 | | -// const count = 1; |
56 | | -// const repeatedStr = repeatStr(str, count); |
57 | | -// expect(repeatedStr).toEqual("hello"); |
58 | | -// }); |
59 | | -// // Case: handle count of 0: |
60 | | -// // Given a target string `str` and a `count` equal to 0, |
61 | | -// // When the repeatStr function is called with these inputs, |
62 | | -// // Then it should return an empty string. |
63 | | -// test("should return the original string when count is 1", () => { |
64 | | -// const str = "hello"; |
65 | | -// const count = 1; |
66 | | -// const repeatedStr = repeatStr(str, count); |
67 | | -// expect(repeatedStr).toEqual("hello"); |
68 | | -// }); |
69 | | -// // Case: Handle count of 0: |
70 | | -// // Given a target string `str` and a `count` equal to 0, |
71 | | -// // When the repeatStr function is called with these inputs, |
72 | | -// // Then it should return an empty string. |
73 | | -// test("should return an empty string when count is 0", () => { |
74 | | -// const str = "hello"; |
75 | | -// const count = 0; |
76 | | -// const repeatedStr = repeatStr(str, count); |
77 | | -// expect(repeatedStr).toEqual(""); |
78 | | -// }); |
79 | | -// // Case: Handle negative count: |
80 | | -// // Given a target string `str` and a negative integer `count`, |
81 | | -// // When the repeatStr function is called with these inputs, |
82 | | -// // Then it should throw an error, as negative counts are not valid. |
83 | | -// test("should throw an error when count is negative", () => { |
84 | | -// const str = "hello"; |
85 | | -// const count = -1; |
86 | | -// expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer"); |
87 | | -// }); |
88 | | -// // Case: Handle non-integer count: |
89 | | -// // Given a target string `str` and a non-integer value for `count`, |
90 | | -// // When the repeatStr function is called with these inputs, |
91 | | -// // Then it should throw an error, as non-integer counts are not valid. |
92 | | -// test("should throw an error when count is not an integer", () => { |
93 | | -// const str = "hello"; |
94 | | -// const count = 2.5; |
95 | | -// expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer"); |
96 | | -// }); |
| 20 | +// Case: count = 0 |
| 21 | +test("should return an empty string when count is 0", () => { |
| 22 | + const str = "hello"; |
| 23 | + const count = 0; |
| 24 | + const repeatedStr = repeatStr(count, str); |
| 25 | + expect(repeatedStr).toEqual(""); |
| 26 | +}); |
| 27 | + |
| 28 | +// Case: negative count |
| 29 | +test("should throw an error when count is negative", () => { |
| 30 | + const str = "hello"; |
| 31 | + const count = -1; |
| 32 | + expect(() => repeatStr(count, str)).toThrow( |
| 33 | + "Count must be a non-negative integer" |
| 34 | + ); |
| 35 | +}); |
| 36 | + |
| 37 | +// Case: non-integer count |
| 38 | +test("should throw an error when count is not an integer", () => { |
| 39 | + const str = "hello"; |
| 40 | + const count = 2.5; |
| 41 | + expect(() => repeatStr(count, str)).toThrow( |
| 42 | + "Count must be a non-negative integer" |
| 43 | + ); |
| 44 | +}); |
0 commit comments