Skip to content

Commit 3b3b80a

Browse files
committed
fix: some more tests
Signed-off-by: Dr. Carsten Leue <[email protected]>
1 parent fdff4e4 commit 3b3b80a

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

record/eq.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ import (
2323
func Eq[K comparable, V any](e E.Eq[V]) E.Eq[map[K]V] {
2424
return G.Eq[map[K]V, K, V](e)
2525
}
26+
27+
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
28+
func FromStrictEquals[K, V comparable]() E.Eq[map[K]V] {
29+
return G.FromStrictEquals[map[K]V]()
30+
}

record/eq_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2024 IBM Corp.
2+
// All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package record
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestFromStrictEquals(t *testing.T) {
25+
m1 := map[string]string{
26+
"a": "A",
27+
"b": "B",
28+
}
29+
m2 := map[string]string{
30+
"a": "A",
31+
"b": "C",
32+
}
33+
m3 := map[string]string{
34+
"a": "A",
35+
"b": "B",
36+
}
37+
m4 := map[string]string{
38+
"a": "A",
39+
"b": "B",
40+
"c": "C",
41+
}
42+
43+
e := FromStrictEquals[string, string]()
44+
assert.True(t, e.Equals(m1, m1))
45+
assert.True(t, e.Equals(m1, m3))
46+
assert.False(t, e.Equals(m1, m2))
47+
assert.False(t, e.Equals(m1, m4))
48+
}

record/generic/eq.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ func Eq[M ~map[K]V, K comparable, V any](e E.Eq[V]) E.Eq[M] {
3737
return equals(left, right, eq)
3838
})
3939
}
40+
41+
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
42+
func FromStrictEquals[M ~map[K]V, K, V comparable]() E.Eq[M] {
43+
return Eq[M](E.FromStrictEquals[V]())
44+
}

record/monoid_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,69 @@ func TestUnionMonoid(t *testing.T) {
5454

5555
assert.Equal(t, res, m.Concat(x, y))
5656
}
57+
58+
func TestUnionFirstMonoid(t *testing.T) {
59+
m := UnionFirstMonoid[string, string]()
60+
61+
e := Empty[string, string]()
62+
63+
x := map[string]string{
64+
"a": "a1",
65+
"b": "b1",
66+
"c": "c1",
67+
}
68+
69+
y := map[string]string{
70+
"b": "b2",
71+
"c": "c2",
72+
"d": "d2",
73+
}
74+
75+
res := map[string]string{
76+
"a": "a1",
77+
"b": "b1",
78+
"c": "c1",
79+
"d": "d2",
80+
}
81+
82+
assert.Equal(t, x, m.Concat(x, m.Empty()))
83+
assert.Equal(t, x, m.Concat(m.Empty(), x))
84+
85+
assert.Equal(t, x, m.Concat(x, e))
86+
assert.Equal(t, x, m.Concat(e, x))
87+
88+
assert.Equal(t, res, m.Concat(x, y))
89+
}
90+
91+
func TestUnionLastMonoid(t *testing.T) {
92+
m := UnionLastMonoid[string, string]()
93+
94+
e := Empty[string, string]()
95+
96+
x := map[string]string{
97+
"a": "a1",
98+
"b": "b1",
99+
"c": "c1",
100+
}
101+
102+
y := map[string]string{
103+
"b": "b2",
104+
"c": "c2",
105+
"d": "d2",
106+
}
107+
108+
res := map[string]string{
109+
"a": "a1",
110+
"b": "b2",
111+
"c": "c2",
112+
"d": "d2",
113+
}
114+
115+
assert.Equal(t, x, m.Concat(x, m.Empty()))
116+
assert.Equal(t, x, m.Concat(m.Empty(), x))
117+
118+
assert.Equal(t, x, m.Concat(x, e))
119+
assert.Equal(t, x, m.Concat(e, x))
120+
121+
assert.Equal(t, res, m.Concat(x, y))
122+
}

record/record_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,25 @@ func TestFromArrayMap(t *testing.T) {
176176
"C": "C",
177177
}, res2)
178178
}
179+
180+
func TestEmpty(t *testing.T) {
181+
nonEmpty := map[string]string{
182+
"a": "A",
183+
"b": "B",
184+
}
185+
empty := Empty[string, string]()
186+
187+
assert.True(t, IsEmpty(empty))
188+
assert.False(t, IsEmpty(nonEmpty))
189+
assert.False(t, IsNonEmpty(empty))
190+
assert.True(t, IsNonEmpty(nonEmpty))
191+
}
192+
193+
func TestHas(t *testing.T) {
194+
nonEmpty := map[string]string{
195+
"a": "A",
196+
"b": "B",
197+
}
198+
assert.True(t, Has("a", nonEmpty))
199+
assert.False(t, Has("c", nonEmpty))
200+
}

0 commit comments

Comments
 (0)