nomino/make_test.go

85 lines
2 KiB
Go
Raw Normal View History

package nomino
import (
"errors"
"testing"
"github.com/nalgeon/be"
)
var errTest = errors.New("sorry")
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)
be.Err(t, err, nil)
be.Equal(t, st, testcase.exp)
2025-03-11 12:42:03 -05:00
})
}
}
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)
}
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)
be.Equal(t, conf.original, "foobar")
be.Equal(t, st, "foo.txt")
be.Err(t, err, nil)
2025-03-13 16:58:55 -05:00
}
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"))
be.Equal(t, conf.original, "")
be.Equal(t, st, "pre_0_foobar.txt")
be.Err(t, err, nil)
2025-03-14 21:46:30 -05:00
st, err = Make(conf, WithOriginal("baz"))
be.Equal(t, conf.original, "")
be.Equal(t, st, "pre_1_baz.txt")
be.Err(t, err, nil)
2025-03-14 21:46:30 -05:00
}