@@ -45,5 +45,59 @@ test("should return an empty string when count is 0", () => {
4545test ( "should throw an error when count is negative" , ( ) => {
4646 const str = "hello" ;
4747 const count = - 2 ;
48- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Invalid count value" ) ;
48+ expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Invalid data value" ) ;
49+ } ) ;
50+
51+ // case: str is not a string:
52+ // Given a non-string input for str (e.g., a number) and a positive integer count,
53+ // When the repeat function is called with these inputs,
54+ // Then it should convert the non-string str to a string and repeat it count times, returning the appropriately repeated string.
55+ test ( "should convert non-string str to string and repeat it count times" , ( ) => {
56+ const str = 123 ;
57+ const count = 2 ;
58+ const repeatedStr = repeat ( str , count ) ;
59+ expect ( repeatedStr ) . toEqual ( "123123" ) ;
60+ } ) ;
61+
62+ // case: count is not a number:
63+ // Given a target string str and a non-numeric input for count (e.g., a string that can be converted to a number),
64+ // When the repeat function is called with these inputs,
65+ // Then it should convert the non-numeric count to a number and repeat the str that many times, returning the appropriately repeated string.
66+ test ( "should convert non-numeric count to number and repeat the string that many times" , ( ) => {
67+ const str = "hello" ;
68+ const count = "3" ;
69+ const repeatedStr = repeat ( str , count ) ;
70+ expect ( repeatedStr ) . toEqual ( "hellohellohello" ) ;
71+ } ) ;
72+
73+ // case: count is a decimal number:
74+ // Given a target string str and a decimal number for count,
75+ // When the repeat function is called with these inputs,
76+ // Then it should round down the count to the nearest integer and repeat the str that many times, returning the appropriately repeated string.
77+ test ( "should round down decimal count to nearest integer and repeat the string that many times" , ( ) => {
78+ const str = "hello" ;
79+ const count = 3.7 ;
80+ const repeatedStr = repeat ( str , count ) ;
81+ expect ( repeatedStr ) . toEqual ( "hellohellohello" ) ;
82+ } ) ;
83+
84+ // case: str is an empty string:
85+ // Given an empty string for str and a positive integer count,
86+ // When the repeat function is called with these inputs,
87+ // Then it should return an empty string, as repeating an empty string any number of times still results in an empty string.
88+ test ( "should return an empty string when str is an empty string" , ( ) => {
89+ const str = "" ;
90+ const count = 5 ;
91+ const repeatedStr = repeat ( str , count ) ;
92+ expect ( repeatedStr ) . toEqual ( "" ) ;
93+ } ) ;
94+
95+ // case: invalid str and count types:
96+ // Given invalid types for both str (e.g., an object) and count (e.g., an array),
97+ // When the repeat function is called with these inputs,
98+ // Then it should throw an error or return an appropriate error message, indicating that the input types are not supported.
99+ test ( "should throw an error when str and count are of invalid types" , ( ) => {
100+ const str = { text : "hello" } ;
101+ const count = [ 3 ] ;
102+ expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Invalid data value" ) ;
49103} ) ;
0 commit comments