@@ -22,3 +22,62 @@ test("should count multiple occurrences of a character", () => {
2222// And a character `char` that does not exist within `str`.
2323// When the function is called with these inputs,
2424// Then it should return 0, indicating that no occurrences of `char` were found.
25+ test ( "should return 0 when character does not appear in string" , ( ) => {
26+ expect ( countChar ( "hello" , "x" ) ) . toEqual ( 0 ) ;
27+ expect ( countChar ( "banana" , "z" ) ) . toEqual ( 0 ) ;
28+ // empty string
29+ expect ( countChar ( "" , "a" ) ) . toEqual ( 0 ) ;
30+ } ) ;
31+
32+ // Scenario: Single Occurence
33+ // Given a string where `char` appears exactly once,
34+ // When countChar is called,
35+ // Then it should return 1.
36+ test ( "should return 1 when character appears exactly once" , ( ) => {
37+ expect ( countChar ( "hello" , "h" ) ) . toEqual ( 1 ) ;
38+ expect ( countChar ( "banana" , "n" ) ) . toEqual ( 1 ) ;
39+ expect ( countChar ( "xyz" , "y" ) ) . toEqual ( 1 ) ;
40+ } ) ;
41+
42+ // Scenario: Case Sensitivity
43+ // Given a string with mixed case letters,
44+ // When countChar is called with a specific case,
45+ // Then it should count only exact matches (case-sensitive).
46+ test ( "should be case-sensitive when counting characters" , ( ) => {
47+ expect ( countChar ( "Hello" , "H" ) ) . toEqual ( 1 ) ;
48+ expect ( countChar ( "Hello" , "h" ) ) . toEqual ( 0 ) ;
49+ expect ( countChar ( "AAAaaa" , "A" ) ) . toEqual ( 3 ) ;
50+ expect ( countChar ( "AAAaaa" , "a" ) ) . toEqual ( 3 ) ;
51+ } ) ;
52+
53+ // Scenario: Special Characters/Symbols
54+ // Given a string containing punctuation or emojis,
55+ // When counting a special character,
56+ // Then it should correctly identify and count it.
57+ test ( "should count special characters and emojis" , ( ) => {
58+ expect ( countChar ( "hello!!!" , "!" ) ) . toEqual ( 3 ) ;
59+ expect ( countChar ( "smile 😊😊😊" , "😊" ) ) . toEqual ( 3 ) ;
60+ } ) ;
61+
62+ // Scenario: Longer Mixed String
63+ // Given a longer string with various characters and spaces,
64+ // When counting different characters,
65+ // Then it should return the correct count for each.
66+ test ( "should count correctly in longer mixed string" , ( ) => {
67+ const str = "i am writing some jest tests right now" ;
68+ // some, now
69+ expect ( countChar ( str , "o" ) ) . toEqual ( 2 ) ;
70+ // some, jest, tests
71+ expect ( countChar ( str , "e" ) ) . toEqual ( 3 ) ;
72+ // spaces
73+ expect ( countChar ( str , " " ) ) . toEqual ( 7 ) ;
74+ } ) ;
75+
76+ // Scenario: Single Character String
77+ // Given a string containing only one character,
78+ // When countChar is called,
79+ // Then it should correctly handle matching or non-matching cases.
80+ test ( "should handle single character strings" , ( ) => {
81+ expect ( countChar ( "a" , "a" ) ) . toEqual ( 1 ) ;
82+ expect ( countChar ( "b" , "a" ) ) . toEqual ( 0 ) ;
83+ } ) ;
0 commit comments