-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphysics_stub_api_test.go
More file actions
77 lines (66 loc) · 2.25 KB
/
Copy pathphysics_stub_api_test.go
File metadata and controls
77 lines (66 loc) · 2.25 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
69
70
71
72
73
74
75
76
77
//go:build !physics
package willow_test
import (
"strings"
"testing"
"github.com/devthicket/willow"
)
// In the default build (no -tags physics) the physics package is the stub:
// the public surface still compiles, but the user-facing entry points panic
// with guidance to rebuild with -tags physics, while the per-frame and
// cleanup hooks the engine calls unconditionally stay safe no-ops.
const wantGuidance = "-tags physics"
func wantsPanic(t *testing.T, name string, fn func()) {
t.Helper()
defer func() {
r := recover()
if r == nil {
t.Fatalf("%s did not panic in the default (no-physics) build", name)
}
msg, ok := r.(string)
if !ok {
t.Fatalf("%s panicked with non-string %T: %v", name, r, r)
}
if !strings.Contains(msg, wantGuidance) {
t.Fatalf("%s panic message %q does not mention %q", name, msg, wantGuidance)
}
}()
fn()
}
// TestStub_UserFacingVerbsPanic asserts every "start using physics" entry
// point fails loud in a default build with the rebuild-with -tags physics
// guidance, so a missing physics build is immediately traceable.
func TestStub_UserFacingVerbsPanic(t *testing.T) {
wantsPanic(t, "EnablePhysics", func() {
willow.NewContainer("n").EnablePhysics(willow.PhysicsConfig{})
})
wantsPanic(t, "SetBody", func() {
willow.NewContainer("n").SetBody(willow.PhysicsDynamic{})
})
wantsPanic(t, "SetBodyEnabled", func() {
willow.NewContainer("n").SetBodyEnabled(true)
})
wantsPanic(t, "StepPhysics", func() {
willow.NewContainer("n").StepPhysics(1.0 / 60.0)
})
}
// TestStub_InternalHooksAreNoOps asserts the cleanup/query/per-frame hooks
// the engine invokes regardless of physics usage stay safe no-ops, so a
// default build that never opts into physics runs without panicking.
func TestStub_InternalHooksAreNoOps(t *testing.T) {
n := willow.NewContainer("n")
child := willow.NewContainer("child")
n.AddChild(child) // exercises markPhysicsListDirty internally
// These must not panic.
n.DisablePhysics()
n.RemoveBody()
n.TickPhysicsTree(1.0 / 60.0)
if b := n.GetBody(); b != nil {
t.Fatalf("GetBody in default build = %v, want nil", b)
}
if n.BodyEnabled() {
t.Fatal("BodyEnabled in default build = true, want false")
}
// Disposing a subtree that never used physics must be safe too.
n.Dispose()
}