2025-03-10 14:53:59 -05:00
|
|
|
package nomino
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestMake(t *testing.T) {
|
2025-03-13 15:36:30 -05:00
|
|
|
genOpt := WithGenerator(func(*Config) (string, error) { return "abc", nil })
|
2025-03-11 12:42:03 -05:00
|
|
|
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"},
|
2025-03-11 16:42:39 -05:00
|
|
|
{"with sep", []Option{WithOriginal("file"), WithSeparator("---")}, "abc---file.txt"},
|
2025-03-11 12:42:03 -05:00
|
|
|
{
|
|
|
|
|
"with all",
|
|
|
|
|
[]Option{
|
|
|
|
|
WithPrefix("pre"),
|
|
|
|
|
WithOriginal("file"),
|
|
|
|
|
WithSuffix("suff"),
|
|
|
|
|
WithExtension("svg"),
|
2025-03-11 16:42:39 -05:00
|
|
|
WithSeparator("+"),
|
2025-03-11 12:42:03 -05:00
|
|
|
},
|
2025-03-11 16:42:39 -05:00
|
|
|
"pre+abc+file+suff.svg",
|
2025-03-11 12:42:03 -05:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
|
t.Run(testcase.name, func(sub *testing.T) {
|
2025-03-14 13:47:44 -05:00
|
|
|
opts := testcase.opts
|
|
|
|
|
opts = append(opts, genOpt)
|
|
|
|
|
|
2025-03-11 12:42:03 -05:00
|
|
|
conf := NewConfig(opts...)
|
|
|
|
|
st, err := Make(conf)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, testcase.exp, st)
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-03-10 14:53:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMakeErr(t *testing.T) {
|
|
|
|
|
retErr := errors.New("oops")
|
2025-03-13 15:36:30 -05:00
|
|
|
conf := NewConfig(WithGenerator(func(*Config) (string, error) { return "foobar", retErr }))
|
2025-03-10 14:53:59 -05:00
|
|
|
st, err := Make(conf)
|
|
|
|
|
assert.Zero(t, st)
|
|
|
|
|
assert.ErrorIs(t, err, retErr)
|
|
|
|
|
}
|
2025-03-13 16:58:55 -05:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2025-03-14 21:46:30 -05:00
|
|
|
|
|
|
|
|
func TestMakeOptsDoesntChangeConf(t *testing.T) {
|
|
|
|
|
gen := Incremental()
|
|
|
|
|
conf := NewConfig(WithGenerator(gen), WithPrefix("pre"))
|
|
|
|
|
|
|
|
|
|
st, err := Make(conf, WithOriginal("foobar"))
|
|
|
|
|
assert.Equal(t, "", conf.original)
|
|
|
|
|
assert.Equal(t, "pre_0_foobar.txt", st)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
st, err = Make(conf, WithOriginal("baz"))
|
|
|
|
|
assert.Equal(t, "", conf.original)
|
|
|
|
|
assert.Equal(t, "pre_1_baz.txt", st)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
}
|