Repository navigation
Expand file tree
/
Copy pathvalidation_test.go
More file actions
68 lines (62 loc) · 2.01 KB
/
Copy pathvalidation_test.go
File metadata and controls
68 lines (62 loc) · 2.01 KB
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
package bedsim
import (
"math"
"reflect"
"testing"
"github.com/go-gl/mathgl/mgl32"
)
type numericStateField struct {
name string
index []int
typ reflect.Type
}
func TestFiniteMovementStateRejectsEveryFloatField(t *testing.T) {
base := newBaseState()
fields := collectNumericStateFields(reflect.TypeOf(*base), nil, "MovementState")
if len(fields) == 0 {
t.Fatal("no numeric movement fields discovered")
}
for _, field := range fields {
t.Run(field.name, func(t *testing.T) {
state := *base
value := reflect.ValueOf(&state).Elem().FieldByIndex(field.index)
switch field.typ {
case reflect.TypeFor[float32]():
value.SetFloat(math.NaN())
case reflect.TypeFor[mgl32.Vec2](), reflect.TypeFor[mgl32.Vec3]():
value.Index(0).SetFloat(math.NaN())
default:
t.Fatalf("unsupported numeric field type %v", field.typ)
}
if finiteMovementState(&state) {
t.Fatalf("finiteMovementState accepted NaN in %s", field.name)
}
})
}
}
// collectNumericStateFields returns every float or movement-vector field in a
// movement state, including fields nested in value structs.
func collectNumericStateFields(t reflect.Type, prefix []int, name string) []numericStateField {
floatType := reflect.TypeFor[float32]()
vec2Type := reflect.TypeFor[mgl32.Vec2]()
vec3Type := reflect.TypeFor[mgl32.Vec3]()
fields := make([]numericStateField, 0)
for i := range t.NumField() {
field := t.Field(i)
index := append(append([]int(nil), prefix...), i)
fieldName := name + "." + field.Name
switch field.Type {
case reflect.TypeFor[collisionShape]():
// Private, derived geometry cannot be supplied by callers. Its inputs
// (Pos, Size and pose/slide state) are covered by this exhaustive walk.
continue
case floatType, vec2Type, vec3Type:
fields = append(fields, numericStateField{name: fieldName, index: index, typ: field.Type})
default:
if field.Type.Kind() == reflect.Struct {
fields = append(fields, collectNumericStateFields(field.Type, index, fieldName)...)
}
}
}
return fields
}