@@ -38,18 +38,125 @@ test("should return 0 when string is empty", () => {
3838 expect ( parseInt ( count ) ) . toEqual ( 0 ) ;
3939} ) ;
4040
41+ // tests for unhappy paths (invalid str)
42+ const errorMessageForString = "Input str should be a string" ;
43+
4144// test for str is an array
42- test ( "should return 1 when str is an array" , ( ) => {
45+ test ( "should throw an error when str is an array" , ( ) => {
4346 const str = [ "a" , "b" , "c" ] ;
4447 const char = "a" ;
45- const count = countChar ( str , char ) ;
46- expect ( parseInt ( count ) ) . toEqual ( 1 ) ;
48+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForString ) ;
4749} ) ;
4850
4951// test for str is a number
50- test ( "should return 1 when str is a number" , ( ) => {
52+ test ( "should throw an error when str is a number" , ( ) => {
5153 const str = 12345 ;
52- const char = "3" ;
54+ const char = "1" ;
55+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForString ) ;
56+ } ) ;
57+
58+ // test for str is an object
59+ test ( "should throw an error when str is an object" , ( ) => {
60+ const str = { a : 1 , b : 2 } ;
61+ const char = "a" ;
62+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForString ) ;
63+ } ) ;
64+
65+ // test for str is null
66+ test ( "should throw an error when str is null" , ( ) => {
67+ const str = null ;
68+ const char = "a" ;
69+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForString ) ;
70+ } ) ;
71+
72+ // test for str is undefined
73+ test ( "should throw an error when str is undefined" , ( ) => {
74+ const str = undefined ;
75+ const char = "a" ;
76+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForString ) ;
77+ } ) ;
78+
79+ // test for str is a boolean
80+ test ( "should throw an error when str is a boolean" , ( ) => {
81+ const str = true ;
82+ const char = "a" ;
83+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForString ) ;
84+ } ) ;
85+
86+ // tests for unhappy paths (invalid char)
87+ const errorMessageForCharacter = "Input char should be a string" ;
88+
89+ // test for char is an array
90+ test ( "should throw an error when char is an array" , ( ) => {
91+ const str = "abcdefg" ;
92+ const char = [ "a" , "b" ] ;
93+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForCharacter ) ;
94+ } ) ;
95+
96+ // test for char is a number
97+ test ( "should throw an error when char is a number" , ( ) => {
98+ const str = "abcdefg" ;
99+ const char = 1 ;
100+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForCharacter ) ;
101+ } ) ;
102+
103+ // test for char is an object
104+ test ( "should throw an error when char is an object" , ( ) => {
105+ const str = "abcdefg" ;
106+ const char = { a : 1 } ;
107+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForCharacter ) ;
108+ } ) ;
109+
110+ // test for char is null
111+ test ( "should throw an error when char is null" , ( ) => {
112+ const str = "abcdefg" ;
113+ const char = null ;
114+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForCharacter ) ;
115+ } ) ;
116+
117+ // test for char is undefined
118+ test ( "should throw an error when char is undefined" , ( ) => {
119+ const str = "abcdefg" ;
120+ const char = undefined ;
121+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForCharacter ) ;
122+ } ) ;
123+
124+ // test for char is a boolean
125+ test ( "should throw an error when char is a boolean" , ( ) => {
126+ const str = "abcdefg" ;
127+ const char = true ;
128+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForCharacter ) ;
129+ } ) ;
130+
131+ // test for char is more than one character
132+ const errorMessageForSingleCharacter =
133+ "Input char should be a single character" ;
134+
135+ test ( "should throw an error when char is more than one character" , ( ) => {
136+ const str = "abcdefg" ;
137+ const char = "ab" ;
138+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForSingleCharacter ) ;
139+ } ) ;
140+
141+ // test for char is a space character
142+ test ( "should return 0 when char is a space character" , ( ) => {
143+ const str = "abcdefg" ;
144+ const char = " " ;
145+ const count = countChar ( str , char ) ;
146+ expect ( parseInt ( count ) ) . toEqual ( 0 ) ;
147+ } ) ;
148+
149+ // test for str is a space character
150+ test ( "should return 0 when str is a space character" , ( ) => {
151+ const str = " " ;
152+ const char = "a" ;
53153 const count = countChar ( str , char ) ;
54- expect ( parseInt ( count ) ) . toEqual ( 1 ) ;
154+ expect ( parseInt ( count ) ) . toEqual ( 0 ) ;
155+ } ) ;
156+
157+ //test for char is an empty string
158+ test ( "should throw an error when char is an empty string" , ( ) => {
159+ const str = "abcdefg" ;
160+ const char = "" ;
161+ expect ( ( ) => countChar ( str , char ) ) . toThrow ( errorMessageForSingleCharacter ) ;
55162} ) ;
0 commit comments