diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-30 10:51:05 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-30 10:51:05 -0600 |
| commit | 1a2af5f242cffaf59c457c847dff716b600ab342 (patch) | |
| tree | 4e20b9f5b5fdd293e455b4cab1c083491f0d94a4 | |
| parent | f79ef71d5cac3b396808edc4dacdf654d65468c2 (diff) | |
feat: add x.NewWithmain
| -rw-r--r-- | pkg/x/option.go | 5 | ||||
| -rw-r--r-- | pkg/x/option_test.go | 81 |
2 files changed, 85 insertions, 1 deletions
diff --git a/pkg/x/option.go b/pkg/x/option.go index b0bf638..156e28c 100644 --- a/pkg/x/option.go +++ b/pkg/x/option.go @@ -5,7 +5,10 @@ type Option[T any] func(T) T type Factory[T any] func() T func New[T any](options ...Option[T]) T { - item := Default[T]() + return NewWith[T](Default[T](), options...) +} + +func NewWith[T any](item T, options ...Option[T]) T { for _, option := range options { item = option(item) } diff --git a/pkg/x/option_test.go b/pkg/x/option_test.go new file mode 100644 index 0000000..7d6a33d --- /dev/null +++ b/pkg/x/option_test.go @@ -0,0 +1,81 @@ +package x + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type person struct { + name string + age int +} + +func withName(name string) Option[*person] { + return With(func(person *person) { + person.name = name + }) +} + +func withAge(age int) Option[*person] { + return With(func(person *person) { + person.age = age + }) +} + +func TestOption(t *testing.T) { + t.Run("New", func(t *testing.T) { + t.Run("without options", func(t *testing.T) { + item := New[*person]() + + require.NotNil(t, item) + assert.Equal(t, "", item.name) + assert.Zero(t, item.age) + }) + + t.Run("with name", func(t *testing.T) { + item := New[*person](withName("mo")) + + require.NotNil(t, item) + assert.Equal(t, "mo", item.name) + }) + + t.Run("with age", func(t *testing.T) { + item := New[*person](withAge(42)) + + require.NotNil(t, item) + assert.Equal(t, 42, item.age) + }) + }) + + t.Run("NewWith", func(t *testing.T) { + t.Run("without options", func(t *testing.T) { + p := &person{} + item := NewWith[*person](p) + + require.NotNil(t, item) + require.Same(t, p, item) + assert.Equal(t, "", item.name) + assert.Zero(t, item.age) + }) + + t.Run("with name", func(t *testing.T) { + p := &person{} + item := NewWith[*person](p, withName("mo")) + + require.NotNil(t, item) + require.Same(t, p, item) + assert.Equal(t, "mo", item.name) + }) + + t.Run("with age", func(t *testing.T) { + p := &person{} + item := NewWith[*person](p, withAge(42)) + + require.NotNil(t, item) + require.Same(t, p, item) + assert.Equal(t, 42, item.age) + }) + }) +} |
