diff --git a/record/eq.go b/record/eq.go index 38b383f..e9a5e43 100644 --- a/record/eq.go +++ b/record/eq.go @@ -23,3 +23,8 @@ import ( func Eq[K comparable, V any](e E.Eq[V]) E.Eq[map[K]V] { 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]() +} diff --git a/record/eq_test.go b/record/eq_test.go new file mode 100644 index 0000000..3619c9c --- /dev/null +++ b/record/eq_test.go @@ -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)) +} diff --git a/record/generic/eq.go b/record/generic/eq.go index 1e28fae..34e4750 100644 --- a/record/generic/eq.go +++ b/record/generic/eq.go @@ -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) }) } + +// 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]()) +} diff --git a/record/monoid_test.go b/record/monoid_test.go index cf44c68..2f349f9 100644 --- a/record/monoid_test.go +++ b/record/monoid_test.go @@ -54,3 +54,69 @@ func TestUnionMonoid(t *testing.T) { 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)) +} diff --git a/record/record_test.go b/record/record_test.go index 1b37322..0b486ee 100644 --- a/record/record_test.go +++ b/record/record_test.go @@ -176,3 +176,25 @@ func TestFromArrayMap(t *testing.T) { "C": "C", }, 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)) +}