Skip to content

Commit 54f7bb1

Browse files
authored
test: improve unit tests assertions (#29)
1 parent 986952c commit 54f7bb1

File tree

7 files changed

+83
-37
lines changed

7 files changed

+83
-37
lines changed

concurrent_map_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestContainsKey(t *testing.T) {
4444
func TestGet(t *testing.T) {
4545
m := prepareConcurrentMap()
4646
assert.Equal(t, m.Get(1), util.Ptr("a"))
47-
assert.Equal(t, m.Get(4), nil)
47+
assert.IsNil(t, m.Get(4))
4848
}
4949

5050
func TestGetOrDefault(t *testing.T) {
@@ -82,7 +82,7 @@ func TestRemove(t *testing.T) {
8282
m := prepareConcurrentMap()
8383
assert.Equal(t, m.Remove(3), util.Ptr("c"))
8484
assert.Equal(t, m.Size(), 2)
85-
assert.Equal(t, m.Remove(5), nil)
85+
assert.IsNil(t, m.Remove(5))
8686
assert.Equal(t, m.Size(), 2)
8787
}
8888

future_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestFuture(t *testing.T) {
2121
res, err := p.Future().Join()
2222

2323
assert.Equal(t, true, *res)
24-
assert.Equal(t, nil, err)
24+
assert.IsNil(t, err)
2525
}
2626

2727
func TestFutureUtils(t *testing.T) {
@@ -57,10 +57,8 @@ func TestFutureFirstCompleted(t *testing.T) {
5757
timeout := FutureTimer[bool](10 * time.Millisecond)
5858
futRes, futErr := FutureFirstCompletedOf(p.Future(), timeout).Join()
5959

60-
assert.Equal(t, nil, futRes)
61-
if futErr == nil {
62-
t.Fatalf("futErr is nil")
63-
}
60+
assert.IsNil(t, futRes)
61+
assert.NotEqual(t, futErr, nil)
6462
}
6563

6664
func TestFutureTransform(t *testing.T) {
@@ -113,7 +111,7 @@ func TestFutureRecover(t *testing.T) {
113111

114112
res, err := future.Join()
115113
assert.Equal(t, 2, *res)
116-
assert.Equal(t, nil, err)
114+
assert.IsNil(t, err)
117115
}
118116

119117
func TestFutureFailure(t *testing.T) {

goroutine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestGoroutineID(t *testing.T) {
1010
gid, err := GoroutineID()
1111

12-
assert.Equal(t, nil, err)
12+
assert.IsNil(t, err)
1313
t.Log(gid)
1414
}
1515

internal/assert/assertions.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package assert
22

33
import (
4+
"fmt"
45
"reflect"
56
"strings"
67
"testing"
@@ -9,12 +10,45 @@ import (
910
// Equal verifies equality of two objects.
1011
func Equal[T any](t *testing.T, a, b T) {
1112
if !reflect.DeepEqual(a, b) {
13+
t.Helper()
1214
t.Fatalf("%v != %v", a, b)
1315
}
1416
}
1517

18+
// NotEqual asserts that the given objects are not equal.
19+
func NotEqual[T any](t *testing.T, a, b T) {
20+
if reflect.DeepEqual(a, b) {
21+
t.Helper()
22+
t.Fatalf("%v == %v", a, b)
23+
}
24+
}
25+
26+
// Same asserts that the given pointers point to the same object.
27+
func Same[T any](t *testing.T, a, b *T) {
28+
if a != b {
29+
t.Helper()
30+
t.Fatalf("%p != %p", a, b)
31+
}
32+
}
33+
34+
// IsNil verifies that the object is nil.
35+
func IsNil(t *testing.T, obj any) {
36+
if obj != nil {
37+
value := reflect.ValueOf(obj)
38+
switch value.Kind() {
39+
case reflect.Ptr, reflect.Map, reflect.Slice,
40+
reflect.Interface, reflect.Func, reflect.Chan:
41+
if value.IsNil() {
42+
return
43+
}
44+
}
45+
t.Helper()
46+
t.Fatalf("%v is not nil", obj)
47+
}
48+
}
49+
1650
// ElementsMatch checks whether the given slices contain the same elements.
17-
func ElementsMatch[T any](t *testing.T, a, b []T) {
51+
func ElementsMatch[S ~[]E, E any](t *testing.T, a, b S) {
1852
if len(a) == len(b) {
1953
count := 0
2054
for _, va := range a {
@@ -29,23 +63,45 @@ func ElementsMatch[T any](t *testing.T, a, b []T) {
2963
return
3064
}
3165
}
66+
t.Helper()
3267
t.Fatalf("Slice elements are not equal: %v != %v", a, b)
3368
}
3469

3570
// ErrorContains checks whether the given error contains the specified string.
3671
func ErrorContains(t *testing.T, err error, str string) {
3772
if err == nil {
73+
t.Helper()
3874
t.Fatalf("Error is nil")
3975
} else if !strings.Contains(err.Error(), str) {
40-
t.Fatalf("Error doen't contain string: %s", str)
76+
t.Helper()
77+
t.Fatalf("Error does not contain string: %s", str)
4178
}
4279
}
4380

44-
// Panic checks whether the given function panics.
45-
func Panic(t *testing.T, f func()) {
81+
// Panics checks whether the given function panics.
82+
func Panics(t *testing.T, f func()) {
4683
defer func() {
4784
if r := recover(); r == nil {
48-
t.Errorf("The function did not panic")
85+
t.Helper()
86+
t.Errorf("Function did not panic")
87+
}
88+
}()
89+
f()
90+
}
91+
92+
// PanicMsgContains checks whether a function panics with a message containing
93+
// the given string.
94+
func PanicMsgContains(t *testing.T, f func(), str string) {
95+
defer func() {
96+
if r := recover(); r != nil {
97+
panicMessage := fmt.Sprintf("%v", r)
98+
if !strings.Contains(panicMessage, str) {
99+
t.Helper()
100+
t.Errorf("Panic message does not contain: %s", str)
101+
}
102+
} else {
103+
t.Helper()
104+
t.Errorf("Function did not panic")
49105
}
50106
}()
51107
f()

task_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestTaskSuccess(t *testing.T) {
1717
res, err := task.Call().Join()
1818

1919
assert.Equal(t, "ok", *res)
20-
assert.Equal(t, nil, err)
20+
assert.IsNil(t, err)
2121
}
2222

2323
func TestTaskFailure(t *testing.T) {
@@ -27,6 +27,6 @@ func TestTaskFailure(t *testing.T) {
2727
})
2828
res, err := task.Call().Join()
2929

30-
assert.Equal(t, nil, res)
30+
assert.IsNil(t, res)
3131
assert.ErrorContains(t, err, "error")
3232
}

value_test.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ func TestValueCompareAndSwap(t *testing.T) {
1212
var value Value
1313
swapped := value.CompareAndSwap(1, 2)
1414
assert.Equal(t, swapped, false)
15-
assert.Equal(t, value.Load(), nil)
15+
assert.IsNil(t, value.Load())
1616

1717
swapped = value.CompareAndSwap(1, nil)
1818
assert.Equal(t, swapped, false)
19-
assert.Equal(t, value.Load(), nil)
19+
assert.IsNil(t, value.Load())
2020

2121
swapped = value.CompareAndSwap(nil, 1)
2222
assert.Equal(t, swapped, false)
23-
assert.Equal(t, value.Load(), nil)
23+
assert.IsNil(t, value.Load())
2424

2525
value.Store(1)
2626

@@ -51,15 +51,11 @@ func TestValueCompareAndSwap(t *testing.T) {
5151
stringPointer := util.Ptr("b")
5252
swapped = value.CompareAndSwap("a", stringPointer)
5353
assert.Equal(t, swapped, true)
54-
if value.Load() != stringPointer {
55-
t.Fail()
56-
}
54+
assert.Same(t, value.Load().(*string), stringPointer)
5755

5856
swapped = value.CompareAndSwap(util.Ptr("b"), "c")
5957
assert.Equal(t, swapped, false)
60-
if value.Load() != stringPointer {
61-
t.Fail()
62-
}
58+
assert.Same(t, value.Load().(*string), stringPointer)
6359

6460
swapped = value.CompareAndSwap(stringPointer, "c")
6561
assert.Equal(t, swapped, true)
@@ -68,7 +64,7 @@ func TestValueCompareAndSwap(t *testing.T) {
6864

6965
func TestValueLoad(t *testing.T) {
7066
var value Value
71-
assert.Equal(t, value.Load(), nil)
67+
assert.IsNil(t, value.Load())
7268

7369
value.Store(1)
7470
assert.Equal(t, value.Load(), 1)
@@ -79,32 +75,28 @@ func TestValueStore(t *testing.T) {
7975
value.Store(1)
8076
assert.Equal(t, value.Load(), 1)
8177

82-
assert.Panic(t, func() { value.Store(nil) })
78+
assert.Panics(t, func() { value.Store(nil) })
8379

8480
value.Store("a")
8581
assert.Equal(t, value.Load(), "a")
8682

8783
stringPointer := util.Ptr("b")
8884
value.Store(stringPointer)
89-
if value.Load() != stringPointer {
90-
t.Fail()
91-
}
85+
assert.Same(t, value.Load().(*string), stringPointer)
9286
}
9387

9488
func TestValueSwap(t *testing.T) {
9589
var value Value
9690
old := value.Swap(1)
97-
assert.Equal(t, old, nil)
91+
assert.IsNil(t, old)
9892

99-
assert.Panic(t, func() { _ = value.Swap(nil) })
93+
assert.Panics(t, func() { _ = value.Swap(nil) })
10094

10195
old = value.Swap("a")
10296
assert.Equal(t, old, 1)
10397

10498
stringPointer := util.Ptr("b")
10599
old = value.Swap(stringPointer)
106100
assert.Equal(t, old, "a")
107-
if value.Load() != stringPointer {
108-
t.Fail()
109-
}
101+
assert.Same(t, value.Load().(*string), stringPointer)
110102
}

wait_group_context_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestWaitGroupContextPanicNegativeCounter(t *testing.T) {
7272
wgc := NewWaitGroupContext(context.Background())
7373
wgc.Add(-2)
7474
}
75-
assert.Panic(t, negativeCounter)
75+
assert.PanicMsgContains(t, negativeCounter, "negative")
7676
}
7777

7878
func TestWaitGroupContextPanicReused(t *testing.T) {
@@ -91,7 +91,7 @@ func TestWaitGroupContextPanicReused(t *testing.T) {
9191
wgc.Wait()
9292
}
9393
}
94-
assert.Panic(t, reusedBeforeWaitReturned)
94+
assert.PanicMsgContains(t, reusedBeforeWaitReturned, "reused")
9595
}
9696

9797
func TestWaitGroupContextReused(t *testing.T) {

0 commit comments

Comments
 (0)