2024-01-31 21:34:46 +01:00
|
|
|
// Copyright (c) 2023 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 writer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
A "github.com/IBM/fp-go/array"
|
2024-02-01 22:11:29 +01:00
|
|
|
EQ "github.com/IBM/fp-go/eq"
|
2024-01-31 21:34:46 +01:00
|
|
|
F "github.com/IBM/fp-go/function"
|
|
|
|
"github.com/IBM/fp-go/internal/utils"
|
|
|
|
M "github.com/IBM/fp-go/monoid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
monoid = A.Monoid[string]()
|
|
|
|
sg = M.ToSemigroup(monoid)
|
2024-02-01 22:11:29 +01:00
|
|
|
|
|
|
|
eq = Eq(A.Eq[string](EQ.FromStrictEquals[string]()), EQ.FromStrictEquals[string]())
|
2024-01-31 21:34:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func getLastName(s utils.Initial) Writer[[]string, string] {
|
2024-02-13 10:44:57 +01:00
|
|
|
return Of[string](monoid, "Doe")
|
2024-01-31 21:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getGivenName(s utils.WithLastName) Writer[[]string, string] {
|
2024-02-13 10:44:57 +01:00
|
|
|
return Of[string](monoid, "John")
|
2024-01-31 21:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBind(t *testing.T) {
|
|
|
|
|
|
|
|
res := F.Pipe3(
|
2024-02-13 10:44:57 +01:00
|
|
|
Do[utils.Initial](monoid, utils.Empty),
|
|
|
|
Bind(sg, utils.SetLastName, getLastName),
|
|
|
|
Bind(sg, utils.SetGivenName, getGivenName),
|
2024-01-31 21:34:46 +01:00
|
|
|
Map[[]string](utils.GetFullName),
|
|
|
|
)
|
|
|
|
|
2024-02-13 10:44:57 +01:00
|
|
|
assert.True(t, eq.Equals(res, Of[string](monoid, "John Doe")))
|
2024-01-31 21:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestApS(t *testing.T) {
|
|
|
|
|
|
|
|
res := F.Pipe3(
|
2024-02-13 10:44:57 +01:00
|
|
|
Do[utils.Initial](monoid, utils.Empty),
|
|
|
|
ApS(sg, utils.SetLastName, Of[string](monoid, "Doe")),
|
|
|
|
ApS(sg, utils.SetGivenName, Of[string](monoid, "John")),
|
2024-01-31 21:34:46 +01:00
|
|
|
Map[[]string](utils.GetFullName),
|
|
|
|
)
|
|
|
|
|
2024-02-13 10:44:57 +01:00
|
|
|
assert.True(t, eq.Equals(res, Of[string](monoid, "John Doe")))
|
2024-01-31 21:34:46 +01:00
|
|
|
}
|