-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_test.go
241 lines (212 loc) · 5.86 KB
/
pi_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package pi_test
import (
_ "embed"
"fmt"
"strconv"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/elgopher/pi"
"github.com/elgopher/pi/image"
)
//go:embed internal/testimage/custom-font.png
var customFont []byte
func TestReset(t *testing.T) {
t.Run("should reset sprite sheet", func(t *testing.T) {
pi.UseEmptySpriteSheet(8, 8)
// when
pi.Reset()
// then
sprSheet := pi.SprSheet()
assert.Equal(t, 128, sprSheet.Width())
assert.Equal(t, 128, sprSheet.Height())
assert.Equal(t, make([]byte, 16384), sprSheet.Pix())
})
t.Run("should reset screen", func(t *testing.T) {
pi.SetScreenSize(256, 256)
// when
pi.Reset()
// then
scr := pi.Scr()
assert.Equal(t, 128, scr.Width())
assert.Equal(t, 128, scr.Height())
assert.Equal(t, make([]byte, 16384), scr.Pix())
assert.Zero(t, pi.Camera)
assert.Equal(t, pi.Region{W: 128, H: 128}, scr.Clip())
})
t.Run("should reset palette", func(t *testing.T) {
color := image.RGB{R: 0xff, G: 0xff, B: 0xff}
pi.Palette[0] = color
pi.Reset()
assert.NotEqual(t, color, pi.Palette[0])
})
t.Run("should reset display palette", func(t *testing.T) {
pi.Pald[0] = 255
pi.Reset()
assert.NotEqual(t, 255, pi.Pald[0])
})
t.Run("should reset draw palette", func(t *testing.T) {
pi.Pal[0] = 255
pi.Reset()
assert.NotEqual(t, 255, pi.Pal[0])
})
t.Run("should reset palette transparency", func(t *testing.T) {
pi.Palt[0] = false
pi.Reset()
assert.True(t, pi.Palt[0])
})
t.Run("should reset system font", func(t *testing.T) {
before := pi.SystemFont().Data[0]
after := before + 1
pi.SystemFont().Data[0] = after
// when
pi.Reset()
// then
assert.Equal(t, before, pi.SystemFont().Data[0])
})
t.Run("should reset custom font", func(t *testing.T) {
pi.CustomFont().Data[0] = 1
pi.SetCustomFontWidth(0)
pi.SetCustomFontSpecialWidth(0)
pi.SetCustomFontHeight(0)
// when
pi.Reset()
// then
expected := pi.Font{
Data: make([]byte, 8*256),
Width: 4,
SpecialWidth: 8,
Height: 6,
}
assert.Equal(t, expected, pi.CustomFont())
})
t.Run("should reset callbacks", func(t *testing.T) {
pi.Draw = func() {}
pi.Update = func() {}
pi.Reset()
assert.Nil(t, pi.Draw)
assert.Nil(t, pi.Update)
})
}
func TestSetScreenSize(t *testing.T) {
invalidScreenSizes := [...]int{-2, -1, 0}
t.Run("should panic when ScreenWidth is not greater than 0", func(t *testing.T) {
for _, size := range invalidScreenSizes {
t.Run(strconv.Itoa(size), func(t *testing.T) {
pi.Reset()
assert.Panics(t, func() {
pi.SetScreenSize(size, pi.Scr().Height())
})
})
}
})
t.Run("should panic when ScreenHeight is not greater than 0", func(t *testing.T) {
for _, size := range invalidScreenSizes {
t.Run(strconv.Itoa(size), func(t *testing.T) {
pi.Reset()
assert.Panics(t, func() {
pi.SetScreenSize(pi.Scr().Width(), size)
})
})
}
})
t.Run("should panic when total number of pixels is higher than 65536", func(t *testing.T) {
tests := []struct{ w, h int }{
{w: 65537, h: 1},
{w: 1, h: 65537},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%dx%d", test.w, test.h), func(t *testing.T) {
pi.Reset()
assert.Panics(t, func() {
pi.SetScreenSize(test.w, test.h)
})
})
}
})
t.Run("should not panic when total number of pixels is lower/equal than 65536", func(t *testing.T) {
tests := []struct{ w, h int }{
{w: 1024, h: 64},
{w: 256, h: 256},
{w: 320, h: 200},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%dx%d", test.w, test.h), func(t *testing.T) {
pi.Reset()
assert.NotPanics(t, func() {
pi.SetScreenSize(test.w, test.h)
})
})
}
})
t.Run("should initialize screen data", func(t *testing.T) {
pi.Reset()
// when
pi.SetScreenSize(2, 3)
// then
assert.Equal(t, make([]byte, 6), pi.Scr().Pix())
})
}
func TestLoad(t *testing.T) {
const color = 7
t.Run("should load sprite-sheet.png", func(t *testing.T) {
pi.Reset()
// when
pi.Load(fstest.MapFS{
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
})
// then
assert.Equal(t, 16, pi.SprSheet().Width())
assert.Equal(t, 16, pi.SprSheet().Height())
img := decodePNG(t, "internal/testimage/sprite-sheet-16x16.png")
assert.Equal(t, img.Pix, pi.SprSheet().Pix())
assert.Equal(t, img.Palette, pi.Palette)
})
t.Run("should load custom-font.png", func(t *testing.T) {
pi.Reset()
// when
pi.Load(fstest.MapFS{
"custom-font.png": &fstest.MapFile{Data: customFont},
})
// then
assert.Equal(t, uint8(0xf), pi.CustomFont().Data[0])
})
t.Run("should use sprite-sheet size loaded from sprite-sheet.png", func(t *testing.T) {
pi.UseEmptySpriteSheet(32, 32) // 2x times bigger than actual sprite-sheet.png width
pi.Load(fstest.MapFS{
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
})
assert.NotPanics(t, func() {
pi.Spr(4, 0, 0) // sprite-sheet.png has only 4 sprites (from 0 to 3)
pi.SprSize(4, 0, 0, 1.0, 1.0)
pi.SprSizeFlip(4, 0, 0, 1.0, 1.0, false, false)
pi.Set(16, 16, color) // sprite-sheet.png is only 16x16 pixels (0..15)
pi.Get(16, 16)
})
})
t.Run("should panic if sprite-sheet size is not multiple of 8", func(t *testing.T) {
assert.Panics(t, func() {
pi.Load(fstest.MapFS{
"sprite-sheet.png": &fstest.MapFile{Data: invalidSpriteSheetWidth},
})
})
assert.Panics(t, func() {
pi.Load(fstest.MapFS{
"sprite-sheet.png": &fstest.MapFile{Data: invalidSpriteSheetHeight},
})
})
})
t.Run("should panic when sprite-sheet total number of pixels is higher than 65536", func(t *testing.T) {
pi.Reset()
assert.Panics(t, func() {
pi.UseEmptySpriteSheet(264, 256)
})
})
}
func TestTime(t *testing.T) {
t.Run("should return 0.0 when game was not run", func(t *testing.T) {
assert.Zero(t, pi.Time)
})
}