49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
// SPDX-FileCopyrightText: 2021 Luke Granger-Brown <depot@lukegb.com>
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package fngen_test
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"hg.lukegb.com/lukegb/depot/web/fup/fuphttp/fngen"
|
|
)
|
|
|
|
func TestIdentityGenerator(t *testing.T) {
|
|
fallback := func(filenamePrefix string, filenameExt string, attempt int) string {
|
|
return fmt.Sprintf("prefix=%s; ext=%s; attempt=%d", filenamePrefix, filenameExt, attempt)
|
|
}
|
|
|
|
gen := fngen.IdentityGenerator(fallback)
|
|
|
|
if got, want := gen("prefix", ".ext", 0), "prefix.ext"; got != want {
|
|
t.Errorf("IdentityGenerator with 0 attempts = %q; want %q", got, want)
|
|
}
|
|
if got, want := gen("prefix", ".ext", 1), "prefix=prefix; ext=.ext; attempt=0"; got != want {
|
|
t.Errorf("IdentityGenerator with 1 attempt = %q; want %q", got, want)
|
|
}
|
|
if got, want := gen("prefix", ".ext", 2), "prefix=prefix; ext=.ext; attempt=1"; got != want {
|
|
t.Errorf("IdentityGenerator with 2 attempts = %q; want %q", got, want)
|
|
}
|
|
|
|
if got, want := gen("prefix", "", 0), "prefix"; got != want {
|
|
t.Errorf("IdentityGenerator with no extension = %q; want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestPetnameGenerator(t *testing.T) {
|
|
got := fngen.PetnameGenerator("prefix", ".ext", 0)
|
|
if !strings.HasSuffix(got, ".ext") {
|
|
t.Errorf("PetnameGenerator(%q, %q, 0) = %q; want something ending in %v", "prefix", ".ext", got, ".ext")
|
|
}
|
|
}
|
|
|
|
func TestBoringGenerator(t *testing.T) {
|
|
got := fngen.BoringGenerator("prefix", ".ext", 0)
|
|
if !strings.HasSuffix(got, ".ext") {
|
|
t.Errorf("BoringGenerator(%q, %q, 0) = %q; want something ending in %v", "prefix", ".ext", got, ".ext")
|
|
}
|
|
}
|