|
4 | 4 | package pifont_test |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + _ "embed" |
| 8 | + "github.com/elgopher/pi/pitest" |
7 | 9 | "testing" |
8 | 10 |
|
9 | 11 | "github.com/elgopher/pi" |
10 | 12 | "github.com/elgopher/pi/pifont" |
11 | 13 | ) |
12 | 14 |
|
| 15 | +//go:embed internal/test/font.png |
| 16 | +var fontPNG []byte |
| 17 | + |
| 18 | +var fontSheet pifont.Sheet |
| 19 | + |
| 20 | +func init() { |
| 21 | + prevPalette := pi.Palette |
| 22 | + defer func() { |
| 23 | + pi.Palette = prevPalette |
| 24 | + }() |
| 25 | + |
| 26 | + pi.Palette = pi.DecodePalette(fontPNG) |
| 27 | + fontCanvas := pi.DecodeCanvas(fontPNG) |
| 28 | + fontSheet = pifont.Sheet{ |
| 29 | + Chars: map[rune]pi.Sprite{ |
| 30 | + 'S': { |
| 31 | + Area: pi.Area[int]{X: 0, Y: 0, W: 8, H: 8}, |
| 32 | + Source: fontCanvas, |
| 33 | + }, |
| 34 | + 'T': { |
| 35 | + Area: pi.Area[int]{X: 8, Y: 0, W: 8, H: 8}, |
| 36 | + Source: fontCanvas, |
| 37 | + }, |
| 38 | + '⬤': { |
| 39 | + Area: pi.Area[int]{X: 0, Y: 8, W: 8, H: 8}, |
| 40 | + Source: fontCanvas, |
| 41 | + }, |
| 42 | + '❤': { |
| 43 | + Area: pi.Area[int]{X: 8, Y: 8, W: 8, H: 8}, |
| 44 | + Source: fontCanvas, |
| 45 | + }, |
| 46 | + }, |
| 47 | + Height: 8, |
| 48 | + FgColor: 1, |
| 49 | + BgColor: 0, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +var ( |
| 54 | + //go:embed internal/test/text-color-equal-to-bg.png |
| 55 | + textColorEqualToBg []byte |
| 56 | + //go:embed internal/test/text-color-equal-to-fg.png |
| 57 | + textColorEqualToFg []byte |
| 58 | + //go:embed internal/test/text-color-different.png |
| 59 | + textColorDifferent []byte |
| 60 | +) |
| 61 | + |
| 62 | +func TestSheet_Print(t *testing.T) { |
| 63 | + t.Run("should print with different colors", func(t *testing.T) { |
| 64 | + tests := map[string]struct { |
| 65 | + bgColor pi.Color |
| 66 | + textColor pi.Color |
| 67 | + png []byte |
| 68 | + }{ |
| 69 | + "text color different than Bg and Fg": { |
| 70 | + bgColor: 0, |
| 71 | + textColor: 2, |
| 72 | + png: textColorDifferent, |
| 73 | + }, |
| 74 | + "text color equal to Bg": { |
| 75 | + bgColor: 2, |
| 76 | + textColor: 0, |
| 77 | + png: textColorEqualToBg, |
| 78 | + }, |
| 79 | + "text color equal to Fg": { |
| 80 | + bgColor: 0, |
| 81 | + textColor: 1, |
| 82 | + png: textColorEqualToFg, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for testName, testCase := range tests { |
| 87 | + t.Run(testName, func(t *testing.T) { |
| 88 | + pi.Palette = pi.DecodePalette(testCase.png) |
| 89 | + expectedCanvas := pi.DecodeCanvas(testCase.png) |
| 90 | + pi.SetScreenSize(8, 8) |
| 91 | + |
| 92 | + pi.Screen().Clear(testCase.bgColor) |
| 93 | + pi.SetColor(testCase.textColor) |
| 94 | + pi.Palt(testCase.textColor, false) |
| 95 | + // when |
| 96 | + fontSheet.Print("S", 0, 0) |
| 97 | + // then |
| 98 | + pitest.AssertSurfaceEqual(t, expectedCanvas, pi.Screen()) |
| 99 | + }) |
| 100 | + } |
| 101 | + }) |
| 102 | +} |
| 103 | + |
13 | 104 | func BenchmarkSheet_Print(b *testing.B) { |
14 | 105 | sheet := pifont.Sheet{ |
15 | 106 | Chars: map[rune]pi.Sprite{ |
|
0 commit comments