11// Implement a function repeat
2- const repeat = require ( "./repeat" ) ;
2+ const repeatStr = require ( "./repeat-str " ) ;
33// Given a target string str and a positive integer count,
44// When the repeat function is called with these inputs,
55// Then it should:
@@ -12,7 +12,7 @@ const repeat = require("./repeat");
1212test ( "should repeat the string count times" , ( ) => {
1313 const str = "hello" ;
1414 const count = 3 ;
15- const repeatedStr = repeat ( str , count ) ;
15+ const repeatedStr = repeatStr ( str , count ) ;
1616 expect ( repeatedStr ) . toEqual ( "hellohellohello" ) ;
1717} ) ;
1818
@@ -24,7 +24,7 @@ test("should repeat the string count times", () => {
2424test ( "should return original string if count is 1" , ( ) => {
2525 const str = "hello" ;
2626 const count = 1 ;
27- expect ( repeat ( str , count ) ) . toEqual ( "hello" ) ;
27+ expect ( repeatStr ( str , count ) ) . toEqual ( "hello" ) ;
2828} ) ;
2929
3030// case: Handle Count of 0:
@@ -35,7 +35,7 @@ test("should return original string if count is 1", () => {
3535test ( "should return empty string if count is 0" , ( ) => {
3636 const str = "hello" ;
3737 const count = 0 ;
38- expect ( repeat ( str , count ) ) . toEqual ( "" ) ;
38+ expect ( repeatStr ( str , count ) ) . toEqual ( "" ) ;
3939} ) ;
4040
4141// case: Negative Count:
@@ -46,56 +46,56 @@ test("should return empty string if count is 0", () => {
4646test ( "should return error message for negative count" , ( ) => {
4747 const str = "hello" ;
4848 const count = - 2 ;
49- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
49+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
5050} ) ;
5151
5252// invalid input tests
5353test ( "should return error message for non-integer count" , ( ) => {
5454 const str = "hello" ;
5555 const count = 2.5 ;
56- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
56+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
5757} ) ;
5858
5959test ( "should return error message for non-string input" , ( ) => {
6060 const str = 123 ;
6161 const count = 3 ;
62- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "First argument must be a string" ) ;
62+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( "First argument must be a string" ) ;
6363} ) ;
6464
6565test ( "should return error message for non-string input with invalid count" , ( ) => {
6666 const str = { text : "hello" } ;
6767 const count = - 2 ;
68- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "First argument must be a string" ) ;
68+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( "First argument must be a string" ) ;
6969} ) ;
7070
7171test ( "should return error message for string input with non number count" , ( ) => {
7272 const str = "hello" ;
7373 const count = "3" ;
7474 const count2 = [ ] ;
75- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
76- expect ( ( ) => repeat ( str , count2 ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
75+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
76+ expect ( ( ) => repeatStr ( str , count2 ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
7777} ) ;
7878
7979test ( "should return error message for string input with NaN count" , ( ) => {
8080 const str = "hello" ;
8181 const count = NaN ;
82- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
82+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
8383} ) ;
8484
8585test ( "should return error message for string input with null count" , ( ) => {
8686 const str = "hello" ;
8787 const count = null ;
88- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
88+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer" ) ;
8989} ) ;
9090
9191test ( "should return error message for string input with undefined count" , ( ) => {
9292 const str = "hello" ;
9393 const count = undefined ;
94- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer. Received undefined" ) ;
94+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( "Second argument must be a non-negative integer. Received undefined" ) ;
9595} ) ;
9696
9797test ( 'should have the correct amount of arguments' , ( ) => {
98- expect ( ( ) => repeat ( 'hello' ) ) . toThrow ( new Error ( "Function requires exactly two arguments: a string and a count. Received 1 arguments" ) ) ;
99- expect ( ( ) => repeat ( "hello" , 3 , 3 ) ) . toThrow ( new Error ( "Function requires exactly two arguments: a string and a count. Received 3 arguments" ) ) ;
98+ expect ( ( ) => repeatStr ( 'hello' ) ) . toThrow ( new Error ( "Function requires exactly two arguments: a string and a count. Received 1 arguments" ) ) ;
99+ expect ( ( ) => repeatStr ( "hello" , 3 , 3 ) ) . toThrow ( new Error ( "Function requires exactly two arguments: a string and a count. Received 3 arguments" ) ) ;
100100 } )
101101
0 commit comments