40 lines
750 B
Go
40 lines
750 B
Go
package idgen
|
|
|
|
import (
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewHasPrefix(t *testing.T) {
|
|
id := New("sg")
|
|
if !strings.HasPrefix(id, "sg_") {
|
|
t.Fatalf("New(%q) = %q, want prefix sg_", "sg", id)
|
|
}
|
|
}
|
|
|
|
func TestNewIsUnique(t *testing.T) {
|
|
seen := make(map[string]bool)
|
|
for i := 0; i < 1000; i++ {
|
|
id := New("sg")
|
|
if seen[id] {
|
|
t.Fatalf("duplicate ID generated: %s", id)
|
|
}
|
|
seen[id] = true
|
|
}
|
|
}
|
|
|
|
func TestNewIsURLSafe(t *testing.T) {
|
|
id := New("al")
|
|
if url.PathEscape(id) != id {
|
|
t.Fatalf("New() = %q is not URL-safe when path-escaped: %q", id, url.PathEscape(id))
|
|
}
|
|
}
|
|
|
|
func TestNewNoPrefix(t *testing.T) {
|
|
id := New("")
|
|
if strings.Contains(id, "_") {
|
|
t.Fatalf("New(\"\") = %q should not contain an underscore", id)
|
|
}
|
|
}
|