Skip to content

Commit bbc5701

Browse files
committed
test: Add unit tests for pisnap package
CaptureOrErr works only on non js OS.
1 parent 909f0aa commit bbc5701

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

pisnap/pisnap.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ package pisnap
77
import (
88
"errors"
99
"fmt"
10-
"github.com/elgopher/pi"
1110
"image"
1211
"image/color"
1312
"image/png"
1413
"os"
1514
"runtime"
15+
16+
"github.com/elgopher/pi"
1617
)
1718

1819
// CaptureOrErr captures a screenshot and saves it to the temporary directory.

pisnap/pisnap_js_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
//go:build js
5+
6+
package pisnap_test
7+
8+
import (
9+
"testing"
10+
11+
"github.com/elgopher/pi/pisnap"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestCaptureOrErr(t *testing.T) {
17+
file, err := pisnap.CaptureOrErr()
18+
assert.Error(t, err)
19+
assert.Empty(t, file)
20+
}

pisnap/pisnap_nonjs_test.go

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+
//go:build !js
5+
6+
package pisnap_test
7+
8+
import (
9+
"bytes"
10+
"image"
11+
"image/png"
12+
"os"
13+
"testing"
14+
15+
"github.com/elgopher/pi"
16+
"github.com/elgopher/pi/pisnap"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
)
20+
21+
func TestCaptureOrErr(t *testing.T) {
22+
// when
23+
file, err := pisnap.CaptureOrErr()
24+
// then
25+
require.NoError(t, err)
26+
assert.NotEmpty(t, file)
27+
// and file exists
28+
f, err := os.ReadFile(file)
29+
require.NoError(t, err, "cannot read PNG file")
30+
// and
31+
img, err := png.Decode(bytes.NewReader(f))
32+
require.NoError(t, err, "file is not a valid PNG")
33+
// and
34+
palettedImage, ok := img.(*image.Paletted)
35+
require.True(t, ok, "image is not a Paletted")
36+
assertPalettedImage(t, palettedImage, pi.Screen())
37+
}

pisnap/pisnap_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2025 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package pisnap_test
5+
6+
import (
7+
"image"
8+
"testing"
9+
10+
"github.com/elgopher/pi"
11+
"github.com/elgopher/pi/pisnap"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestPalettedImage(t *testing.T) {
16+
pi.SetScreenSize(2, 3)
17+
pi.Palette[1] = 0xffaa44
18+
pi.Palette[2] = 0xff0000
19+
pi.Palette[3] = 0x00ff00
20+
pi.Palette[4] = 0x0000ff
21+
pi.Palette[5] = 0x00ffff
22+
pi.Palette[6] = 0xff00ff
23+
screen := pi.Screen()
24+
screen.SetAll(1, 2, 3, 4, 5, 6)
25+
// when
26+
img := pisnap.PalettedImage()
27+
// then
28+
assertPalettedImage(t, img, screen)
29+
}
30+
31+
func assertPalettedImage(t *testing.T, img image.PalettedImage, screen pi.Canvas) {
32+
t.Helper()
33+
34+
// then color indexes are the same
35+
for y := 0; y < screen.H(); y++ {
36+
for x := 0; x < screen.W(); x++ {
37+
actual := img.ColorIndexAt(x, y)
38+
expected := screen.Get(x, y)
39+
assert.Equal(t, expected, actual)
40+
}
41+
}
42+
// and RGBA colors match
43+
for y := 0; y < screen.H(); y++ {
44+
for x := 0; x < screen.W(); x++ {
45+
r, g, b, a := img.At(x, y).RGBA()
46+
assert.Equal(t, uint8(0xff), uint8(a))
47+
actual := pi.FromRGB(uint8(r), uint8(g), uint8(b))
48+
expected := pi.Palette[screen.Get(x, y)]
49+
assert.Equal(t, expected, actual)
50+
}
51+
}
52+
// and size is the same
53+
assert.Equal(t,
54+
image.Rectangle{
55+
Max: image.Point{X: screen.W(), Y: screen.H()},
56+
},
57+
img.Bounds(),
58+
"image size is not same as screen size",
59+
)
60+
}

0 commit comments

Comments
 (0)