@@ -53,3 +53,53 @@ test("should return 0 when the input string is empty", () => {
5353 expect ( count ) . toEqual ( 0 ) ;
5454} ) ;
5555
56+ // Scenario: Numeric Characters
57+ test ( "should count numeric characters in the string" , ( ) => {
58+ const str = "123123123" ;
59+ const char = "2" ;
60+ const count = countChar ( str , char ) ;
61+ expect ( count ) . toEqual ( 3 ) ;
62+ } ) ;
63+
64+ // Scenario: Whitespace Characters
65+ test ( "should count whitespace characters in the string" , ( ) => {
66+ const str = "a b c d e f " ;
67+ const char = " " ;
68+ const count = countChar ( str , char ) ;
69+ expect ( count ) . toEqual ( 6 ) ;
70+ } ) ;
71+
72+ // Scenario: Edge Case - Character Not a Single Character
73+ test ( 'should throw error if char is not a single character' , ( ) => {
74+ const str = 'hello' ;
75+ const char = 'll' ;
76+ expect ( ( ) => {
77+ countChar ( str , char ) ;
78+ } ) . toThrow ( 'Character to find must be a single character' ) ;
79+ } )
80+
81+ // Scenario: Special Case - Special Characters
82+ test . each ( [
83+ { str : "!!!@@@###" , char : "!" , expected : 3 } ,
84+ { str : "$$$%%%^^^" , char : "%" , expected : 3 } ,
85+ { str : "&&&***(((" , char : "(" , expected : 3 } ,
86+ ] ) (
87+ "should count special characters correctly in '$str'" ,
88+ ( { str, char, expected } ) => {
89+ const count = countChar ( str , char ) ;
90+ expect ( count ) . toEqual ( expected ) ;
91+ }
92+ ) ;
93+ //Scenario: Invalid Input Types
94+ test . each ( [
95+ { str : 12345 , char : "a" , error : "First argument must be a string." } ,
96+ { str : "hello" , char : 5 , error : "Second argument must be a string." } ,
97+ { str : [ ] , char : "a" , error : "First argument must be a string." } ,
98+ { str : "hello" , char : { } , error : "Second argument must be a string." } ,
99+ { str : null , char : "a" , error : "First argument must be a string." } ,
100+ { str : "hi" , char : undefined , error : "Second argument must be a string." } ,
101+ ] ) ( "should throw error for invalid inputs" , ( { str, char, error } ) => {
102+ expect ( ( ) => {
103+ countChar ( str , char ) ;
104+ } ) . toThrow ( error ) ;
105+ } ) ;
0 commit comments