Documentation
¶
Overview ¶
Package savetest is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package savetest provides test helpers that verify save.Game registries round-trip every registered part through a save.Store without losing or altering state.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckRoundTrip ¶
CheckRoundTrip performs the same verification as RoundTrip using dir as the temporary store directory and returns an error instead of failing a testing.TB.
Example ¶
CheckRoundTrip saves every registered part to a temporary store, loads it back and compares the state, so a save adapter that loses state fails the test that calls it.
package main
import (
"fmt"
"os"
"github.com/nsgo-dev/game/pkg/save"
"github.com/nsgo-dev/game/pkg/save/savetest"
)
type wallet struct {
Coins int `json:"coins"`
}
// CheckRoundTrip saves every registered part to a temporary store, loads it back and compares the state, so a save adapter that loses state fails the test that calls it.
func main() {
coins := 42
g := save.NewGame()
g.Register("wallet", 1, save.FuncPart(
func() (wallet, error) { return wallet{Coins: coins}, nil },
func(w wallet) error { coins = w.Coins; return nil },
))
dir, err := os.MkdirTemp("", "savetest")
if err != nil {
fmt.Println(err)
return
}
defer os.RemoveAll(dir)
fmt.Println(savetest.CheckRoundTrip(dir, g))
}
Output: <nil>
func CheckRoundTripInto ¶
CheckRoundTripInto saves src into dir, loads the saved slot into dst, verifies both games register the same parts and versions, and checks that every part's JSON state matches.
func RoundTrip ¶
RoundTrip saves g to a temporary save.Store, loads the slot back into a fresh *save.Game, and compares every registered part's serialized state before and after the load.
Pass a fresh factory (func() *save.Game) to load into a newly constructed game instance and catch parts whose LoadState is a no-op or fails to restore state. When fresh is omitted, RoundTrip resets any registered parts that implement save.Resetter on g before loading back into g.
Types ¶
This section is empty.