Slugify lowercases text and keeps only ASCII letters and digits, turning every run of other characters into a single hyphen with none left at either end. Comes with a doc comment and table-driven tests. (closes #9) Model: opus-5
35 lines
999 B
Go
35 lines
999 B
Go
package util
|
|
|
|
import "testing"
|
|
|
|
func TestSlugify(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
}{
|
|
{"already a slug", "hello-world", "hello-world"},
|
|
{"mixed case", "Hello World", "hello-world"},
|
|
{"punctuation", "What's new?", "what-s-new"},
|
|
{"run of separators", "one --- two", "one-two"},
|
|
{"leading separators", "///one", "one"},
|
|
{"trailing separators", "one///", "one"},
|
|
{"separators at both ends", " --one two-- ", "one-two"},
|
|
{"digits are kept", "Go 1.14 release", "go-1-14-release"},
|
|
{"underscores are separators", "some_name_here", "some-name-here"},
|
|
{"nothing usable", "!!! ???", ""},
|
|
{"empty string", "", ""},
|
|
{"outside ascii", "Grüße, Welt", "gr-e-welt"},
|
|
{"only characters outside ascii", "日本語", ""},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got := Slugify(test.input)
|
|
if got != test.expected {
|
|
t.Errorf("expected %q got %q", test.expected, got)
|
|
}
|
|
})
|
|
}
|
|
}
|