Add Make function

This is the important one
This commit is contained in:
Dan Jones 2025-03-10 14:53:59 -05:00
commit 7126ef97a4
2 changed files with 38 additions and 0 deletions

15
make.go Normal file
View file

@ -0,0 +1,15 @@
package nomino
import "fmt"
// Make generates a random filename. The behavior can be controlled by specifying Options
// In general, the final filename will be [prefix]_[generated_string]_[original_filename]_[suffix].[extension].
// If the name generator returns an error (generally, it shouldn't), that will be returned instead.
func Make(conf Config) (string, error) {
name, err := conf.generator()
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.original), nil
}

23
make_test.go Normal file
View file

@ -0,0 +1,23 @@
package nomino
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMake(t *testing.T) {
conf := NewConfig(WithGenerator(func() (string, error) { return "abc", nil }))
st, err := Make(conf)
assert.NoError(t, err)
assert.Equal(t, "abc", st)
}
func TestMakeErr(t *testing.T) {
retErr := errors.New("oops")
conf := NewConfig(WithGenerator(func() (string, error) { return "foobar", retErr }))
st, err := Make(conf)
assert.Zero(t, st)
assert.ErrorIs(t, err, retErr)
}