1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-07-17 01:32:23 +02:00

fix: more testing

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2024-05-25 15:41:32 +02:00
parent 598a7b261b
commit 391754e5a6
11 changed files with 367 additions and 1 deletions

View File

@ -1,2 +1,3 @@
@echo off
go tool cover -html=build/cover.out -o build/cover.html
cov-report -ex ".*/cli/.*.go|.*/gen.go|.*/binds.go" build\cover.out

View File

@ -1,8 +1,10 @@
package either
import (
"fmt"
"testing"
TST "github.com/IBM/fp-go/internal/testing"
"github.com/stretchr/testify/assert"
)
@ -16,3 +18,33 @@ func TestCompactArray(t *testing.T) {
res := CompactArray(ar)
assert.Equal(t, 2, len(res))
}
func TestSequenceArray(t *testing.T) {
s := TST.SequenceArrayTest(
FromStrictEquals[error, bool](),
Pointed[error, string](),
Pointed[error, bool](),
Functor[error, []string, bool](),
SequenceArray[error, string],
)
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
}
}
func TestSequenceArrayError(t *testing.T) {
s := TST.SequenceArrayErrorTest(
FromStrictEquals[error, bool](),
Left[string, error],
Left[bool, error],
Pointed[error, string](),
Pointed[error, bool](),
Functor[error, []string, bool](),
SequenceArray[error, string],
)
// run across four bits
s(4)(t)
}

31
either/functor.go Normal file
View File

@ -0,0 +1,31 @@
// Copyright (c) 2024 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 either
import (
"github.com/IBM/fp-go/internal/functor"
)
type eitherFunctor[E, A, B any] struct{}
func (o *eitherFunctor[E, A, B]) Map(f func(A) B) func(Either[E, A]) Either[E, B] {
return Map[E, A, B](f)
}
// Functor implements the functoric operations for [Either]
func Functor[E, A, B any]() functor.Functor[A, B, Either[E, A], Either[E, B]] {
return &eitherFunctor[E, A, B]{}
}

31
either/pointed.go Normal file
View File

@ -0,0 +1,31 @@
// Copyright (c) 2024 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 either
import (
"github.com/IBM/fp-go/internal/pointed"
)
type eitherPointed[E, A any] struct{}
func (o *eitherPointed[E, A]) Of(a A) Either[E, A] {
return Of[E, A](a)
}
// Pointed implements the pointedic operations for [Either]
func Pointed[E, A any]() pointed.Pointed[A, Either[E, A]] {
return &eitherPointed[E, A]{}
}

View File

@ -62,3 +62,84 @@ func SequenceArrayTest[
}
}
}
// SequenceArrayErrorTest tests if the sequence operation works in case the operation can error
func SequenceArrayErrorTest[
HKTA,
HKTB,
HKTAA any, // HKT[[]A]
](
eq EQ.Eq[HKTB],
left func(error) HKTA,
leftB func(error) HKTB,
pa pointed.Pointed[string, HKTA],
pb pointed.Pointed[bool, HKTB],
faa functor.Functor[[]string, bool, HKTAA, HKTB],
seq func([]HKTA) HKTAA,
) func(count int) func(t *testing.T) {
return func(count int) func(t *testing.T) {
expGood := make([]string, count)
good := make([]HKTA, count)
expBad := make([]error, count)
bad := make([]HKTA, count)
for i := 0; i < count; i++ {
goodVal := fmt.Sprintf("TestData %d", i)
badVal := fmt.Errorf("ErrorData %d", i)
expGood[i] = goodVal
good[i] = pa.Of(goodVal)
expBad[i] = badVal
bad[i] = left(badVal)
}
total := 1 << count
return func(t *testing.T) {
// test the good case
res := F.Pipe2(
good,
seq,
faa.Map(func(act []string) bool {
return assert.Equal(t, expGood, act)
}),
)
assert.True(t, eq.Equals(res, pb.Of(true)))
// iterate and test the bad cases
for i := 1; i < total; i++ {
// run the test
t.Run(fmt.Sprintf("Bitmask test %d", i), func(t1 *testing.T) {
// the actual
act := make([]HKTA, count)
// the expected error
var exp error
// prepare the values bases on the bit mask
mask := 1
for j := 0; j < count; j++ {
if (i & mask) == 0 {
act[j] = good[j]
} else {
act[j] = bad[j]
if exp == nil {
exp = expBad[j]
}
}
mask <<= 1
}
// test the good case
res := F.Pipe2(
act,
seq,
faa.Map(func(act []string) bool {
return assert.Equal(t, expGood, act)
}),
)
// validate the error
assert.True(t, eq.Equals(res, leftB(exp)))
})
}
}
}
}

View File

