@@ -416,3 +416,67 @@ func TestResolveAndValidatePath(t *testing.T) {
416416 })
417417 }
418418}
419+
420+
421+ func TestTruncateStringWithEllipsis (t * testing.T ) {
422+
423+ tests := []struct {
424+ name string
425+ input string
426+ maxSize int
427+ expected string
428+ }{
429+ {
430+ name : "No truncation required" ,
431+ input : "Hello" ,
432+ maxSize : 10 ,
433+ expected : "Hello" ,
434+ },
435+ {
436+ name : "Truncate with ellipsis" ,
437+ input : "Hello, World!" ,
438+ maxSize : 10 ,
439+ expected : "Hello, ..." ,
440+ },
441+ {
442+ name : "String exactly maxSize" ,
443+ input : "Hello, World!" ,
444+ maxSize : 13 ,
445+ expected : "Hello, World!" ,
446+ },
447+ {
448+ name : "Small maxSize adds ellipsis only" ,
449+ input : "Hello" ,
450+ maxSize : 3 ,
451+ expected : "..." ,
452+ },
453+ {
454+ name : "UTF-8 truncation valid" ,
455+ input : "你好,世界" , // "Hello, World" in Chinese
456+ maxSize : 8 ,
457+ expected : "你..." ,
458+ },
459+ {
460+ name : "Empty string" ,
461+ input : "" ,
462+ maxSize : 5 ,
463+ expected : "" ,
464+ },
465+ {
466+ name : "UTF-8 truncation with invalid byte" ,
467+ input : "Hello, 世界\xef \xbf \xbd " , // Invalid UTF-8 at the end
468+ maxSize : 10 ,
469+ expected : "Hello, ..." ,
470+ },
471+ }
472+
473+ for _ , test := range tests {
474+ t .Run (test .name , func (t * testing.T ) {
475+ g := NewWithT (t )
476+ result := truncateStringWithEllipsis (test .input , test .maxSize )
477+ g .Expect (result ).To (Equal (test .expected ), "Expected %q but got %q for input %q with maxSize %d" , test .expected , result , test .input , test .maxSize )
478+ })
479+ }
480+ }
481+
482+
0 commit comments