Die API-Referenz stammt aus den Go-Doc-Kommentaren im Quellcode und ist auf Englisch verfasst.
package gametest
import "github.com/nsgo-dev/game/pkg/gametest"Übersicht
Package gametest is experimental: its API and documented formats may change in a minor release. See the API stability policy. provides deterministic software rendering of nsgo Canvas draw operations, golden PNG comparison, and visual diff generation for tests.
Index
- Konstanten
- func ApplyColorVision(img image.Image, mode a11y.ColorVision, severity float64, correct bool) *image.RGBA
- func AssertColorDistinguishable(t testing.TB, mode a11y.ColorVision, a, b core.Color, minDistance float64)
- func AssertGolden(t testing.TB, name string, got image.Image)
- func AssertGoldenAt(t testing.TB, goldenPath string, got image.Image)
- func AssertOpsGolden(t testing.TB, name string, width, height int, d *core.Drawing, opts ...RenderOptions)
- func AssertRegionsDistinguishable(t testing.TB, img image.Image, regionA, regionB image.Rectangle, mode a11y.ColorVision, minDistance float64)
- func AssertSameImages(t testing.TB, got, want *core.Drawing)
- func AverageRegionColor(img image.Image, region image.Rectangle) core.Color
- func ColorDistance(a, b core.Color) float64
- func CompareGolden(got image.Image, goldenPath string, update bool) (*image.RGBA, int, error)
- func DiffImage(got, want image.Image) (diff *image.RGBA, mismatchedPixels int)
- func GoldenPath(dir, name string) string
- func ReadPNG(path string) (image.Image, error)
- func RenderOps(width, height int, d *core.Drawing, opts ...RenderOptions) *image.RGBA
- func ShouldUpdateGolden() bool
- func SimulateColorVision(img image.Image, mode a11y.ColorVision, severity float64) *image.RGBA
- func WritePNG(path string, img image.Image) error
- type ClipStep
- type ImageDraw
- type PhotosensitivityFailure
- type PhotosensitivityOptions
- type PhotosensitivityReport
- type RenderOptions
Konstanten
const DefaultGoldenDir = "testdata/golden"DefaultGoldenDir is the relative directory used by AssertGolden and AssertOpsGolden.
Funktionen
func ApplyColorVision(img image.Image, mode a11y.ColorVision, severity float64, correct bool) *image.RGBAApplyColorVision filters img through postfx.ColorVisionEffect for mode at severity in [0, 1] (clamped, with NaN treated as 0). When correct is false, it simulates the deficiency; when correct is true, it applies daltonization correction. The input image is not mutated.
func AssertColorDistinguishable(t testing.TB, mode a11y.ColorVision, a, b core.Color, minDistance float64)AssertColorDistinguishable simulates a and b under mode and fails t if their Euclidean sRGB distance is smaller than minDistance.
func AssertGolden(t testing.TB, name string, got image.Image)AssertGolden compares got against testdata/golden/<name>.png and fails t on mismatch, writing <name>.got.png and <name>.diff.png beside the golden (or under GOLDEN_DIFF_DIR when set) so they outlive the test for inspection.
func AssertGoldenAt(t testing.TB, goldenPath string, got image.Image)AssertGoldenAt compares got against goldenPath and fails t on mismatch, writing <name>.got.png and <name>.diff.png beside goldenPath (or under GOLDEN_DIFF_DIR when set) so they remain on disk after the test ends. On a passing or updating run, any stale failure images at those paths are removed.
func AssertOpsGolden(t testing.TB, name string, width, height int, d *core.Drawing, opts ...RenderOptions)AssertOpsGolden rasterizes d with RenderOps and asserts the resulting image matches testdata/golden/<name>.png.
func AssertRegionsDistinguishable(t testing.TB, img image.Image, regionA, regionB image.Rectangle, mode a11y.ColorVision, minDistance float64)AssertRegionsDistinguishable simulates img under mode (when mode is not VisionNormal) and fails t if the average colors of regionA and regionB have Euclidean sRGB distance less than minDistance.
func AssertSameImages(t testing.TB, got, want *core.Drawing)AssertSameImages fails t unless got and want draw the same image rectangles, in the same order, to the same places with the same alpha, composite, tint, sampling and clip, their numbers within 1e-3.
Unlike comparing the operations themselves, it holds however the images were recorded, such as one DrawImageFrom per sprite against one DrawAtlas. It compares only images, as ImageDraws reads them: a test whose recordings also fill, stroke or write text checks those on its own.
func AverageRegionColor(img image.Image, region image.Rectangle) core.ColorAverageRegionColor returns the mean straight sRGB color of opaque/translucent pixels inside region (intersected with img's bounds). Returns core.Transparent when the region is empty or fully transparent.
func ColorDistance(a, b core.Color) float64ColorDistance returns the Euclidean sRGB distance between a and b in 8-bit channel units [0, 441.67], ignoring alpha.
func CompareGolden(got image.Image, goldenPath string, update bool) (*image.RGBA, int, error)CompareGolden compares got against the PNG stored at goldenPath.
When update is true, got is written to goldenPath and CompareGolden returns (nil, 0, nil). When update is false, CompareGolden decodes goldenPath, round-trips got through PNG encoding and decoding so comparison runs in the space stored on disk, and returns DiffImage(gotPNG, want).
func DiffImage(got, want image.Image) (diff *image.RGBA, mismatchedPixels int)DiffImage compares got and want pixel by pixel and returns a visual diff image along with the count of mismatched pixels.
Matching pixels are rendered as a dimmed grayscale context; mismatched pixels (including pixels outside either image's bounds when dimensions differ) are highlighted in solid magenta (#ff00ff).
func GoldenPath(dir, name string) stringGoldenPath returns the standard golden PNG file path under dir for name.
func ReadPNG(path string) (image.Image, error)ReadPNG opens and decodes a PNG image from path.
func RenderOps(width, height int, d *core.Drawing, opts ...RenderOptions) *image.RGBARenderOps deterministically rasterizes the operations a core.Drawing records into an RGBA image of size width by height pixels.
Supported operations include Save, Restore, Translate, Scale, Rotate, Transform, SetTransform, SetAlpha, SetFillColor, SetStrokeColor, SetLineWidth, Clear, Rect + Fill/Stroke (including FillRect and StrokeRect), polygon paths (MoveTo, LineTo, ClosePath + Fill/Stroke), DrawImage / DrawImageIn / DrawImageFrom and DrawAtlas. Images and atlas sprites resolve against the drawing's own buffers, as the DrawOp.ImageIndex and DrawOp.SpriteStart indexes require.
func ShouldUpdateGolden() boolShouldUpdateGolden reports whether golden updates are enabled via the -update-golden flag or the UPDATE_GOLDEN environment variable.
func SimulateColorVision(img image.Image, mode a11y.ColorVision, severity float64) *image.RGBASimulateColorVision returns a copy of img filtered through color-vision deficiency simulation for mode at severity in [0, 1]. Passing severity == 1.0 matches full dichromacy simulation (a11y.Simulate).
func WritePNG(path string, img image.Image) errorWritePNG encodes img as a PNG file at path, creating parent directories as needed.
Typen
type ClipStep struct {
// Op is the path operation (MoveTo, LineTo, QuadTo, CubicTo, Arc, Rect, RoundRect, Ellipse or ClosePath) or the Clip.
Op core.DrawOp
// Transform is the drawing's transform then, as the arguments a, b, c, d, e, f of Drawing.Transform.
Transform [6]float32
}ClipStep is one operation of a clipping path, or the Clip that ends one, with the drawing's transform when it was recorded.
type ImageDraw struct {
// Image is the image drawn from.
Image core.ImageSource
// SX, SY, SW and SH are the rectangle of the image drawn, in the image's points; SW and SH are 0 when the operation draws the whole image.
SX, SY, SW, SH float32
// Corners are where the drawn rectangle's top left, top right, bottom right and bottom left land on the canvas, after the drawing's transform. An image drawn at its natural size (DrawImage, DrawTintedImage) takes the size of its source rectangle, or of the whole image when the source sets Size or Pixels; with neither, its size is unknown and all four corners are its top left.
Corners [4]core.Point
// Alpha is the opacity it draws with: the drawing's alpha, times the sprite's own for a DrawAtlas sprite.
Alpha float32
// Composite is how it mixes with what is under it.
Composite core.CompositeOp
// Tinted reports whether the image's channels are multiplied by Tint, as DrawTintedImage records; a DrawAtlas sprite is never tinted.
Tinted bool
// Tint is the color a tinted image is multiplied by.
Tint core.Color
// Sampling is how the image's texels are sampled.
Sampling core.ImageSampling
// Clip is the clipping in effect, outermost first: the operations of each clipping path, each followed by the Clip that used it. It is empty when nothing clips the image.
Clip []ClipStep
}ImageDraw is one rectangle of an image that recorded operations put on the canvas: a DrawImage, DrawImageIn or DrawImageFrom, or one sprite of a DrawAtlas.
func ImageDraws(d *core.Drawing) []ImageDrawImageDraws replays the state that d's operations set (Save, Restore, Translate, Scale, Rotate, Transform, SetTransform, SetAlpha, SetComposite and clipping paths) and returns every image rectangle they draw, in order. It reads only images: fills, strokes, texts and meshes are left out.
It lets a test check where sprites land and what they show however they were recorded: one DrawImageFrom per sprite, or many sprites in one DrawAtlas, as sprite.Batch draws them. Images and atlas sprites resolve against the drawing's own buffers, as the DrawOp.ImageIndex and DrawOp.SpriteStart indexes require.
type PhotosensitivityFailure struct {
// Kind is "flash" for more than three flashes in one second, or "pattern" for a high-contrast stripe pattern.
Kind string
// FirstFrame is the index of the first frame of the failing range.
FirstFrame int
// LastFrame is the index of the last frame of the failing range.
LastFrame int
// Summary describes the failure for a test log.
Summary string
}PhotosensitivityFailure describes a detected flash or high-contrast pattern.
type PhotosensitivityOptions struct{ FrameRate float64 }PhotosensitivityOptions controls frame analysis. FrameRate defaults to 60 when zero.
type PhotosensitivityReport struct {
// Failures lists the flash failure, if any, and every frame with a stripe pattern.
Failures []PhotosensitivityFailure
}PhotosensitivityReport contains failures found in a sequence of rendered frames.
func AnalyzePhotosensitivity(frames []image.Image, opts PhotosensitivityOptions) PhotosensitivityReportAnalyzePhotosensitivity checks rendered frames for repeated full-frame flashes and high-contrast stripe patterns.
A flash is a pair of opposing changes in the frame's mean relative luminance of at least 0.1, where the darker state is below 0.8; more than three flashes within one second of frames fail, and only the first failing second is reported. A frame whose horizontally adjacent pixels differ in relative luminance by at least 0.5 across 20% of the frame fails as a pattern.
type RenderOptions struct {
// Background fills the output image before replaying ops.
// The zero value leaves the canvas transparent.
Background color.RGBA
// Images resolves core.ImageSource.URI values referenced by DrawImage ops
// when neither Pixels nor Data is set on the ImageSource.
Images map[string]image.Image
// ColorVision, when set to a non-normal mode (such as a11y.VisionProtanopia,
// a11y.VisionDeuteranopia, or a11y.VisionTritanopia), passes the rasterized
// frame through the color-vision filter so tests can render goldens under
// deficiency simulation or daltonization correction.
ColorVision a11y.ColorVision
// ColorVisionSeverity sets the filter strength in [0, 1]. When zero and
// ColorVision is active, it defaults to 1.0 (full deficiency).
ColorVisionSeverity float64
// ColorVisionCorrect applies daltonization correction instead of simulation
// when ColorVision is active.
ColorVisionCorrect bool
}RenderOptions configures optional inputs for RenderOps.