package names import ( "strings" "testing" ) func TestSlug(t *testing.T) { cases := []struct { kind string in string want string }{ {"repo", "test repo", "test-repo"}, {"repo", " Test Repo ", "test-repo"}, {"repo", "test-repo", "test-repo"}, {"repo", "test_repo", "test_repo"}, {"repo", "foo---bar", "foo-bar"}, {"repo", "foo.bar", "foo.bar"}, {"repo", "foo/bar", "foo-bar"}, {"repo", "BlueGit", "bluegit"}, {"repo", "café repo", "cafe-repo"}, {"repo", "123 test", "123-test"}, {"repo", " a ", "a"}, {"repo", ".git", "git"}, {"group", "test repo", "test-repo"}, {"group", "Test Groep", "test-groep"}, {"group", "foo.bar", "foo-bar"}, {"group", "foo.git", "foo-git"}, {"group", "Werk", "werk"}, } for _, tc := range cases { got, err := Slug(tc.kind, tc.in) if err != nil || got != tc.want { t.Errorf("Slug(%s, %q) = %q, %v; wil %q", tc.kind, tc.in, got, err, tc.want) } } bad := []struct{ kind, in string }{ {"repo", "!!!"}, {"repo", "foo.git"}, {"repo", ".."}, {"repo", ""}, {"repo", " "}, {"group", "123 test"}, {"group", "new"}, {"group", "settings"}, {"group", "!!!"}, {"other", "test"}, } for _, tc := range bad { if _, err := Slug(tc.kind, tc.in); err == nil { t.Errorf("Slug(%s, %q) hoort te falen", tc.kind, tc.in) } } got, err := Slug("repo", "test "+strings.Repeat("a", 80)) if err != nil || len(got) > 63 || !strings.HasPrefix(got, "test-") { t.Fatalf("lange naam %q %v", got, err) } }