@@ -444,6 +444,40 @@ func TestReuseReversedStringBuilder(t *testing.T) {
444444 }
445445}
446446
447+ func TestStringBuilderSubstring (t * testing.T ) {
448+ tests := []struct {
449+ name string
450+ start int
451+ end int
452+ substring string
453+ errorMessage string
454+ }{
455+ {"Substring with negative start" , - 1 , 3 , "" , "start should always be greater than or equal to zero" },
456+ {"Substring with end exceeding string builder length" , 0 , 5 , "" , "end cannot be greater than the length of string builder" },
457+ {"Substring with start greater than end" , 3 , 2 , "" , "start cannot be greater than the end for Substring() function" },
458+ {"Substring with start equal to zero" , 0 , 3 , "abc" , "" },
459+ {"Substring with end equal to length of string builder" , 0 , 4 , "abcd" , "" },
460+ {"Substring of length 1" , 0 , 1 , "a" , "" },
461+ {"Substring of length 0" , 0 , 0 , "" , "" },
462+ {"Substring in middle of string builder" , 1 , 3 , "bc" , "" },
463+ }
464+ for _ , tt := range tests {
465+ t .Run (tt .name , func (t * testing.T ) {
466+ sb := NewStringBuilderFromString ("abcd" )
467+ s , err := sb .Substring (tt .start , tt .end )
468+ if err != nil {
469+ if err .Error () != tt .errorMessage {
470+ t .Errorf ("StringBuilder.Substring() expected error message = %v, got = %v" , tt .errorMessage , err .Error ())
471+ }
472+
473+ }
474+ if s != tt .substring {
475+ t .Errorf ("StringBuilder.Substring() expected substring = %v, got = %v" , tt .substring , s )
476+ }
477+ })
478+ }
479+ }
480+
447481func slicesEqual (a []int , b []int ) bool {
448482 if len (a ) != len (b ) {
449483 return false
0 commit comments