54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package nomino
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMake(t *testing.T) {
|
|
genOpt := WithGenerator(func() (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() (string, error) { return "foobar", retErr }))
|
|
st, err := Make(conf)
|
|
assert.Zero(t, st)
|
|
assert.ErrorIs(t, err, retErr)
|
|
}
|