mirror of
https://github.com/IBM/fp-go.git
synced 2025-11-25 22:21:49 +02:00
* fix: initial checkin of v2 Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: slowly migrate IO Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: migrate MonadTraverseArray and TraverseArray Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: migrate traversal Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: complete migration of IO Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: migrate ioeither Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: refactorY Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: next step in migration Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: adjust IO generation code Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: get rid of more IO methods Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: get rid of more IO * fix: convert iooption Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: convert reader Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: convert a bit of reader Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: new build script Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: cleanup Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: reformat Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: simplify Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: some cleanup Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: adjust Pair to Haskell semantic Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: documentation and testcases Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: some performance optimizations Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: remove coverage Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> * fix: better doc Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com> --------- Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
165 lines
3.9 KiB
Go
165 lines
3.9 KiB
Go
// Copyright (c) 2023 - 2025 IBM Corp.
|
|
// All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package readerioeither
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
E "github.com/IBM/fp-go/v2/either"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestEitherize0(t *testing.T) {
|
|
f := func(ctx context.Context) (int, error) {
|
|
return ctx.Value("key").(int), nil
|
|
}
|
|
|
|
result := Eitherize0(f)()
|
|
ctx := context.WithValue(context.Background(), "key", 42)
|
|
assert.Equal(t, E.Right[error](42), result(ctx)())
|
|
}
|
|
|
|
func TestEitherize1(t *testing.T) {
|
|
f := func(ctx context.Context, x int) (int, error) {
|
|
return x * 2, nil
|
|
}
|
|
|
|
result := Eitherize1(f)(5)
|
|
ctx := context.Background()
|
|
assert.Equal(t, E.Right[error](10), result(ctx)())
|
|
}
|
|
|
|
func TestEitherize2(t *testing.T) {
|
|
f := func(ctx context.Context, x, y int) (int, error) {
|
|
return x + y, nil
|
|
}
|
|
|
|
result := Eitherize2(f)(5, 3)
|
|
ctx := context.Background()
|
|
assert.Equal(t, E.Right[error](8), result(ctx)())
|
|
}
|
|
|
|
func TestEitherize3(t *testing.T) {
|
|
f := func(ctx context.Context, x, y, z int) (int, error) {
|
|
return x + y + z, nil
|
|
}
|
|
|
|
result := Eitherize3(f)(5, 3, 2)
|
|
ctx := context.Background()
|
|
assert.Equal(t, E.Right[error](10), result(ctx)())
|
|
}
|
|
|
|
func TestUneitherize0(t *testing.T) {
|
|
f := func() ReaderIOEither[context.Context, error, int] {
|
|
return Of[context.Context, error](42)
|
|
}
|
|
|
|
result := Uneitherize0(f)
|
|
ctx := context.Background()
|
|
val, err := result(ctx)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 42, val)
|
|
}
|
|
|
|
func TestUneitherize1(t *testing.T) {
|
|
f := func(x int) ReaderIOEither[context.Context, error, int] {
|
|
return Of[context.Context, error](x * 2)
|
|
}
|
|
|
|
result := Uneitherize1(f)
|
|
ctx := context.Background()
|
|
val, err := result(ctx, 5)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 10, val)
|
|
}
|
|
|
|
func TestUneitherize2(t *testing.T) {
|
|
f := func(x, y int) ReaderIOEither[context.Context, error, int] {
|
|
return Of[context.Context, error](x + y)
|
|
}
|
|
|
|
result := Uneitherize2(f)
|
|
ctx := context.Background()
|
|
val, err := result(ctx, 5, 3)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 8, val)
|
|
}
|
|
|
|
func TestFrom0(t *testing.T) {
|
|
f := func(ctx context.Context) func() (int, error) {
|
|
return func() (int, error) {
|
|
return 42, nil
|
|
}
|
|
}
|
|
|
|
result := From0(f)()
|
|
ctx := context.Background()
|
|
assert.Equal(t, E.Right[error](42), result(ctx)())
|
|
}
|
|
|
|
func TestFrom1(t *testing.T) {
|
|
f := func(ctx context.Context, x int) func() (int, error) {
|
|
return func() (int, error) {
|
|
return x * 2, nil
|
|
}
|
|
}
|
|
|
|
result := From1(f)(5)
|
|
ctx := context.Background()
|
|
assert.Equal(t, E.Right[error](10), result(ctx)())
|
|
}
|
|
|
|
func TestFrom2(t *testing.T) {
|
|
f := func(ctx context.Context, x, y int) func() (int, error) {
|
|
return func() (int, error) {
|
|
return x + y, nil
|
|
}
|
|
}
|
|
|
|
result := From2(f)(5, 3)
|
|
ctx := context.Background()
|
|
assert.Equal(t, E.Right[error](8), result(ctx)())
|
|
}
|
|
|
|
func TestEitherizeWithError(t *testing.T) {
|
|
f := func(ctx context.Context, x int) (int, error) {
|
|
if x < 0 {
|
|
return 0, errors.New("negative value")
|
|
}
|
|
return x * 2, nil
|
|
}
|
|
|
|
result := Eitherize1(f)(-5)
|
|
ctx := context.Background()
|
|
assert.True(t, E.IsLeft(result(ctx)()))
|
|
}
|
|
|
|
func TestUneitherizeWithError(t *testing.T) {
|
|
f := func(x int) ReaderIOEither[context.Context, error, int] {
|
|
if x < 0 {
|
|
return Left[context.Context, int](errors.New("negative value"))
|
|
}
|
|
return Of[context.Context, error](x * 2)
|
|
}
|
|
|
|
result := Uneitherize1(f)
|
|
ctx := context.Background()
|
|
_, err := result(ctx, -5)
|
|
assert.Error(t, err)
|
|
}
|