package models import ( "strings" "testing" ) // slugCases is the table the admin SPA's slug preset is tested against as // well (admin/tests/form/formState.test.ts in the framework repository, // "slug preset matches the server rule"): the organisation model and the form // must derive the same slug from the same name. var slugCases = []struct{ text, want string }{ {"Acme Records", "acme-records"}, {" Acme -- Records ", "acme-records"}, {"ACME_Records 2", "acme-records-2"}, {"Zażółć gęślą", "za-g-l"}, {"Rock & Roll!!!", "rock-roll"}, {"...leading and trailing...", "leading-and-trailing"}, {"tab\tand\nnewline", "tab-and-newline"}, {"emoji 🎵 music", "emoji-music"}, {"CamelCaseText", "camelcasetext"}, {"9 lives", "9-lives"}, {"a", "a"}, {"---", ""}, {" ", ""}, {"", ""}, {"ąęść", ""}, {strings.Repeat("Long Name ", 40), strings.TrimSuffix(strings.Repeat("long-name-", 40), "-")}, } func TestSlugifyTable(t *testing.T) { for _, tc := range slugCases { if got := Slugify(tc.text); got != tc.want { t.Errorf("Slugify(%q) = %q, want %q", tc.text, got, tc.want) } } // A slug is a fixed point, and it only holds lower-case ASCII letters, // digits and single hyphens between them. for _, tc := range slugCases { slug := Slugify(tc.text) if again := Slugify(slug); again != slug { t.Errorf("Slugify(Slugify(%q)) = %q, want %q", tc.text, again, slug) } if strings.HasPrefix(slug, "-") || strings.HasSuffix(slug, "-") || strings.Contains(slug, "--") { t.Errorf("Slugify(%q) = %q has a stray hyphen", tc.text, slug) } for _, r := range slug { if !(r >= 'a' && r <= 'z') && !(r >= '0' && r <= '9') && r != '-' { t.Errorf("Slugify(%q) = %q holds %q", tc.text, slug, r) } } } }