2023-06-07 09:44:50 -08:00
package strings_test
import (
2026-08-05 11:59:37 +01:00
"bytes"
2025-10-02 19:54:07 +01:00
"fmt"
2023-06-07 09:44:50 -08:00
"testing"
2025-07-29 15:24:11 +01:00
"github.com/stretchr/testify/assert"
2023-06-07 09:44:50 -08:00
"github.com/stretchr/testify/require"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/util/strings"
2023-06-07 09:44:50 -08:00
)
func TestJoinWithConjunction ( t * testing . T ) {
t . Parallel ()
require . Equal ( t , "foo" , strings . JoinWithConjunction ([] string { "foo" }))
require . Equal ( t , "foo and bar" , strings . JoinWithConjunction ([] string { "foo" , "bar" }))
require . Equal ( t , "foo, bar and baz" , strings . JoinWithConjunction ([] string { "foo" , "bar" , "baz" }))
}
2023-12-07 16:59:13 +00:00
func TestTruncate ( t * testing . T ) {
t . Parallel ()
for _ , tt := range [] struct {
s string
n int
expected string
2025-10-02 19:54:07 +01:00
options [] strings . TruncateOption
2023-12-07 16:59:13 +00:00
}{
2025-10-02 19:54:07 +01:00
{ "foo" , 4 , "foo" , nil },
{ "foo" , 3 , "foo" , nil },
{ "foo" , 2 , "fo" , nil },
{ "foo" , 1 , "f" , nil },
{ "foo" , 0 , "" , nil },
{ "foo" , - 1 , "" , nil },
2026-08-05 11:59:37 +01:00
{ "" , 5 , "" , nil },
2025-10-02 19:54:07 +01:00
{ "foo bar" , 7 , "foo bar" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "foo bar" , 6 , "foo b…" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "foo bar" , 5 , "foo …" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "foo bar" , 4 , "foo…" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "foo bar" , 3 , "fo…" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "foo bar" , 2 , "f…" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "foo bar" , 1 , "…" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "foo bar" , 0 , "" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "foo bar" , 7 , "foo bar" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
{ "foo bar" , 6 , "foo" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
{ "foo bar" , 5 , "foo" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
{ "foo bar" , 4 , "foo" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
{ "foo bar" , 3 , "foo" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
{ "foo bar" , 2 , "fo" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
{ "foo bar" , 1 , "f" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
{ "foo bar" , 0 , "" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
{ "foo bar" , 7 , "foo bar" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "foo bar" , 6 , "foo…" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "foo bar" , 5 , "foo…" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "foo bar" , 4 , "foo…" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "foo bar" , 3 , "fo…" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "foo bar" , 2 , "f…" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "foo bar" , 1 , "…" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "foo bar" , 0 , "" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "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 }},
2026-02-27 17:46:37 +00:00
// Multi-byte rune handling.
{ "日本語テスト" , 3 , "日本語" , nil },
{ "日本語テスト" , 4 , "日本語テ" , nil },
{ "日本語テスト" , 6 , "日本語テスト" , nil },
{ "日本語テスト" , 4 , "日本語…" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "🎉🎊🎈🎁" , 2 , "🎉🎊" , nil },
{ "🎉🎊🎈🎁" , 3 , "🎉🎊…" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
// Multi-byte with full-word truncation.
{ "hello 日本語" , 7 , "hello…" , [] strings . TruncateOption { strings . TruncateWithFullWords , strings . TruncateWithEllipsis }},
{ "hello 日本語" , 8 , "hello 日…" , [] strings . TruncateOption { strings . TruncateWithEllipsis }},
{ "日本語 テスト" , 4 , "日本語" , [] strings . TruncateOption { strings . TruncateWithFullWords }},
2023-12-07 16:59:13 +00:00
} {
2025-10-02 19:54:07 +01:00
tName := fmt . Sprintf ( "%s_%d" , tt . s , tt . n )
for _ , opt := range tt . options {
tName += fmt . Sprintf ( "_%v" , opt )
}
t . Run ( tName , func ( t * testing . T ) {
2023-12-07 16:59:13 +00:00
t . Parallel ()
2025-10-02 19:54:07 +01:00
actual := strings . Truncate ( tt . s , tt . n , tt . options ... )
2023-12-07 16:59:13 +00:00
require . Equal ( t , tt . expected , actual )
})
}
}
2025-07-29 15:24:11 +01:00
2026-08-05 11:59:37 +01:00
func BenchmarkTruncate ( b * testing . B ) {
b . Run ( "NoTruncationNeeded" , func ( b * testing . B ) {
s := "a short string well under the limit"
b . ReportAllocs ()
for b . Loop () {
strings . Truncate ( s , 1000 )
}
})
b . Run ( "ActualTruncation" , func ( b * testing . B ) {
var buf bytes . Buffer
for range 2000 {
buf . WriteString ( "日本語テスト word " )
}
s := buf . String ()
b . ReportAllocs ()
for b . Loop () {
strings . Truncate ( s , 100 , strings . TruncateWithEllipsis , strings . TruncateWithFullWords )
}
})
}
2025-07-29 15:24:11 +01:00
func TestUISanitize ( t * testing . T ) {
t . Parallel ()
for _ , tt := range [] struct {
s string
expected string
}{
{ "normal text" , "normal text" },
{ "\tfoo \r\\nbar " , "foo bar" },
{ "通常のテキスト" , "通常のテキスト" },
{ "foo\nbar" , "foo bar" },
{ "foo\tbar" , "foo bar" },
{ "foo\rbar" , "foo bar" },
{ "foo\x00bar" , "foobar" },
{ "\u202Eabc" , "abc" },
{ "\u200Bzero width" , "zero width" },
{ "foo\x1b[31mred\x1b[0mbar" , "fooredbar" },
{ "foo\u0008bar" , "foobar" },
{ "foo\x07bar" , "foobar" },
{ "foo\uFEFFbar" , "foobar" },
{ "<a href='javascript:alert(1)'>link</a>" , "link" },
{ "<style>body{display:none}</style>" , "" },
{ "<html>HTML</html>" , "HTML" },
{ "<br>line break" , "line break" },
{ "<link rel='stylesheet' href='evil.css'>" , "" },
{ "<img src=1 onerror=alert(1)>" , "" },
{ "<!-- comment -->visible" , "visible" },
{ "<script>alert('xss')</script>" , "" },
{ "<iframe src='evil.com'></iframe>" , "" },
} {
t . Run ( tt . expected , func ( t * testing . T ) {
t . Parallel ()
actual := strings . UISanitize ( tt . s )
assert . Equal ( t , tt . expected , actual )
})
}
}
2026-02-27 17:46:37 +00:00
func TestCapitalize ( t * testing . T ) {
t . Parallel ()
tests := [] struct {
input string
expected string
}{
{ "" , "" },
{ "hello" , "Hello" },
{ "über" , "Über" },
{ "Hello" , "Hello" },
{ "a" , "A" },
}
for _ , tt := range tests {
t . Run ( fmt . Sprintf ( "%q" , tt . input ), func ( t * testing . T ) {
t . Parallel ()
assert . Equal ( t , tt . expected , strings . Capitalize ( tt . input ))
})
}
}