Line data Source code
1 : package vrt
2 :
3 : import (
4 : "fmt"
5 : "os"
6 : "testing"
7 :
8 : "github.com/hajimehoshi/ebiten/v2"
9 : )
10 :
11 : // testHostGame はテスト実行中にebitenグラフィックスコンテキストを維持するゲーム
12 : type testHostGame struct {
13 : testFunc func() int
14 : started bool
15 : }
16 :
17 : func (g *testHostGame) Update() error {
18 1 : if !g.started {
19 1 : g.started = true
20 1 : go func() {
21 : // ゲームループが回っている状態で、テストは別goroutineとして実行する
22 1 : code := g.testFunc()
23 1 : // テスト完了後、ebitenのシャットダウン処理でEGL/X11ドライバがsegfaultすることがあるため、
24 : // テスト結果が確定した時点で即座にプロセスを終了する
25 0 : os.Exit(code)
26 0 : }()
27 : }
28 1 : return nil
29 : }
30 :
31 1 : func (g *testHostGame) Draw(_ *ebiten.Image) {}
32 :
33 : func (g *testHostGame) Layout(_, _ int) (int, int) {
34 1 : return 1, 1
35 1 : }
36 :
37 : // RunTestMain はTestMainから呼び出し、ebitenゲームループ内で全テストを実行する。
38 : // これにより各テスト関数で ebiten.NewImage + ReadPixels が使える。
39 : // gamepad初期化のEINTR回避のため、bwrapで/dev/inputを隠した環境で実行すること
40 : func RunTestMain(m *testing.M) int {
41 1 : g := &testHostGame{testFunc: m.Run}
42 1 : // ebiten.RunGame()はメインスレッドをブロックする。
43 1 : if err := ebiten.RunGame(g); err != nil {
44 0 : fmt.Fprintf(os.Stderr, "RunGame failed: %v\n", err)
45 0 : return 1
46 0 : }
47 : // テストが正常完了した場合、goroutine内でos.Exitが呼ばれるためここには到達しない
48 0 : return 0
49 : }
|