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:
@@ -16,25 +16,13 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
F "github.com/IBM/fp-go/function"
|
||||
TST "github.com/IBM/fp-go/internal/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSequenceArray(t *testing.T) {
|
||||
|
||||
one := Of(1)
|
||||
two := Of(2)
|
||||
|
||||
res := F.Pipe1(
|
||||
[]Option[int]{one, two},
|
||||
SequenceArray[int],
|
||||
)
|
||||
|
||||
assert.Equal(t, res, Of([]int{1, 2}))
|
||||
}
|
||||
|
||||
func TestCompactArray(t *testing.T) {
|
||||
ar := []Option[string]{
|
||||
Of("ok"),
|
||||
@@ -45,3 +33,18 @@ func TestCompactArray(t *testing.T) {
|
||||
res := CompactArray(ar)
|
||||
assert.Equal(t, 2, len(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))
|
||||
}
|
||||
}
|
||||
|
31
option/functor.go
Normal file
31
option/functor.go
Normal 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 option
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/functor"
|
||||
)
|
||||
|
||||
type optionFunctor[A, B any] struct{}
|
||||
|
||||
func (o *optionFunctor[A, B]) Map(f func(A) B) func(Option[A]) Option[B] {
|
||||
return Map[A, B](f)
|
||||
}
|
||||
|
||||
// Functor implements the functoric operations for [Option]
|
||||
func Functor[A, B any]() functor.Functor[A, B, Option[A], Option[B]] {
|
||||
return &optionFunctor[A, B]{}
|
||||
}
|
31
option/pointed.go
Normal file
31
option/pointed.go
Normal 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 option
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/internal/pointed"
|
||||
)
|
||||
|
||||
type optionPointed[A any] struct{}
|
||||
|
||||
func (o *optionPointed[A]) Of(a A) Option[A] {
|
||||
return Of[A](a)
|
||||
}
|
||||
|
||||
// Pointed implements the Pointed operations for [Option]
|
||||
func Pointed[A any]() pointed.Pointed[A, Option[A]] {
|
||||
return &optionPointed[A]{}
|
||||
}
|
Reference in New Issue
Block a user