11package strings_test
22
33import (
4+ "fmt"
45"testing"
56
67"github.com/stretchr/testify/assert"
@@ -23,17 +24,47 @@ func TestTruncate(t *testing.T) {
2324s string
2425n int
2526expected string
27+ options []strings.TruncateOption
2628}{
27- {"foo" ,4 ,"foo" },
28- {"foo" ,3 ,"foo" },
29- {"foo" ,2 ,"fo" },
30- {"foo" ,1 ,"f" },
31- {"foo" ,0 ,"" },
32- {"foo" ,- 1 ,"" },
29+ {"foo" ,4 ,"foo" ,nil },
30+ {"foo" ,3 ,"foo" ,nil },
31+ {"foo" ,2 ,"fo" ,nil },
32+ {"foo" ,1 ,"f" ,nil },
33+ {"foo" ,0 ,"" ,nil },
34+ {"foo" ,- 1 ,"" ,nil },
35+ {"foo bar" ,7 ,"foo bar" , []strings.TruncateOption {strings .TruncateWithEllipsis }},
36+ {"foo bar" ,6 ,"foo b…" , []strings.TruncateOption {strings .TruncateWithEllipsis }},
37+ {"foo bar" ,5 ,"foo …" , []strings.TruncateOption {strings .TruncateWithEllipsis }},
38+ {"foo bar" ,4 ,"foo…" , []strings.TruncateOption {strings .TruncateWithEllipsis }},
39+ {"foo bar" ,3 ,"fo…" , []strings.TruncateOption {strings .TruncateWithEllipsis }},
40+ {"foo bar" ,2 ,"f…" , []strings.TruncateOption {strings .TruncateWithEllipsis }},
41+ {"foo bar" ,1 ,"…" , []strings.TruncateOption {strings .TruncateWithEllipsis }},
42+ {"foo bar" ,0 ,"" , []strings.TruncateOption {strings .TruncateWithEllipsis }},
43+ {"foo bar" ,7 ,"foo bar" , []strings.TruncateOption {strings .TruncateWithFullWords }},
44+ {"foo bar" ,6 ,"foo" , []strings.TruncateOption {strings .TruncateWithFullWords }},
45+ {"foo bar" ,5 ,"foo" , []strings.TruncateOption {strings .TruncateWithFullWords }},
46+ {"foo bar" ,4 ,"foo" , []strings.TruncateOption {strings .TruncateWithFullWords }},
47+ {"foo bar" ,3 ,"foo" , []strings.TruncateOption {strings .TruncateWithFullWords }},
48+ {"foo bar" ,2 ,"fo" , []strings.TruncateOption {strings .TruncateWithFullWords }},
49+ {"foo bar" ,1 ,"f" , []strings.TruncateOption {strings .TruncateWithFullWords }},
50+ {"foo bar" ,0 ,"" , []strings.TruncateOption {strings .TruncateWithFullWords }},
51+ {"foo bar" ,7 ,"foo bar" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
52+ {"foo bar" ,6 ,"foo…" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
53+ {"foo bar" ,5 ,"foo…" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
54+ {"foo bar" ,4 ,"foo…" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
55+ {"foo bar" ,3 ,"fo…" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
56+ {"foo bar" ,2 ,"f…" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
57+ {"foo bar" ,1 ,"…" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
58+ {"foo bar" ,0 ,"" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
59+ {"This is a very long task prompt that should be truncated to 160 characters. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." ,160 ,"This is a very long task prompt that should be truncated to 160 characters. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor…" , []strings.TruncateOption {strings .TruncateWithFullWords ,strings .TruncateWithEllipsis }},
3360} {
34- t .Run (tt .expected ,func (t * testing.T ) {
61+ tName := fmt .Sprintf ("%s_%d" ,tt .s ,tt .n )
62+ for _ ,opt := range tt .options {
63+ tName += fmt .Sprintf ("_%v" ,opt )
64+ }
65+ t .Run (tName ,func (t * testing.T ) {
3566t .Parallel ()
36- actual := strings .Truncate (tt .s ,tt .n )
67+ actual := strings .Truncate (tt .s ,tt .n , tt . options ... )
3768require .Equal (t ,tt .expected ,actual )
3869})
3970}