Skip to content

Commit 3db5979

Browse files
apocelipesgopherbot
authored andcommitted
testing: use reflect.TypeAssert and reflect.TypeFor
To simplify the code. Updates golang#62121 Updates golang#60088 Change-Id: I1cef27b19961721b89a965c708320072a052d9af GitHub-Last-Rev: ffd07a7 GitHub-Pull-Request: golang#75494 Reviewed-on: https://go-review.googlesource.com/c/go/+/704575 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 6a8dbbe commit 3db5979

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/testing/fuzz.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,23 @@ func (f *F) Add(args ...any) {
163163

164164
// supportedTypes represents all of the supported types which can be fuzzed.
165165
var supportedTypes = map[reflect.Type]bool{
166-
reflect.TypeOf(([]byte)("")): true,
167-
reflect.TypeOf((string)("")): true,
168-
reflect.TypeOf((bool)(false)): true,
169-
reflect.TypeOf((byte)(0)): true,
170-
reflect.TypeOf((rune)(0)): true,
171-
reflect.TypeOf((float32)(0)): true,
172-
reflect.TypeOf((float64)(0)): true,
173-
reflect.TypeOf((int)(0)): true,
174-
reflect.TypeOf((int8)(0)): true,
175-
reflect.TypeOf((int16)(0)): true,
176-
reflect.TypeOf((int32)(0)): true,
177-
reflect.TypeOf((int64)(0)): true,
178-
reflect.TypeOf((uint)(0)): true,
179-
reflect.TypeOf((uint8)(0)): true,
180-
reflect.TypeOf((uint16)(0)): true,
181-
reflect.TypeOf((uint32)(0)): true,
182-
reflect.TypeOf((uint64)(0)): true,
166+
reflect.TypeFor[[]byte](): true,
167+
reflect.TypeFor[string](): true,
168+
reflect.TypeFor[bool](): true,
169+
reflect.TypeFor[byte](): true,
170+
reflect.TypeFor[rune](): true,
171+
reflect.TypeFor[float32](): true,
172+
reflect.TypeFor[float64](): true,
173+
reflect.TypeFor[int](): true,
174+
reflect.TypeFor[int8](): true,
175+
reflect.TypeFor[int16](): true,
176+
reflect.TypeFor[int32](): true,
177+
reflect.TypeFor[int64](): true,
178+
reflect.TypeFor[uint](): true,
179+
reflect.TypeFor[uint8](): true,
180+
reflect.TypeFor[uint16](): true,
181+
reflect.TypeFor[uint32](): true,
182+
reflect.TypeFor[uint64](): true,
183183
}
184184

185185
// Fuzz runs the fuzz function, ff, for fuzz testing. If ff fails for a set of
@@ -224,7 +224,7 @@ func (f *F) Fuzz(ff any) {
224224
if fnType.Kind() != reflect.Func {
225225
panic("testing: F.Fuzz must receive a function")
226226
}
227-
if fnType.NumIn() < 2 || fnType.In(0) != reflect.TypeOf((*T)(nil)) {
227+
if fnType.NumIn() < 2 || fnType.In(0) != reflect.TypeFor[*T]() {
228228
panic("testing: fuzz target must receive at least two arguments, where the first argument is a *T")
229229
}
230230
if fnType.NumOut() != 0 {

src/testing/quick/quick.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
6464
// hint is used for shrinking as a function of indirection level so
6565
// that recursive data structures will terminate.
6666
func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value, ok bool) {
67-
if m, ok := reflect.Zero(t).Interface().(Generator); ok {
67+
if m, ok := reflect.TypeAssert[Generator](reflect.Zero(t)); ok {
6868
return m.Generate(rand, size), true
6969
}
7070

0 commit comments

Comments
 (0)