Skip to content

Commit 2e0f16f

Browse files
committed
use gotest tool to validate tests assertions
Signed-off-by: Guillaume Lours <[email protected]>
1 parent d7e8ab7 commit 2e0f16f

File tree

7 files changed

+92
-220
lines changed

7 files changed

+92
-220
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ var mapRight Either[error, string] = MapEither(right, mapper)
103103
fmt.Println(mapRight.GetOrElse("good")) // Print "10"
104104
```
105105

106-
For more usage details check [tests](./api/control/either_test.go2).
106+
For more usage details check [tests](./api/control/either_test.go).

api/collection/list_test.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,12 @@ func TestIsEmpty(t *testing.T) {
105105
}
106106
for _, testCase := range testCases {
107107
t.Run(testCase.name, func(t *testing.T) {
108-
if testCase.value.IsEmpty() != testCase.isEmpty {
109-
t.Errorf("expected %t but value is %t", testCase.isEmpty, testCase.value.IsEmpty())
110-
}
108+
assert.Equal(t, testCase.value.IsEmpty(), testCase.isEmpty, fmt.Sprintf("expected %t but value is %t", testCase.isEmpty, testCase.value.IsEmpty()))
111109
})
112110
}
113111
}
114112

115-
func TestLenght(t *testing.T) {
113+
func TestLength(t *testing.T) {
116114
testCases := []struct {
117115
name string
118116
value List[int]
@@ -136,9 +134,7 @@ func TestLenght(t *testing.T) {
136134
}
137135
for _, testCase := range testCases {
138136
t.Run(testCase.name, func(t *testing.T) {
139-
if testCase.value.Length() != testCase.length {
140-
t.Errorf("expected %d but value is %d", testCase.length, testCase.value.Length())
141-
}
137+
assert.Equal(t, testCase.value.Length(), testCase.length, fmt.Sprintf("expected %d but value is %d", testCase.length, testCase.value.Length()))
142138
})
143139
}
144140
}
@@ -168,9 +164,7 @@ func TestAppend(t *testing.T) {
168164
for _, testCase := range testCases {
169165
t.Run(testCase.name, func(t *testing.T) {
170166
result := testCase.value.Append(10)
171-
if result.Length() != testCase.length {
172-
t.Errorf("expected %d but value is %d", testCase.length, result.Length())
173-
}
167+
assert.Equal(t, result.Length(), testCase.length, fmt.Sprintf("expected %d but value is %d", testCase.length, result.Length()))
174168
})
175169
}
176170
}
@@ -200,9 +194,7 @@ func TestAppendAll(t *testing.T) {
200194
for _, testCase := range testCases {
201195
t.Run(testCase.name, func(t *testing.T) {
202196
result := testCase.value.AppendAll([]int{10, 20, 30})
203-
if result.Length() != testCase.length {
204-
t.Errorf("expected %d but value is %d", testCase.length, result.Length())
205-
}
197+
assert.Equal(t, result.Length(), testCase.length, fmt.Sprintf("expected %d but value is %d", testCase.length, result.Length()))
206198
})
207199
}
208200
}
@@ -235,9 +227,7 @@ func TestMapList(t *testing.T) {
235227
for _, testCase := range testCases {
236228
t.Run(testCase.name, func(t *testing.T) {
237229
result := MapList[int, string](testCase.value, mapper)
238-
if result != testCase.expected {
239-
t.Errorf("expected %+v but value is %+v", testCase.expected, result)
240-
}
230+
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
241231
})
242232
}
243233
}
@@ -267,9 +257,7 @@ func TestFilterList(t *testing.T) {
267257
for _, testCase := range testCases {
268258
t.Run(testCase.name, func(t *testing.T) {
269259
result := testCase.value.Filter(evenPredicate)
270-
if result != testCase.expected {
271-
t.Errorf("expected %+v but value is %+v", testCase.expected, result)
272-
}
260+
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
273261
})
274262
}
275263
}
@@ -309,9 +297,7 @@ func TestRemoveFromList(t *testing.T) {
309297
for _, testCase := range testCases {
310298
t.Run(testCase.name, func(t *testing.T) {
311299
result := testCase.original.Remove(testCase.valueToRemove)
312-
if result != testCase.expected {
313-
t.Errorf("expected %+v but value is %+v", testCase.expected, result)
314-
}
300+
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
315301
})
316302
}
317303
}
@@ -351,9 +337,7 @@ func TestRemoveFromListWithPredicate(t *testing.T) {
351337
for _, testCase := range testCases {
352338
t.Run(testCase.name, func(t *testing.T) {
353339
result := testCase.original.RemovePredicate(testCase.predicate)
354-
if result != testCase.expected {
355-
t.Errorf("expected %+v but value is %+v", testCase.expected, result)
356-
}
340+
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
357341
})
358342
}
359343
}
@@ -365,6 +349,7 @@ func TestInsertInList(t *testing.T) {
365349
index int
366350
expected List[int]
367351
checkError bool
352+
errorMessage string
368353
}{
369354
{
370355
name: "Empty List index 0",
@@ -379,13 +364,15 @@ func TestInsertInList(t *testing.T) {
379364
index: -1,
380365
expected: Empty[int](),
381366
checkError: true,
367+
errorMessage: "index out of range -1 on empty List",
382368
},
383369
{
384370
name: "Empty List index > 0",
385371
original: emptyList,
386372
index: 1,
387373
expected: Empty[int](),
388374
checkError: true,
375+
errorMessage: "index out of range 1 on empty List",
389376
},
390377
{
391378
name: "Single Element List index 0",
@@ -407,13 +394,15 @@ func TestInsertInList(t *testing.T) {
407394
index: 2,
408395
expected: Empty[int](),
409396
checkError: true,
397+
errorMessage: "index out of range 1 on empty List",
410398
},
411399
{
412400
name: "Single Element List index < 0",
413401
original: singleElementList,
414402
index: -1,
415403
expected: Empty[int](),
416404
checkError: true,
405+
errorMessage: "index out of range -1 on List",
417406
},
418407
{
419408
name: "Multiple Elements List index 0",
@@ -435,19 +424,22 @@ func TestInsertInList(t *testing.T) {
435424
index: 7,
436425
expected: Empty[int](),
437426
checkError: true,
427+
errorMessage: "index out of range 2 on empty List",
438428
},
439429
{
440430
name: "Multiple Elements List negative index",
441431
original: multipleElementsList,
442432
index: -1,
443433
expected: Empty[int](),
444434
checkError: true,
435+
errorMessage: "index out of range -1 on List",
445436
},
446437
}
447438
for _, testCase := range testCases {
448439
t.Run(testCase.name, func(t *testing.T) {
449440
result, err := testCase.original.Insert(testCase.index, 7)
450441
if testCase.checkError {
442+
assert.Error(t, err, testCase.errorMessage , "index of range error was expected")
451443
if err == nil {
452444
t.Errorf("index of range error was expected")
453445
}
@@ -485,9 +477,7 @@ func TestReverseList(t *testing.T) {
485477
for _, testCase := range testCases {
486478
t.Run(testCase.name, func(t *testing.T) {
487479
result := testCase.original.Reverse()
488-
if result != testCase.expected {
489-
t.Errorf("expected %+v but value is %+v", testCase.expected, result)
490-
}
480+
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
491481
})
492482
}
493483
}

api/control/either_test.go

Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package control
33
import (
44
"errors"
55
"fmt"
6+
"gotest.tools/v3/assert"
67
"strconv"
78
"testing"
89
)
@@ -16,116 +17,67 @@ var (
1617
)
1718

1819
func TestIsRight(t *testing.T) {
19-
if !right.IsRight() {
20-
t.Errorf("should be a Right not Left")
21-
}
22-
23-
if left.IsRight() {
24-
t.Errorf("should be a Left not Right")
25-
}
20+
assert.Assert(t, right.IsRight(), "should be a Right not Left")
21+
assert.Assert(t, !left.IsRight(), "should be a Left not Right")
2622
}
2723

2824
func TestIsLeft(t *testing.T) {
29-
if right.IsLeft() {
30-
t.Errorf("should be a Right not Left")
31-
}
32-
33-
if !left.IsLeft() {
34-
t.Errorf("should be a Left not Right")
35-
}
25+
assert.Assert(t, !right.IsLeft(), "should be a Right not Left")
26+
assert.Assert(t, left.IsLeft(), "should be a Left not Right")
3627
}
3728

3829
func TestSwap(t *testing.T) {
39-
if right.Swap().IsRight() {
40-
t.Errorf("should be a Left not Right")
41-
}
42-
43-
if left.Swap().IsLeft() {
44-
t.Errorf("should be a Right not Left")
45-
}
30+
assert.Assert(t, right.Swap().IsLeft(), "should be a Left not Right")
31+
assert.Assert(t, left.Swap().IsRight(), "should be a Right not Left")
4632
}
4733

4834
func TestEitherOrElse(t *testing.T) {
49-
if right.OrElse(left).IsLeft() {
50-
t.Errorf("should be a Right not Left")
51-
}
52-
53-
if left.OrElse(right).IsLeft() {
54-
t.Errorf("should be a Right not Left")
55-
}
35+
assert.Assert(t, right.OrElse(left).IsRight(), "should be a Right not Left")
36+
assert.Assert(t, left.OrElse(right).IsRight(), "should be a Right not Left")
5637
}
5738

5839
func TestEitherGetOrElse(t *testing.T) {
59-
if right.GetOrElse(20) != 10 {
60-
t.Errorf("value should be 10")
61-
}
62-
63-
if left.GetOrElse(20) != 20 {
64-
t.Errorf("value should be 20")
65-
}
40+
assert.Equal(t, right.GetOrElse(20), 10, "value should be 10")
41+
assert.Equal(t, left.GetOrElse(20), 20, "value should be 20")
6642
}
6743

6844
func TestEitherFilterOrElse(t *testing.T) {
6945
transform := func(value int) error {
7046
return fmt.Errorf("doesn't pass the EvenPredicate")
7147
}
72-
73-
if right.FilterOrElse(EvenPredicate, transform).IsLeft() {
74-
t.Error("should not be a Left")
75-
}
76-
77-
if left.FilterOrElse(EvenPredicate, transform).IsRight() {
78-
t.Error("should not be a Right")
79-
}
48+
assert.Assert(t, right.FilterOrElse(EvenPredicate, transform).IsRight(), "should not be a Left")
49+
assert.Assert(t, left.FilterOrElse(EvenPredicate, transform).IsLeft(), "should not be a Right")
8050

8151
odd := RightOf[error, int](11)
82-
if odd.FilterOrElse(EvenPredicate, transform).IsRight() {
83-
t.Error("should not be a Right")
84-
}
52+
assert.Assert(t, odd.FilterOrElse(EvenPredicate, transform).IsLeft(), "should not be a Right")
8553
}
8654

8755
func TestEitherFilter(t *testing.T) {
88-
if right.Filter(EvenPredicate).IsEmpty() {
89-
t.Error("should be a Some of Either")
90-
}
91-
92-
if !left.Filter(EvenPredicate).IsEmpty() {
93-
t.Error("should be a Empty of Either")
94-
}
56+
assert.Assert(t, !right.Filter(EvenPredicate).IsEmpty(), "should be a Some of Either")
57+
assert.Assert(t, left.Filter(EvenPredicate).IsEmpty(), "should be a Empty of Either")
9558

9659
odd := RightOf[error, int](11)
97-
if !odd.Filter(EvenPredicate).IsEmpty() {
98-
t.Error("should be a Empty of Either")
99-
}
60+
assert.Assert(t, odd.Filter(EvenPredicate).IsEmpty(), "should be a Empty of Either")
10061
}
10162

10263
func TestMapEither(t *testing.T) {
10364
var mapper = func(value int) string {
10465
return strconv.Itoa(value)
10566
}
10667
var mapRight Either[error, string] = MapEither(right, mapper)
107-
if mapRight.GetOrElse("good") != "10" {
108-
t.Errorf("value should be 10")
109-
}
68+
assert.Equal(t, mapRight.GetOrElse("good"), "10", "value should be 10")
11069

11170
var mapLeft Either[error, string] = MapEither[error, int, string](left, mapper)
112-
if mapLeft.IsRight() {
113-
t.Errorf("should be an Left Either")
114-
}
71+
assert.Assert(t, mapLeft.IsLeft(), "should be an Left Either")
11572
}
11673

11774
func TestFlatMapEither(t *testing.T) {
11875
var mapper = func(value int) Either[error, string] {
11976
return RightOf[error, string](strconv.Itoa(value))
12077
}
12178
var mapRight Either[error, string] = FlatMapEither(right, mapper)
122-
if mapRight.GetOrElse("good") != "10" {
123-
t.Errorf("value should be 10")
124-
}
79+
assert.Equal(t, mapRight.GetOrElse("good"), "10", "value should be 10")
12580

12681
var mapLeft Either[error, string] = FlatMapEither[error, int, string](left, mapper)
127-
if mapLeft.IsRight() {
128-
t.Errorf("should be an Left Either")
129-
}
130-
82+
assert.Assert(t, mapLeft.IsLeft(), "should be an Left Either")
13183
}

0 commit comments

Comments
 (0)