-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslug_test.go
More file actions
64 lines (56 loc) · 1.71 KB
/
Copy pathslug_test.go
File metadata and controls
64 lines (56 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package deploykit
import (
"regexp"
"testing"
)
var slugPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*[a-z0-9]$`)
func TestGenerateSlug(t *testing.T) {
t.Run("normal name", func(t *testing.T) {
slug := GenerateSlug("My Cool App")
if !slugPattern.MatchString(slug) {
t.Fatalf("slug %q does not match expected pattern", slug)
}
if len(slug) < len("my-cool-app-")+6 {
t.Fatalf("slug %q is too short", slug)
}
})
t.Run("special characters", func(t *testing.T) {
slug := GenerateSlug("Hello, World!!!")
if !slugPattern.MatchString(slug) {
t.Fatalf("slug %q does not match expected pattern", slug)
}
})
t.Run("already slugified", func(t *testing.T) {
slug := GenerateSlug("my-app")
if !slugPattern.MatchString(slug) {
t.Fatalf("slug %q does not match expected pattern", slug)
}
})
t.Run("long name truncated", func(t *testing.T) {
long := "this-is-a-very-long-project-name-that-should-be-truncated-to-fit-within-limits"
slug := GenerateSlug(long)
// 40 chars base + 1 hyphen + 6 hex = 47 max
if len(slug) > 47 {
t.Fatalf("slug %q is too long (%d chars)", slug, len(slug))
}
if !slugPattern.MatchString(slug) {
t.Fatalf("slug %q does not match expected pattern", slug)
}
})
t.Run("all special chars uses fallback", func(t *testing.T) {
slug := GenerateSlug("!!!")
if !slugPattern.MatchString(slug) {
t.Fatalf("slug %q does not match expected pattern", slug)
}
if len(slug) < len("project-")+6 {
t.Fatalf("slug %q should start with project- prefix", slug)
}
})
t.Run("unique slugs for same name", func(t *testing.T) {
a := GenerateSlug("my-app")
b := GenerateSlug("my-app")
if a == b {
t.Fatalf("expected different slugs, got %q both times", a)
}
})
}