mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: some more tests
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
@@ -23,3 +23,8 @@ import (
|
|||||||
func Eq[K comparable, V any](e E.Eq[V]) E.Eq[map[K]V] {
|
func Eq[K comparable, V any](e E.Eq[V]) E.Eq[map[K]V] {
|
||||||
return G.Eq[map[K]V, K, V](e)
|
return G.Eq[map[K]V, K, V](e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
|
||||||
|
func FromStrictEquals[K, V comparable]() E.Eq[map[K]V] {
|
||||||
|
return G.FromStrictEquals[map[K]V]()
|
||||||
|
}
|
||||||
|
48
record/eq_test.go
Normal file
48
record/eq_test.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
// 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 record
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFromStrictEquals(t *testing.T) {
|
||||||
|
m1 := map[string]string{
|
||||||
|
"a": "A",
|
||||||
|
"b": "B",
|
||||||
|
}
|
||||||
|
m2 := map[string]string{
|
||||||
|
"a": "A",
|
||||||
|
"b": "C",
|
||||||
|
}
|
||||||
|
m3 := map[string]string{
|
||||||
|
"a": "A",
|
||||||
|
"b": "B",
|
||||||
|
}
|
||||||
|
m4 := map[string]string{
|
||||||
|
"a": "A",
|
||||||
|
"b": "B",
|
||||||
|
"c": "C",
|
||||||
|
}
|
||||||
|
|
||||||
|
e := FromStrictEquals[string, string]()
|
||||||
|
assert.True(t, e.Equals(m1, m1))
|
||||||
|
assert.True(t, e.Equals(m1, m3))
|
||||||
|
assert.False(t, e.Equals(m1, m2))
|
||||||
|
assert.False(t, e.Equals(m1, m4))
|
||||||
|
}
|
@@ -37,3 +37,8 @@ func Eq[M ~map[K]V, K comparable, V any](e E.Eq[V]) E.Eq[M] {
|
|||||||
return equals(left, right, eq)
|
return equals(left, right, eq)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
|
||||||
|
func FromStrictEquals[M ~map[K]V, K, V comparable]() E.Eq[M] {
|
||||||
|
return Eq[M](E.FromStrictEquals[V]())
|
||||||
|
}
|
||||||
|
@@ -54,3 +54,69 @@ func TestUnionMonoid(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, res, m.Concat(x, y))
|
assert.Equal(t, res, m.Concat(x, y))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnionFirstMonoid(t *testing.T) {
|
||||||
|
m := UnionFirstMonoid[string, string]()
|
||||||
|
|
||||||
|
e := Empty[string, string]()
|
||||||
|
|
||||||
|
x := map[string]string{
|
||||||
|
"a": "a1",
|
||||||
|
"b": "b1",
|
||||||
|
"c": "c1",
|
||||||
|
}
|
||||||
|
|
||||||
|
y := map[string]string{
|
||||||
|
"b": "b2",
|
||||||
|
"c": "c2",
|
||||||
|
"d": "d2",
|
||||||
|
}
|
||||||
|
|
||||||
|
res := map[string]string{
|
||||||
|
"a": "a1",
|
||||||
|
"b": "b1",
|
||||||
|
"c": "c1",
|
||||||
|
"d": "d2",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, x, m.Concat(x, m.Empty()))
|
||||||
|
assert.Equal(t, x, m.Concat(m.Empty(), x))
|
||||||
|
|
||||||
|
assert.Equal(t, x, m.Concat(x, e))
|
||||||
|
assert.Equal(t, x, m.Concat(e, x))
|
||||||
|
|
||||||
|
assert.Equal(t, res, m.Concat(x, y))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnionLastMonoid(t *testing.T) {
|
||||||
|
m := UnionLastMonoid[string, string]()
|
||||||
|
|
||||||
|
e := Empty[string, string]()
|
||||||
|
|
||||||
|
x := map[string]string{
|
||||||
|
"a": "a1",
|
||||||
|
"b": "b1",
|
||||||
|
"c": "c1",
|
||||||
|
}
|
||||||
|
|
||||||
|
y := map[string]string{
|
||||||
|
"b": "b2",
|
||||||
|
"c": "c2",
|
||||||
|
"d": "d2",
|
||||||
|
}
|
||||||
|
|
||||||
|
res := map[string]string{
|
||||||
|
"a": "a1",
|
||||||
|
"b": "b2",
|
||||||
|
"c": "c2",
|
||||||
|
"d": "d2",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, x, m.Concat(x, m.Empty()))
|
||||||
|
assert.Equal(t, x, m.Concat(m.Empty(), x))
|
||||||
|
|
||||||
|
assert.Equal(t, x, m.Concat(x, e))
|
||||||
|
assert.Equal(t, x, m.Concat(e, x))
|
||||||
|
|
||||||
|
assert.Equal(t, res, m.Concat(x, y))
|
||||||
|
}
|
||||||
|
@@ -176,3 +176,25 @@ func TestFromArrayMap(t *testing.T) {
|
|||||||
"C": "C",
|
"C": "C",
|
||||||
}, res2)
|
}, res2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEmpty(t *testing.T) {
|
||||||
|
nonEmpty := map[string]string{
|
||||||
|
"a": "A",
|
||||||
|
"b": "B",
|
||||||
|
}
|
||||||
|
empty := Empty[string, string]()
|
||||||
|
|
||||||
|
assert.True(t, IsEmpty(empty))
|
||||||
|
assert.False(t, IsEmpty(nonEmpty))
|
||||||
|
assert.False(t, IsNonEmpty(empty))
|
||||||
|
assert.True(t, IsNonEmpty(nonEmpty))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHas(t *testing.T) {
|
||||||
|
nonEmpty := map[string]string{
|
||||||
|
"a": "A",
|
||||||
|
"b": "B",
|
||||||
|
}
|
||||||
|
assert.True(t, Has("a", nonEmpty))
|
||||||
|
assert.False(t, Has("c", nonEmpty))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user