|
1 | | -// Implement a function repeatStr |
2 | 1 | const repeatStr = require("./repeat-str"); |
3 | | -// Given a target string `str` and a positive integer `count`, |
4 | | -// When the repeatStr function is called with these inputs, |
5 | | -// Then it should: |
6 | 2 |
|
7 | | -// Case: handle multiple repetitions: |
8 | | -// Given a target string `str` and a positive integer `count` greater than 1, |
9 | | -// When the repeatStr function is called with these inputs, |
10 | | -// Then it should return a string that contains the original `str` repeated `count` times. |
| 3 | +// ─────────────────────────────────────────────────────── |
| 4 | +// Rule Summary: |
| 5 | +// • count > 1 → str repeated count times ("hi", 3 → "hihihi") |
| 6 | +// • count = 1 → original string unchanged |
| 7 | +// • count = 0 → empty string "" |
| 8 | +// • count < 0 → throw error (invalid input) |
| 9 | +// ─────────────────────────────────────────────────────── |
11 | 10 |
|
12 | | -test("should repeat the string count times", () => { |
13 | | - const str = "hello"; |
14 | | - const count = 3; |
15 | | - const repeatedStr = repeatStr(str, count); |
16 | | - expect(repeatedStr).toEqual("hellohellohello"); |
| 11 | +// ── Case 1: Multiple repetitions (count > 1) ── |
| 12 | +test("repeats string correctly for count > 1", () => { |
| 13 | + expect(repeatStr("hello", 3)).toEqual("hellohellohello"); |
| 14 | + expect(repeatStr("a", 5)).toEqual("aaaaa"); |
| 15 | + expect(repeatStr("xy", 2)).toEqual("xyxy"); |
17 | 16 | }); |
18 | 17 |
|
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. |
| 18 | +// ── Case 2: Single repetition (count = 1) ── |
| 19 | +test("returns original string unchanged when count = 1", () => { |
| 20 | + expect(repeatStr("bye", 1)).toEqual("bye"); |
| 21 | + expect(repeatStr("x", 1)).toEqual("x"); |
| 22 | + expect(repeatStr("test", 1)).toEqual("test"); |
| 23 | +}); |
| 24 | + |
| 25 | +// ── Case 3: Zero repetition (count = 0) ── |
| 26 | +test("returns empty string when count = 0", () => { |
| 27 | + expect(repeatStr("no", 0)).toEqual(""); |
| 28 | + expect(repeatStr("anything", 0)).toEqual(""); |
| 29 | + expect(repeatStr("", 0)).toEqual(""); |
| 30 | +}); |
23 | 31 |
|
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, |
27 | | -// Then it should return an empty string. |
| 32 | +// ── Case 4: Invalid input (negative count) ── |
| 33 | +test("throws error for negative count values", () => { |
| 34 | + expect(() => repeatStr("str", -1)).toThrow("negative counts are not valid"); |
| 35 | + expect(() => repeatStr("x", -10)).toThrow("negative counts are not valid"); |
| 36 | +}); |
28 | 37 |
|
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. |
|
0 commit comments