mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
Separate allFieldsAreSet function
This commit is contained in:
parent
5e413e4ab7
commit
6e646046c8
1 changed files with 52 additions and 41 deletions
|
|
@ -342,7 +342,7 @@ func TestCopyHeader(t *testing.T) {
|
|||
t.Run("filled_header", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
h := Header{
|
||||
h := &Header{
|
||||
ParentHash: common.Hash{1},
|
||||
UncleHash: common.Hash{2},
|
||||
Coinbase: common.Address{3},
|
||||
|
|
@ -368,7 +368,7 @@ func TestCopyHeader(t *testing.T) {
|
|||
|
||||
allFieldsAreSet(t, h)
|
||||
|
||||
cpy := CopyHeader(&h)
|
||||
cpy := CopyHeader(h)
|
||||
|
||||
want := &Header{
|
||||
ParentHash: common.Hash{1},
|
||||
|
|
@ -396,7 +396,42 @@ func TestCopyHeader(t *testing.T) {
|
|||
assert.Equal(t, want, cpy)
|
||||
|
||||
// Mutate each non-value field to ensure they are not shared
|
||||
v := reflect.ValueOf(&h).Elem()
|
||||
fieldsAreDeepCopied(t, h, cpy)
|
||||
})
|
||||
}
|
||||
|
||||
func ptrTo[T any](x T) *T { return &x }
|
||||
|
||||
func allFieldsAreSet(t *testing.T, x any) {
|
||||
t.Helper()
|
||||
require.Equal(t, reflect.Ptr.String(), reflect.TypeOf(x).Kind().String(), "x must be a pointer")
|
||||
v := reflect.ValueOf(x).Elem()
|
||||
typ := v.Type()
|
||||
require.Equal(t, reflect.Struct.String(), typ.Kind().String())
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := v.Field(i)
|
||||
fieldName := typ.Field(i).Name
|
||||
fieldValue := field
|
||||
if unicode.IsLower(rune(fieldName[0])) { // unexported
|
||||
require.Falsef(t, field.IsNil(), "field %q is nil", fieldName)
|
||||
field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
|
||||
fieldValue = field
|
||||
}
|
||||
if field.Kind() == reflect.Pointer {
|
||||
require.NotNilf(t, field.Interface(), "field %q is nil", fieldName)
|
||||
fieldValue = field.Elem()
|
||||
}
|
||||
isSet := fieldValue.IsValid() && !fieldValue.IsZero()
|
||||
require.True(t, isSet, "field %q is not set", fieldName)
|
||||
}
|
||||
}
|
||||
|
||||
func fieldsAreDeepCopied(t *testing.T, original, cpy any) {
|
||||
t.Helper()
|
||||
require.Equal(t, reflect.Ptr.String(), reflect.TypeOf(original).Kind().String(), "original must be a pointer")
|
||||
require.Equal(t, reflect.Ptr.String(), reflect.TypeOf(cpy).Kind().String(), "cpy must be a pointer")
|
||||
|
||||
v := reflect.ValueOf(original).Elem()
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := v.Field(i)
|
||||
fieldName := v.Type().Field(i).Name
|
||||
|
|
@ -430,32 +465,8 @@ func TestCopyHeader(t *testing.T) {
|
|||
default:
|
||||
t.Fatalf("unexpected field kind %v for %q", field.Kind(), fieldName)
|
||||
}
|
||||
cpyField := reflect.ValueOf(*cpy).Field(i).Interface()
|
||||
|
||||
cpyField := reflect.ValueOf(cpy).Elem().Field(i).Interface()
|
||||
assert.NotEqualf(t, originalField, cpyField, "field %q", fieldName)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func ptrTo[T any](x T) *T { return &x }
|
||||
|
||||
func allFieldsAreSet(t *testing.T, x any) {
|
||||
v := reflect.ValueOf(x)
|
||||
typ := v.Type()
|
||||
require.Equal(t, reflect.Struct, typ.Kind())
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := v.Field(i)
|
||||
fieldName := typ.Field(i).Name
|
||||
fieldValue := field
|
||||
if unicode.IsLower(rune(fieldName[0])) { // unexported
|
||||
require.Falsef(t, field.IsNil(), "field %q is nil", fieldName)
|
||||
field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
|
||||
fieldValue = field
|
||||
}
|
||||
if field.Kind() == reflect.Pointer {
|
||||
require.NotNilf(t, field.Interface(), "field %q is nil", fieldName)
|
||||
fieldValue = field.Elem()
|
||||
}
|
||||
isSet := fieldValue.IsValid() && !fieldValue.IsZero()
|
||||
require.True(t, isSet, "field %q is not set", fieldName)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue