@@ -16,34 +16,77 @@ test("should repeat the string count times", () => {
1616 expect ( repeatedStr ) . toEqual ( "hellohellohellohello" ) ;
1717} ) ;
1818
19- // case: handle Count of 1:
19+ / c o n s t r e p e a t = r e q u i r e ( " ./ repeat ");
20+
21+ // case: Handle Count of 1 :
2022// Given a target string str and a count equal to 1,
2123// When the repeat function is called with these inputs,
22- // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
23-
24+ // Then it should return the original str without repetition.
2425test ( "should return the original string when count is 1" , ( ) => {
2526 const str = "hello" ;
2627 const count = 1 ;
2728 const repeatedStr = repeat ( str , count ) ;
2829 expect ( repeatedStr ) . toEqual ( "hello" ) ;
2930} ) ;
30- // case: Handle Count of 0:
31- // Given a target string str and a count equal to 0,
32- // When the repeat function is called with these inputs,
33- // Then it should return an empty string, ensuring that a count of 0 results in an empty output.
3431
32+ // case: Handle Count of 0 :
33+ // When count is 0, it should return an empty string.
3534test ( "should return an empty string when count is 0" , ( ) => {
3635 const str = "hello" ;
3736 const count = 0 ;
3837 const repeatedStr = repeat ( str , count ) ;
3938 expect ( repeatedStr ) . toEqual ( "" ) ;
4039} ) ;
41- // case: Negative Count:
42- // Given a target string str and a negative integer count,
43- // When the repeat function is called with these inputs,
44- // Then it should throw an error or return an appropriate error message,as negative counts are not valid.
40+
41+ // case: Negative Count :
42+ // When count is negative, it should throw an error.
4543test ( "should throw an error when count is negative" , ( ) => {
4644 const str = "hello" ;
4745 const count = - 2 ;
4846 expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Count must be a non-negative integer" ) ;
49- } ) ;
47+ } ) ;
48+
49+
50+ // case: Non-string input for str :
51+ // When str is not a string (e.g., number), it should still repeat it as a string.
52+ test ( "should convert non-string input to string before repeating" , ( ) => {
53+ const str = 123 ;
54+ const count = 2 ;
55+ const repeatedStr = repeat ( str , count ) ;
56+ expect ( repeatedStr ) . toEqual ( "123123" ) ;
57+ } ) ;
58+
59+ // case: Non-integer count :
60+ // When count is a non-integer (e.g., 2.5), it should repeat only the integer part.
61+ test ( "should handle non-integer count by truncating it" , ( ) => {
62+ const str = "hi" ;
63+ const count = 2.5 ;
64+ const repeatedStr = repeat ( str , Math . floor ( count ) ) ;
65+ expect ( repeatedStr ) . toEqual ( "hihi" ) ;
66+ } ) ;
67+
68+ // case: Empty string input :
69+ // When str is empty, the result should always be empty, regardless of count.
70+ test ( "should return an empty string when str is empty" , ( ) => {
71+ const str = "" ;
72+ const count = 5 ;
73+ const repeatedStr = repeat ( str , count ) ;
74+ expect ( repeatedStr ) . toEqual ( "" ) ;
75+ } ) ;
76+
77+ // case: Undefined or missing arguments :
78+ // When arguments are missing or undefined, it should throw an error.
79+ test ( "should handle undefined inputs gracefully" , ( ) => {
80+ expect ( ( ) => repeat ( undefined , 3 ) ) . toThrow ( ) ;
81+ expect ( ( ) => repeat ( "hello" ) ) . toThrow ( ) ;
82+ expect ( ( ) => repeat ( ) ) . toThrow ( ) ;
83+ } ) ;
84+
85+ // case: Boolean inputs :
86+ // When str or count are booleans, it should convert str to string and handle count normally.
87+ test ( "should handle boolean values as inputs" , ( ) => {
88+ const str = true ;
89+ const count = 2 ;
90+ const repeatedStr = repeat ( str , count ) ;
91+ expect ( repeatedStr ) . toEqual ( "truetrue" ) ;
92+ } ) ;
0 commit comments