Files
prysm/encoding/ssz/query/testutil/runner.go
Jun Song b1dc5e485d SSZ-QL: Handle List type & Populate the actual value dynamically (#15637)
* Add VariableTestContainer in ssz_query.proto

* Add listInfo

* Use errors.New for making an error with a static string literal

* Add listInfo field when analyzing the List type

* Persist the field order in the container

* Add actualOffset and goFieldName at fieldInfo

* Add PopulateFromValue function & update test runner

* Handle slice of ssz object for marshalling

* Add CalculateOffsetAndLength test

* Add comments for better doc

* Changelog :)

* Apply reviews from Radek

* Remove actualOffset and update offset field instead

* Add Nested container of variable-sized for testing nested path

* Fix offset adding logics: for variable-sized field, always add 4 instead of its fixed size

* Fix multiple import issue

---------

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
2025-09-08 16:50:24 +00:00

42 lines
1.2 KiB
Go

package testutil
import (
"testing"
"github.com/OffchainLabs/prysm/v6/encoding/ssz/query"
"github.com/OffchainLabs/prysm/v6/testing/require"
ssz "github.com/prysmaticlabs/fastssz"
)
func RunStructTest(t *testing.T, spec TestSpec) {
t.Run(spec.Name, func(t *testing.T) {
info, err := query.AnalyzeObject(spec.Type)
require.NoError(t, err)
testInstance := spec.Instance
err = query.PopulateVariableLengthInfo(info, testInstance)
require.NoError(t, err)
marshaller, ok := testInstance.(ssz.Marshaler)
require.Equal(t, true, ok, "Test instance must implement ssz.Marshaler, got %T", testInstance)
marshalledData, err := marshaller.MarshalSSZ()
require.NoError(t, err)
for _, pathTest := range spec.PathTests {
t.Run(pathTest.Path, func(t *testing.T) {
path, err := query.ParsePath(pathTest.Path)
require.NoError(t, err)
_, offset, length, err := query.CalculateOffsetAndLength(info, path)
require.NoError(t, err)
expectedRawBytes := marshalledData[offset : offset+length]
rawBytes, err := marshalAny(pathTest.Expected)
require.NoError(t, err, "Marshalling expected value should not return an error")
require.DeepEqual(t, expectedRawBytes, rawBytes, "Extracted value should match expected")
})
}
})
}