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