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

fix: add more tests

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2024-05-24 23:19:24 +02:00
parent f0f1a48965
commit 598a7b261b
15 changed files with 522 additions and 16 deletions

View File

@@ -24,3 +24,8 @@ import (
func Eq[A any](e EQ.Eq[A]) EQ.Eq[IO[A]] {
return G.Eq[IO[A]](e)
}
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
func FromStrictEquals[A comparable]() EQ.Eq[IO[A]] {
return G.FromStrictEquals[IO[A]]()
}

26
io/functor.go Normal file
View File

@@ -0,0 +1,26 @@
// 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 io
import (
"github.com/IBM/fp-go/internal/functor"
G "github.com/IBM/fp-go/io/generic"
)
// Functor returns the monadic operations for [IO]
func Functor[A, B any]() functor.Functor[A, B, IO[A], IO[B]] {
return G.Functor[A, B, IO[A], IO[B]]()
}

View File

@@ -33,3 +33,8 @@ func Eq[GA ~func() A, A any](e EQ.Eq[A]) EQ.Eq[GA] {
return eq(l, r)()
})
}
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
func FromStrictEquals[GA ~func() A, A comparable]() EQ.Eq[GA] {
return Eq[GA](EQ.FromStrictEquals[A]())
}

31
io/generic/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 generic
import (
"github.com/IBM/fp-go/internal/functor"
)
type ioFunctor[A, B any, GA ~func() A, GB ~func() B] struct{}
func (o *ioFunctor[A, B, GA, GB]) Map(f func(A) B) func(GA) GB {
return Map[GA, GB, A, B](f)
}
// Functor implements the functoric operations for [IO]
func Functor[A, B any, GA ~func() A, GB ~func() B]() functor.Functor[A, B, GA, GB] {
return &ioFunctor[A, B, GA, GB]{}
}

31
io/generic/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 generic
import (
"github.com/IBM/fp-go/internal/pointed"
)
type ioPointed[A any, GA ~func() A] struct{}
func (o *ioPointed[A, GA]) Of(a A) GA {
return Of[GA, A](a)
}
// Pointed implements the pointedic operations for [IO]
func Pointed[A any, GA ~func() A]() pointed.Pointed[A, GA] {
return &ioPointed[A, GA]{}
}

26
io/pointed.go Normal file
View File

@@ -0,0 +1,26 @@
// 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 io
import (
"github.com/IBM/fp-go/internal/pointed"
G "github.com/IBM/fp-go/io/generic"
)
// Pointed returns the monadic operations for [IO]
func Pointed[A any]() pointed.Pointed[A, IO[A]] {
return G.Pointed[A, IO[A]]()
}

View File

@@ -16,8 +16,11 @@
package io
import (
"fmt"
A "github.com/IBM/fp-go/array"
F "github.com/IBM/fp-go/function"
TST "github.com/IBM/fp-go/internal/testing"
"github.com/stretchr/testify/assert"
"testing"
@@ -45,3 +48,18 @@ func TestMapSeq(t *testing.T) {
assert.True(t, res())
}
func TestSequenceArray(t *testing.T) {
s := TST.SequenceArrayTest(
FromStrictEquals[bool](),
Pointed[string](),
Pointed[bool](),
Functor[[]string, bool](),
SequenceArray[string],
)
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("TestSequenceArray %d", i), s(i))
}
}