Skip to content

Commit fbb75f7

Browse files
committed
☎️ all: improve mustPanic to print correct caller
We could simply call t.Helper() inside the deferred function, but moving the t.Errorf call out of the deferred function seems to result in cleaner code.
1 parent d8e441d commit fbb75f7

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed

bitset/bitset_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@ import (
55
"testing"
66
)
77

8-
func assertPanic(t *testing.T, f func()) {
8+
func mustPanic(t *testing.T, f func(), name string) {
99
t.Helper()
10-
defer func() {
11-
if r := recover(); r == nil {
12-
t.Error("expected panic, got none")
13-
}
14-
}()
10+
defer func() { _ = recover() }()
1511
f()
12+
t.Errorf("%s did not panic", name)
1613
}
1714

1815
func testBitSetIndexOutOfRange(t *testing.T, s BitSet) {
1916
index := s.Capacity()
20-
assertPanic(t, func() { s.IsSet(index) })
21-
assertPanic(t, func() { s.Set(index) })
22-
assertPanic(t, func() { s.Unset(index) })
23-
assertPanic(t, func() { s.Flip(index) })
17+
mustPanic(t, func() { _ = s.IsSet(index) }, "s.IsSet(index)")
18+
mustPanic(t, func() { s.Set(index) }, "s.Set(index)")
19+
mustPanic(t, func() { s.Unset(index) }, "s.Unset(index)")
20+
mustPanic(t, func() { s.Flip(index) }, "s.Flip(index)")
2421
}
2522

2623
func assertEmptyBitSet(t *testing.T, s BitSet) {

conn/addr_test.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,32 +119,29 @@ func TestAddrIsDomain(t *testing.T) {
119119
}
120120
}
121121

122-
func assertPanic(t *testing.T, f func()) {
122+
func mustPanic(t *testing.T, f func(), name string) {
123123
t.Helper()
124-
defer func() {
125-
if r := recover(); r == nil {
126-
t.Error("Expected panic, got none.")
127-
}
128-
}()
124+
defer func() { _ = recover() }()
129125
f()
126+
t.Errorf("%s did not panic", name)
130127
}
131128

132129
func TestAddrIP(t *testing.T) {
133130
if ip := addrIP.IP(); ip != addrIPAddr {
134131
t.Errorf("%q.IP() = %q, want %q", addrIP, ip, addrIPAddr)
135132
}
136133

137-
assertPanic(t, func() { addrZero.IP() })
138-
assertPanic(t, func() { addrDomain.IP() })
134+
mustPanic(t, func() { _ = addrZero.IP() }, "addrZero.IP()")
135+
mustPanic(t, func() { _ = addrDomain.IP() }, "addrDomain.IP()")
139136
}
140137

141138
func TestAddrDomain(t *testing.T) {
142139
if domain := addrDomain.Domain(); domain != addrDomainHost {
143140
t.Errorf("%q.Domain() = %q, want %q", addrDomain, domain, addrDomainHost)
144141
}
145142

146-
assertPanic(t, func() { addrZero.Domain() })
147-
assertPanic(t, func() { addrIP.Domain() })
143+
mustPanic(t, func() { _ = addrZero.Domain() }, "addrZero.Domain()")
144+
mustPanic(t, func() { _ = addrIP.Domain() }, "addrIP.Domain()")
148145
}
149146

150147
func TestAddrPort(t *testing.T) {
@@ -167,8 +164,8 @@ func TestAddrIPPort(t *testing.T) {
167164
t.Errorf("%q.IPPort() = %q, want %q", addrIP, ap, addrIPAddrPort)
168165
}
169166

170-
assertPanic(t, func() { addrZero.IPPort() })
171-
assertPanic(t, func() { addrDomain.IPPort() })
167+
mustPanic(t, func() { _ = addrZero.IPPort() }, "addrZero.IPPort()")
168+
mustPanic(t, func() { _ = addrDomain.IPPort() }, "addrDomain.IPPort()")
172169
}
173170

174171
func TestAddrResolveIP(t *testing.T) {
@@ -190,7 +187,7 @@ func TestAddrResolveIP(t *testing.T) {
190187
t.Errorf("%q.ResolveIP().IsValid() = false, want true", addrDomain)
191188
}
192189

193-
assertPanic(t, func() { addrZero.ResolveIP(ctx, "ip") })
190+
mustPanic(t, func() { _, _ = addrZero.ResolveIP(ctx, "ip") }, "addrZero.ResolveIP()")
194191
}
195192

196193
func TestAddrResolveIPPort(t *testing.T) {
@@ -215,7 +212,7 @@ func TestAddrResolveIPPort(t *testing.T) {
215212
t.Errorf("%q.ResolveIPPort() = %q, want port %d", addrDomain, ipPort, addrDomainPort)
216213
}
217214

218-
assertPanic(t, func() { addrZero.ResolveIPPort(ctx, "ip") })
215+
mustPanic(t, func() { _, _ = addrZero.ResolveIPPort(ctx, "ip") }, "addrZero.ResolveIPPort()")
219216
}
220217

221218
func TestAddrHost(t *testing.T) {
@@ -227,7 +224,7 @@ func TestAddrHost(t *testing.T) {
227224
t.Errorf("%q.Host() = %q, want %q", addrDomain, host, addrDomainHost)
228225
}
229226

230-
assertPanic(t, func() { addrZero.Host() })
227+
mustPanic(t, func() { _ = addrZero.Host() }, "addrZero.Host()")
231228
}
232229

233230
func TestAddrString(t *testing.T) {

portset/portset_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@ import (
66
"testing"
77
)
88

9-
func assertPanic(t *testing.T, f func()) {
9+
func mustPanic(t *testing.T, f func(), name string) {
1010
t.Helper()
11-
defer func() {
12-
if r := recover(); r == nil {
13-
t.Error("expected panic, got none")
14-
}
15-
}()
11+
defer func() { _ = recover() }()
1612
f()
13+
t.Errorf("%s did not panic", name)
1714
}
1815

1916
func TestPortSetBadPort(t *testing.T) {
2017
var s PortSet
21-
assertPanic(t, func() { s.Contains(0) })
22-
assertPanic(t, func() { s.Add(0) })
23-
assertPanic(t, func() { s.AddRange(0, 1) })
24-
assertPanic(t, func() { s.AddRange(1, 0) })
25-
assertPanic(t, func() { s.AddRange(1, 1) })
18+
mustPanic(t, func() { _ = s.Contains(0) }, "s.Contains(0)")
19+
mustPanic(t, func() { s.Add(0) }, "s.Add(0)")
20+
mustPanic(t, func() { s.AddRange(0, 1) }, "s.AddRange(0, 1)")
21+
mustPanic(t, func() { s.AddRange(1, 0) }, "s.AddRange(1, 0)")
22+
mustPanic(t, func() { s.AddRange(1, 1) }, "s.AddRange(1, 1)")
2623
}
2724

2825
func assertPortSetFirst(t *testing.T, s *PortSet, from uint16) {

0 commit comments

Comments
 (0)