fn: add operations to Result API

This commit is contained in:
Keagan McClelland
2024-08-13 16:36:07 -07:00
parent 5faad77f33
commit 8971c4c3ae
2 changed files with 63 additions and 4 deletions

View File

@@ -2,7 +2,9 @@ package fn
import (
"errors"
"fmt"
"testing"
"testing/quick"
"github.com/stretchr/testify/require"
)
@@ -17,3 +19,29 @@ func TestOkToSome(t *testing.T) {
t, Err[uint8](errors.New("err")).OkToSome(), None[uint8](),
)
}
func TestMapOk(t *testing.T) {
inc := func(i int) int {
return i + 1
}
f := func(i int) bool {
ok := Ok(i)
return MapOk(inc)(ok) == Ok(inc(i))
}
require.NoError(t, quick.Check(f, nil))
}
func TestFlattenResult(t *testing.T) {
f := func(i int) bool {
e := fmt.Errorf("error")
x := FlattenResult(Ok(Ok(i))) == Ok(i)
y := FlattenResult(Ok(Err[int](e))) == Err[int](e)
z := FlattenResult(Err[Result[int]](e)) == Err[int](e)
return x && y && z
}
require.NoError(t, quick.Check(f, nil))
}