diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 5139336..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,9 +0,0 @@ -version: '3' - -services: - dev: - build: . - volumes: - - ./:/go/src/github.com/samber/mo - working_dir: /go/src/github.com/samber/mo - command: bash -c 'make tools ; make watch-test' diff --git a/either3_test.go b/either3_test.go deleted file mode 100644 index 10b84e1..0000000 --- a/either3_test.go +++ /dev/null @@ -1,297 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewEither3(t *testing.T) { - is := assert.New(t) - - either3Arg1 := NewEither3Arg1[int, bool, float64](42) - is.Equal(Either3[int, bool, float64]{argId: either3ArgId1, arg1: 42}, either3Arg1) - - either3Arg2 := NewEither3Arg2[int, bool, float64](true) - is.Equal(Either3[int, bool, float64]{argId: either3ArgId2, arg2: true}, either3Arg2) - - either3Arg3 := NewEither3Arg3[int, bool, float64](1.2) - is.Equal(Either3[int, bool, float64]{argId: either3ArgId3, arg3: 1.2}, either3Arg3) -} - -func TestEither3IsArg(t *testing.T) { - is := assert.New(t) - - either3Arg1 := NewEither3Arg1[int, bool, float64](42) - either3Arg2 := NewEither3Arg2[int, bool, float64](true) - either3Arg3 := NewEither3Arg3[int, bool, float64](1.2) - - is.True(either3Arg1.IsArg1()) - is.False(either3Arg1.IsArg2()) - is.False(either3Arg1.IsArg3()) - - is.False(either3Arg2.IsArg1()) - is.True(either3Arg2.IsArg2()) - is.False(either3Arg2.IsArg3()) - - is.False(either3Arg3.IsArg1()) - is.False(either3Arg3.IsArg2()) - is.True(either3Arg3.IsArg3()) -} - -func TestEither3GetArg(t *testing.T) { - is := assert.New(t) - - either3Arg1 := NewEither3Arg1[int, bool, float64](42) - either3Arg2 := NewEither3Arg2[int, bool, float64](true) - either3Arg3 := NewEither3Arg3[int, bool, float64](1.2) - - result1_1, ok1_1 := either3Arg1.Arg1() - _, ok1_2 := either3Arg1.Arg2() - _, ok1_3 := either3Arg1.Arg3() - - is.Equal(42, result1_1) - is.True(ok1_1) - is.False(ok1_2) - is.False(ok1_3) - - _, ok2_1 := either3Arg2.Arg1() - result2, ok2_2 := either3Arg2.Arg2() - _, ok2_3 := either3Arg2.Arg3() - - is.Equal(true, result2) - is.False(ok2_1) - is.True(ok2_2) - is.False(ok2_3) - - _, ok3_1 := either3Arg3.Arg1() - _, ok3_2 := either3Arg3.Arg2() - result3, ok3_3 := either3Arg3.Arg3() - - is.Equal(1.2, result3) - is.False(ok3_1) - is.False(ok3_2) - is.True(ok3_3) -} - -func TestEither3MustArg(t *testing.T) { - is := assert.New(t) - - either3Arg1 := NewEither3Arg1[int, bool, float64](42) - either3Arg2 := NewEither3Arg2[int, bool, float64](true) - either3Arg3 := NewEither3Arg3[int, bool, float64](1.2) - - is.NotPanics(func() { - is.Equal(42, either3Arg1.MustArg1()) - }) - is.Panics(func() { - either3Arg1.MustArg2() - }) - is.Panics(func() { - either3Arg1.MustArg3() - }) - - is.Panics(func() { - either3Arg2.MustArg1() - }) - is.NotPanics(func() { - is.Equal(true, either3Arg2.MustArg2()) - }) - is.Panics(func() { - either3Arg2.MustArg3() - }) - - is.Panics(func() { - either3Arg3.MustArg1() - }) - is.Panics(func() { - either3Arg3.MustArg2() - }) - is.NotPanics(func() { - is.Equal(1.2, either3Arg3.MustArg3()) - }) -} - -func TestEither3Unpack(t *testing.T) { - is := assert.New(t) - - either := NewEither3Arg1[int, bool, float64](42) - either3Arg1, either3Arg2, either3Arg3 := either.Unpack() - - is.Equal(42, either3Arg1) - is.Equal(false, either3Arg2) - is.Equal(float64(0), either3Arg3) -} - -func TestEither3GetOrElse(t *testing.T) { - is := assert.New(t) - - either3Arg1 := NewEither3Arg1[int, bool, float64](42) - either3Arg2 := NewEither3Arg2[int, bool, float64](true) - either3Arg3 := NewEither3Arg3[int, bool, float64](1.2) - - is.Equal(42, either3Arg1.Arg1OrElse(21)) - is.Equal(false, either3Arg1.Arg2OrElse(false)) - is.Equal(2.1, either3Arg1.Arg3OrElse(2.1)) - - is.Equal(21, either3Arg2.Arg1OrElse(21)) - is.Equal(true, either3Arg2.Arg2OrElse(false)) - is.Equal(2.1, either3Arg2.Arg3OrElse(2.1)) - - is.Equal(21, either3Arg3.Arg1OrElse(21)) - is.Equal(false, either3Arg3.Arg2OrElse(false)) - is.Equal(1.2, either3Arg3.Arg3OrElse(2.1)) -} - -func TestEither3GetOrEmpty(t *testing.T) { - is := assert.New(t) - - either3Arg1 := NewEither3Arg1[int, bool, float64](42) - either3Arg2 := NewEither3Arg2[int, bool, float64](true) - either3Arg3 := NewEither3Arg3[int, bool, float64](1.2) - - is.Equal(42, either3Arg1.Arg1OrEmpty()) - is.Equal(false, either3Arg1.Arg2OrEmpty()) - is.Equal(0.0, either3Arg1.Arg3OrEmpty()) - - is.Equal(0, either3Arg2.Arg1OrEmpty()) - is.Equal(true, either3Arg2.Arg2OrEmpty()) - is.Equal(0.0, either3Arg2.Arg3OrEmpty()) - - is.Equal(0, either3Arg3.Arg1OrEmpty()) - is.Equal(false, either3Arg3.Arg2OrEmpty()) - is.Equal(1.2, either3Arg3.Arg3OrEmpty()) -} - -func TestEither3ForEach(t *testing.T) { - is := assert.New(t) - - NewEither3Arg1[int, bool, float64](42).ForEach(func(v1 int) { - is.Equal(42, v1) - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Fail("should not enter here") - }) - - NewEither3Arg2[int, bool, float64](true).ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Equal(true, v2) - }, func(v3 float64) { - is.Fail("should not enter here") - }) - - NewEither3Arg3[int, bool, float64](1.2).ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Equal(1.2, v3) - }) -} - -func TestEither3Match(t *testing.T) { - is := assert.New(t) - - e1 := NewEither3Arg1[int, bool, float64](42).Match(func(v1 int) Either3[int, bool, float64] { - is.Equal(42, v1) - return NewEither3Arg1[int, bool, float64](21) - }, func(v2 bool) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg2[int, bool, float64](false) - }, func(v3 float64) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg3[int, bool, float64](2.1) - }) - - is.Equal(NewEither3Arg1[int, bool, float64](21), e1) - - e2 := NewEither3Arg2[int, bool, float64](true).Match(func(v1 int) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg1[int, bool, float64](21) - }, func(v2 bool) Either3[int, bool, float64] { - is.Equal(true, v2) - return NewEither3Arg2[int, bool, float64](false) - }, func(v3 float64) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg3[int, bool, float64](2.1) - }) - - is.Equal(NewEither3Arg2[int, bool, float64](false), e2) - - e3 := NewEither3Arg3[int, bool, float64](1.2).Match(func(v1 int) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg1[int, bool, float64](21) - }, func(v2 bool) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg2[int, bool, float64](false) - }, func(v3 float64) Either3[int, bool, float64] { - is.Equal(1.2, v3) - return NewEither3Arg3[int, bool, float64](2.1) - }) - - is.Equal(NewEither3Arg3[int, bool, float64](2.1), e3) -} - -func TestEither3MapArg(t *testing.T) { - is := assert.New(t) - - either3Arg1 := NewEither3Arg1[int, bool, float64](42) - either3Arg2 := NewEither3Arg2[int, bool, float64](true) - either3Arg3 := NewEither3Arg3[int, bool, float64](1.2) - - result1_1 := either3Arg1.MapArg1(func(v int) Either3[int, bool, float64] { - is.Equal(42, v) - return NewEither3Arg1[int, bool, float64](21) - }) - is.Equal(NewEither3Arg1[int, bool, float64](21), result1_1) - - result1_2 := either3Arg1.MapArg2(func(v bool) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg2[int, bool, float64](false) - }) - is.Equal(either3Arg1, result1_2) - - result1_3 := either3Arg1.MapArg3(func(v float64) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg3[int, bool, float64](2.1) - }) - is.Equal(either3Arg1, result1_3) - - result2_1 := either3Arg2.MapArg1(func(v int) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg1[int, bool, float64](21) - }) - is.Equal(either3Arg2, result2_1) - - result2_2 := either3Arg2.MapArg2(func(v bool) Either3[int, bool, float64] { - is.Equal(true, v) - return NewEither3Arg2[int, bool, float64](false) - }) - is.Equal(NewEither3Arg2[int, bool, float64](false), result2_2) - - result2_3 := either3Arg2.MapArg3(func(v float64) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg3[int, bool, float64](2.1) - }) - is.Equal(either3Arg2, result2_3) - - result3_1 := either3Arg3.MapArg1(func(v int) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg3[int, bool, float64](21) - }) - is.Equal(either3Arg3, result3_1) - - result3_2 := either3Arg3.MapArg2(func(v bool) Either3[int, bool, float64] { - is.Fail("should not enter here") - return NewEither3Arg2[int, bool, float64](false) - }) - is.Equal(either3Arg3, result3_2) - - result3_3 := either3Arg3.MapArg3(func(v float64) Either3[int, bool, float64] { - is.Equal(1.2, v) - return NewEither3Arg3[int, bool, float64](2.1) - }) - is.Equal(NewEither3Arg3[int, bool, float64](2.1), result3_3) -} diff --git a/either4_test.go b/either4_test.go deleted file mode 100644 index 4d5726b..0000000 --- a/either4_test.go +++ /dev/null @@ -1,453 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewEither4(t *testing.T) { - is := assert.New(t) - - either4Arg1 := NewEither4Arg1[int, bool, float64, string](42) - is.Equal(Either4[int, bool, float64, string]{argId: either4ArgId1, arg1: 42}, either4Arg1) - - either4Arg2 := NewEither4Arg2[int, bool, float64, string](true) - is.Equal(Either4[int, bool, float64, string]{argId: either4ArgId2, arg2: true}, either4Arg2) - - either4Arg3 := NewEither4Arg3[int, bool, float64, string](1.2) - is.Equal(Either4[int, bool, float64, string]{argId: either4ArgId3, arg3: 1.2}, either4Arg3) - - either4Arg4 := NewEither4Arg4[int, bool, float64, string]("Hello") - is.Equal(Either4[int, bool, float64, string]{argId: either4ArgId4, arg4: "Hello"}, either4Arg4) -} - -func TestEither4IsArg(t *testing.T) { - is := assert.New(t) - - either4Arg1 := NewEither4Arg1[int, bool, float64, string](42) - either4Arg2 := NewEither4Arg2[int, bool, float64, string](true) - either4Arg3 := NewEither4Arg3[int, bool, float64, string](1.2) - either4Arg4 := NewEither4Arg4[int, bool, float64, string]("Hello") - - is.True(either4Arg1.IsArg1()) - is.False(either4Arg1.IsArg2()) - is.False(either4Arg1.IsArg3()) - is.False(either4Arg1.IsArg4()) - - is.False(either4Arg2.IsArg1()) - is.True(either4Arg2.IsArg2()) - is.False(either4Arg2.IsArg3()) - is.False(either4Arg2.IsArg4()) - - is.False(either4Arg3.IsArg1()) - is.False(either4Arg3.IsArg2()) - is.True(either4Arg3.IsArg3()) - is.False(either4Arg3.IsArg4()) - - is.False(either4Arg4.IsArg1()) - is.False(either4Arg4.IsArg2()) - is.False(either4Arg4.IsArg3()) - is.True(either4Arg4.IsArg4()) -} - -func TestEither4GetArg(t *testing.T) { - is := assert.New(t) - - either4Arg1 := NewEither4Arg1[int, bool, float64, string](42) - either4Arg2 := NewEither4Arg2[int, bool, float64, string](true) - either4Arg3 := NewEither4Arg3[int, bool, float64, string](1.2) - either4Arg4 := NewEither4Arg4[int, bool, float64, string]("Hello") - - result1_1, ok1_1 := either4Arg1.Arg1() - _, ok1_2 := either4Arg1.Arg2() - _, ok1_3 := either4Arg1.Arg3() - _, ok1_4 := either4Arg1.Arg4() - - is.Equal(42, result1_1) - is.True(ok1_1) - is.False(ok1_2) - is.False(ok1_3) - is.False(ok1_4) - - _, ok2_1 := either4Arg2.Arg1() - result2, ok2_2 := either4Arg2.Arg2() - _, ok2_3 := either4Arg2.Arg3() - _, ok2_4 := either4Arg2.Arg4() - - is.Equal(true, result2) - is.False(ok2_1) - is.True(ok2_2) - is.False(ok2_3) - is.False(ok2_4) - - _, ok3_1 := either4Arg3.Arg1() - _, ok3_2 := either4Arg3.Arg2() - result3, ok3_3 := either4Arg3.Arg3() - _, ok3_4 := either4Arg3.Arg4() - - is.Equal(1.2, result3) - is.False(ok3_1) - is.False(ok3_2) - is.True(ok3_3) - is.False(ok3_4) - - _, ok4_1 := either4Arg4.Arg1() - _, ok4_2 := either4Arg4.Arg2() - _, ok4_3 := either4Arg4.Arg3() - result4, ok4_4 := either4Arg4.Arg4() - - is.Equal("Hello", result4) - is.False(ok4_1) - is.False(ok4_2) - is.False(ok4_3) - is.True(ok4_4) -} - -func TestEither4MustArg(t *testing.T) { - is := assert.New(t) - - either4Arg1 := NewEither4Arg1[int, bool, float64, string](42) - either4Arg2 := NewEither4Arg2[int, bool, float64, string](true) - either4Arg3 := NewEither4Arg3[int, bool, float64, string](1.2) - either4Arg4 := NewEither4Arg4[int, bool, float64, string]("Hello") - - is.NotPanics(func() { - is.Equal(42, either4Arg1.MustArg1()) - }) - is.Panics(func() { - either4Arg1.MustArg2() - }) - is.Panics(func() { - either4Arg1.MustArg3() - }) - is.Panics(func() { - either4Arg1.MustArg4() - }) - - is.Panics(func() { - either4Arg2.MustArg1() - }) - is.NotPanics(func() { - is.Equal(true, either4Arg2.MustArg2()) - }) - is.Panics(func() { - either4Arg2.MustArg3() - }) - is.Panics(func() { - either4Arg2.MustArg4() - }) - - is.Panics(func() { - either4Arg3.MustArg1() - }) - is.Panics(func() { - either4Arg3.MustArg2() - }) - is.NotPanics(func() { - is.Equal(1.2, either4Arg3.MustArg3()) - }) - is.Panics(func() { - either4Arg3.MustArg4() - }) - - is.Panics(func() { - either4Arg4.MustArg1() - }) - is.Panics(func() { - either4Arg4.MustArg2() - }) - is.Panics(func() { - either4Arg4.MustArg3() - }) - is.NotPanics(func() { - is.Equal("Hello", either4Arg4.MustArg4()) - }) -} - -func TestEither4Unpack(t *testing.T) { - is := assert.New(t) - - either := NewEither4Arg1[int, bool, float64, string](42) - either4Arg1, either4Arg2, either4Arg3, either4Arg4 := either.Unpack() - - is.Equal(42, either4Arg1) - is.Equal(false, either4Arg2) - is.Equal(float64(0), either4Arg3) - is.Equal("", either4Arg4) -} - -func TestEither4GetOrElse(t *testing.T) { - is := assert.New(t) - - either4Arg1 := NewEither4Arg1[int, bool, float64, string](42) - either4Arg2 := NewEither4Arg2[int, bool, float64, string](true) - either4Arg3 := NewEither4Arg3[int, bool, float64, string](1.2) - either4Arg4 := NewEither4Arg4[int, bool, float64, string]("Hello") - - is.Equal(42, either4Arg1.Arg1OrElse(21)) - is.Equal(false, either4Arg1.Arg2OrElse(false)) - is.Equal(2.1, either4Arg1.Arg3OrElse(2.1)) - is.Equal("Bye", either4Arg1.Arg4OrElse("Bye")) - - is.Equal(21, either4Arg2.Arg1OrElse(21)) - is.Equal(true, either4Arg2.Arg2OrElse(false)) - is.Equal(2.1, either4Arg2.Arg3OrElse(2.1)) - is.Equal("Bye", either4Arg2.Arg4OrElse("Bye")) - - is.Equal(21, either4Arg3.Arg1OrElse(21)) - is.Equal(false, either4Arg3.Arg2OrElse(false)) - is.Equal(1.2, either4Arg3.Arg3OrElse(2.1)) - is.Equal("Bye", either4Arg3.Arg4OrElse("Bye")) - - is.Equal(21, either4Arg4.Arg1OrElse(21)) - is.Equal(false, either4Arg4.Arg2OrElse(false)) - is.Equal(2.1, either4Arg4.Arg3OrElse(2.1)) - is.Equal("Hello", either4Arg4.Arg4OrElse("Bye")) -} - -func TestEither4GetOrEmpty(t *testing.T) { - is := assert.New(t) - - either4Arg1 := NewEither4Arg1[int, bool, float64, string](42) - either4Arg2 := NewEither4Arg2[int, bool, float64, string](true) - either4Arg3 := NewEither4Arg3[int, bool, float64, string](1.2) - either4Arg4 := NewEither4Arg4[int, bool, float64, string]("Hello") - - is.Equal(42, either4Arg1.Arg1OrEmpty()) - is.Equal(false, either4Arg1.Arg2OrEmpty()) - is.Equal(0.0, either4Arg1.Arg3OrEmpty()) - is.Equal("", either4Arg1.Arg4OrEmpty()) - - is.Equal(0, either4Arg2.Arg1OrEmpty()) - is.Equal(true, either4Arg2.Arg2OrEmpty()) - is.Equal(0.0, either4Arg2.Arg3OrEmpty()) - is.Equal("", either4Arg2.Arg4OrEmpty()) - - is.Equal(0, either4Arg3.Arg1OrEmpty()) - is.Equal(false, either4Arg3.Arg2OrEmpty()) - is.Equal(1.2, either4Arg3.Arg3OrEmpty()) - is.Equal("", either4Arg3.Arg4OrEmpty()) - - is.Equal(0, either4Arg4.Arg1OrEmpty()) - is.Equal(false, either4Arg4.Arg2OrEmpty()) - is.Equal(0.0, either4Arg4.Arg3OrEmpty()) - is.Equal("Hello", either4Arg4.Arg4OrEmpty()) -} - -func TestEither4ForEach(t *testing.T) { - is := assert.New(t) - - NewEither4Arg1[int, bool, float64, string](42).ForEach(func(v1 int) { - is.Equal(42, v1) - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Fail("should not enter here") - }, func(v4 string) { - is.Fail("should not enter here") - }) - - NewEither4Arg2[int, bool, float64, string](true).ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Equal(true, v2) - }, func(v3 float64) { - is.Fail("should not enter here") - }, func(v4 string) { - is.Fail("should not enter here") - }) - - NewEither4Arg3[int, bool, float64, string](1.2).ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Equal(1.2, v3) - }, func(v4 string) { - is.Fail("should not enter here") - }) - - NewEither4Arg4[int, bool, float64, string]("Hello").ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Fail("should not enter here") - }, func(v4 string) { - is.Equal("Hello", v4) - }) -} - -func TestEither4Match(t *testing.T) { - is := assert.New(t) - - e1 := NewEither4Arg1[int, bool, float64, string](42).Match(func(v1 int) Either4[int, bool, float64, string] { - is.Equal(42, v1) - return NewEither4Arg1[int, bool, float64, string](21) - }, func(v2 bool) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg2[int, bool, float64, string](false) - }, func(v3 float64) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg3[int, bool, float64, string](2.1) - }, func(v4 string) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg4[int, bool, float64, string]("Bye") - }) - - is.Equal(NewEither4Arg1[int, bool, float64, string](21), e1) - - e2 := NewEither4Arg2[int, bool, float64, string](true).Match(func(v1 int) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg1[int, bool, float64, string](21) - }, func(v2 bool) Either4[int, bool, float64, string] { - is.Equal(true, v2) - return NewEither4Arg2[int, bool, float64, string](false) - }, func(v3 float64) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg3[int, bool, float64, string](2.1) - }, func(v4 string) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg4[int, bool, float64, string]("Bye") - }) - - is.Equal(NewEither4Arg2[int, bool, float64, string](false), e2) - - e3 := NewEither4Arg3[int, bool, float64, string](1.2).Match(func(v1 int) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg1[int, bool, float64, string](21) - }, func(v2 bool) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg2[int, bool, float64, string](false) - }, func(v3 float64) Either4[int, bool, float64, string] { - is.Equal(1.2, v3) - return NewEither4Arg3[int, bool, float64, string](2.1) - }, func(v4 string) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg4[int, bool, float64, string]("Bye") - }) - - is.Equal(NewEither4Arg3[int, bool, float64, string](2.1), e3) - - e4 := NewEither4Arg4[int, bool, float64, string]("Hello").Match(func(v1 int) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg1[int, bool, float64, string](21) - }, func(v2 bool) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg2[int, bool, float64, string](false) - }, func(v3 float64) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg3[int, bool, float64, string](2.1) - }, func(v4 string) Either4[int, bool, float64, string] { - is.Equal("Hello", v4) - return NewEither4Arg4[int, bool, float64, string]("Bye") - }) - - is.Equal(NewEither4Arg4[int, bool, float64, string]("Bye"), e4) -} - -func TestEither4MapArg(t *testing.T) { - is := assert.New(t) - - either4Arg1 := NewEither4Arg1[int, bool, float64, string](42) - either4Arg2 := NewEither4Arg2[int, bool, float64, string](true) - either4Arg3 := NewEither4Arg3[int, bool, float64, string](1.2) - either4Arg4 := NewEither4Arg4[int, bool, float64, string]("Hello") - - result1_1 := either4Arg1.MapArg1(func(v int) Either4[int, bool, float64, string] { - is.Equal(42, v) - return NewEither4Arg1[int, bool, float64, string](21) - }) - is.Equal(NewEither4Arg1[int, bool, float64, string](21), result1_1) - - result1_2 := either4Arg1.MapArg2(func(v bool) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg2[int, bool, float64, string](false) - }) - is.Equal(either4Arg1, result1_2) - - result1_3 := either4Arg1.MapArg3(func(v float64) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg3[int, bool, float64, string](2.1) - }) - is.Equal(either4Arg1, result1_3) - - result1_4 := either4Arg1.MapArg4(func(v string) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg4[int, bool, float64, string]("Bye") - }) - is.Equal(either4Arg1, result1_4) - - result2_1 := either4Arg2.MapArg1(func(v int) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg1[int, bool, float64, string](21) - }) - is.Equal(either4Arg2, result2_1) - - result2_2 := either4Arg2.MapArg2(func(v bool) Either4[int, bool, float64, string] { - is.Equal(true, v) - return NewEither4Arg2[int, bool, float64, string](false) - }) - is.Equal(NewEither4Arg2[int, bool, float64, string](false), result2_2) - - result2_3 := either4Arg2.MapArg3(func(v float64) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg3[int, bool, float64, string](2.1) - }) - is.Equal(either4Arg2, result2_3) - - result2_4 := either4Arg2.MapArg4(func(v string) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg4[int, bool, float64, string]("Bye") - }) - is.Equal(either4Arg2, result2_4) - - result3_1 := either4Arg3.MapArg1(func(v int) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg3[int, bool, float64, string](21) - }) - is.Equal(either4Arg3, result3_1) - - result3_2 := either4Arg3.MapArg2(func(v bool) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg2[int, bool, float64, string](false) - }) - is.Equal(either4Arg3, result3_2) - - result3_3 := either4Arg3.MapArg3(func(v float64) Either4[int, bool, float64, string] { - is.Equal(1.2, v) - return NewEither4Arg3[int, bool, float64, string](2.1) - }) - is.Equal(NewEither4Arg3[int, bool, float64, string](2.1), result3_3) - - result3_4 := either4Arg3.MapArg4(func(v string) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg4[int, bool, float64, string]("Bye") - }) - is.Equal(either4Arg3, result3_4) - - result4_1 := either4Arg4.MapArg1(func(v int) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg1[int, bool, float64, string](21) - }) - is.Equal(either4Arg4, result4_1) - - result4_2 := either4Arg4.MapArg2(func(v bool) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg2[int, bool, float64, string](false) - }) - is.Equal(either4Arg4, result4_2) - - result4_3 := either4Arg4.MapArg3(func(v float64) Either4[int, bool, float64, string] { - is.Fail("should not enter here") - return NewEither4Arg3[int, bool, float64, string](2.1) - }) - is.Equal(either4Arg4, result4_3) - - result4_4 := either4Arg4.MapArg4(func(v string) Either4[int, bool, float64, string] { - is.Equal("Hello", v) - return NewEither4Arg4[int, bool, float64, string]("Bye") - }) - is.Equal(NewEither4Arg4[int, bool, float64, string]("Bye"), result4_4) -} diff --git a/either5_example_test.go b/either5_example_test.go deleted file mode 100644 index 53fe20f..0000000 --- a/either5_example_test.go +++ /dev/null @@ -1,108 +0,0 @@ -package mo - -import "fmt" - -func ExampleNewEither5Arg1() { - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - result1 := either5Arg1.Arg1OrElse(21) - result2 := either5Arg1.Arg4OrElse("Bye") - - fmt.Println(result1, result2) - // Output: 42 Bye -} - -func ExampleEither5_IsArg1() { - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - result1 := either5Arg1.IsArg1() - result2 := either5Arg1.IsArg4() - - fmt.Println(result1, result2) - // Output: true false -} - -func ExampleEither5_Arg1() { - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - result1, ok1 := either5Arg1.Arg1() - result2, ok2 := either5Arg1.Arg3() - - fmt.Println(result1, ok1) - fmt.Println(result2, ok2) - // Output: - // 42 true - // 0 false -} - -func ExampleEither5_MustArg1() { - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - - // result = either5Arg1.MustArg4() - // Panics - - result := either5Arg1.MustArg1() - fmt.Println(result) - // Output: 42 -} - -func ExampleEither5_Unpack() { - either5 := NewEither5Arg1[int, bool, float64, string, byte](42) - a, b, c, d, e := either5.Unpack() - - fmt.Println(a, b, c, d, e) - // Output: 42 false 0 0 -} - -func ExampleEither5_Arg1OrElse() { - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - result1 := either5Arg1.Arg1OrElse(21) - result2 := either5Arg1.Arg4OrElse("Bye") - - fmt.Println(result1, result2) - // Output: 42 Bye -} - -func ExampleEither5_Arg1OrEmpty() { - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - result1 := either5Arg1.Arg1OrEmpty() - result2 := either5Arg1.Arg2OrEmpty() - - fmt.Println(result1, result2) - // Output: 42 false -} - -func ExampleEither5_Match() { - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - - result1 := either5Arg1.Match(func(v int) Either5[int, bool, float64, string, byte] { - return NewEither5Arg1[int, bool, float64, string, byte](21) - }, func(v bool) Either5[int, bool, float64, string, byte] { - return NewEither5Arg2[int, bool, float64, string, byte](false) - }, func(v float64) Either5[int, bool, float64, string, byte] { - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }, func(v string) Either5[int, bool, float64, string, byte] { - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }, func(v byte) Either5[int, bool, float64, string, byte] { - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - - fmt.Println(result1.MustArg1()) - // Output: 21 -} - -func ExampleEither5_MapArg1() { - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - - result1 := either5Arg1.MapArg1( - func(v int) Either5[int, bool, float64, string, byte] { - return NewEither5Arg1[int, bool, float64, string, byte](21) - }, - ) - - result2 := either5Arg1.MapArg4( - func(v string) Either5[int, bool, float64, string, byte] { - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }, - ) - - fmt.Println(result1.MustArg1(), result2.MustArg1()) - // Output: 21 42 -} diff --git a/either5_test.go b/either5_test.go deleted file mode 100644 index 754f141..0000000 --- a/either5_test.go +++ /dev/null @@ -1,647 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewEither5(t *testing.T) { - is := assert.New(t) - - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - is.Equal(Either5[int, bool, float64, string, byte]{argId: either5ArgId1, arg1: 42}, either5Arg1) - - either5Arg2 := NewEither5Arg2[int, bool, float64, string, byte](true) - is.Equal(Either5[int, bool, float64, string, byte]{argId: either5ArgId2, arg2: true}, either5Arg2) - - either5Arg3 := NewEither5Arg3[int, bool, float64, string, byte](1.2) - is.Equal(Either5[int, bool, float64, string, byte]{argId: either5ArgId3, arg3: 1.2}, either5Arg3) - - either5Arg4 := NewEither5Arg4[int, bool, float64, string, byte]("Hello") - is.Equal(Either5[int, bool, float64, string, byte]{argId: either5ArgId4, arg4: "Hello"}, either5Arg4) - - either5Arg5 := NewEither5Arg5[int, bool, float64, string, byte](5) - is.Equal(Either5[int, bool, float64, string, byte]{argId: either5ArgId5, arg5: 5}, either5Arg5) -} - -func TestEither5IsArg(t *testing.T) { - is := assert.New(t) - - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - either5Arg2 := NewEither5Arg2[int, bool, float64, string, byte](true) - either5Arg3 := NewEither5Arg3[int, bool, float64, string, byte](1.2) - either5Arg4 := NewEither5Arg4[int, bool, float64, string, byte]("Hello") - either5Arg5 := NewEither5Arg5[int, bool, float64, string, byte](5) - - is.True(either5Arg1.IsArg1()) - is.False(either5Arg1.IsArg2()) - is.False(either5Arg1.IsArg3()) - is.False(either5Arg1.IsArg4()) - is.False(either5Arg1.IsArg5()) - - is.False(either5Arg2.IsArg1()) - is.True(either5Arg2.IsArg2()) - is.False(either5Arg2.IsArg3()) - is.False(either5Arg2.IsArg4()) - is.False(either5Arg2.IsArg5()) - - is.False(either5Arg3.IsArg1()) - is.False(either5Arg3.IsArg2()) - is.True(either5Arg3.IsArg3()) - is.False(either5Arg3.IsArg4()) - is.False(either5Arg3.IsArg5()) - - is.False(either5Arg4.IsArg1()) - is.False(either5Arg4.IsArg2()) - is.False(either5Arg4.IsArg3()) - is.True(either5Arg4.IsArg4()) - is.False(either5Arg4.IsArg5()) - - is.False(either5Arg5.IsArg1()) - is.False(either5Arg5.IsArg2()) - is.False(either5Arg5.IsArg3()) - is.False(either5Arg5.IsArg4()) - is.True(either5Arg5.IsArg5()) -} - -func TestEither5GetArg(t *testing.T) { - is := assert.New(t) - - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - either5Arg2 := NewEither5Arg2[int, bool, float64, string, byte](true) - either5Arg3 := NewEither5Arg3[int, bool, float64, string, byte](1.2) - either5Arg4 := NewEither5Arg4[int, bool, float64, string, byte]("Hello") - either5Arg5 := NewEither5Arg5[int, bool, float64, string, byte](5) - - result1_1, ok1_1 := either5Arg1.Arg1() - _, ok1_2 := either5Arg1.Arg2() - _, ok1_3 := either5Arg1.Arg3() - _, ok1_4 := either5Arg1.Arg4() - _, ok1_5 := either5Arg1.Arg5() - - is.Equal(42, result1_1) - is.True(ok1_1) - is.False(ok1_2) - is.False(ok1_3) - is.False(ok1_4) - is.False(ok1_5) - - _, ok2_1 := either5Arg2.Arg1() - result2, ok2_2 := either5Arg2.Arg2() - _, ok2_3 := either5Arg2.Arg3() - _, ok2_4 := either5Arg2.Arg4() - _, ok2_5 := either5Arg2.Arg5() - - is.Equal(true, result2) - is.False(ok2_1) - is.True(ok2_2) - is.False(ok2_3) - is.False(ok2_4) - is.False(ok2_5) - - _, ok3_1 := either5Arg3.Arg1() - _, ok3_2 := either5Arg3.Arg2() - result3, ok3_3 := either5Arg3.Arg3() - _, ok3_4 := either5Arg3.Arg4() - _, ok3_5 := either5Arg3.Arg5() - - is.Equal(1.2, result3) - is.False(ok3_1) - is.False(ok3_2) - is.True(ok3_3) - is.False(ok3_4) - is.False(ok3_5) - - _, ok4_1 := either5Arg4.Arg1() - _, ok4_2 := either5Arg4.Arg2() - _, ok4_3 := either5Arg4.Arg3() - result4, ok4_4 := either5Arg4.Arg4() - _, ok4_5 := either5Arg4.Arg5() - - is.Equal("Hello", result4) - is.False(ok4_1) - is.False(ok4_2) - is.False(ok4_3) - is.True(ok4_4) - is.False(ok4_5) - - _, ok5_1 := either5Arg5.Arg1() - _, ok5_2 := either5Arg5.Arg2() - _, ok5_3 := either5Arg5.Arg3() - _, ok5_4 := either5Arg5.Arg4() - result5, ok5_5 := either5Arg5.Arg5() - - is.Equal(byte(5), result5) - is.False(ok5_1) - is.False(ok5_2) - is.False(ok5_3) - is.False(ok5_4) - is.True(ok5_5) -} - -func TestEither5MustArg(t *testing.T) { - is := assert.New(t) - - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - either5Arg2 := NewEither5Arg2[int, bool, float64, string, byte](true) - either5Arg3 := NewEither5Arg3[int, bool, float64, string, byte](1.2) - either5Arg4 := NewEither5Arg4[int, bool, float64, string, byte]("Hello") - either5Arg5 := NewEither5Arg5[int, bool, float64, string, byte](5) - - is.NotPanics(func() { - is.Equal(42, either5Arg1.MustArg1()) - }) - is.Panics(func() { - either5Arg1.MustArg2() - }) - is.Panics(func() { - either5Arg1.MustArg3() - }) - is.Panics(func() { - either5Arg1.MustArg4() - }) - is.Panics(func() { - either5Arg1.MustArg5() - }) - - is.Panics(func() { - either5Arg2.MustArg1() - }) - is.NotPanics(func() { - is.Equal(true, either5Arg2.MustArg2()) - }) - is.Panics(func() { - either5Arg2.MustArg3() - }) - is.Panics(func() { - either5Arg2.MustArg4() - }) - is.Panics(func() { - either5Arg2.MustArg5() - }) - - is.Panics(func() { - either5Arg3.MustArg1() - }) - is.Panics(func() { - either5Arg3.MustArg2() - }) - is.NotPanics(func() { - is.Equal(1.2, either5Arg3.MustArg3()) - }) - is.Panics(func() { - either5Arg3.MustArg4() - }) - is.Panics(func() { - either5Arg3.MustArg5() - }) - - is.Panics(func() { - either5Arg4.MustArg1() - }) - is.Panics(func() { - either5Arg4.MustArg2() - }) - is.Panics(func() { - either5Arg4.MustArg3() - }) - is.NotPanics(func() { - is.Equal("Hello", either5Arg4.MustArg4()) - }) - is.Panics(func() { - either5Arg4.MustArg5() - }) - - is.Panics(func() { - either5Arg5.MustArg1() - }) - is.Panics(func() { - either5Arg5.MustArg2() - }) - is.Panics(func() { - either5Arg5.MustArg3() - }) - is.Panics(func() { - either5Arg5.MustArg4() - }) - is.NotPanics(func() { - is.Equal(byte(5), either5Arg5.MustArg5()) - }) -} - -func TestEither5Unpack(t *testing.T) { - is := assert.New(t) - - either := NewEither5Arg1[int, bool, float64, string, string](42) - either5Arg1, either5Arg2, either5Arg3, either5Arg4, either5Arg5 := either.Unpack() - - is.Equal(42, either5Arg1) - is.Equal(false, either5Arg2) - is.Equal(float64(0), either5Arg3) - is.Equal("", either5Arg4) - is.Equal("", either5Arg5) -} - -func TestEither5GetOrElse(t *testing.T) { - is := assert.New(t) - - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - either5Arg2 := NewEither5Arg2[int, bool, float64, string, byte](true) - either5Arg3 := NewEither5Arg3[int, bool, float64, string, byte](1.2) - either5Arg4 := NewEither5Arg4[int, bool, float64, string, byte]("Hello") - either5Arg5 := NewEither5Arg5[int, bool, float64, string, byte](5) - - is.Equal(42, either5Arg1.Arg1OrElse(21)) - is.Equal(false, either5Arg1.Arg2OrElse(false)) - is.Equal(2.1, either5Arg1.Arg3OrElse(2.1)) - is.Equal("Bye", either5Arg1.Arg4OrElse("Bye")) - is.Equal(byte(10), either5Arg1.Arg5OrElse(10)) - - is.Equal(21, either5Arg2.Arg1OrElse(21)) - is.Equal(true, either5Arg2.Arg2OrElse(false)) - is.Equal(2.1, either5Arg2.Arg3OrElse(2.1)) - is.Equal("Bye", either5Arg2.Arg4OrElse("Bye")) - is.Equal(byte(10), either5Arg2.Arg5OrElse(10)) - - is.Equal(21, either5Arg3.Arg1OrElse(21)) - is.Equal(false, either5Arg3.Arg2OrElse(false)) - is.Equal(1.2, either5Arg3.Arg3OrElse(2.1)) - is.Equal("Bye", either5Arg3.Arg4OrElse("Bye")) - is.Equal(byte(10), either5Arg3.Arg5OrElse(10)) - - is.Equal(21, either5Arg4.Arg1OrElse(21)) - is.Equal(false, either5Arg4.Arg2OrElse(false)) - is.Equal(2.1, either5Arg4.Arg3OrElse(2.1)) - is.Equal("Hello", either5Arg4.Arg4OrElse("Bye")) - is.Equal(byte(10), either5Arg4.Arg5OrElse(10)) - - is.Equal(21, either5Arg5.Arg1OrElse(21)) - is.Equal(false, either5Arg5.Arg2OrElse(false)) - is.Equal(2.1, either5Arg5.Arg3OrElse(2.1)) - is.Equal("Bye", either5Arg5.Arg4OrElse("Bye")) - is.Equal(byte(5), either5Arg5.Arg5OrElse(10)) -} - -func TestEither5GetOrEmpty(t *testing.T) { - is := assert.New(t) - - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - either5Arg2 := NewEither5Arg2[int, bool, float64, string, byte](true) - either5Arg3 := NewEither5Arg3[int, bool, float64, string, byte](1.2) - either5Arg4 := NewEither5Arg4[int, bool, float64, string, byte]("Hello") - either5Arg5 := NewEither5Arg5[int, bool, float64, string, byte](5) - - is.Equal(42, either5Arg1.Arg1OrEmpty()) - is.Equal(false, either5Arg1.Arg2OrEmpty()) - is.Equal(0.0, either5Arg1.Arg3OrEmpty()) - is.Equal("", either5Arg1.Arg4OrEmpty()) - is.Equal(byte(0), either5Arg1.Arg5OrEmpty()) - - is.Equal(0, either5Arg2.Arg1OrEmpty()) - is.Equal(true, either5Arg2.Arg2OrEmpty()) - is.Equal(0.0, either5Arg2.Arg3OrEmpty()) - is.Equal("", either5Arg2.Arg4OrEmpty()) - is.Equal(byte(0), either5Arg2.Arg5OrEmpty()) - - is.Equal(0, either5Arg3.Arg1OrEmpty()) - is.Equal(false, either5Arg3.Arg2OrEmpty()) - is.Equal(1.2, either5Arg3.Arg3OrEmpty()) - is.Equal("", either5Arg3.Arg4OrEmpty()) - is.Equal(byte(0), either5Arg3.Arg5OrEmpty()) - - is.Equal(0, either5Arg4.Arg1OrEmpty()) - is.Equal(false, either5Arg4.Arg2OrEmpty()) - is.Equal(0.0, either5Arg4.Arg3OrEmpty()) - is.Equal("Hello", either5Arg4.Arg4OrEmpty()) - is.Equal(byte(0), either5Arg4.Arg5OrEmpty()) - - is.Equal(0, either5Arg5.Arg1OrEmpty()) - is.Equal(false, either5Arg5.Arg2OrEmpty()) - is.Equal(0.0, either5Arg5.Arg3OrEmpty()) - is.Equal("", either5Arg5.Arg4OrEmpty()) - is.Equal(byte(5), either5Arg5.Arg5OrEmpty()) -} - -func TestEither5ForEach(t *testing.T) { - is := assert.New(t) - - NewEither5Arg1[int, bool, float64, string, byte](42).ForEach(func(v1 int) { - is.Equal(42, v1) - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Fail("should not enter here") - }, func(v4 string) { - is.Fail("should not enter here") - }, func(v5 byte) { - is.Fail("should not enter here") - }) - - NewEither5Arg2[int, bool, float64, string, byte](true).ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Equal(true, v2) - }, func(v3 float64) { - is.Fail("should not enter here") - }, func(v4 string) { - is.Fail("should not enter here") - }, func(v5 byte) { - is.Fail("should not enter here") - }) - - NewEither5Arg3[int, bool, float64, string, byte](1.2).ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Equal(1.2, v3) - }, func(v4 string) { - is.Fail("should not enter here") - }, func(v5 byte) { - is.Fail("should not enter here") - }) - - NewEither5Arg4[int, bool, float64, string, byte]("Hello").ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Fail("should not enter here") - }, func(v4 string) { - is.Equal("Hello", v4) - }, func(v5 byte) { - is.Fail("should not enter here") - }) - - NewEither5Arg5[int, bool, float64, string, byte](5).ForEach(func(v1 int) { - is.Fail("should not enter here") - }, func(v2 bool) { - is.Fail("should not enter here") - }, func(v3 float64) { - is.Fail("should not enter here") - }, func(v4 string) { - is.Fail("should not enter here") - }, func(v5 byte) { - is.Equal(byte(5), v5) - }) -} - -func TestEither5Match(t *testing.T) { - is := assert.New(t) - - e1 := NewEither5Arg1[int, bool, float64, string, byte](42).Match(func(v1 int) Either5[int, bool, float64, string, byte] { - is.Equal(42, v1) - return NewEither5Arg1[int, bool, float64, string, byte](21) - }, func(v2 bool) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg2[int, bool, float64, string, byte](false) - }, func(v3 float64) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }, func(v4 string) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }, func(v5 byte) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - - is.Equal(NewEither5Arg1[int, bool, float64, string, byte](21), e1) - - e2 := NewEither5Arg2[int, bool, float64, string, byte](true).Match(func(v1 int) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg1[int, bool, float64, string, byte](21) - }, func(v2 bool) Either5[int, bool, float64, string, byte] { - is.Equal(true, v2) - return NewEither5Arg2[int, bool, float64, string, byte](false) - }, func(v3 float64) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }, func(v4 string) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }, func(v5 byte) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - - is.Equal(NewEither5Arg2[int, bool, float64, string, byte](false), e2) - - e3 := NewEither5Arg3[int, bool, float64, string, byte](1.2).Match(func(v1 int) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg1[int, bool, float64, string, byte](21) - }, func(v2 bool) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg2[int, bool, float64, string, byte](false) - }, func(v3 float64) Either5[int, bool, float64, string, byte] { - is.Equal(1.2, v3) - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }, func(v4 string) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }, func(v5 byte) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - - is.Equal(NewEither5Arg3[int, bool, float64, string, byte](2.1), e3) - - e4 := NewEither5Arg4[int, bool, float64, string, byte]("Hello").Match(func(v1 int) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg1[int, bool, float64, string, byte](21) - }, func(v2 bool) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg2[int, bool, float64, string, byte](false) - }, func(v3 float64) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }, func(v4 string) Either5[int, bool, float64, string, byte] { - is.Equal("Hello", v4) - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }, func(v5 byte) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - - is.Equal(NewEither5Arg4[int, bool, float64, string, byte]("Bye"), e4) - - e5 := NewEither5Arg5[int, bool, float64, string, byte](5).Match(func(v1 int) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg1[int, bool, float64, string, byte](21) - }, func(v2 bool) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg2[int, bool, float64, string, byte](false) - }, func(v3 float64) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }, func(v4 string) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }, func(v5 byte) Either5[int, bool, float64, string, byte] { - is.Equal(byte(5), v5) - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - - is.Equal(NewEither5Arg5[int, bool, float64, string, byte](10), e5) -} - -func TestEither5MapArg(t *testing.T) { - is := assert.New(t) - - either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) - either5Arg2 := NewEither5Arg2[int, bool, float64, string, byte](true) - either5Arg3 := NewEither5Arg3[int, bool, float64, string, byte](1.2) - either5Arg4 := NewEither5Arg4[int, bool, float64, string, byte]("Hello") - either5Arg5 := NewEither5Arg5[int, bool, float64, string, byte](5) - - result1_1 := either5Arg1.MapArg1(func(v int) Either5[int, bool, float64, string, byte] { - is.Equal(42, v) - return NewEither5Arg1[int, bool, float64, string, byte](21) - }) - is.Equal(NewEither5Arg1[int, bool, float64, string, byte](21), result1_1) - - result1_2 := either5Arg1.MapArg2(func(v bool) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg2[int, bool, float64, string, byte](false) - }) - is.Equal(either5Arg1, result1_2) - - result1_3 := either5Arg1.MapArg3(func(v float64) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }) - is.Equal(either5Arg1, result1_3) - - result1_4 := either5Arg1.MapArg4(func(v string) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }) - is.Equal(either5Arg1, result1_4) - - result1_5 := either5Arg1.MapArg5(func(v byte) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - is.Equal(either5Arg1, result1_5) - - result2_1 := either5Arg2.MapArg1(func(v int) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg1[int, bool, float64, string, byte](21) - }) - is.Equal(either5Arg2, result2_1) - - result2_2 := either5Arg2.MapArg2(func(v bool) Either5[int, bool, float64, string, byte] { - is.Equal(true, v) - return NewEither5Arg2[int, bool, float64, string, byte](false) - }) - is.Equal(NewEither5Arg2[int, bool, float64, string, byte](false), result2_2) - - result2_3 := either5Arg2.MapArg3(func(v float64) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }) - is.Equal(either5Arg2, result2_3) - - result2_4 := either5Arg2.MapArg4(func(v string) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }) - is.Equal(either5Arg2, result2_4) - - result2_5 := either5Arg2.MapArg5(func(v byte) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - is.Equal(either5Arg2, result2_5) - - result3_1 := either5Arg3.MapArg1(func(v int) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](21) - }) - is.Equal(either5Arg3, result3_1) - - result3_2 := either5Arg3.MapArg2(func(v bool) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg2[int, bool, float64, string, byte](false) - }) - is.Equal(either5Arg3, result3_2) - - result3_3 := either5Arg3.MapArg3(func(v float64) Either5[int, bool, float64, string, byte] { - is.Equal(1.2, v) - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }) - is.Equal(NewEither5Arg3[int, bool, float64, string, byte](2.1), result3_3) - - result3_4 := either5Arg3.MapArg4(func(v string) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }) - is.Equal(either5Arg3, result3_4) - - result3_5 := either5Arg3.MapArg5(func(v byte) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - is.Equal(either5Arg3, result3_5) - - result4_1 := either5Arg4.MapArg1(func(v int) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg1[int, bool, float64, string, byte](21) - }) - is.Equal(either5Arg4, result4_1) - - result4_2 := either5Arg4.MapArg2(func(v bool) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg2[int, bool, float64, string, byte](false) - }) - is.Equal(either5Arg4, result4_2) - - result4_3 := either5Arg4.MapArg3(func(v float64) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }) - is.Equal(either5Arg4, result4_3) - - result4_4 := either5Arg4.MapArg4(func(v string) Either5[int, bool, float64, string, byte] { - is.Equal("Hello", v) - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }) - is.Equal(NewEither5Arg4[int, bool, float64, string, byte]("Bye"), result4_4) - - result4_5 := either5Arg4.MapArg5(func(v byte) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - is.Equal(either5Arg4, result4_5) - - result5_1 := either5Arg5.MapArg1(func(v int) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg1[int, bool, float64, string, byte](21) - }) - is.Equal(either5Arg5, result5_1) - - result5_2 := either5Arg5.MapArg2(func(v bool) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg2[int, bool, float64, string, byte](false) - }) - is.Equal(either5Arg5, result5_2) - - result5_3 := either5Arg5.MapArg3(func(v float64) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg3[int, bool, float64, string, byte](2.1) - }) - is.Equal(either5Arg5, result5_3) - - result5_4 := either5Arg5.MapArg4(func(v string) Either5[int, bool, float64, string, byte] { - is.Fail("should not enter here") - return NewEither5Arg4[int, bool, float64, string, byte]("Bye") - }) - is.Equal(either5Arg5, result5_4) - - result5_5 := either5Arg5.MapArg5(func(v byte) Either5[int, bool, float64, string, byte] { - is.Equal(byte(5), v) - return NewEither5Arg5[int, bool, float64, string, byte](10) - }) - is.Equal(NewEither5Arg5[int, bool, float64, string, byte](10), result5_5) -} diff --git a/either_example_test.go b/either_example_test.go deleted file mode 100644 index ad8fd59..0000000 --- a/either_example_test.go +++ /dev/null @@ -1,366 +0,0 @@ -package mo - -import "fmt" - -func ExampleLeft() { - left := Left[string, int]("hello") - result1 := left.LeftOrElse("world") - result2 := left.RightOrElse(1234) - - fmt.Println(result1, result2) - // Output: hello 1234 -} - -func ExampleRight() { - right := Right[string, int](42) - result1 := right.LeftOrElse("world") - result2 := right.RightOrElse(1234) - - fmt.Println(result1, result2) - // Output: world 42 -} - -func ExampleEither_Unpack_left() { - either := Left[string, int]("42") - left, right := either.Unpack() - - fmt.Println(left, right) - // Output: 42 0 -} - -func ExampleEither_Unpack_right() { - either := Right[string, int](42) - left, right := either.Unpack() - - fmt.Println(left, right) - // Output: 42 -} - -func ExampleEither_IsLeft_left() { - left := Left[string, int]("hello") - result := left.IsLeft() - - fmt.Println(result) - // Output: true -} - -func ExampleEither_IsLeft_right() { - right := Right[string, int](42) - result := right.IsLeft() - - fmt.Println(result) - // Output: false -} - -func ExampleEither_IsRight_left() { - left := Left[string, int]("hello") - result := left.IsRight() - - fmt.Println(result) - // Output: false -} - -func ExampleEither_IsRight_right() { - right := Right[string, int](42) - result := right.IsRight() - - fmt.Println(result) - // Output: true -} - -func ExampleEither_Left_left() { - left := Left[string, int]("hello") - result, ok := left.Left() - - fmt.Println(result) - fmt.Println(ok) - // Output: - // hello - // true -} - -func ExampleEither_Left_right() { - right := Right[string, int](42) - result, ok := right.Left() - - fmt.Println(result) - fmt.Println(ok) - // Output: - // false -} - -func ExampleEither_Right_left() { - left := Left[string, int]("hello") - result, ok := left.Right() - - fmt.Println(result) - fmt.Println(ok) - // Output: - // 0 - // false -} - -func ExampleEither_Right_right() { - right := Right[string, int](42) - result, ok := right.Right() - - fmt.Println(result) - fmt.Println(ok) - // Output: - // 42 - // true -} - -func ExampleEither_MustLeft_left() { - left := Left[string, int]("hello") - result := left.MustLeft() - - fmt.Println(result) - // Output: hello -} - -// func ExampleEither_MustLeft_right() { -// right := Right[string, int](42) -// result := right.MustLeft() - -// fmt.Println(result) -// // Output: panics -// } - -// func ExampleEither_MustRight_left() { -// left := Left[string, int]("hello") -// result := left.MustRight() - -// fmt.Println(result) -// // Output: panics -// } - -func ExampleEither_MustRight_right() { - right := Right[string, int](42) - result := right.MustRight() - - fmt.Println(result) - // Output: 42 -} - -func ExampleEither_LeftOrElse_left() { - left := Left[string, int]("hello") - result := left.LeftOrElse("world") - - fmt.Println(result) - // Output: hello -} - -func ExampleEither_LeftOrElse_right() { - right := Right[string, int](42) - result := right.LeftOrElse("world") - - fmt.Println(result) - // Output: world -} - -func ExampleEither_RightOrElse_left() { - left := Left[string, int]("hello") - result := left.RightOrElse(1234) - - fmt.Println(result) - // Output: 1234 -} - -func ExampleEither_RightOrElse_right() { - right := Right[string, int](42) - result := right.RightOrElse(1234) - - fmt.Println(result) - // Output: 42 -} - -func ExampleEither_LeftOrEmpty_left() { - left := Left[string, int]("hello") - result := left.LeftOrEmpty() - - fmt.Println(result) - // Output: hello -} - -func ExampleEither_LeftOrEmpty_right() { - right := Right[string, int](42) - result := right.LeftOrEmpty() - - fmt.Println(result) - // Output: -} - -func ExampleEither_RightOrEmpty_left() { - left := Left[string, int]("hello") - result := left.RightOrEmpty() - - fmt.Println(result) - // Output: 0 -} - -func ExampleEither_RightOrEmpty_right() { - right := Right[string, int](42) - result := right.RightOrEmpty() - - fmt.Println(result) - // Output: 42 -} - -func ExampleEither_Swap_left() { - left := Left[string, int]("hello") - right := left.Swap() - result1, ok1 := right.Left() - result2, ok2 := right.Right() - - fmt.Println(result1) - fmt.Println(ok1) - fmt.Println(result2) - fmt.Println(ok2) - // Output: - // 0 - // false - // hello - // true -} - -func ExampleEither_Swap_right() { - right := Right[string, int](42) - left := right.Swap() - result1, ok1 := left.Left() - result2, ok2 := left.Right() - - fmt.Println(result1) - fmt.Println(ok1) - fmt.Println(result2) - fmt.Println(ok2) - // 42 - // true - // - // false -} - -func ExampleEither_Match_left() { - left := Left[string, int]("hello") - result := left.Match( - func(s string) Either[string, int] { - return Right[string, int](1234) - }, - func(i int) Either[string, int] { - return Right[string, int](i * 42) - }, - ) - result1, ok1 := result.Left() - result2, ok2 := result.Right() - - fmt.Println(result1) - fmt.Println(ok1) - fmt.Println(result2) - fmt.Println(ok2) - // Output: - // false - // 1234 - // true -} -func ExampleEither_Match_right() { - right := Right[string, int](42) - result := right.Match( - func(s string) Either[string, int] { - return Left[string, int]("world") - }, - func(i int) Either[string, int] { - return Left[string, int]("foobar") - }, - ) - result1, ok1 := result.Left() - result2, ok2 := result.Right() - - fmt.Println(result1) - fmt.Println(ok1) - fmt.Println(result2) - fmt.Println(ok2) - // Output: - // foobar - // true - // 0 - // false -} - -func ExampleEither_MapLeft_left() { - left := Left[string, int]("hello") - result := left.MapLeft( - func(s string) Either[string, int] { - return Right[string, int](1234) - }, - ) - result1, ok1 := result.Left() - result2, ok2 := result.Right() - - fmt.Println(result1) - fmt.Println(ok1) - fmt.Println(result2) - fmt.Println(ok2) - // Output: - // false - // 1234 - // true -} -func ExampleEither_MapLeft_right() { - right := Right[string, int](42) - result := right.MapLeft( - func(s string) Either[string, int] { - return Left[string, int]("world") - }, - ) - result1, ok1 := result.Left() - result2, ok2 := result.Right() - - fmt.Println(result1) - fmt.Println(ok1) - fmt.Println(result2) - fmt.Println(ok2) - // Output: - // false - // 42 - // true -} - -func ExampleEither_MapRight_left() { - left := Left[string, int]("hello") - result := left.MapRight( - func(i int) Either[string, int] { - return Right[string, int](1234) - }, - ) - result1, ok1 := result.Left() - result2, ok2 := result.Right() - - fmt.Println(result1) - fmt.Println(ok1) - fmt.Println(result2) - fmt.Println(ok2) - // Output: - // hello - // true - // 0 - // false -} -func ExampleEither_MapRight_right() { - right := Right[string, int](42) - result := right.MapRight( - func(i int) Either[string, int] { - return Right[string, int](1234) - }, - ) - result1, ok1 := result.Left() - result2, ok2 := result.Right() - - fmt.Println(result1) - fmt.Println(ok1) - fmt.Println(result2) - fmt.Println(ok2) - // Output: - // false - // 1234 - // true -} diff --git a/either_test.go b/either_test.go deleted file mode 100644 index e09f7f0..0000000 --- a/either_test.go +++ /dev/null @@ -1,213 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestEitherLeft(t *testing.T) { - is := assert.New(t) - - left := Left[int, bool](42) - is.Equal(Either[int, bool]{left: 42, right: false, isLeft: true}, left) -} - -func TestEitherRight(t *testing.T) { - is := assert.New(t) - - right := Right[int, bool](true) - is.Equal(Either[int, bool]{left: 0, right: true, isLeft: false}, right) -} - -func TestEitherUnpack(t *testing.T) { - is := assert.New(t) - - left1, right1 := Left[int, bool](42).Unpack() - left2, right2 := Right[int, bool](true).Unpack() - - is.Equal(42, left1) - is.Equal(false, right1) - is.Equal(0, left2) - is.Equal(true, right2) -} - -func TestEitherIsLeftOrRight(t *testing.T) { - is := assert.New(t) - - left := Left[int, bool](42) - right := Right[int, bool](true) - - is.True(left.IsLeft()) - is.False(left.IsRight()) - is.False(right.IsLeft()) - is.True(right.IsRight()) -} - -func TestEitherLeftOrRight(t *testing.T) { - is := assert.New(t) - - left := Left[int, bool](42) - right := Right[int, bool](true) - - result1, ok1 := left.Left() - result2, ok2 := left.Right() - result3, ok3 := right.Left() - result4, ok4 := right.Right() - - is.Equal(42, result1) - is.True(ok1) - is.Equal(false, result2) - is.False(ok2) - is.Equal(0, result3) - is.False(ok3) - is.Equal(true, result4) - is.True(ok4) -} - -func TestEitherMustLeftOrRight(t *testing.T) { - is := assert.New(t) - - left := Left[int, bool](42) - right := Right[int, bool](true) - - is.NotPanics(func() { - is.Equal(42, left.MustLeft()) - }) - is.Panics(func() { - left.MustRight() - }) - is.Panics(func() { - right.MustLeft() - }) - is.NotPanics(func() { - is.Equal(true, right.MustRight()) - }) -} - -func TestEitherGetOrElse(t *testing.T) { - is := assert.New(t) - - left := Left[int, string](42) - right := Right[int, string]("foobar") - - is.Equal(42, left.LeftOrElse(21)) - is.Equal(21, right.LeftOrElse(21)) - is.Equal("baz", left.RightOrElse("baz")) - is.Equal("foobar", right.RightOrElse("baz")) -} - -func TestEitherGetOrEmpty(t *testing.T) { - is := assert.New(t) - - left := Left[int, string](42) - right := Right[int, string]("foobar") - - is.Equal(42, left.LeftOrEmpty()) - is.Equal(0, right.LeftOrEmpty()) - is.Equal("", left.RightOrEmpty()) - is.Equal("foobar", right.RightOrEmpty()) -} - -func TestEitherSwap(t *testing.T) { - is := assert.New(t) - - left := Left[int, string](42) - right := Right[int, string]("foobar") - - is.Equal(Either[string, int]{left: "", right: 42, isLeft: false}, left.Swap()) - is.Equal(Either[string, int]{left: "foobar", right: 0, isLeft: true}, right.Swap()) -} - -func TestEitherForEach(t *testing.T) { - is := assert.New(t) - - Left[int, string](42).ForEach( - func(a int) { - is.Equal(42, a) - }, - func(b string) { - is.Fail("should not enter here") - }, - ) - - Right[int, string]("foobar").ForEach( - func(a int) { - is.Fail("should not enter here") - }, - func(b string) { - is.Equal("foobar", b) - }, - ) -} - -func TestEitherMatch(t *testing.T) { - is := assert.New(t) - - e1 := Left[int, string](42).Match( - func(a int) Either[int, string] { - is.Equal(42, a) - return Left[int, string](21) - }, - func(b string) Either[int, string] { - is.Fail("should not enter here") - return Left[int, string](1) - }, - ) - - e2 := Right[int, string]("foobar").Match( - func(a int) Either[int, string] { - is.Fail("should not enter here") - return Right[int, string]("baz") - }, - func(b string) Either[int, string] { - is.Equal("foobar", b) - return Right[int, string]("plop") - }, - ) - - is.Equal(Either[int, string]{left: 21, right: "", isLeft: true}, e1) - is.Equal(Either[int, string]{left: 0, right: "plop", isLeft: false}, e2) -} - -func TestEitherMapLeft(t *testing.T) { - is := assert.New(t) - - e1 := Left[int, string](42).MapLeft( - func(a int) Either[int, string] { - is.Equal(42, a) - return Left[int, string](21) - }, - ) - - e2 := Right[int, string]("foobar").MapLeft( - func(a int) Either[int, string] { - is.Fail("should not enter here") - return Right[int, string]("plop") - }, - ) - - is.Equal(Either[int, string]{left: 21, right: "", isLeft: true}, e1) - is.Equal(Either[int, string]{left: 0, right: "foobar", isLeft: false}, e2) -} - -func TestEitherMapRight(t *testing.T) { - is := assert.New(t) - - e1 := Left[int, string](42).MapRight( - func(b string) Either[int, string] { - is.Fail("should not enter here") - return Left[int, string](21) - }, - ) - - e2 := Right[int, string]("foobar").MapRight( - func(b string) Either[int, string] { - is.Equal("foobar", b) - return Right[int, string]("plop") - }, - ) - - is.Equal(Either[int, string]{left: 42, right: "", isLeft: true}, e1) - is.Equal(Either[int, string]{left: 0, right: "plop", isLeft: false}, e2) -} diff --git a/future_example_test.go b/future_example_test.go deleted file mode 100644 index 2ea86bb..0000000 --- a/future_example_test.go +++ /dev/null @@ -1,171 +0,0 @@ -package mo - -import "fmt" - -func ExampleNewFuture_resolve() { - value, err := NewFuture(func(resolve func(string), reject func(error)) { - resolve("foobar") - }).Collect() - - fmt.Println(value) - fmt.Println(err) - // Output: - // foobar - // -} - -func ExampleNewFuture_reject() { - value, err := NewFuture(func(resolve func(string), reject func(error)) { - reject(fmt.Errorf("failure")) - }).Collect() - - fmt.Println(value) - fmt.Println(err) - // Output: - // - // failure -} - -func ExampleFuture_Collect_resolve() { - value, err := NewFuture(func(resolve func(string), reject func(error)) { - resolve("foobar") - }).Collect() - - fmt.Println(value) - fmt.Println(err) - // Output: - // foobar - // -} - -func ExampleFuture_Collect_reject() { - value, err := NewFuture(func(resolve func(string), reject func(error)) { - reject(fmt.Errorf("failure")) - }).Collect() - - fmt.Println(value) - fmt.Println(err) - // Output: - // - // failure -} - -func ExampleFuture_Result_resolve() { - result := NewFuture(func(resolve func(string), reject func(error)) { - resolve("foobar") - }).Result() - - fmt.Println(result.OrEmpty()) - fmt.Println(result.Error()) - // Output: - // foobar - // -} - -func ExampleFuture_Result_reject() { - result := NewFuture(func(resolve func(string), reject func(error)) { - reject(fmt.Errorf("failure")) - }).Result() - - fmt.Println(result.OrEmpty()) - fmt.Println(result.Error()) - // Output: - // - // failure -} - -func ExampleFuture_Then_resolve() { - result := NewFuture(func(resolve func(string), reject func(error)) { - resolve("foobar") - }).Then(func(s string) (string, error) { - return "baz", nil - }).Result() - - fmt.Println(result.OrEmpty()) - fmt.Println(result.Error()) - // Output: - // baz - // -} - -func ExampleFuture_Then_reject() { - result := NewFuture(func(resolve func(string), reject func(error)) { - reject(fmt.Errorf("failure")) - }).Then(func(s string) (string, error) { - return "foobar", nil - }).Result() - - fmt.Println(result.OrEmpty()) - fmt.Println(result.Error()) - // Output: - // - // failure -} - -func ExampleFuture_Catch_resolve() { - result := NewFuture(func(resolve func(string), reject func(error)) { - resolve("foobar") - }).Catch(func(err error) (string, error) { - return "baz", nil - }).Result() - - fmt.Println(result.OrEmpty()) - fmt.Println(result.Error()) - // Output: - // foobar - // -} - -func ExampleFuture_Catch_reject() { - result := NewFuture(func(resolve func(string), reject func(error)) { - reject(fmt.Errorf("failure")) - }).Catch(func(err error) (string, error) { - return "foobar", nil - }).Result() - - fmt.Println(result.OrEmpty()) - fmt.Println(result.Error()) - // Output: - // foobar - // -} - -func ExampleFuture_Finally_resolve() { - result := NewFuture(func(resolve func(string), reject func(error)) { - resolve("foobar") - }).Finally(func(value string, err error) (string, error) { - return "baz", nil - }).Result() - - fmt.Println(result.OrEmpty()) - fmt.Println(result.Error()) - // Output: - // baz - // -} - -func ExampleFuture_Finally_reject() { - result := NewFuture(func(resolve func(string), reject func(error)) { - reject(fmt.Errorf("failure")) - }).Finally(func(value string, err error) (string, error) { - return "foobar", nil - }).Result() - - fmt.Println(result.OrEmpty()) - fmt.Println(result.Error()) - // Output: - // foobar - // -} - -func ExampleFuture_Cancel_resolve() { - NewFuture(func(resolve func(string), reject func(error)) { - resolve("foobar") - }).Cancel() -} - -func ExampleFuture_Cancel_reject() { - NewFuture(func(resolve func(string), reject func(error)) { - reject(fmt.Errorf("failure")) - }).Cancel() -} diff --git a/future_test.go b/future_test.go deleted file mode 100644 index 2e12f53..0000000 --- a/future_test.go +++ /dev/null @@ -1,326 +0,0 @@ -package mo - -import ( - "fmt" - "sync/atomic" - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func assertAndIncrement(is *assert.Assertions, expected int, i *int32) { - got := atomic.LoadInt32(i) - - is.Equal(int32(expected), got) - - atomic.AddInt32(i, 1) -} - -func TestFuture(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - resolve(42) - }).Then(func(value int) (int, error) { - is.Equal(42, value) - return 21, assert.AnError - }).Catch(func(err error) (int, error) { - is.Equal(assert.AnError, err) - return 0, nil - }).Then(func(value int) (int, error) { - is.Equal(0, value) - return 84, nil - }).Collect() - - is.Equal(84, result) - is.Nil(err) -} - -func TestFutureSimpleResolve(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - resolve(42) - }).Collect() - - is.Equal(42, result) - is.Nil(err) -} - -func TestFutureSimpleReject(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }).Collect() - - is.Equal(0, result) - is.NotNil(err) - is.Equal(assert.AnError, err) -} - -func TestFutureMultipleResolve(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - resolve(42) - }).Then(func(value int) (int, error) { - is.Equal(42, value) - return 84, nil - }).Then(func(value int) (int, error) { - is.Equal(84, value) - return 21, nil - }).Collect() - - is.Equal(21, result) - is.Nil(err) -} - -func TestFutureMultipleReject(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - resolve(42) - }).Catch(func(err error) (int, error) { - is.Fail("should not enter here") - return 84, assert.AnError - }).Then(func(value int) (int, error) { - is.Equal(42, value) - return 21, assert.AnError - }).Catch(func(err error) (int, error) { - is.Equal(assert.AnError, err) - return 1, nil - }).Collect() - - is.Equal(1, result) - is.Nil(err) -} - -func TestFutureSingleReject(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }).Catch(func(err error) (int, error) { - is.Equal(assert.AnError, err) - return 84, nil - }).Collect() - - is.Equal(84, result) - is.Nil(err) -} - -func TestFutureErrorResult(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }).Collect() - - is.Equal(0, result) - is.NotNil(err) - is.Equal(assert.AnError, err) -} - -func TestFutureFinallyResolve(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - resolve(21) - }).Finally(func(value int, err error) (int, error) { - is.Equal(21, value) - is.Nil(err) - - return 42, nil - }).Collect() - - is.Equal(42, result) - is.Nil(err) -} - -func TestFutureFinallyReject(t *testing.T) { - is := assert.New(t) - - result, err := NewFuture[int](func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }).Finally(func(value int, err error) (int, error) { - is.Equal(0, value) - is.NotNil(err) - is.Equal(assert.AnError, err) - - return 42, nil - }).Collect() - - is.Equal(42, result) - is.Nil(err) -} - -func TestFutureOrder(t *testing.T) { - is := assert.New(t) - - var i int32 = 0 - - _ = NewFuture[int](func(resolve func(int), reject func(error)) { - assertAndIncrement(is, 1, &i) - - resolve(42) - }).Then(func(value int) (int, error) { - assertAndIncrement(is, 2, &i) - - return 21, assert.AnError - }).Catch(func(err error) (int, error) { - assertAndIncrement(is, 3, &i) - - return 1, nil - }).Finally(func(value int, err error) (int, error) { - assertAndIncrement(is, 4, &i) - - return 21, nil - }) - - assertAndIncrement(is, 0, &i) -} - -func TestFutureOrderCollect(t *testing.T) { - is := assert.New(t) - - var i int32 = 0 - - _, _ = NewFuture[int](func(resolve func(int), reject func(error)) { - assertAndIncrement(is, 0, &i) - - resolve(42) - }).Then(func(value int) (int, error) { - assertAndIncrement(is, 1, &i) - - return 21, assert.AnError - }).Catch(func(err error) (int, error) { - assertAndIncrement(is, 2, &i) - - return 1, nil - }).Finally(func(value int, err error) (int, error) { - assertAndIncrement(is, 3, &i) - - return 1, nil - }).Collect() - - assertAndIncrement(is, 4, &i) -} - -func TestFutureCancel(t *testing.T) { - is := assert.New(t) - - var i int32 = 0 - - future := NewFuture[int](func(resolve func(int), reject func(error)) { - assertAndIncrement(is, 0, &i) - - time.Sleep(5 * time.Millisecond) - - resolve(42) - }).Then(func(value int) (int, error) { - assertAndIncrement(is, 3, &i) - is.Fail("should not enter here") - - return 21, assert.AnError - }) - - time.Sleep(1 * time.Millisecond) - assertAndIncrement(is, 1, &i) - future.Cancel() - - time.Sleep(10 * time.Millisecond) - assertAndIncrement(is, 2, &i) -} - -func TestFutureCancelDelayed(t *testing.T) { - is := assert.New(t) - - var i int32 = 0 - - future := NewFuture[int](func(resolve func(int), reject func(error)) { - time.Sleep(1 * time.Millisecond) - assertAndIncrement(is, 1, &i) - - resolve(42) - }).Then(func(value int) (int, error) { - assertAndIncrement(is, 2, &i) - - return 21, assert.AnError - }) - - assertAndIncrement(is, 0, &i) - - time.Sleep(10 * time.Millisecond) - - future.Cancel() - - assertAndIncrement(is, 3, &i) -} - -func TestFutureCancelTerminated(t *testing.T) { - is := assert.New(t) - - var i int32 = 0 - - future := NewFuture[int](func(resolve func(int), reject func(error)) { - time.Sleep(1 * time.Millisecond) - assertAndIncrement(is, 1, &i) - - resolve(42) - }).Then(func(value int) (int, error) { - assertAndIncrement(is, 2, &i) - - return 21, assert.AnError - }) - - assertAndIncrement(is, 0, &i) - - _, _ = future.Collect() - - assertAndIncrement(is, 3, &i) - - future.Cancel() - - assertAndIncrement(is, 4, &i) -} - -func TestFutureResultResult(t *testing.T) { - is := assert.New(t) - - result := NewFuture[int](func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }).Result() - - is.Equal(Err[int](assert.AnError), result) - is.NotNil(result.Error()) - is.Equal(assert.AnError, result.Error()) -} - -func TestFutureResultEither(t *testing.T) { - is := assert.New(t) - - either := NewFuture[int](func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }).Either() - - is.Equal(Left[error, int](assert.AnError), either) - is.NotNil(either.Left()) - is.Equal(assert.AnError, either.MustLeft()) -} - -func TestFutureCompleteBeforeThen(t *testing.T) { - completed := make(chan struct{}) - fut := NewFuture(func(resolve func(int), reject func(error)) { - resolve(1) - close(completed) - }) - - <-completed - //nolint:errcheck - fut.Then(func(in int) (int, error) { - fmt.Println(in) // will never been print - return in, nil - }).Collect() // deadlock -} diff --git a/go.mod b/go.mod index 976c777..e684e02 100644 --- a/go.mod +++ b/go.mod @@ -5,11 +5,3 @@ go 1.18 // // Dependencies are excluded from releases. Please check CI. // - -require github.com/stretchr/testify v1.7.0 - -require ( - github.com/davecgh/go-spew v1.1.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect -) diff --git a/go.sum b/go.sum index acb88a4..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +0,0 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/io_either_example_test.go b/io_either_example_test.go deleted file mode 100644 index 3e21fca..0000000 --- a/io_either_example_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package mo - -import ( - "errors" - "fmt" - "os" -) - -func ExampleIOEither1() { - io := NewIOEither1(func(path string) (bool, error) { - _, err := os.Stat(path) - - if errors.Is(err, os.ErrNotExist) { - return false, nil - } else if err != nil { - // other errors - return false, err - } - - return true, nil - }) - - either1 := io.Run("./io_either.go") - either2 := io.Run("./foo_bar.go") - - exist1, _ := either1.Right() - exist2, _ := either2.Right() - - fmt.Println(exist1) - fmt.Println(exist2) - // Output: - // true - // false -} diff --git a/io_either_test.go b/io_either_test.go deleted file mode 100644 index c0dbf05..0000000 --- a/io_either_test.go +++ /dev/null @@ -1,106 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestIOEither(t *testing.T) { - is := assert.New(t) - - ioEither := NewIOEither(func() (int, error) { - return 42, nil - }) - result := ioEither.Run() - - is.False(result.isLeft) - is.Nil(result.Left()) - is.True(result.IsRight()) - is.Equal(42, result.MustRight()) -} - -func TestIOEither1(t *testing.T) { - is := assert.New(t) - - ioEither := NewIOEither1(func(a string) (int, error) { - is.Equal("foo", a) - return 42, nil - }) - result := ioEither.Run("foo") - - is.False(result.isLeft) - is.Nil(result.Left()) - is.True(result.IsRight()) - is.Equal(42, result.MustRight()) -} - -func TestIOEither2(t *testing.T) { - is := assert.New(t) - - ioEither := NewIOEither2(func(a string, b string) (int, error) { - is.Equal("foo", a) - is.Equal("bar", b) - return 42, nil - }) - result := ioEither.Run("foo", "bar") - - is.False(result.isLeft) - is.Nil(result.Left()) - is.True(result.IsRight()) - is.Equal(42, result.MustRight()) -} - -func TestIOEither3(t *testing.T) { - is := assert.New(t) - - ioEither := NewIOEither3(func(a string, b string, c string) (int, error) { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - return 42, nil - }) - result := ioEither.Run("foo", "bar", "hello") - - is.False(result.isLeft) - is.Nil(result.Left()) - is.True(result.IsRight()) - is.Equal(42, result.MustRight()) -} - -func TestIOEither4(t *testing.T) { - is := assert.New(t) - - ioEither := NewIOEither4(func(a string, b string, c string, d string) (int, error) { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - is.Equal("world", d) - return 42, nil - }) - result := ioEither.Run("foo", "bar", "hello", "world") - - is.False(result.isLeft) - is.Nil(result.Left()) - is.True(result.IsRight()) - is.Equal(42, result.MustRight()) -} - -func TestIOEither5(t *testing.T) { - is := assert.New(t) - - ioEither := NewIOEither5(func(a string, b string, c string, d string, e bool) (int, error) { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - is.Equal("world", d) - is.True(e) - return 42, nil - }) - result := ioEither.Run("foo", "bar", "hello", "world", true) - - is.False(result.isLeft) - is.Nil(result.Left()) - is.True(result.IsRight()) - is.Equal(42, result.MustRight()) -} diff --git a/io_example_test.go b/io_example_test.go deleted file mode 100644 index ee55d41..0000000 --- a/io_example_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package mo - -import ( - "fmt" - "time" -) - -func ExampleIO() { - io := NewIO(func() int { - return time.Now().Year() - }) - - result1 := io.Run() - result2 := io.Run() - result3 := io.Run() - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - // Output: - // 2023 - // 2023 - // 2023 -} diff --git a/io_test.go b/io_test.go deleted file mode 100644 index 61e1499..0000000 --- a/io_test.go +++ /dev/null @@ -1,88 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestIO(t *testing.T) { - is := assert.New(t) - - io := NewIO(func() int { - return 42 - }) - result := io.Run() - - is.Equal(42, result) -} - -func TestIO1(t *testing.T) { - is := assert.New(t) - - io := NewIO1(func(a string) int { - is.Equal("foo", a) - return 42 - }) - result := io.Run("foo") - - is.Equal(42, result) -} - -func TestIO2(t *testing.T) { - is := assert.New(t) - - io := NewIO2(func(a string, b string) int { - is.Equal("foo", a) - is.Equal("bar", b) - return 42 - }) - result := io.Run("foo", "bar") - - is.Equal(42, result) -} - -func TestIO3(t *testing.T) { - is := assert.New(t) - - io := NewIO3(func(a string, b string, c string) int { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - return 42 - }) - result := io.Run("foo", "bar", "hello") - - is.Equal(42, result) -} - -func TestIO4(t *testing.T) { - is := assert.New(t) - - io := NewIO4(func(a string, b string, c string, d string) int { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - is.Equal("world", d) - return 42 - }) - result := io.Run("foo", "bar", "hello", "world") - - is.Equal(42, result) -} - -func TestIO5(t *testing.T) { - is := assert.New(t) - - io := NewIO5(func(a string, b string, c string, d string, e bool) int { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - is.Equal("world", d) - is.True(e) - return 42 - }) - result := io.Run("foo", "bar", "hello", "world", true) - - is.Equal(42, result) -} diff --git a/option_example_test.go b/option_example_test.go deleted file mode 100644 index 4e9064b..0000000 --- a/option_example_test.go +++ /dev/null @@ -1,347 +0,0 @@ -package mo - -import ( - "encoding/json" - "fmt" -) - -func ExampleSome() { - some := Some(42) - result := some.OrElse(1234) - - fmt.Println(result) - // Output: 42 -} - -func ExampleNone() { - none := None[int]() - result := none.OrElse(1234) - - fmt.Println(result) - // Output: 1234 -} - -func ExampleTupleToOption() { - m := map[string]int{ - "foo": 21, - "bar": 42, - "baz": 84, - } - - value, ok := m["hello world"] - - none := TupleToOption(value, ok) - result := none.OrElse(1234) - - fmt.Println(result) - // Output: 1234 -} - -func ExampleEmptyableToOption() { - cb := func(ok bool) error { - if ok { - return nil - } - - return fmt.Errorf("an error") - } - - err := cb(false) - - none := EmptyableToOption(err) - result, ok := none.Get() - - fmt.Println(result) - fmt.Println(ok) - // Output: - // an error - // true -} - -func ExampleOption_some() { - some := Some(42) - result := some.OrElse(1234) - - fmt.Println(result) - // Output: 42 -} - -func ExampleOption_none() { - none := None[int]() - result := none.OrElse(1234) - - fmt.Println(result) - // Output: 1234 -} - -func ExampleOption_IsPresent_some() { - some := Some(42) - result := some.IsPresent() - - fmt.Println(result) - // Output: true -} - -func ExampleOption_IsPresent_none() { - none := None[int]() - result := none.IsPresent() - - fmt.Println(result) - // Output: false -} - -func ExampleOption_IsAbsent_some() { - some := Some(42) - result := some.IsAbsent() - - fmt.Println(result) - // Output: false -} - -func ExampleOption_IsAbsent_none() { - none := None[int]() - result := none.IsAbsent() - - fmt.Println(result) - // Output: true -} - -func ExampleOption_Size_some() { - some := Some(42) - result := some.Size() - - fmt.Println(result) - // Output: 1 -} - -func ExampleOption_Size_none() { - none := None[int]() - result := none.Size() - - fmt.Println(result) - // Output: 0 -} - -func ExampleOption_Get_some() { - some := Some(42) - result, ok := some.Get() - - fmt.Println(result) - fmt.Println(ok) - // Output: - // 42 - // true -} - -func ExampleOption_Get_none() { - none := None[int]() - result, ok := none.Get() - - fmt.Println(result) - fmt.Println(ok) - // Output: - // 0 - // false -} - -func ExampleOption_MustGet_some() { - some := Some(42) - result := some.MustGet() - - fmt.Println(result) - // Output: 42 -} - -// func ExampleOption_MustGet_none() { -// none := None[int]() -// result := none.MustGet() - -// fmt.Println(result) -// // Output: panics -// } - -func ExampleOption_OrElse_some() { - some := Some(42) - result := some.OrElse(1234) - - fmt.Println(result) - // Output: 42 -} - -func ExampleOption_OrElse_none() { - none := None[int]() - result := none.OrElse(1234) - - fmt.Println(result) - // Output: 1234 -} - -func ExampleOption_OrEmpty_some() { - some := Some(42) - result := some.OrEmpty() - - fmt.Println(result) - // Output: 42 -} - -func ExampleOption_OrEmpty_none() { - none := None[int]() - result := none.OrEmpty() - - fmt.Println(result) - // Output: 0 -} - -func ExampleOption_Match_some() { - some := Some(42) - result := some.Match( - func(i int) (int, bool) { - return 0, false - }, - func() (int, bool) { - return 2, true - }, - ) - - fmt.Println(result.IsPresent(), result.OrEmpty()) - // Output: false 0 -} - -func ExampleOption_Match_none() { - none := None[int]() - result := none.Match( - func(i int) (int, bool) { - return 0, false - }, - func() (int, bool) { - return 2, true - }, - ) - - fmt.Println(result.IsPresent(), result.OrEmpty()) - // Output: true 2 -} - -func ExampleOption_Map_some() { - some := Some(42) - result := some.Map(func(i int) (int, bool) { - return 1234, true - }) - - fmt.Println(result.IsPresent(), result.OrEmpty()) - // Output: true 1234 -} - -func ExampleOption_Map_none() { - none := None[int]() - result := none.Map(func(i int) (int, bool) { - return 1234, true - }) - - fmt.Println(result.IsPresent(), result.OrEmpty()) - // Output: false 0 -} - -func ExampleOption_MapNone_some() { - some := Some(42) - result := some.MapNone(func() (int, bool) { - return 1234, true - }) - - fmt.Println(result.IsPresent(), result.OrEmpty()) - // Output: true 42 -} - -func ExampleOption_MapNone_none() { - none := None[int]() - result := none.MapNone(func() (int, bool) { - return 1234, true - }) - - fmt.Println(result.IsPresent(), result.OrEmpty()) - // Output: true 1234 -} - -func ExampleOption_FlatMap_some() { - some := Some(42) - result := some.FlatMap(func(i int) Option[int] { - return Some(21) - }) - - fmt.Println(result.IsPresent(), result.OrEmpty()) - // Output: true 21 -} - -func ExampleOption_FlatMap_none() { - none := None[int]() - result := none.FlatMap(func(i int) Option[int] { - return Some(21) - }) - - fmt.Println(result.IsPresent(), result.OrEmpty()) - // Output: false 0 -} - -func ExampleOption_MarshalJSON_some() { - type test struct { - Email Option[string] `json:"email"` - } - - value := test{Email: Some("samuel@example.com")} - result, err := json.Marshal(value) - - fmt.Println(string(result)) - fmt.Println(err) - // Output: - // {"email":"samuel@example.com"} - // -} - -func ExampleOption_MarshalJSON_none() { - type test struct { - Email Option[string] `json:"email"` - } - - value := test{Email: None[string]()} - result, err := json.Marshal(value) - - fmt.Println(string(result)) - fmt.Println(err) - // Output: - // {"email":null} - // -} - -func ExampleOption_UnmarshalJSON_some() { - type test struct { - Email Option[string] `json:"email"` - } - - value := []byte(`{"email":"samuel@example.com"}`) - - var result test - err := json.Unmarshal(value, &result) - - fmt.Println(result.Email.Get()) - fmt.Println(err) - // Output: - // samuel@example.com true - // -} - -func ExampleOption_UnmarshalJSON_none() { - type test struct { - Email Option[string] `json:"email"` - } - - value := []byte(`{"email":null}`) - - var result test - err := json.Unmarshal(value, &result) - - fmt.Println(result.Email.Get()) - fmt.Println(err) - // Output: - // false - // -} diff --git a/option_test.go b/option_test.go deleted file mode 100644 index 052cb4d..0000000 --- a/option_test.go +++ /dev/null @@ -1,415 +0,0 @@ -package mo - -import ( - "database/sql" - "encoding/json" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestOptionSome(t *testing.T) { - is := assert.New(t) - - is.Equal(Option[int]{value: 42, isPresent: true}, Some(42)) -} - -func TestOptionNone(t *testing.T) { - is := assert.New(t) - - is.Equal(Option[int]{isPresent: false}, None[int]()) -} - -func TestTupleToOption(t *testing.T) { - is := assert.New(t) - - cb := func(v int, ok bool) func() (int, bool) { - return func() (int, bool) { - return v, ok - } - } - - is.Equal(Option[int]{isPresent: false}, TupleToOption(cb(42, false)())) - is.Equal(Option[int]{isPresent: true, value: 42}, TupleToOption(cb(42, true)())) -} - -func TestOptionEmptyableToOption(t *testing.T) { - is := assert.New(t) - - is.Equal(Option[error]{isPresent: false}, EmptyableToOption[error](nil)) - is.Equal(Option[error]{isPresent: true, value: assert.AnError}, EmptyableToOption(assert.AnError)) - - is.Equal(Option[int]{isPresent: false}, EmptyableToOption(0)) - is.Equal(Option[int]{isPresent: true, value: 42}, EmptyableToOption(42)) -} - -func TestOptionPointerToOption(t *testing.T) { - is := assert.New(t) - - is.Equal(Option[error]{isPresent: false}, PointerToOption[error](nil)) - is.Equal(Option[error]{isPresent: true, value: assert.AnError}, PointerToOption(&assert.AnError)) - - zero := 0 - fortyTwo := 42 - is.Equal(Option[int]{isPresent: true, value: 0}, PointerToOption(&zero)) - is.Equal(Option[int]{isPresent: true, value: 42}, PointerToOption(&fortyTwo)) -} - -func TestOptionIsPresent(t *testing.T) { - is := assert.New(t) - - is.True(Some(42).IsPresent()) - is.False(None[int]().IsPresent()) -} - -func TestOptionIsAbsent(t *testing.T) { - is := assert.New(t) - - is.False(Some(42).IsAbsent()) - is.True(None[int]().IsAbsent()) -} - -func TestOptionSize(t *testing.T) { - is := assert.New(t) - - is.Equal(1, Some(42).Size()) - is.Equal(0, None[int]().Size()) -} - -func TestOptionGet(t *testing.T) { - is := assert.New(t) - - v1, ok1 := Some(42).Get() - v2, ok2 := None[int]().Get() - - is.Equal(42, v1) - is.Equal(true, ok1) - is.Equal(0, v2) - is.Equal(false, ok2) -} - -func TestOptionMustGet(t *testing.T) { - is := assert.New(t) - - is.NotPanics(func() { - Some(42).MustGet() - }) - is.Panics(func() { - None[int]().MustGet() - }) - - is.Equal(42, Some(42).MustGet()) -} - -func TestOptionOrElse(t *testing.T) { - is := assert.New(t) - - is.Equal(42, Some(42).OrElse(21)) - is.Equal(21, None[int]().OrElse(21)) -} - -func TestOptionOrEmpty(t *testing.T) { - is := assert.New(t) - - is.Equal(42, Some(42).OrEmpty()) - is.Equal(0, None[int]().OrEmpty()) -} - -func TestOptionForEach(t *testing.T) { - is := assert.New(t) - - tmp := 0 - f := func(x int) { - tmp = x - } - - None[int]().ForEach(f) - is.Equal(0, tmp) - - Some(42).ForEach(f) - is.Equal(42, tmp) -} - -func TestOptionMatch(t *testing.T) { - is := assert.New(t) - - onValue := func(i int) (int, bool) { - return i * 2, true - } - onNone := func() (int, bool) { - return 0, false - } - - opt1 := Some(21).Match(onValue, onNone) - opt2 := None[int]().Match(onValue, onNone) - - is.Equal(Option[int]{value: 42, isPresent: true}, opt1) - is.Equal(Option[int]{value: 0, isPresent: false}, opt2) -} - -func TestOptionMap(t *testing.T) { - is := assert.New(t) - - opt1 := Some(21).Map(func(i int) (int, bool) { - return i * 2, true - }) - opt2 := None[int]().Map(func(i int) (int, bool) { - is.Fail("should not be called") - return 42, true - }) - - is.Equal(Option[int]{value: 42, isPresent: true}, opt1) - is.Equal(Option[int]{value: 0, isPresent: false}, opt2) -} - -func TestOptionMapNone(t *testing.T) { - is := assert.New(t) - - opt1 := Some(21).MapNone(func() (int, bool) { - is.Fail("should not be called") - return 42, true - }) - opt2 := None[int]().MapNone(func() (int, bool) { - return 42, true - }) - - is.Equal(Option[int]{value: 21, isPresent: true}, opt1) - is.Equal(Option[int]{value: 42, isPresent: true}, opt2) -} - -func TestOptionFlatMap(t *testing.T) { - is := assert.New(t) - - opt1 := Some(21).FlatMap(func(i int) Option[int] { - return Some(42) - }) - opt2 := None[int]().FlatMap(func(i int) Option[int] { - return Some(42) - }) - - is.Equal(Option[int]{value: 42, isPresent: true}, opt1) - is.Equal(Option[int]{value: 0, isPresent: false}, opt2) -} - -func TestOptionMarshalJSON(t *testing.T) { - is := assert.New(t) - - option1 := Some("foo") - option2 := None[string]() - option3 := Some("") - - value, err := option1.MarshalJSON() - is.NoError(err) - is.Equal(`"foo"`, string(value)) - - value, err = option2.MarshalJSON() - is.NoError(err) - is.Equal(`null`, string(value)) - - value, err = option3.MarshalJSON() - is.NoError(err) - is.Equal(`""`, string(value)) - - type testStruct struct { - Field Option[string] - } - - optionInStruct := testStruct{ - Field: option1, - } - var marshalled []byte - marshalled, err = json.Marshal(optionInStruct) - is.NoError(err) - is.Equal(`{"Field":"foo"}`, string(marshalled)) -} - -func TestOptionUnmarshalJSON(t *testing.T) { - is := assert.New(t) - - option1 := Some("foo") - option2 := None[string]() - - err := option1.UnmarshalJSON([]byte(`"foo"`)) - is.NoError(err) - is.Equal(Some("foo"), option1) - - var res Option[string] - err = json.Unmarshal([]byte(`"foo"`), &res) - is.NoError(err) - is.Equal(res, option1) - - err = option2.UnmarshalJSON([]byte(`null`)) - is.NoError(err) - is.Equal(None[string](), option2) - - type testStruct struct { - Field Option[string] - } - - unmarshal := testStruct{} - err = json.Unmarshal([]byte(`{"Field": "foo"}`), &unmarshal) - is.NoError(err) - is.Equal(testStruct{ - Field: Some("foo"), - }, unmarshal) - - unmarshal = testStruct{} - err = json.Unmarshal([]byte(`{"Field": null}`), &unmarshal) - is.NoError(err) - is.Equal(testStruct{Field: None[string]()}, unmarshal) - - unmarshal = testStruct{} - err = json.Unmarshal([]byte(`{}`), &unmarshal) - is.NoError(err) - is.Equal(testStruct{Field: None[string]()}, unmarshal) - - unmarshal = testStruct{} - err = json.Unmarshal([]byte(`{"Field": ""}`), &unmarshal) - is.NoError(err) - is.Equal(testStruct{Field: Some("")}, unmarshal) - - unmarshal = testStruct{} - err = json.Unmarshal([]byte(`{"Field": "}`), &unmarshal) - is.Error(err) -} - -func TestOptionMarshalText(t *testing.T) { - is := assert.New(t) - - bytes1, err1 := Some(42).MarshalText() - bytes2, err2 := None[int]().MarshalText() - bytes3, err3 := Some("42").MarshalText() - - is.Equal([]byte("42"), bytes1) - is.Nil(err1) - is.Equal([]byte("null"), bytes2) - is.Nil(err2) - is.Equal([]byte("\"42\""), bytes3) - is.Nil(err3) -} - -func TestOptionUnmarshalText(t *testing.T) { - is := assert.New(t) - - option1 := Option[int]{} - option2 := Option[int]{} - option3 := Option[string]{} - - err1 := option1.UnmarshalText([]byte("null")) - err2 := option2.UnmarshalText([]byte("42")) - err3 := option3.UnmarshalText([]byte("\"42\"")) - - is.Equal(None[int](), option1) - is.Nil(err1) - is.Equal(Some[int](42), option2) - is.Nil(err2) - is.Equal(Some[string]("42"), option3) - is.Nil(err3) -} - -func TestOptionMarshalBinary(t *testing.T) { - is := assert.New(t) - - binary1, err1 := Some(42).MarshalBinary() - binary2, err2 := None[int]().MarshalBinary() - binary3, err3 := Some("42").MarshalBinary() - - is.Equal([]byte{1, 0x3, 0x4, 0x0, 0x54}, binary1) - is.Nil(err1) - is.Equal([]byte{0}, binary2) - is.Nil(err2) - is.Equal([]byte{1, 0x5, 0xc, 0x0, 0x2, 0x34, 0x32}, binary3) - is.Nil(err3) -} - -func TestOptionUnmarshalBinary(t *testing.T) { - is := assert.New(t) - - option1 := Option[int]{} - option2 := Option[int]{} - option3 := Option[string]{} - - err1 := option1.UnmarshalBinary([]byte{0}) - err2 := option2.UnmarshalBinary([]byte{1, 0x3, 0x4, 0x0, 0x54}) - err3 := option3.UnmarshalBinary([]byte{1, 0x5, 0xc, 0x0, 0x2, 0x34, 0x32}) - - is.Equal(None[int](), option1) - is.Nil(err1) - is.Equal(Some[int](42), option2) - is.Nil(err2) - is.Equal(Some[string]("42"), option3) - is.Nil(err3) -} - -func TestOptionGobEncode(t *testing.T) { - is := assert.New(t) - - binary1, err1 := Some(42).GobEncode() - binary2, err2 := None[int]().GobEncode() - binary3, err3 := Some("42").GobEncode() - - is.Equal([]byte{1, 0x3, 0x4, 0x0, 0x54}, binary1) - is.Nil(err1) - is.Equal([]byte{0}, binary2) - is.Nil(err2) - is.Equal([]byte{1, 0x5, 0xc, 0x0, 0x2, 0x34, 0x32}, binary3) - is.Nil(err3) -} - -func TestOptionGobDecode(t *testing.T) { - is := assert.New(t) - - option1 := Option[int]{} - option2 := Option[int]{} - option3 := Option[string]{} - - err1 := option1.GobDecode([]byte{0}) - err2 := option2.GobDecode([]byte{1, 0x3, 0x4, 0x0, 0x54}) - err3 := option3.GobDecode([]byte{1, 0x5, 0xc, 0x0, 0x2, 0x34, 0x32}) - - is.Equal(None[int](), option1) - is.Nil(err1) - is.Equal(Some[int](42), option2) - is.Nil(err2) - is.Equal(Some[string]("42"), option3) - is.Nil(err3) -} - -func TestOptionScan(t *testing.T) { - is := assert.New(t) - - option1 := Some("foo") - option2 := None[string]() - - nullString1 := sql.NullString{String: "foo", Valid: true} - nullString2 := sql.NullString{String: "", Valid: false} - - res1Exp, err1Exp := nullString1.Value() - res1, err1 := option1.Value() - - res2Exp, err2Exp := nullString2.Value() - res2, err2 := option2.Value() - - is.Equal(res1Exp, res1) - is.Equal(err1Exp, err1) - is.Equal(res2Exp, res2) - is.Equal(err2Exp, err2) -} - -func TestOptionValue(t *testing.T) { - is := assert.New(t) - - option1 := Option[string]{} - option2 := Option[string]{} - - nullString1, _ := sql.NullString{String: "foo", Valid: true}.Value() - nullString2, _ := sql.NullString{String: "", Valid: false}.Value() - - err1 := option1.Scan(nullString1) - err2 := option2.Scan(nullString2) - - is.EqualValues(Some("foo"), option1) - is.Nil(err1) - is.EqualValues(None[string](), option2) - is.Nil(err2) -} diff --git a/result_example_test.go b/result_example_test.go deleted file mode 100644 index 7158301..0000000 --- a/result_example_test.go +++ /dev/null @@ -1,339 +0,0 @@ -package mo - -import ( - "fmt" -) - -var err = fmt.Errorf("error") - -func ExampleOk() { - ok := Ok(42) - result := ok.OrElse(1234) - _err := ok.Error() - - fmt.Println(result, _err) - // Output: 42 -} - -func ExampleErr() { - ko := Err[int](err) - result := ko.OrElse(1234) - _err := ko.Error() - - fmt.Println(result, _err) - // Output: 1234 error -} - -func ExampleTupleToResult() { - randomFunc := func() (int, error) { - return 42, err - } - - value, _err := randomFunc() - - none := TupleToResult(value, _err) - result := none.OrElse(1234) - - fmt.Println(result) - // Output: 1234 -} - -func ExampleTry_ok() { - randomFunc := func() (int, error) { - return 42, nil - } - - result := Try(randomFunc) - value, err := result.Get() - - fmt.Println(value) - fmt.Println(err) - // Output: - // 42 - // -} - -func ExampleTry_err() { - randomFunc := func() (int, error) { - return 42, err - } - - result := Try(randomFunc) - value, err := result.Get() - - fmt.Println(value) - fmt.Println(err) - // Output: - // 0 - // error -} - -func ExampleResult_ok() { - ok := Ok(42) - result := ok.OrElse(1234) - _err := ok.Error() - - fmt.Println(result, _err) - // Output: 42 -} - -func ExampleResult_err() { - ko := Err[int](err) - result := ko.OrElse(1234) - _err := ko.Error() - - fmt.Println(result, _err) - // Output: 1234 error -} - -func ExampleResult_IsOk_ok() { - ok := Ok(42) - result := ok.IsOk() - - fmt.Println(result) - // Output: true -} - -func ExampleResult_IsOk_err() { - ko := Err[int](err) - result := ko.IsOk() - - fmt.Println(result) - // Output: false -} - -func ExampleResult_IsError_ok() { - ok := Ok(42) - result := ok.IsError() - - fmt.Println(result) - // Output: false -} - -func ExampleResult_IsError_err() { - ko := Err[int](err) - result := ko.IsError() - - fmt.Println(result) - // Output: true -} - -func ExampleResult_Error_ok() { - ok := Ok(42) - result := ok.Error() - - fmt.Println(result) - // Output: -} - -func ExampleResult_Error_err() { - ko := Err[int](err) - result := ko.Error() - - fmt.Println(result) - // Output: error -} - -func ExampleResult_Get_ok() { - ok := Ok(42) - result, err := ok.Get() - - fmt.Println(result) - fmt.Println(err) - // Output: - // 42 - // -} - -func ExampleResult_Get_err() { - ko := Err[int](err) - result, err := ko.Get() - - fmt.Println(result) - fmt.Println(err) - // Output: - // 0 - // error -} - -func ExampleResult_MustGet_ok() { - ok := Ok(42) - result := ok.MustGet() - - fmt.Println(result) - // Output: 42 -} - -// func ExampleResult_MustGet_err() { -// ko := Err[int](err) -// result := ko.MustGet() - -// fmt.Println(result) -// // Output: panics -// } - -func ExampleResult_OrElse_ok() { - ok := Ok(42) - result := ok.OrElse(1234) - - fmt.Println(result) - // Output: 42 -} - -func ExampleResult_OrElse_err() { - ko := Err[int](err) - result := ko.OrElse(1234) - - fmt.Println(result) - // Output: 1234 -} - -func ExampleResult_OrEmpty_ok() { - ok := Ok(42) - result := ok.OrEmpty() - - fmt.Println(result) - // Output: 42 -} - -func ExampleResult_OrEmpty_err() { - ko := Err[int](err) - result := ko.OrEmpty() - - fmt.Println(result) - // Output: 0 -} - -func ExampleResult_ToEither_ok() { - ok := Ok(42) - either := ok.ToEither() - - err, isLeft := either.Left() - value, isRight := either.Right() - - fmt.Println(isLeft, isRight) - fmt.Println(err) - fmt.Println(value) - // Output: - // false true - // - // 42 -} - -func ExampleResult_ToEither_err() { - ko := Err[int](err) - either := ko.ToEither() - - err, isLeft := either.Left() - value, isRight := either.Right() - - fmt.Println(isLeft, isRight) - fmt.Println(err) - fmt.Println(value) - // Output: - // true false - // error - // 0 -} - -func ExampleResult_Match_ok() { - ok := Ok(42) - result := ok.Match( - func(i int) (int, error) { - return i * 2, nil - }, - func(err error) (int, error) { - return 21, nil - }, - ) - - fmt.Println(result.IsError(), result.OrEmpty(), result.Error()) - // Output: false 84 -} - -func ExampleResult_Match_err() { - ko := Err[int](err) - result := ko.Match( - func(i int) (int, error) { - return i * 2, nil - }, - func(err error) (int, error) { - return 21, nil - }, - ) - - fmt.Println(result.IsError(), result.OrEmpty(), result.Error()) - // Output: false 21 -} - -func ExampleResult_Map_ok() { - ok := Ok(42) - result := ok.Map( - func(i int) (int, error) { - return i * 2, nil - }, - ) - - fmt.Println(result.IsError(), result.OrEmpty(), result.Error()) - // Output: false 84 -} - -func ExampleResult_Map_err() { - ko := Err[int](err) - result := ko.Map( - func(i int) (int, error) { - return i * 2, nil - }, - ) - - fmt.Println(result.IsError(), result.OrEmpty(), result.Error()) - // Output: true 0 error -} - -func ExampleResult_MapErr_ok() { - ok := Ok(42) - result := ok.MapErr( - func(_err error) (int, error) { - return 1234, nil - }, - ) - - fmt.Println(result.IsError(), result.OrEmpty(), result.Error()) - // Output: false 42 -} - -func ExampleResult_MapErr_err() { - ko := Err[int](err) - result := ko.MapErr( - func(_err error) (int, error) { - return 1234, nil - }, - ) - - fmt.Println(result.IsError(), result.OrEmpty(), result.Error()) - // Output: false 1234 -} - -func ExampleResult_FlatMap_ok() { - ok := Ok(42) - result := ok.FlatMap( - func(i int) Result[int] { - return Ok(1234) - }, - ) - - fmt.Println(result.IsError(), result.OrEmpty(), result.Error()) - // Output: false 1234 -} - -func ExampleResult_FlatMap_err() { - ko := Err[int](err) - result := ko.FlatMap( - func(i int) Result[int] { - return Ok(1234) - }, - ) - - fmt.Println(result.IsError(), result.OrEmpty(), result.Error()) - // Output: true 0 error -} diff --git a/result_test.go b/result_test.go deleted file mode 100644 index a3fed8d..0000000 --- a/result_test.go +++ /dev/null @@ -1,197 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestResultOk(t *testing.T) { - is := assert.New(t) - - is.Equal(Result[int]{value: 42, isErr: false, err: nil}, Ok(42)) -} - -func TestResultErr(t *testing.T) { - is := assert.New(t) - - is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, Err[int](assert.AnError)) -} - -func TestResultTupleToResult(t *testing.T) { - is := assert.New(t) - - is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, TupleToResult(42, assert.AnError)) -} - -func TestResultTry(t *testing.T) { - is := assert.New(t) - - is.Equal(Result[int]{value: 42, isErr: false, err: nil}, Try(func() (int, error) { - return 42, nil - })) - is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, Try(func() (int, error) { - return 42, assert.AnError - })) -} - -func TestResultIsOk(t *testing.T) { - is := assert.New(t) - - is.True(Ok(42).IsOk()) - is.False(Err[int](assert.AnError).IsOk()) -} - -func TestResultIsError(t *testing.T) { - is := assert.New(t) - - is.False(Ok(42).IsError()) - is.True(Err[int](assert.AnError).IsError()) -} - -func TestResultError(t *testing.T) { - is := assert.New(t) - - is.Nil(Ok(42).Error()) - is.NotNil(Err[int](assert.AnError).Error()) - is.Equal(assert.AnError, Err[int](assert.AnError).Error()) -} - -func TestResultGet(t *testing.T) { - is := assert.New(t) - - v1, err1 := Ok(42).Get() - v2, err2 := Err[int](assert.AnError).Get() - - is.Equal(42, v1) - is.Nil(err1) - is.Error(assert.AnError, err1) - - is.Equal(0, v2) - is.NotNil(err2) - is.Error(assert.AnError, err2) -} - -func TestResultMustGet(t *testing.T) { - is := assert.New(t) - - is.NotPanics(func() { - Ok(42).MustGet() - }) - is.Panics(func() { - Err[int](assert.AnError).MustGet() - }) - - is.Equal(42, Ok(42).MustGet()) -} - -func TestResultOrElse(t *testing.T) { - is := assert.New(t) - - is.Equal(42, Ok(42).OrElse(21)) - is.Equal(21, Err[int](assert.AnError).OrElse(21)) -} - -func TestResultOrEmpty(t *testing.T) { - is := assert.New(t) - - is.Equal(42, Ok(42).OrEmpty()) - is.Equal(0, Err[int](assert.AnError).OrEmpty()) -} - -func TestResultToEither(t *testing.T) { - is := assert.New(t) - - right, ok1 := Ok(42).ToEither().Right() - left, ok2 := Err[int](assert.AnError).ToEither().Left() - - is.Equal(42, right) - is.True(ok1) - is.Equal(assert.AnError, left) - is.True(ok2) -} - -func TestResultForEach(t *testing.T) { - is := assert.New(t) - - Err[int](assert.AnError).ForEach(func(i int) { - is.Fail("should not enter here") - }) - - Ok(42).ForEach(func(i int) { - is.Equal(42, i) - }) -} - -func TestResultMatch(t *testing.T) { - is := assert.New(t) - - opt1 := Ok(21).Match( - func(i int) (int, error) { - is.Equal(21, i) - return i * 2, nil - }, - func(err error) (int, error) { - is.Fail("should not enter here") - return 0, err - }, - ) - opt2 := Err[int](assert.AnError).Match( - func(i int) (int, error) { - is.Fail("should not enter here") - return i * 2, nil - }, - func(err error) (int, error) { - is.Equal(assert.AnError, err) - return 0, err - }, - ) - - is.Equal(Result[int]{value: 42, isErr: false, err: nil}, opt1) - is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, opt2) -} - -func TestResultMap(t *testing.T) { - is := assert.New(t) - - opt1 := Ok(21).Map(func(i int) (int, error) { - return i * 2, nil - }) - opt2 := Err[int](assert.AnError).Map(func(i int) (int, error) { - is.Fail("should not be called") - return 42, nil - }) - - is.Equal(Result[int]{value: 42, isErr: false, err: nil}, opt1) - is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, opt2) -} - -func TestResultMapErr(t *testing.T) { - is := assert.New(t) - - opt1 := Ok(21).MapErr(func(err error) (int, error) { - is.Fail("should not be called") - return 42, nil - }) - opt2 := Err[int](assert.AnError).MapErr(func(err error) (int, error) { - return 42, nil - }) - - is.Equal(Result[int]{value: 21, isErr: false, err: nil}, opt1) - is.Equal(Result[int]{value: 42, isErr: false, err: nil}, opt2) -} - -func TestResultFlatMap(t *testing.T) { - is := assert.New(t) - - opt1 := Ok(21).FlatMap(func(i int) Result[int] { - return Ok(42) - }) - opt2 := Err[int](assert.AnError).FlatMap(func(i int) Result[int] { - is.Fail("should not be called") - return Ok(42) - }) - - is.Equal(Result[int]{value: 42, isErr: false, err: nil}, opt1) - is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, opt2) -} diff --git a/task_either_test.go b/task_either_test.go deleted file mode 100644 index b4196b8..0000000 --- a/task_either_test.go +++ /dev/null @@ -1,123 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestTaskEither(t *testing.T) { - is := assert.New(t) - - taskEither := NewTaskEither(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - resolve(42) - }) - }) - - result := taskEither.Run().Result().MustGet() - - is.Equal(42, result) -} - -func TestTaskEitherOrElse(t *testing.T) { - is := assert.New(t) - - taskEither1 := NewTaskEither(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - resolve(42) - }) - }) - taskEither2 := NewTaskEither(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }) - }) - - result1 := taskEither1.OrElse(1234) - result2 := taskEither2.OrElse(1234) - - is.Equal(42, result1) - is.Equal(1234, result2) -} - -func TestTaskEitherMatch(t *testing.T) { - is := assert.New(t) - - taskEither := NewTaskEither(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - resolve(42) - }) - }) - - mapped := taskEither.Match( - func(err error) Either[error, int] { - return Right[error, int](1234) - }, - func(i int) Either[error, int] { - return Right[error, int](i) - }, - ) - - v, ok := mapped.Right() - - is.Equal(42, v) - is.True(ok) -} - -func TestTaskEitherTryCatch(t *testing.T) { - is := assert.New(t) - - taskEither := NewTaskEither(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - resolve(42) - }) - }) - - mapped := taskEither.TryCatch( - func(err error) Either[error, int] { - return Right[error, int](1234) - }, - func(i int) Either[error, int] { - return Right[error, int](i) - }, - ) - - v, ok := mapped.Right() - - is.Equal(42, v) - is.True(ok) -} - -func TestTaskEitherToTask(t *testing.T) { - is := assert.New(t) - - taskEither := NewTaskEither(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }) - }) - - task := taskEither.ToTask(1234) - - result := task.Run().Result().MustGet() - - is.Equal(1234, result) -} - -func TestTaskEitherToEither(t *testing.T) { - is := assert.New(t) - - taskEither := NewTaskEither(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - reject(assert.AnError) - }) - }) - - either := taskEither.ToEither() - err, isError := either.Left() - - is.True(isError) - is.NotNil(err) - is.Equal(assert.AnError, err) -} diff --git a/task_example_test.go b/task_example_test.go deleted file mode 100644 index ef34af5..0000000 --- a/task_example_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package mo - -import ( - "fmt" - "time" -) - -func ExampleTask() { - task := NewTask(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - resolve(time.Now().Year()) - }) - }) - - // returns a future - future := task.Run() - - // a Task never fail - result, _ := future.Collect() - - fmt.Println(result) - // Output: - // 2023 -} diff --git a/task_test.go b/task_test.go deleted file mode 100644 index c9c93e1..0000000 --- a/task_test.go +++ /dev/null @@ -1,100 +0,0 @@ -package mo - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestTask(t *testing.T) { - is := assert.New(t) - - task := NewTask(func() *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - resolve(42) - }) - }) - result := task.Run().Result().MustGet() - - is.Equal(42, result) -} - -func TestTask1(t *testing.T) { - is := assert.New(t) - - task := NewTask1(func(a string) *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - is.Equal("foo", a) - resolve(42) - }) - }) - result := task.Run("foo").Result().MustGet() - - is.Equal(42, result) -} - -func TestTask2(t *testing.T) { - is := assert.New(t) - - task := NewTask2(func(a string, b string) *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - is.Equal("foo", a) - is.Equal("bar", b) - resolve(42) - }) - }) - result := task.Run("foo", "bar").Result().MustGet() - - is.Equal(42, result) -} - -func TestTask3(t *testing.T) { - is := assert.New(t) - - task := NewTask3(func(a string, b string, c string) *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - resolve(42) - }) - }) - result := task.Run("foo", "bar", "hello").Result().MustGet() - - is.Equal(42, result) -} - -func TestTask4(t *testing.T) { - is := assert.New(t) - - task := NewTask4(func(a string, b string, c string, d string) *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - is.Equal("world", d) - resolve(42) - }) - }) - result := task.Run("foo", "bar", "hello", "world").Result().MustGet() - - is.Equal(42, result) -} - -func TestTask5(t *testing.T) { - is := assert.New(t) - - task := NewTask5(func(a string, b string, c string, d string, e bool) *Future[int] { - return NewFuture(func(resolve func(int), reject func(error)) { - is.Equal("foo", a) - is.Equal("bar", b) - is.Equal("hello", c) - is.Equal("world", d) - is.True(e) - resolve(42) - }) - }) - result := task.Run("foo", "bar", "hello", "world", true).Result().MustGet() - - is.Equal(42, result) -}