frosttest

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: BSD-3-Clause Imports: 14 Imported by: 0

README

This package contains things which are useful for testing code which runs under Ebitengine.

matchesImage.go contains a MatchesImage function which takes a *testing.T, an image name, and an image, and compares the image to the expected output (stored as a .png file in testdata/expected). If it fails to match, or the expected image is missing, it reports a failure to the *testing.T, attempts to write the failed image to testdata/failed (creating the folder if it doesn't exist), and returns false. If it matches, it returns true. It accepts both regular images and *ebiten.Images.

test.go enables testing things under Ebitengine. To do so, you first need a main_test.go file containing:

func TestMain(m *testing.M) {
	frosttest.OnTestMain(m)
}

And then for each test file you have which needs to test something under ebitengine (in Draw or Update or Layout), you can write tests like so:

func Test_SomeTests(t *testing.T) {
	frosttest.QueueLayoutTest(t, test_SomeLayoutTest)
	frosttest.QueueUpdateTest(t, test_SomeUpdateTest)
	frosttest.QueueDrawTest(t, test_SomeDrawTest)	
}

func test_SomeLayoutTest(t *testing.T, outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	// Your actual test here. Also return screen dimensions for the Layout function to return:
	return outsideWidth, outsideHeight // you can return something else if you like
}

func test_SomeUpdateTest(t *testing.T) {
	// Your actual test here
}

func test_SomeDrawTest(t *testing.T, screen *ebiten.Image) {
	// Your actual test here
}

You can have multiple tests per file, of course, and they can each queue as many things as they want.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchesImage

func MatchesImage(t *testing.T, imageName string, img image.Image) bool

MatchesImage compares an image.Image to "testdata/expected/<imageName>.png". If img is not nil, it attempts to open "testdata/expected/<imageName>.png". If it succeeds, it converts it to an image.Image, and then compares the two images. If it fails, it writes the image to "testdata/failed/<imageName>.png" and raises a test failure. It can handle *ebiten.Images and save them as PNGs. Also returns true if the images match, and false if they don't.

func OnTestMain

func OnTestMain(m *testing.M)

func QueueDrawTest

func QueueDrawTest(t *testing.T, f func(t *testing.T, screen *ebiten.Image))

func QueueLayoutTest

func QueueLayoutTest(t *testing.T, f func(t *testing.T, outsideWidth, outsideHeight int) (screenWidth, screenHeight int))

func QueueUpdateTest

func QueueUpdateTest(t *testing.T, f func(t *testing.T))

Types

type DrawTest

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

type DrawTestFunc

type DrawTestFunc func(t *testing.T, screen *ebiten.Image)

type LayoutTest

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

type LayoutTestFunc

type LayoutTestFunc func(t *testing.T, outsideWidth, outsideHeight int) (screenWidth, screenHeight int)

type TestGame

type TestGame struct{}

func (*TestGame) Draw

func (game *TestGame) Draw(screen *ebiten.Image)

func (*TestGame) Layout

func (game *TestGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int)

func (*TestGame) Update

func (game *TestGame) Update() (err error)

type UpdateTest

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

type UpdateTestFunc

type UpdateTestFunc func(t *testing.T)

Jump to

Keyboard shortcuts

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