Skip to content

Commit d8b6612

Browse files
committed
[refactor] Reuse booting code in unit tests
1 parent a9d212f commit d8b6612

File tree

1 file changed

+18
-52
lines changed

1 file changed

+18
-52
lines changed

screen_test.go

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,7 @@ func testSpr(t *testing.T, spr func(spriteNo int, x int, y int)) {
409409
}
410410
for name, test := range tests {
411411
t.Run(name, func(t *testing.T) {
412-
pi.ScreenWidth = 8
413-
pi.ScreenHeight = 8
414-
pi.Resources = fstest.MapFS{
415-
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
416-
}
417-
pi.BootOrPanic()
412+
boot(8, 8, spriteSheet16x16)
418413
expectedScreen := decodePNG(t, "internal/testimage/"+test.expectedScreenFile)
419414
// when
420415
pi.Camera(test.cameraX, test.cameraY)
@@ -439,12 +434,7 @@ func testSpr(t *testing.T, spr func(spriteNo int, x int, y int)) {
439434
}
440435
for name, test := range tests {
441436
t.Run(name, func(t *testing.T) {
442-
pi.ScreenWidth = 8
443-
pi.ScreenHeight = 8
444-
pi.Resources = fstest.MapFS{
445-
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
446-
}
447-
pi.BootOrPanic()
437+
boot(8, 8, spriteSheet16x16)
448438
expectedScreen := decodePNG(t, "internal/testimage/"+test.expectedScreenFile)
449439
// when
450440
pi.Clip(test.clipX, test.clipY, test.clipW, test.clipH)
@@ -456,13 +446,7 @@ func testSpr(t *testing.T, spr func(spriteNo int, x int, y int)) {
456446
})
457447

458448
t.Run("should not draw color 0 by default", func(t *testing.T) {
459-
pi.Reset()
460-
pi.ScreenWidth = 8
461-
pi.ScreenHeight = 8
462-
pi.Resources = fstest.MapFS{
463-
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
464-
}
465-
pi.BootOrPanic()
449+
boot(8, 8, spriteSheet16x16)
466450
spr(2, 0, 0)
467451
// when
468452
spr(1, 0, 0)
@@ -472,13 +456,7 @@ func testSpr(t *testing.T, spr func(spriteNo int, x int, y int)) {
472456
})
473457

474458
t.Run("should not draw color 0 after PaltReset", func(t *testing.T) {
475-
pi.Reset()
476-
pi.ScreenWidth = 8
477-
pi.ScreenHeight = 8
478-
pi.Resources = fstest.MapFS{
479-
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
480-
}
481-
pi.BootOrPanic()
459+
boot(8, 8, spriteSheet16x16)
482460
spr(2, 0, 0)
483461
pi.Palt(0, false) // make color 0 opaque
484462
// when
@@ -490,13 +468,7 @@ func testSpr(t *testing.T, spr func(spriteNo int, x int, y int)) {
490468
})
491469

492470
t.Run("should not draw transparent colors", func(t *testing.T) {
493-
pi.Reset()
494-
pi.ScreenWidth = 8
495-
pi.ScreenHeight = 8
496-
pi.Resources = fstest.MapFS{
497-
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
498-
}
499-
pi.BootOrPanic()
471+
boot(8, 8, spriteSheet16x16)
500472
spr(2, 0, 0)
501473
// when
502474
pi.Palt(0, false)
@@ -568,12 +540,7 @@ func testSprSize(t *testing.T, sprSize func(spriteNo int, x, y int, w, h float64
568540
}
569541
for name, test := range tests {
570542
t.Run(name, func(t *testing.T) {
571-
pi.ScreenWidth = 16
572-
pi.ScreenHeight = 16
573-
pi.Resources = fstest.MapFS{
574-
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
575-
}
576-
pi.BootOrPanic()
543+
boot(16, 16, spriteSheet16x16)
577544
expectedScreen := decodePNG(t, "internal/testimage/"+test.expectedScreenFile)
578545
// when
579546
sprSize(test.spriteNo, test.x, test.y, test.w, test.h)
@@ -603,12 +570,7 @@ func TestSprSizeFlip(t *testing.T) {
603570
}
604571
for name, test := range tests {
605572
t.Run(name, func(t *testing.T) {
606-
pi.ScreenWidth = 8
607-
pi.ScreenHeight = 8
608-
pi.Resources = fstest.MapFS{
609-
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
610-
}
611-
pi.BootOrPanic()
573+
boot(8, 8, spriteSheet16x16)
612574
expectedScreen := decodePNG(t, "internal/testimage/"+test.expectedScreenFile)
613575
// when
614576
pi.SprSizeFlip(0, 0, 0, 1.0, test.h, test.flipX, test.flipY)
@@ -619,13 +581,7 @@ func TestSprSizeFlip(t *testing.T) {
619581
})
620582

621583
t.Run("should not draw transparent colors", func(t *testing.T) {
622-
pi.Reset()
623-
pi.ScreenWidth = 8
624-
pi.ScreenHeight = 8
625-
pi.Resources = fstest.MapFS{
626-
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet16x16},
627-
}
628-
pi.BootOrPanic()
584+
boot(8, 8, spriteSheet16x16)
629585
pi.SprSizeFlip(2, 0, 0, 1.0, 1.0, true, false)
630586
// when
631587
pi.Palt(0, false)
@@ -654,3 +610,13 @@ func decodePNG(t *testing.T, file string) image.Image {
654610
require.NoError(t, err)
655611
return data
656612
}
613+
614+
func boot(screenWidth, screenHeight int, spriteSheet []byte) {
615+
pi.Reset()
616+
pi.ScreenWidth = screenWidth
617+
pi.ScreenHeight = screenHeight
618+
pi.Resources = fstest.MapFS{
619+
"sprite-sheet.png": &fstest.MapFile{Data: spriteSheet},
620+
}
621+
pi.BootOrPanic()
622+
}

0 commit comments

Comments
 (0)