-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples_test.go
219 lines (175 loc) · 5.04 KB
/
examples_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
package chessImager
import (
"os"
"testing"
"gopkg.in/freeeve/pgn.v1"
)
func TestSimpleExample(t *testing.T) {
t.Parallel()
filename := "simple.png"
const fen = "b2r3r/k3Rp1p/p2q1np1/Np1P4/3p1Q2/P4PPB/1PP4P/1K6 b - - 1 25"
img, err := NewImager().Render(fen)
if err != nil {
t.Errorf("failed to render : %v", err)
}
if img == nil {
t.Errorf("image is nil")
}
compareImages(t, filename, &img)
}
func TestMediumExample(t *testing.T) {
t.Parallel()
filename := "medium.png"
// Create a new imager using embedded default.json settings
imager := NewImager()
// Create a new image context
ctx := imager.NewContext("b2r3r/k3Rp1p/p2q1np1/Np1P4/3p1Q2/P4PPB/1PP4P/1K6 b - - 1 25")
// Highlight square e7
// Annotate square e7 with "!!"
// Show move e1-e7
ctx.AddHighlight("e7").AddAnnotation("e7", "!!").AddMove("e1", "e7")
// Render the image
img, err := imager.RenderWithContext(ctx)
if err != nil {
t.Errorf("failed to render : %v", err)
}
if img == nil {
t.Errorf("image is nil")
}
compareImages(t, filename, &img)
}
func TestAdvancedExample(t *testing.T) {
t.Parallel()
filename := "advanced.png"
// Create a new imager using embedded default.json settings
imager := NewImager()
// Set the rendering order
err := imager.SetOrder([]int{0, 1, 2, 3, 5, 4, 6})
if err != nil {
t.Errorf("failed to set rendering order : %v", err)
}
// Create a new image context
const fen = "b2r3r/k3Rp1p/p2q1np1/Np1P4/3p1Q2/P4PPB/1PP4P/1K6 b - - 1 25"
ctx := imager.NewContext(fen)
// Create a highlight style, for the square e7
hs, err := ctx.NewHighlightStyle(
HighlightTypeFilledCircle, // Highlight type
"#88E57C", // Highlight color
4, // Highlight circle width
0.9, // Highlight factor (not used for this Type)
)
if err != nil {
t.Errorf("failed to create highlight style : %v", err)
}
// Create an annotation style, for the square e7
as, err := ctx.NewAnnotationStyle(
PositionTypeTopLeft, // Position
25, 20, 1, // Size, font size, border width
"#E8E57C", "#000000", "#FFFFFF", // Background, font, border color
)
if err != nil {
t.Errorf("failed to create annotation style : %v", err)
}
// Create a move style, for the move e1-e7
ms, err := ctx.NewMoveStyle(
MoveTypeDots, // Move type
"#9D6B5EFF", // Dot color
"#9D6B5EFF", // Dot color 2
0.2, // Dot size
0, // Padding
)
if err != nil {
t.Errorf("failed to create move style : %v", err)
}
// Highlight the e7 square, annotate e7 as a brilliant move (!!) and
// show move e1-e7.
ctx.AddHighlightWithStyle("e7", hs).AddAnnotationWithStyle("e7", "!!", as).AddMoveWithStyle("e1", "e7", ms)
// Render the image
img, err := imager.RenderWithContext(ctx)
if err != nil {
t.Errorf("failed to render : %v", err)
}
if img == nil {
t.Errorf("image is nil")
}
compareImages(t, filename, &img)
}
func TestOtherExample(t *testing.T) {
t.Parallel()
filename := "other.png"
// Create a new imager using your custom JSON file
imager, err := NewImagerFromPath("test/data/other.json")
if err != nil {
t.Errorf("failed to create imager : %v", err)
}
// Create a new image context and highlight the e7 square, annotate e7 as a
// brilliant move (!!) and show move e1-e7.
const fen = "b2r3r/k3Rp1p/p2q1np1/Np1P4/3p1Q2/P4PPB/1PP4P/1K6 b - - 1 25"
ctx := imager.NewContext(fen).
AddHighlight("e7").
AddAnnotation("e7", "!!").
AddMove("e1", "e7")
// Render the image
img, err := imager.RenderWithContext(ctx)
if err != nil {
t.Errorf("failed to render : %v", err)
}
if img == nil {
t.Errorf("image is nil")
}
compareImages(t, filename, &img)
}
func TestCastlingExample(t *testing.T) {
t.Parallel()
filename := "castling.png"
// Create a new imager using embedded default.json settings
imager := NewImager()
// Create a new image context, and add white king side castling,
// and black queen side castling.
ctx := imager.NewContext("2kr4/8/8/8/8/8/8/5RK1 b - - 1 25").AddMove("0-0", "").AddMove("", "0-0-0")
// Render the image
img, err := imager.RenderWithContext(ctx)
if err != nil {
t.Errorf("failed to render : %v", err)
}
if img == nil {
t.Errorf("image is nil")
}
compareImages(t, filename, &img)
}
func TestPGNExample(t *testing.T) {
t.Parallel()
imager := NewImager()
f, err := os.Open("./test/data/game.pgn")
if err != nil {
t.Errorf("failed to open PGN file : %v", err)
}
ps := pgn.NewPGNScanner(f)
for ps.Next() {
game, err := ps.Scan()
if err != nil {
t.Errorf("failed to scan PGN file : %v", err)
}
b := pgn.NewBoard()
i := 1
for _, move := range game.Moves {
// Let's just test the first 10 moves
if i > 10 {
continue
}
_ = b.MakeMove(move)
ctx := imager.NewContext(b.String()).
AddMove(move.From.String(), move.To.String()).
AddHighlight(move.From.String()).
AddHighlight(move.To.String())
img, err := imager.RenderWithContext(ctx)
if err != nil {
t.Errorf("failed to render image %d : %v", i, err)
}
if img == nil {
t.Errorf("image is nil")
}
i++
}
}
}