-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_test.go
More file actions
72 lines (60 loc) · 1.77 KB
/
pointer_test.go
File metadata and controls
72 lines (60 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package require_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zimmermanncode/go-require"
)
func TestNilPtr(t *testing.T) {
t.Parallel()
t.Run("should accept nil pointer & return nil", func(t *testing.T) {
t.Parallel()
assert.Nil(t, require.NilPtr("Test pointer", (*int)(nil)))
})
t.Run("should panic when pointer is not nil", func(t *testing.T) {
value := "test"
t.Parallel()
assert.PanicsWithValue(t, "assertion failed: Test pointer should be a nil pointer", func() {
require.NilPtr("Test pointer", &value)
})
})
t.Run("should use given name in panic message", func(t *testing.T) {
value := 42
t.Parallel()
assert.PanicsWithValue(t, "assertion failed: Other value should be a nil pointer", func() {
require.NilPtr("Other value", &value)
})
})
}
func TestNotNilPtr(t *testing.T) {
t.Parallel()
t.Run("should accept not-nil pointer & return same pointer", func(t *testing.T) {
value := 42
t.Parallel()
assert.Same(t, &value, require.NotNilPtr("Test pointer", &value))
})
t.Run("should panic when pointer is nil", func(t *testing.T) {
t.Parallel()
assert.PanicsWithValue(t, "assertion failed: Test pointer should not be a nil pointer", func() {
require.NotNilPtr("Test pointer", (*string)(nil))
})
})
t.Run("should use given name in panic message", func(t *testing.T) {
t.Parallel()
assert.PanicsWithValue(t, "assertion failed: Other value should not be a nil pointer", func() {
require.NotNilPtr("Other value", (*float64)(nil))
})
})
}
func BenchmarkNilPtr(b *testing.B) {
b.ResetTimer()
for range b.N {
require.NilPtr("Benchmark pointer", (*int)(nil))
}
}
func BenchmarkNotNilPtr(b *testing.B) {
value := "benchmark"
b.ResetTimer()
for range b.N {
require.NotNilPtr("Benchmark pointer", &value)
}
}