|
1 | | -// Implement a function repeat |
| 1 | +const repeatString = require("./repeat"); |
2 | 2 |
|
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: |
| 3 | +function assertEquals(actualOutput, targetOutput) { |
| 4 | + console.assert( |
| 5 | + actualOutput === targetOutput, |
| 6 | + `Expected "${actualOutput}" to equal "${targetOutput}"` |
| 7 | + ); |
| 8 | +} |
6 | 9 |
|
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. |
| 10 | +describe("repeatString Function Tests", () => { |
| 11 | + test("Repeats string 3 times", () => { |
| 12 | + const result = repeatString("hello", 3); |
| 13 | + assertEquals(result, "hello hello hello"); |
| 14 | + }); |
11 | 15 |
|
12 | | -// case: handle Count of 1: |
13 | | -// Given a target string str and a count equal to 1, |
14 | | -// When the repeat function is called with these inputs, |
15 | | -// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. |
| 16 | + test("Repeats string 1 time", () => { |
| 17 | + const result = repeatString("world", 1); |
| 18 | + assertEquals(result, "world"); |
| 19 | + }); |
16 | 20 |
|
17 | | -// case: Handle Count of 0: |
18 | | -// Given a target string str and a count equal to 0, |
19 | | -// When the repeat function is called with these inputs, |
20 | | -// Then it should return an empty string, ensuring that a count of 0 results in an empty output. |
| 21 | + test("Repeats string 0 times", () => { |
| 22 | + const result = repeatString("test", 0); |
| 23 | + assertEquals(result, ""); |
| 24 | + }); |
21 | 25 |
|
22 | | -// case: Negative Count: |
23 | | -// Given a target string str and a negative integer count, |
24 | | -// When the repeat function is called with these inputs, |
25 | | -// Then it should throw an error or return an appropriate error message, as negative counts are not valid. |
| 26 | + test("Handles negative count", () => { |
| 27 | + const result = repeatString("errorTest", -2); |
| 28 | + assertEquals(result, "error your number is negative"); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +console.log("All test cases executed!"); |
0 commit comments