testutils

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: LGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Seed uint64 //nolint:gochecknoglobals // intentionally global for test reproducibility

Functions

func GobMarshal added in v0.16.9

func GobMarshal(v any) []byte

GobMarshal is the gob encoder shared by every gob-backed test double, here and in other fixture packages, so there is one definition of what those bytes are.

Doubles derive SizeWire and AppendWire from their own MarshalWire — len() and append() of it — rather than from this directly. That costs an encode per size call, which is irrelevant in a fixture, and buys the property that actually matters: the three cannot disagree, whatever a given double encodes with. Not every double here uses gob; SimpleEvent writes a varint by hand.

func NewRand

func NewRand(t *testing.T) *rand.Rand

func Pick

func Pick[T any](g *Gen, slice []T) T

Pick returns an element from the slice.

func RandBool added in v0.10.3

func RandBool(r *rand.Rand) bool

RandBool returns a random boolean.

func RandMapKey

func RandMapKey[K cmp.Ordered, V any](r *rand.Rand, m map[K]V) K

RandMapKey returns a random key from a map. Panics if the map is empty. It returns the n-th smallest key for a random n, not the n-th key in map iteration order, so the same seed picks the same key.

func RandString added in v0.10.0

func RandString(r *rand.Rand, length int) string

RandString generates a random alphanumeric string of the given length.

func RandWeightedOp

func RandWeightedOp(r *rand.Rand, ops OpWeights) string

RandWeightedOp returns a random operation from a map, using each op's value as its weight. It walks the ops in sorted order, not map iteration order, so the same seed picks the same sequence.

func Shuffle

func Shuffle[T any](g *Gen, slice []T)

Shuffle shuffles the slice in-place, exhaustively generating all permutations.

Types

type AnotherEvent

type AnotherEvent struct {
	Data string
}

func (AnotherEvent) AppendWire added in v0.16.9

func (e AnotherEvent) AppendWire(b []byte) []byte

func (AnotherEvent) MarshalWire added in v0.15.0

func (e AnotherEvent) MarshalWire() []byte

func (AnotherEvent) Name

func (AnotherEvent) Name() string

func (AnotherEvent) SizeWire added in v0.16.9

func (e AnotherEvent) SizeWire() int

MarshalWire is a test double for generated event wire code (explicit encoding, no serialization lib).

func (AnotherEvent) UnmarshalWire added in v0.15.0

func (AnotherEvent) UnmarshalWire(b []byte) (any, error)

type CommandA added in v0.10.0

type CommandA struct {
	X, Y, Z float64
}

func (CommandA) AppendWire added in v0.16.9

func (c CommandA) AppendWire(b []byte) []byte

func (CommandA) MarshalWire added in v0.15.0

func (c CommandA) MarshalWire() []byte

func (CommandA) Name added in v0.10.0

func (CommandA) Name() string

func (CommandA) SizeWire added in v0.16.9

func (c CommandA) SizeWire() int

func (CommandA) UnmarshalWire added in v0.15.0

func (CommandA) UnmarshalWire(data []byte) (any, error)

type CommandB added in v0.10.0

type CommandB struct {
	ID      uint64
	Label   string
	Enabled bool
}

func (CommandB) AppendWire added in v0.16.9

func (c CommandB) AppendWire(b []byte) []byte

func (CommandB) MarshalWire added in v0.15.0

func (c CommandB) MarshalWire() []byte

func (CommandB) Name added in v0.10.0

func (CommandB) Name() string

func (CommandB) SizeWire added in v0.16.9

func (c CommandB) SizeWire() int

func (CommandB) UnmarshalWire added in v0.15.0

func (CommandB) UnmarshalWire(data []byte) (any, error)

type CommandC added in v0.10.0

type CommandC struct {
	Values  [8]int32
	Counter uint16
}

func (CommandC) AppendWire added in v0.16.9

func (c CommandC) AppendWire(b []byte) []byte

func (CommandC) MarshalWire added in v0.15.0

func (c CommandC) MarshalWire() []byte

func (CommandC) Name added in v0.10.0

func (CommandC) Name() string

func (CommandC) SizeWire added in v0.16.9

func (c CommandC) SizeWire() int

func (CommandC) UnmarshalWire added in v0.15.0

func (CommandC) UnmarshalWire(data []byte) (any, error)

type ComponentA

type ComponentA struct {
	X, Y, Z float64
}

func (ComponentA) AppendWire added in v0.16.9

func (c ComponentA) AppendWire(b []byte) []byte

func (ComponentA) MarshalWire added in v0.15.0

func (c ComponentA) MarshalWire() []byte

func (ComponentA) Name

func (ComponentA) Name() string

func (ComponentA) SizeWire added in v0.16.9

func (c ComponentA) SizeWire() int

func (ComponentA) UnmarshalWire added in v0.15.0

func (ComponentA) UnmarshalWire(b []byte) (any, error)

type ComponentB

type ComponentB struct {
	ID      uint64
	Label   string
	Enabled bool
}

func (ComponentB) AppendWire added in v0.16.9

func (c ComponentB) AppendWire(b []byte) []byte

func (ComponentB) MarshalWire added in v0.15.0

func (c ComponentB) MarshalWire() []byte

func (ComponentB) Name

func (ComponentB) Name() string

func (ComponentB) SizeWire added in v0.16.9

func (c ComponentB) SizeWire() int

func (ComponentB) UnmarshalWire added in v0.15.0

func (ComponentB) UnmarshalWire(b []byte) (any, error)

type ComponentC

type ComponentC struct {
	Values  [8]int32
	Counter uint16
}

func (ComponentC) AppendWire added in v0.16.9

func (c ComponentC) AppendWire(b []byte) []byte

