@@ -9,7 +9,6 @@ const countChar = require("./count");
99// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
1010// When the function is called with these inputs,
1111// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').
12-
1312test ( "should count multiple occurrences of a character" , ( ) => {
1413 const str = "aaaaa" ;
1514 const char = "a" ;
@@ -22,3 +21,101 @@ test("should count multiple occurrences of a character", () => {
2221// And a character char that does not exist within the case-sensitive str,
2322// When the function is called with these inputs,
2423// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
24+ test ( "should return 0 if the character does not exist in the string" , ( ) => {
25+ expect ( countChar ( "hello" , "z" ) ) . toEqual ( 0 ) ;
26+ } ) ;
27+
28+ // Scenario: Single Occurrence
29+ // Given the input string str,
30+ // And a character char that occurs exactly once in str,
31+ // When the function is called with these inputs,
32+ // Then it should return 1, indicating one occurrence of char in str.
33+ test ( "should count a single occurrence of a character" , ( ) => {
34+ expect ( countChar ( "hello" , "h" ) ) . toEqual ( 1 ) ;
35+ } ) ;
36+
37+ // Scenario: Empty String
38+ // Given an empty input string str,
39+ // And any character char,
40+ // When the function is called with these inputs,
41+ // Then it should return 0, as there are no characters to count.
42+ test ( "should return error if the string is empty" , ( ) => {
43+ expect ( countChar ( "" , "a" ) ) . toEqual ( "Error: The string cannot be empty." ) ;
44+ } ) ;
45+
46+ // Scenario: Case Sensitivity
47+ // Given the input string str containing both uppercase and lowercase versions of a letter,
48+ // And a character char that matches only one case (either uppercase or lowercase),
49+ // When the function is called with these inputs,
50+ // Then it should count only the occurrences matching the exact case of char.
51+ test ( "should count only characters that match the exact case" , ( ) => {
52+ const str = "AaAaA" ;
53+ const lowercaseResult = countChar ( str , "a" ) ; // lowercase only
54+ const uppercaseResult = countChar ( str , "A" ) ; // uppercase only
55+ expect ( lowercaseResult ) . toEqual ( 2 ) ; // Only lowercase 'a's
56+ expect ( uppercaseResult ) . toEqual ( 3 ) ; // Only uppercase 'A's
57+ } ) ;
58+
59+ // Scenario: Special Characters
60+ // Given the input string str containing special or non-alphabetic characters,
61+ // And a character char that matches one of those special characters,
62+ // When the function is called with these inputs,
63+ // Then it should return the number of times that special character occurs in str.
64+ test ( "should count special characters" , ( ) => {
65+ expect ( countChar ( "!?!?" , "?" ) ) . toEqual ( 2 ) ;
66+ } ) ;
67+
68+ // Scenario: Spaces
69+ // Given the input string str that contains space characters,
70+ // And a character char that is a space,
71+ // When the function is called with these inputs,
72+ // Then it should return the number of space characters in str.
73+ test ( "should count spaces correctly" , ( ) => {
74+ expect ( countChar ( "a b c" , " " ) ) . toEqual ( 2 ) ;
75+ } ) ;
76+
77+ // Scenario: Null or Undefined Input (Optional Defensive Case)
78+ // Given a null or undefined input string str,
79+ // And a character char,
80+ // When the function is called with these inputs,
81+ // Then it should safely return 0 instead of throwing an error.
82+ test ( "should return error if the string is null or undefined" , ( ) => {
83+ expect ( countChar ( null , "a" ) ) . toEqual (
84+ "Error: The input string cannot be null or undefined."
85+ ) ;
86+ expect ( countChar ( undefined , "a" ) ) . toEqual (
87+ "Error: The input string cannot be null or undefined."
88+ ) ;
89+ } ) ;
90+
91+ // Scenario: Character Not a Single Character (Optional Defensive Case)
92+ // Given the input string str,
93+ // And a character char that is either an empty string or has more than one character,
94+ // When the function is called with these inputs,
95+ // Then it should return an error indicating that char must be a single character.
96+ test ( "should return error if the character to count is not a single character" , ( ) => {
97+ expect ( countChar ( "hello" , "" ) ) . toEqual (
98+ "Error: The character to count must be a single character."
99+ ) ;
100+ expect ( countChar ( "hello" , "ll" ) ) . toEqual (
101+ "Error: The character to count must be a single character."
102+ ) ;
103+ } ) ;
104+
105+ // Scenario: Unicode Characters
106+ // Given the input string str containing Unicode characters (e.g., emojis),
107+ // And a character char that is a Unicode character,
108+ // When the function is called with these inputs,
109+ // Then it should return the number of times that Unicode character occurs in str.
110+ test ( "should count unicode characters correctly" , ( ) => {
111+ expect ( countChar ( "😀😃😀😄" , "😀" ) ) . toEqual ( 2 ) ;
112+ } ) ;
113+ // Scenario: Long Strings
114+ // Given a very long input string str,
115+ // And a character char that occurs multiple times in str,
116+ // When the function is called with these inputs,
117+ // Then it should efficiently return the correct count of char in str.
118+ test ( "should handle long strings efficiently" , ( ) => {
119+ const longString = "a" . repeat ( 10000 ) + "b" . repeat ( 5000 ) + "a" . repeat ( 3000 ) ;
120+ expect ( countChar ( longString , "a" ) ) . toEqual ( 13000 ) ;
121+ } ) ;
0 commit comments