summaryrefslogtreecommitdiff
path: root/pkg/x/option_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/x/option_test.go')
-rw-r--r--pkg/x/option_test.go81
1 files changed, 81 insertions, 0 deletions
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)
+ })
+ })
+}