Skip to content

Commit dddd940

Browse files
committed
test(assertion): refactored tests and improved coverage
Signed-off-by: Frederic BIDON <[email protected]>
1 parent 5007325 commit dddd940

27 files changed

+2691
-2249
lines changed

internal/assertions/boolean_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
package assertions
55

6-
import "testing"
6+
import (
7+
"iter"
8+
"slices"
9+
"testing"
10+
)
711

812
func TestBooleanTrue(t *testing.T) {
913
t.Parallel()
1014

11-
mock := new(testing.T)
15+
mock := new(mockT)
1216

1317
if !True(mock, true) {
1418
t.Error("True should return true")
@@ -22,7 +26,7 @@ func TestBooleanTrue(t *testing.T) {
2226
func TestBooleanFalse(t *testing.T) {
2327
t.Parallel()
2428

25-
mock := new(testing.T)
29+
mock := new(mockT)
2630

2731
if !False(mock, false) {
2832
t.Error("False should return true")
@@ -65,3 +69,34 @@ func TestBooleanTrueTFalseT(t *testing.T) {
6569
}
6670
})
6771
}
72+
73+
func TestBooleanErrorMessages(t *testing.T) {
74+
t.Parallel()
75+
76+
runFailCases(t, booleanFailCases())
77+
}
78+
79+
func booleanFailCases() iter.Seq[failCase] {
80+
return slices.Values([]failCase{
81+
{
82+
name: "True/false-value",
83+
assertion: func(t T) bool { return True(t, false) },
84+
wantError: "Should be true",
85+
},
86+
{
87+
name: "False/true-value",
88+
assertion: func(t T) bool { return False(t, true) },
89+
wantError: "Should be false",
90+
},
91+
{
92+
name: "TrueT/false-value",
93+
assertion: func(t T) bool { return TrueT(t, false) },
94+
wantError: "Should be true",
95+
},
96+
{
97+
name: "FalseT/true-value",
98+
assertion: func(t T) bool { return FalseT(t, true) },
99+
wantError: "Should be false",
100+
},
101+
})
102+
}

internal/assertions/collection_impl_test.go

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,28 @@ func testContainsElement() func(*testing.T) {
2424
list2 := []int{1, 2}
2525
simpleMap := map[any]any{"Foo": "Bar"}
2626

27-
ok, found := containsElement("Hello World", "World")
28-
True(t, ok)
29-
True(t, found)
30-
31-
ok, found = containsElement(list1, "Foo")
32-
True(t, ok)
33-
True(t, found)
34-
35-
ok, found = containsElement(list1, "Bar")
36-
True(t, ok)
37-
True(t, found)
38-
39-
ok, found = containsElement(list2, 1)
40-
True(t, ok)
41-
True(t, found)
42-
43-
ok, found = containsElement(list2, 2)
44-
True(t, ok)
45-
True(t, found)
46-
47-
ok, found = containsElement(list1, "Foo!")
48-
True(t, ok)
49-
False(t, found)
50-
51-
ok, found = containsElement(list2, 3)
52-
True(t, ok)
53-
False(t, found)
54-
55-
ok, found = containsElement(list2, "1")
56-
True(t, ok)
57-
False(t, found)
58-
59-
ok, found = containsElement(simpleMap, "Foo")
60-
True(t, ok)
61-
True(t, found)
62-
63-
ok, found = containsElement(simpleMap, "Bar")
64-
True(t, ok)
65-
False(t, found)
27+
checkContains := func(list any, elem any, expectOK, expectFound bool) {
28+
t.Helper()
29+
ok, found := containsElement(list, elem)
30+
if ok != expectOK {
31+
t.Errorf("containsElement(%v, %v): expected ok=%v, got %v", list, elem, expectOK, ok)
32+
}
33+
if found != expectFound {
34+
t.Errorf("containsElement(%v, %v): expected found=%v, got %v", list, elem, expectFound, found)
35+
}
36+
}
6637

67-
ok, found = containsElement(1433, "1")
68-
False(t, ok)
69-
False(t, found)
38+
checkContains("Hello World", "World", true, true)
39+
checkContains(list1, "Foo", true, true)
40+
checkContains(list1, "Bar", true, true)
41+
checkContains(list2, 1, true, true)
42+
checkContains(list2, 2, true, true)
43+
checkContains(list1, "Foo!", true, false)
44+
checkContains(list2, 3, true, false)
45+
checkContains(list2, "1", true, false)
46+
checkContains(simpleMap, "Foo", true, true)
47+
checkContains(simpleMap, "Bar", true, false)
48+
checkContains(1433, "1", false, false)
7049
}
7150
}
7251

@@ -76,14 +55,22 @@ func testGetLen() func(*testing.T) {
7655

7756
for v := range collectionImplGetLenFalseCases() {
7857
l, ok := getLen(v)
79-
False(t, ok, "Expected getLen fail to get length of %#v", v)
80-
Equal(t, 0, l, "getLen should return 0 for %#v", v)
58+
if ok {
59+
t.Errorf("expected getLen to fail for %#v", v)
60+
}
61+
if l != 0 {
62+
t.Errorf("expected getLen to return 0 for %#v, got %d", v, l)
63+
}
8164
}
8265

8366
for c := range collectionImplGetLenTrueCases() {
8467
l, ok := getLen(c.v)
85-
True(t, ok, "Expected getLen success to get length of %#v", c.v)
86-
Equal(t, c.l, l)
68+
if !ok {
69+
t.Errorf("expected getLen to succeed for %#v", c.v)
70+
}
71+
if c.l != l {
72+
t.Errorf("expected length %d for %#v, got %d", c.l, c.v, l)
73+
}
8774
}
8875
}
8976
}

0 commit comments

Comments
 (0)