nomino/make_test.go
2025-09-26 20:23:32 -05:00

84 lines
2 KiB
Go

package nomino
import (
"errors"
"testing"
"github.com/nalgeon/be"
)
var errTest = errors.New("sorry")
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 := testcase.opts
opts = append(opts, genOpt)
conf := NewConfig(opts...)
st, err := Make(conf)
be.Err(t, err, nil)
be.Equal(t, st, testcase.exp)
})
}
}
func TestMakeErr(t *testing.T) {
conf := NewConfig(WithGenerator(func(*Config) (string, error) { return "foobar", errTest }))
st, err := Make(conf)
be.Equal(t, st, "")
be.Err(t, err, errTest)
}
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)
be.Equal(t, conf.original, "foobar")
be.Equal(t, st, "foo.txt")
be.Err(t, err, nil)
}
func TestMakeOptsDoesntChangeConf(t *testing.T) {
gen := Incremental()
conf := NewConfig(WithGenerator(gen), WithPrefix("pre"))
st, err := Make(conf, WithOriginal("foobar"))
be.Equal(t, conf.original, "")
be.Equal(t, st, "pre_0_foobar.txt")
be.Err(t, err, nil)
st, err = Make(conf, WithOriginal("baz"))
be.Equal(t, conf.original, "")
be.Equal(t, st, "pre_1_baz.txt")
be.Err(t, err, nil)
}