Files
aego/editor/ext/extension_test.go

114 lines
2.8 KiB
Go

package ext
import (
"aego/editor/ext/inspect"
"errors"
"testing"
"aego/core/mathx"
"aego/editor/core/command"
"aego/editor/core/context"
"aego/editor/core/gizmo"
"aego/editor/core/services"
"aego/editor/core/shape"
"aego/editor/core/templates"
"aego/editor/core/workspace"
"aego/scene"
"aego/schema"
)
type boomPanel struct{}
func (boomPanel) ID() string { return "quest" }
func (boomPanel) Title() string { return "Quest" }
func (boomPanel) Draw(*services.Frame, mathx.Rect) { panic("boom") }
func hook(fail bool) func(*Registry) error {
return func(r *Registry) error {
if err := r.Panel(workspace.Desc{Panel: boomPanel{}}); err != nil {
return err
}
if err := r.Command(command.Desc{ID: "game.spawn", Run: func() error { panic("cmd") }}); err != nil {
return err
}
if fail {
return errors.New("nope")
}
return nil
}
}
func setup(t *testing.T) (*Registry, Deps) {
t.Helper()
types := scene.NewTypes(schema.NewRegistry())
if err := types.Schema().Freeze(); err != nil {
t.Fatal(err)
}
d := Deps{
Editor: &context.Editor{},
Workspace: workspace.New(t.TempDir(), nil),
Inspectors: inspect.NewRegistry(),
Gizmos: gizmo.NewRegistry(),
Templates: templates.New(),
Shapes: shape.NewRegistry(),
Commands: command.New(),
Schema: types.Schema(),
}
return New(d), d
}
func TestPanickingPanelDisablesExtension(t *testing.T) {
r, d := setup(t)
if err := r.Run("game", hook(false)); err != nil {
t.Fatal(err)
}
p, ok := d.Workspace.Panel("quest")
if !ok {
t.Fatal("panel not registered")
}
func() {
defer func() { recover() }()
p.Draw(&services.Frame{UI: d.Editor.UI}, mathx.Rect{})
}()
r.Flush()
if _, ok := r.Disabled()["game"]; !ok {
t.Fatal("extension not disabled")
}
if d.Workspace.Has("quest") {
t.Fatal("panel of a disabled extension must be unregistered")
}
if _, ok := d.Commands.Get("game.spawn"); ok {
t.Fatal("command of a disabled extension must be unregistered")
}
}
func TestPanickingCommandReturnsError(t *testing.T) {
r, d := setup(t)
if err := r.Run("game", hook(false)); err != nil {
t.Fatal(err)
}
if err := d.Commands.Run("game.spawn"); err == nil {
t.Fatal("panic must surface as an error")
}
r.Flush()
if len(r.Disabled()) != 1 {
t.Fatal("extension not disabled")
}
}
func TestFailedRegistrationRollsBack(t *testing.T) {
r, d := setup(t)
if err := r.Run("game", hook(true)); err == nil {
t.Fatal("registration error swallowed")
}
if d.Workspace.Has("quest") {
t.Fatal("panel of a failed extension must be unregistered")
}
if _, ok := d.Commands.Get("game.spawn"); ok {
t.Fatal("command of a failed extension must be unregistered")
}
if _, ok := r.Disabled()["game"]; !ok {
t.Fatal("failed extension must be marked disabled")
}
}