forked from prysmaticlabs/go-ssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_utils_test.go
81 lines (71 loc) · 2.27 KB
/
struct_utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ssz
import (
"reflect"
"testing"
)
type structWithTags struct {
NestedItem [][][][]byte `ssz-size:"4,1,4,1"`
UnboundedItem [][]byte `ssz-size:"?,4"`
}
func TestInferTypeFromStructTags(t *testing.T) {
structExample := structWithTags{
NestedItem: [][][][]byte{{{{4}}}, {{{3}}}},
UnboundedItem: [][]byte{{2, 3, 4, 5}, {1, 2, 3, 4}},
}
// We then verify that highly nested items can have their types
// inferred via SSZ field tags.
typ := reflect.TypeOf(structExample)
sizes, exists, err := parseSSZFieldTags(typ.Field(0))
if err != nil {
t.Fatal(err)
}
if !exists {
t.Fatal("Expected struct tags to exist")
}
fType := inferFieldTypeFromSizeTags(typ.Field(0), sizes)
expectedField := [4][1][4][1]byte{}
expectedFieldType := reflect.TypeOf(expectedField)
if expectedFieldType != fType {
t.Errorf("Expected inferred field: %v, received %v", expectedFieldType, fType)
}
// We then verify that unbounded items can be formed via SSZ field tags
// and their type can be correctly inferred.
sizes, exists, err = parseSSZFieldTags(typ.Field(1))
if err != nil {
t.Fatal(err)
}
if !exists {
t.Fatal("Expected struct tags to exist")
}
fType = inferFieldTypeFromSizeTags(typ.Field(1), sizes)
unboundedExpectedField := [][4]byte{}
unboundedExpectedFieldType := reflect.TypeOf(unboundedExpectedField)
if unboundedExpectedFieldType != fType {
t.Errorf("Expected inferred field: %v, received %v", expectedFieldType, fType)
}
}
// Regression test for https://github.com/prysmaticlabs/go-ssz/issues/44.
func TestDetermineFieldCapacity_HandlesOverflow(t *testing.T) {
input := struct {
Data string `ssz-max:"18446744073709551615"` // max uint64
}{}
result, _ := determineFieldCapacity(reflect.TypeOf(input).Field(0))
want := uint64(18446744073709551615)
if result != want {
t.Errorf("got: %d, wanted %d", result, want)
}
}
// Regression test for https://github.com/prysmaticlabs/go-ssz/issues/44.
func TestParseSSZFieldTags_HandlesOverflow(t *testing.T) {
input := struct {
Data string `ssz-size:"18446744073709551615"` // max uint64
}{}
result, _, err := parseSSZFieldTags(reflect.TypeOf(input).Field(0))
if err != nil {
t.Fatal(err)
}
want := uint64(18446744073709551615)
if result[0] != want {
t.Errorf("got: %d, wanted %d", result, want)
}
}