nomino/make_test.go
2025-03-13 16:58:55 -05:00

66 lines
1.7 KiB
Go

package nomino
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMake(t *testing.T) {
genOpt := WithGenerator(func(*Config) (string, error) { return "abc", nil })
testcases := []struct {
name string
opts []Option
exp string
}{
{"basic", nil, "abc.txt"},
{"with prefix", []Option{WithPrefix("foo")}, "foo_abc.txt"},
{"with suffix", []Option{WithSuffix("bar")}, "abc_bar.txt"},
{"with original", []Option{WithOriginal("file")}, "abc_file.txt"},
{"without ext", []Option{WithoutExtension()}, "abc"},
{"with ext", []Option{WithExtension("xml")}, "abc.xml"},
{"with sep", []Option{WithOriginal("file"), WithSeparator("---")}, "abc---file.txt"},
{
"with all",
[]Option{
WithPrefix("pre"),
WithOriginal("file"),
WithSuffix("suff"),
WithExtension("svg"),
WithSeparator("+"),
},
"pre+abc+file+suff.svg",
},
}
for _, testcase := range testcases {
t.Run(testcase.name, func(sub *testing.T) {
opts := append(testcase.opts, genOpt)
conf := NewConfig(opts...)
st, err := Make(conf)
assert.NoError(t, err)
assert.Equal(t, testcase.exp, st)
})
}
}
func TestMakeErr(t *testing.T) {
retErr := errors.New("oops")
conf := NewConfig(WithGenerator(func(*Config) (string, error) { return "foobar", retErr }))
st, err := Make(conf)
assert.Zero(t, st)
assert.ErrorIs(t, err, retErr)
}
func TestMakeDoesntChangeConf(t *testing.T) {
gen := func(c *Config) (string, error) {
c.original = ""
return "foo", nil
}
conf := NewConfig(WithGenerator(gen), WithOriginal("foobar"))
st, err := Make(conf)
assert.Equal(t, "foobar", conf.original)
assert.Equal(t, "foo.txt", st)
assert.NoError(t, err)
}