1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: add ioeither

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-07-14 17:30:58 +02:00
parent e350f70659
commit 5020437b6a
80 changed files with 5436 additions and 2110 deletions

61
ioeither/testing/laws.go Normal file
View File

@@ -0,0 +1,61 @@
package testing
import (
"testing"
ET "github.com/ibm/fp-go/either"
EQ "github.com/ibm/fp-go/eq"
L "github.com/ibm/fp-go/internal/monad/testing"
IOE "github.com/ibm/fp-go/ioeither"
)
// AssertLaws asserts the apply monad laws for the `IOEither` monad
func AssertLaws[E, A, B, C any](t *testing.T,
eqe EQ.Eq[E],
eqa EQ.Eq[A],
eqb EQ.Eq[B],
eqc EQ.Eq[C],
ab func(A) B,
bc func(B) C,
) func(a A) bool {
return L.AssertLaws(t,
IOE.Eq(ET.Eq(eqe, eqa)),
IOE.Eq(ET.Eq(eqe, eqb)),
IOE.Eq(ET.Eq(eqe, eqc)),
IOE.Of[E, A],
IOE.Of[E, B],
IOE.Of[E, C],
IOE.Of[E, func(A) A],
IOE.Of[E, func(A) B],
IOE.Of[E, func(B) C],
IOE.Of[E, func(func(A) B) B],
IOE.MonadMap[E, A, A],
IOE.MonadMap[E, A, B],
IOE.MonadMap[E, A, C],
IOE.MonadMap[E, B, C],
IOE.MonadMap[E, func(B) C, func(func(A) B) func(A) C],
IOE.MonadChain[E, A, A],
IOE.MonadChain[E, A, B],
IOE.MonadChain[E, A, C],
IOE.MonadChain[E, B, C],
IOE.MonadAp[A, E, A],
IOE.MonadAp[B, E, A],
IOE.MonadAp[C, E, B],
IOE.MonadAp[C, E, A],
IOE.MonadAp[B, E, func(A) B],
IOE.MonadAp[func(A) C, E, func(A) B],
ab,
bc,
)
}

View File

@@ -0,0 +1,33 @@
package testing
import (
"fmt"
"testing"
EQ "github.com/ibm/fp-go/eq"
"github.com/stretchr/testify/assert"
)
func TestMonadLaws(t *testing.T) {
// some comparison
eqe := EQ.FromStrictEquals[string]()
eqa := EQ.FromStrictEquals[bool]()
eqb := EQ.FromStrictEquals[int]()
eqc := EQ.FromStrictEquals[string]()
ab := func(a bool) int {
if a {
return 1
}
return 0
}
bc := func(b int) string {
return fmt.Sprintf("value %d", b)
}
laws := AssertLaws(t, eqe, eqa, eqb, eqc, ab, bc)
assert.True(t, laws(true))
assert.True(t, laws(false))
}