Skip to content

Commit f6264d8

Browse files
committed
test: piebiten.CopyCanvasToEbitenImage
Add basic integration tests for piebiten.CopyCanvasToEbitenImage
1 parent 51b7cc6 commit f6264d8

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

palettemap.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// the contents of the Screen to the game window.
1515
//
1616
// The array index is the original color, and the value is the mapped color.
17-
var PaletteMapping PaletteMap = notSwappedPaletteMapping
17+
var PaletteMapping PaletteMap = notRemappedPalette
1818

1919
// PaletteMap defines the color mapping for display.
2020
//
@@ -32,10 +32,10 @@ func (p PaletteMap) String() string {
3232
}
3333

3434
func ResetPaletteMapping() {
35-
PaletteMapping = notSwappedPaletteMapping
35+
PaletteMapping = notRemappedPalette
3636
}
3737

38-
var notSwappedPaletteMapping = func() PaletteMap {
38+
var notRemappedPalette = func() PaletteMap {
3939
var m PaletteMap
4040
for i := 0; i < MaxColors; i++ {
4141
m[i] = Color(i)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package ebitentesting
5+
6+
import (
7+
"os"
8+
"testing"
9+
10+
"github.com/hajimehoshi/ebiten/v2"
11+
)
12+
13+
type gameWithOneUpdate struct {
14+
m *testing.M
15+
code int
16+
}
17+
18+
func (g *gameWithOneUpdate) Update() error {
19+
g.code = g.m.Run()
20+
return ebiten.Termination
21+
}
22+
23+
func (*gameWithOneUpdate) Draw(*ebiten.Image) {}
24+
25+
func (*gameWithOneUpdate) Layout(int, int) (int, int) {
26+
return 1, 1
27+
}
28+
29+
// MainWithRunLoop should be Run from TestMain(*testing.M) in order to
30+
// run all tests inside Ebitengine's game loop
31+
func MainWithRunLoop(m *testing.M) {
32+
g := &gameWithOneUpdate{m: m, code: 1}
33+
if err := ebiten.RunGame(g); err != nil {
34+
panic(err) // RunGame does not return an error for ebiten.Termination
35+
}
36+
os.Exit(g.code)
37+
}

piebiten/piebiten_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package piebiten_test
5+
6+
import (
7+
"github.com/elgopher/pi"
8+
"github.com/elgopher/pi/piebiten"
9+
"github.com/elgopher/pi/piebiten/internal/ebitentesting"
10+
"github.com/hajimehoshi/ebiten/v2"
11+
"github.com/stretchr/testify/assert"
12+
"testing"
13+
)
14+
15+
func TestMain(m *testing.M) {
16+
ebitentesting.MainWithRunLoop(m)
17+
}
18+
19+
func TestCopyCanvasToEbitenImage(t *testing.T) {
20+
t.Run("should copy canvas to Ebiten image", func(t *testing.T) {
21+
canvas := pi.NewCanvas(3, 2)
22+
pi.Palette[0] = 0xFF0000
23+
pi.Palette[1] = 0x00FF00
24+
pi.Palette[2] = 0x0000FF
25+
pi.Palette[3] = 0x00FFFF
26+
pi.Palette[4] = 0xFFFF00
27+
pi.Palette[5] = 0xFF00FF
28+
canvas.SetAll(
29+
0, 1, 2,
30+
3, 4, 5,
31+
)
32+
img := ebiten.NewImage(3, 2)
33+
// when
34+
piebiten.CopyCanvasToEbitenImage(canvas, img)
35+
// then
36+
bounds := img.Bounds()
37+
out := make([]byte, bounds.Dx()*bounds.Dy()*4)
38+
img.ReadPixels(out)
39+
for i := 0; i < 6; i++ {
40+
pix := out[i*4 : i*4+4]
41+
rgb := pi.FromRGB(pix[0], pix[1], pix[2])
42+
assert.Equal(t, pi.Palette[i], rgb, "pixel %d", i)
43+
assert.Equal(t, uint8(0xff), pix[3], "alpha for pixel %d", i)
44+
}
45+
})
46+
47+
t.Run("should take into account palette mapping", func(t *testing.T) {
48+
canvas := pi.NewCanvas(1, 1)
49+
pi.Palette[0] = 0x000000
50+
pi.Palette[1] = 0xFFFFFF
51+
pi.PaletteMapping[0] = 1 // map 0 to 1 (0x000000 to 0xFFFFFF)
52+
img := ebiten.NewImage(1, 1)
53+
// when
54+
piebiten.CopyCanvasToEbitenImage(canvas, img)
55+
// then
56+
bounds := img.Bounds()
57+
out := make([]byte, bounds.Dx()*bounds.Dy()*4)
58+
img.ReadPixels(out)
59+
pix := out[0:4]
60+
rgb := pi.FromRGB(pix[0], pix[1], pix[2])
61+
assert.Equal(t, pi.RGB(0xFFFFFF), rgb)
62+
})
63+
}

0 commit comments

Comments
 (0)