@ -17,6 +17,7 @@ package generic
import (
ET "github.com/IBM/fp-go/either"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/monad"
"github.com/IBM/fp-go/internal/pointed"
)
@ -25,6 +26,8 @@ type ioEitherPointed[E, A any, GA ~func() ET.Either[E, A]] struct{}
type ioEitherMonad[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B], GAB ~func() ET.Either[E, func(A) B]] struct{}
type ioEitherFunctor[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B]] struct{}
func (o *ioEitherPointed[E, A, GA]) Of(a A) GA {
return Of[GA, E, A](a)
}
@ -45,11 +48,20 @@ func (o *ioEitherMonad[E, A, B, GA, GB, GAB]) Ap(fa GA) func(GAB) GB {
return Ap[GB, GAB, GA, E, A, B](fa)
}
func (o *ioEitherFunctor[E, A, B, GA, GB]) Map(f func(A) B) func(GA) GB {
return Map[GA, GB, E, A, B](f)
}
// Pointed implements the pointed operations for [IOEither]
func Pointed[E, A any, GA ~func() ET.Either[E, A]]() pointed.Pointed[A, GA] {
return &ioEitherPointed[E, A, GA]{}
}
// Functor implements the monadic operations for [IOEither]
func Functor[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B]]() functor.Functor[A, B, GA, GB] {
return &ioEitherFunctor[E, A, B, GA, GB]{}
}
// Monad implements the monadic operations for [IOEither]
func Monad[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B], GAB ~func() ET.Either[E, func(A) B]]() monad.Monad[A, B, GA, GB, GAB] {
return &ioEitherMonad[E, A, B, GA, GB, GAB]{}

View File

@ -16,6 +16,7 @@
package ioeither
import (
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/monad"
"github.com/IBM/fp-go/internal/pointed"
G "github.com/IBM/fp-go/ioeither/generic"
@ -26,6 +27,11 @@ func Pointed[E, A any]() pointed.Pointed[A, IOEither[E, A]] {
return G.Pointed[E, A, IOEither[E, A]]()
}
// Functor returns the functor operations for [IOEither]
func Functor[E, A, B any]() functor.Functor[A, B, IOEither[E, A], IOEither[E, B]] {
return G.Functor[E, A, B, IOEither[E, A], IOEither[E, B]]()
}
// Monad returns the monadic operations for [IOEither]
func Monad[E, A, B any]() monad.Monad[A, B, IOEither[E, A], IOEither[E, B], IOEither[E, func(A) B]] {
return G.Monad[E, A, B, IOEither[E, A], IOEither[E, B], IOEither[E, func(A) B]]()

View File

@ -16,11 +16,15 @@
package ioeither
import (
"fmt"
A "github.com/IBM/fp-go/array"
E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function"
"github.com/stretchr/testify/assert"
TST "github.com/IBM/fp-go/internal/testing"
"testing"
)
@ -46,3 +50,33 @@ func TestMapSeq(t *testing.T) {
assert.Equal(t, E.Of[error](true), res())
}
func TestSequenceArray(t *testing.T) {
s := TST.SequenceArrayTest(
FromStrictEquals[error, bool](),
Pointed[error, string](),
Pointed[error, bool](),
Functor[error, []string, bool](),
SequenceArray[error, string],
)
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
}
}
func TestSequenceArrayError(t *testing.T) {
s := TST.SequenceArrayErrorTest(
FromStrictEquals[error, bool](),
Left[string, error],
Left[bool, error],
Pointed[error, string](),
Pointed[error, bool](),
Functor[error, []string, bool](),
SequenceArray[error, string],
)
// run across four bits
s(4)(t)
}

View File

@ -17,11 +17,13 @@ package readerioeither
import (
"context"
"fmt"
"testing"
A "github.com/IBM/fp-go/array"
ET "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function"
TST "github.com/IBM/fp-go/internal/testing"
"github.com/stretchr/testify/assert"
)
@ -37,3 +39,33 @@ func TestTraverseArray(t *testing.T) {
assert.Equal(t, ET.Right[string]([]string{"aa", "bb"}), F.Pipe1([]string{"a", "b"}, f)(ctx)())
assert.Equal(t, ET.Left[[]string]("e"), F.Pipe1([]string{"a", ""}, f)(ctx)())
}
func TestSequenceArray(t *testing.T) {
s := TST.SequenceArrayTest(
FromStrictEquals[context.Context, error, bool]()(context.Background()),
Pointed[context.Context, error, string](),
Pointed[context.Context, error, bool](),
Functor[context.Context, error, []string, bool](),
SequenceArray[context.Context, error, string],
)
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
}
}
func TestSequenceArrayError(t *testing.T) {
s := TST.SequenceArrayErrorTest(
FromStrictEquals[context.Context, error, bool]()(context.Background()),
Left[context.Context, string, error],
Left[context.Context, bool, error],
Pointed[context.Context, error, string](),
Pointed[context.Context, error, bool](),
Functor[context.Context, error, []string, bool](),
SequenceArray[context.Context, error, string],
)
// run across four bits
s(4)(t)
}

View File

@ -0,0 +1,68 @@
// Copyright (c) 2024 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 generic
import (
ET "github.com/IBM/fp-go/either"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/monad"
"github.com/IBM/fp-go/internal/pointed"
)
type readerIOEitherPointed[R, E, A any, GRA ~func(R) GIOA, GIOA ~func() ET.Either[E, A]] struct{}
type readerIOEitherMonad[R, E, A, B any, GRA ~func(R) GIOA, GRB ~func(R) GIOB, GRAB ~func(R) GIOAB, GIOA ~func() ET.Either[E, A], GIOB ~func() ET.Either[E, B], GIOAB ~func() ET.Either[E, func(A) B]] struct{}
type readerIOEitherFunctor[R, E, A, B any, GRA ~func(R) GIOA, GRB ~func(R) GIOB, GIOA ~func() ET.Either[E, A], GIOB ~func() ET.Either[E, B]] struct{}
func (o *readerIOEitherPointed[R, E, A, GRA, GIOA]) Of(a A) GRA {
return Of[GRA, GIOA, R, E, A](a)
}
func (o *readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]) Of(a A) GRA {
return Of[GRA, GIOA, R, E, A](a)
}
func (o *readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]) Map(f func(A) B) func(GRA) GRB {
return Map[GRA, GRB, GIOA, GIOB, R, E, A, B](f)
}
func (o *readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]) Chain(f func(A) GRB) func(GRA) GRB {
return Chain[GRA, GRB, GIOA, GIOB, R, E, A, B](f)
}
func (o *readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]) Ap(fa GRA) func(GRAB) GRB {
return Ap[GRA, GRB, GRAB, GIOA, GIOB, GIOAB, R, E, A, B](fa)
}
func (o *readerIOEitherFunctor[R, E, A, B, GRA, GRB, GIOA, GIOB]) Map(f func(A) B) func(GRA) GRB {
return Map[GRA, GRB, GIOA, GIOB, R, E, A, B](f)
}
// Pointed implements the pointed operations for [ReaderIOEither]
func Pointed[R, E, A any, GRA ~func(R) GIOA, GIOA ~func() ET.Either[E, A]]() pointed.Pointed[A, GRA] {
return &readerIOEitherPointed[R, E, A, GRA, GIOA]{}
}
// Functor implements the monadic operations for [ReaderIOEither]
func Functor[R, E, A, B any, GRA ~func(R) GIOA, GRB ~func(R) GIOB, GIOA ~func() ET.Either[E, A], GIOB ~func() ET.Either[E, B]]() functor.Functor[A, B, GRA, GRB] {
return &readerIOEitherFunctor[R, E, A, B, GRA, GRB, GIOA, GIOB]{}
}
// Monad implements the monadic operations for [ReaderIOEither]
func Monad[R, E, A, B any, GRA ~func(R) GIOA, GRB ~func(R) GIOB, GRAB ~func(R) GIOAB, GIOA ~func() ET.Either[E, A], GIOB ~func() ET.Either[E, B], GIOAB ~func() ET.Either[E, func(A) B]]() monad.Monad[A, B, GRA, GRB, GRAB] {
return &readerIOEitherMonad[R, E, A, B, GRA, GRB, GRAB, GIOA, GIOB, GIOAB]{}
}

38
readerioeither/monad.go Normal file
View File

@ -0,0 +1,38 @@
// Copyright (c) 2024 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 (
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/monad"
"github.com/IBM/fp-go/internal/pointed"
G "github.com/IBM/fp-go/readerioeither/generic"
)
// Pointed returns the pointed operations for [ReaderIOEither]
func Pointed[R, E, A any]() pointed.Pointed[A, ReaderIOEither[R, E, A]] {
return G.Pointed[R, E, A, ReaderIOEither[R, E, A]]()
}
// Functor returns the functor operations for [ReaderIOEither]
func Functor[R, E, A, B any]() functor.Functor[A, B, ReaderIOEither[R, E, A], ReaderIOEither[R, E, B]] {
return G.Functor[R, E, A, B, ReaderIOEither[R, E, A], ReaderIOEither[R, E, B]]()
}
// Monad returns the monadic operations for [ReaderIOEither]
func Monad[R, E, A, B any]() monad.Monad[A, B, ReaderIOEither[R, E, A], ReaderIOEither[R, E, B], ReaderIOEither[R, E, func(A) B]] {
return G.Monad[R, E, A, B, ReaderIOEither[R, E, A], ReaderIOEither[R, E, B], ReaderIOEither[R, E, func(A) B]]()
}