func (ComponentC) MarshalWire added in v0.15.0

func (c ComponentC) MarshalWire() []byte

func (ComponentC) Name

func (ComponentC) Name() string

func (ComponentC) SizeWire added in v0.16.9

func (c ComponentC) SizeWire() int

func (ComponentC) UnmarshalWire added in v0.15.0

func (ComponentC) UnmarshalWire(b []byte) (any, error)

type Gen

type Gen struct {
	// contains filtered or unexported fields
}

Gen is an exhaustive generator that iterates through all possible combinations of values.

The implementation is tricky, refer to the post for details.

On each iteration of `while (!g.done())` loop, Gen generates a sequence of numbers. Internally, it remembers this sequence together with bounds the user requested:

value: 3 1 4 4 bound: 5 4 4 4

To advance to the next iteration, Gen finds the smallest sequence of values which is larger than the current one, but still satisfies all the bounds. "Smallest" means that Gen tries to increment the rightmost number.

In the above example, the last two "4"s already match the bound, so we can't increment them. However, we can increment the second number, "1", to get 3 2 4 4. This isn’t the smallest sequence though, 3 2 0 0 is be smaller. So, after incrementing the rightmost number possible, we zero the rest.

See: <https://matklad.github.io/2021/11/07/generate-all-the-things.html>

func NewGen

func NewGen() *Gen

NewGen creates a new exhaustive generator.

func (*Gen) Bool

func (g *Gen) Bool() bool

Bool returns an exhaustive boolean value.

func (*Gen) Done

func (g *Gen) Done() bool

Done returns true when all combinations have been exhausted.

func (*Gen) Index

func (g *Gen) Index(length int) int

Index returns a valid index into a slice of the given length.

func (*Gen) Intn

func (g *Gen) Intn(bound int) int

Intn returns an int in range [0, bound] (inclusive).

func (*Gen) Range

func (g *Gen) Range(minVal, maxVal int) int

Range returns an int in range [minVal, maxVal] (inclusive).

type OpWeights added in v0.10.0

type OpWeights = map[string]uint64

OpWeights maps operation names to their weights.

func RandOpWeights added in v0.10.0

func RandOpWeights(r *rand.Rand, ops []string) OpWeights

RandOpWeights randomly selects a subset of operations and assigns random weights (1-100) to each. At minimum 1 operation is enabled, at most all operations are enabled.

type SimpleCommand

type SimpleCommand struct {
	Value int
}

func (SimpleCommand) AppendWire added in v0.16.9

func (c SimpleCommand) AppendWire(b []byte) []byte

func (SimpleCommand) MarshalWire added in v0.15.0

func (c SimpleCommand) MarshalWire() []byte

func (SimpleCommand) Name

func (SimpleCommand) Name() string

func (SimpleCommand) SizeWire added in v0.16.9

func (c SimpleCommand) SizeWire() int

func (SimpleCommand) UnmarshalWire added in v0.15.0

func (SimpleCommand) UnmarshalWire(data []byte) (any, error)

type SimpleComponent

type SimpleComponent struct {
	Value int
}

func (SimpleComponent) AppendWire added in v0.16.9

func (c SimpleComponent) AppendWire(b []byte) []byte

func (SimpleComponent) MarshalWire added in v0.15.0

func (c SimpleComponent) MarshalWire() []byte

func (SimpleComponent) Name

func (SimpleComponent) Name() string

func (SimpleComponent) SizeWire added in v0.16.9

func (c SimpleComponent) SizeWire() int

func (SimpleComponent) UnmarshalWire added in v0.15.0

func (SimpleComponent) UnmarshalWire(b []byte) (any, error)

type SimpleEvent

type SimpleEvent struct {
	Value int
}

func (SimpleEvent) AppendWire added in v0.16.9

func (s SimpleEvent) AppendWire(b []byte) []byte

func (SimpleEvent) MarshalWire added in v0.15.0

func (s SimpleEvent) MarshalWire() []byte

func (SimpleEvent) Name

func (SimpleEvent) Name() string

func (SimpleEvent) SizeWire added in v0.16.9

func (s SimpleEvent) SizeWire() int

MarshalWire / UnmarshalWire are a test double for generated event wire code — the engine requires the wire codec. Deliberately an explicit encoding, not a serialization library, so testutils stays free of any wire-format dependency.

func (SimpleEvent) UnmarshalWire added in v0.15.0

func (SimpleEvent) UnmarshalWire(b []byte) (any, error)

type SimpleSystemEvent

type SimpleSystemEvent struct {
	Value int
}

func (SimpleSystemEvent) AppendWire added in v0.16.9

func (c SimpleSystemEvent) AppendWire(b []byte) []byte

func (SimpleSystemEvent) MarshalWire added in v0.15.0

func (c SimpleSystemEvent) MarshalWire() []byte

func (SimpleSystemEvent) Name

func (SimpleSystemEvent) Name() string

func (SimpleSystemEvent) SizeWire added in v0.16.9

func (c SimpleSystemEvent) SizeWire() int

func (SimpleSystemEvent) UnmarshalWire added in v0.15.0

func (SimpleSystemEvent) UnmarshalWire(b []byte) (any, error)

type SystemEventA

type SystemEventA struct {
	X, Y, Z float64
}

func (SystemEventA) Name

func (SystemEventA) Name() string

type SystemEventB

type SystemEventB struct {
	ID      uint64
	Label   string
	Enabled bool
}

func (SystemEventB) Name

func (SystemEventB) Name() string

type SystemEventC

type SystemEventC struct {
	Values  [8]int32
	Counter uint16
}

func (SystemEventC) Name

func (SystemEventC) Name() string

Jump to

Keyboard shortcuts

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