Add testing package

This commit is contained in:
Hajime Hoshi 2020-04-01 18:03:08 +09:00
parent 9302160486
commit 47d5c3b5e1
5 changed files with 49 additions and 68 deletions

View File

@ -15,32 +15,17 @@
package graphicscommand_test package graphicscommand_test
import ( import (
"errors"
"image/color" "image/color"
"os"
"testing" "testing"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
. "github.com/hajimehoshi/ebiten/internal/graphicscommand" . "github.com/hajimehoshi/ebiten/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/internal/testflock" t "github.com/hajimehoshi/ebiten/internal/testing"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
testflock.Lock() t.MainWithRunLoop(m)
defer testflock.Unlock()
code := 0
regularTermination := errors.New("regular termination")
f := func(screen *ebiten.Image) error {
code = m.Run()
return regularTermination
}
if err := ebiten.Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
panic(err)
}
os.Exit(code)
} }
func quadVertices(w, h float32) []float32 { func quadVertices(w, h float32) []float32 {

View File

@ -15,34 +15,19 @@
package restorable_test package restorable_test
import ( import (
"errors"
"image" "image"
"image/color" "image/color"
"os"
"testing" "testing"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
. "github.com/hajimehoshi/ebiten/internal/restorable" . "github.com/hajimehoshi/ebiten/internal/restorable"
"github.com/hajimehoshi/ebiten/internal/testflock" t "github.com/hajimehoshi/ebiten/internal/testing"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
testflock.Lock()
defer testflock.Unlock()
EnableRestoringForTesting() EnableRestoringForTesting()
code := 0 t.MainWithRunLoop(m)
regularTermination := errors.New("regular termination")
f := func(screen *ebiten.Image) error {
code = m.Run()
return regularTermination
}
if err := ebiten.Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
panic(err)
}
os.Exit(code)
} }
func pixelsToColor(p *Pixels, i, j int) color.RGBA { func pixelsToColor(p *Pixels, i, j int) color.RGBA {

View File

@ -15,33 +15,18 @@
package shareable_test package shareable_test
import ( import (
"errors"
"image/color" "image/color"
"os"
"runtime" "runtime"
"testing" "testing"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
. "github.com/hajimehoshi/ebiten/internal/shareable" . "github.com/hajimehoshi/ebiten/internal/shareable"
"github.com/hajimehoshi/ebiten/internal/testflock" t "github.com/hajimehoshi/ebiten/internal/testing"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
testflock.Lock() t.MainWithRunLoop(m)
defer testflock.Unlock()
code := 0
regularTermination := errors.New("regular termination")
f := func(screen *ebiten.Image) error {
code = m.Run()
return regularTermination
}
if err := ebiten.Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
panic(err)
}
os.Exit(code)
} }
func quadVertices(sw, sh, x, y int, scalex float32) []float32 { func quadVertices(sw, sh, x, y int, scalex float32) []float32 {

View File

@ -0,0 +1,41 @@
// Copyright 2020 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testing
import (
"errors"
"os"
"testing"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/testflock"
)
func MainWithRunLoop(m *testing.M) {
testflock.Lock()
defer testflock.Unlock()
code := 0
// Run an Ebiten process so that (*Image).At is available.
regularTermination := errors.New("regular termination")
f := func(screen *ebiten.Image) error {
code = m.Run()
return regularTermination
}
if err := ebiten.Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
panic(err)
}
os.Exit(code)
}

View File

@ -15,33 +15,18 @@
package text_test package text_test
import ( import (
"errors"
"image/color" "image/color"
"os"
"testing" "testing"
"github.com/hajimehoshi/bitmapfont" "github.com/hajimehoshi/bitmapfont"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/testflock" t "github.com/hajimehoshi/ebiten/internal/testing"
. "github.com/hajimehoshi/ebiten/text" . "github.com/hajimehoshi/ebiten/text"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
testflock.Lock() t.MainWithRunLoop(m)
defer testflock.Unlock()
code := 0
// Run an Ebiten process so that (*Image).At is available.
regularTermination := errors.New("regular termination")
f := func(screen *ebiten.Image) error {
code = m.Run()
return regularTermination
}
if err := ebiten.Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
panic(err)
}
os.Exit(code)
} }
func TestTextColor(t *testing.T) { func TestTextColor(t *testing.T) {