savetest

package
v0.4.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func CheckRoundTrip(dir string, g *save.Game, fresh ...func() *save.Game) error

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

func CheckRoundTripInto(dir string, src, dst *save.Game) error

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

func RoundTrip(t testing.TB, g *save.Game, fresh ...func() *save.Game)

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.

func RoundTripInto

func RoundTripInto(t testing.TB, src, dst *save.Game)

RoundTripInto saves src to a temporary save.Store, loads the slot into dst, and compares every registered part's serialized state between src and dst.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL