Skip to content

Commit

Permalink
fix: some more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed May 27, 2024
1 parent fdff4e4 commit 3b3b80a
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
5 changes: 5 additions & 0 deletions record/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()
}
48 changes: 48 additions & 0 deletions record/eq_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
5 changes: 5 additions & 0 deletions record/generic/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]())
}
66 changes: 66 additions & 0 deletions record/monoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
22 changes: 22 additions & 0 deletions record/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 3b3b80a

Please sign in to comment.