|
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 | | - |
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. |
11 | 2 |
|
| 3 | +// Case 1: handle count of 1 |
12 | 4 | 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"); |
| 5 | + const repeats = [ |
| 6 | + { a: "hello", b: 1, c: "hello" }, |
| 7 | + { a: "Silo", b: 1, c: "Silo" }, |
| 8 | + { a: "Ugly Betty", b: 1, c: "Ugly Betty" }, |
| 9 | + { a: "ByteOil", b: 1, c: "ByteOil" }, |
| 10 | + ]; |
| 11 | + |
| 12 | + for (const { a, b, c } of repeats) { |
| 13 | + expect(repeatStr(a, b)).toEqual(c); |
| 14 | + } |
| 15 | +}); |
| 16 | + |
| 17 | +// Case 2: handle count greater than 1 |
| 18 | +test("should return the original string repeated", () => { |
| 19 | + const repeats = [ |
| 20 | + { a: "hello", b: 3, c: "hellohellohello" }, |
| 21 | + { a: "CYF ", b: 5, c: "CYF CYF CYF CYF CYF " }, |
| 22 | + { |
| 23 | + a: "My Year of Meat", |
| 24 | + b: 2, |
| 25 | + c: "My Year of MeatMy Year of Meat", |
| 26 | + }, |
| 27 | + { a: "Y2K", b: 10, c: "Y2KY2KY2KY2KY2KY2KY2KY2KY2KY2K" }, |
| 28 | + ]; |
| 29 | + |
| 30 | + for (const { a, b, c } of repeats) { |
| 31 | + expect(repeatStr(a, b)).toEqual(c); |
| 32 | + } |
17 | 33 | }); |
18 | 34 |
|
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. |
| 35 | +// Case 3: Handle count of 0 |
| 36 | +test("should return an empty string when count is 0", () => { |
| 37 | + const repeats = [ |
| 38 | + { a: "The Office", b: 0, c: "" }, |
| 39 | + { a: "Angela Martin", b: 0, c: "" }, |
| 40 | + { a: "Michael Scott", b: 0, c: "" }, |
| 41 | + { a: "Kevin Malone", b: 0, c: "" }, |
| 42 | + ]; |
23 | 43 |
|
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. |
| 44 | + for (const { a, b, c } of repeats) { |
| 45 | + expect(repeatStr(a, b)).toEqual(c); |
| 46 | + } |
| 47 | +}); |
28 | 48 |
|
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. |
| 49 | +// Case 4: Handle negative count |
| 50 | +test("should return an empty string when count is negative", () => { |
| 51 | + const repeats = [ |
| 52 | + { a: "Osuofia in London", b: -1 }, |
| 53 | + { a: "Nkem Owoh", b: -5 }, |
| 54 | + { a: "Mara Derwent", b: -10 }, |
| 55 | + { a: "Cynthia Okereke", b: -100 }, |
| 56 | + ]; |
| 57 | + |
| 58 | + for (const { a, b } of repeats) { |
| 59 | + expect(() => repeatStr(a, b)).toThrow(); |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +// Case 5: Handle non-string input |
| 64 | +test("should throw an error when input is not a string", () => { |
| 65 | + const repeats = [ |
| 66 | + { a: 123, b: 3 }, |
| 67 | + { a: "123", b: "3" }, |
| 68 | + { a: "true", b: true }, |
| 69 | + { a: null, b: 1 }, |
| 70 | + { a: "undefined", b: undefined }, |
| 71 | + { a: {}, b: 4 }, |
| 72 | + { a: [], b: 0 }, |
| 73 | + ]; |
| 74 | + |
| 75 | + for (const { a, b } of repeats) { |
| 76 | + expect(() => repeatStr(a, b)).toThrow(); |
| 77 | + } |
| 78 | +}); |
0 commit comments