-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathalias_test.go
More file actions
324 lines (307 loc) · 8.56 KB
/
alias_test.go
File metadata and controls
324 lines (307 loc) · 8.56 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package charts
import (
"testing"
"github.com/golang/freetype/truetype"
"github.com/stretchr/testify/assert"
"github.com/go-analyze/charts/chartdraw/matrix"
)
func TestFillFontStyleDefaults(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input FontStyle
defaultSize float64
defaultColor Color
fontOptions []*truetype.Font
expected FontStyle
}{
{
name: "empty_font_style_gets_all_defaults",
input: FontStyle{},
defaultSize: 12,
defaultColor: ColorRed,
expected: FontStyle{
FontSize: 12,
FontColor: ColorRed,
Font: getPreferredFont(),
},
},
{
name: "partial_font_style_keeps_existing_values",
input: FontStyle{
FontSize: 16,
FontColor: ColorBlue,
},
defaultSize: 12,
defaultColor: ColorRed,
expected: FontStyle{
FontSize: 16, // keeps existing
FontColor: ColorBlue, // keeps existing
Font: getPreferredFont(),
},
},
{
name: "zero_font_size_gets_default",
input: FontStyle{
FontSize: 0,
FontColor: ColorGreen,
},
defaultSize: 14,
defaultColor: ColorRed,
expected: FontStyle{
FontSize: 14, // uses default
FontColor: ColorGreen, // keeps existing
Font: getPreferredFont(),
},
},
{
name: "zero_color_gets_default",
input: FontStyle{
FontSize: 18,
FontColor: Color{}, // zero color
},
defaultSize: 12,
defaultColor: ColorYellow,
expected: FontStyle{
FontSize: 18, // keeps existing
FontColor: ColorYellow, // uses default
Font: getPreferredFont(),
},
},
{
name: "existing_font_is_preserved",
input: FontStyle{
Font: &truetype.Font{}, // some font
},
defaultSize: 12,
defaultColor: ColorRed,
expected: FontStyle{
FontSize: 12,
FontColor: ColorRed,
Font: &truetype.Font{}, // keeps existing font
},
},
{
name: "all_values_set_no_defaults_applied",
input: FontStyle{
FontSize: 20,
FontColor: ColorPurple,
Font: &truetype.Font{},
},
defaultSize: 12,
defaultColor: ColorRed,
expected: FontStyle{
FontSize: 20, // keeps existing
FontColor: ColorPurple, // keeps existing
Font: &truetype.Font{}, // keeps existing
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := fillFontStyleDefaults(tt.input, tt.defaultSize, tt.defaultColor, tt.fontOptions...)
assert.InDelta(t, tt.expected.FontSize, result.FontSize, matrix.DefaultEpsilon)
assert.Equal(t, tt.expected.FontColor, result.FontColor)
if tt.expected.Font != nil && result.Font != nil {
// Both have fonts, compare if they're the same instance
if tt.input.Font != nil {
// If input had a font, it should be preserved
assert.Equal(t, tt.input.Font, result.Font)
} else {
// If input had no font, should get default font
assert.NotNil(t, result.Font)
}
} else if tt.expected.Font == nil && result.Font == nil {
// Both should be nil
} else {
t.Errorf("Font mismatch: expected %v, got %v", tt.expected.Font, result.Font)
}
})
}
}
func TestMergeFontStyles(t *testing.T) {
t.Parallel()
tests := []struct {
name string
primary FontStyle
defaults []FontStyle
expected FontStyle
}{
{
name: "empty_primary_uses_first_available_defaults",
primary: FontStyle{},
defaults: []FontStyle{
{FontSize: 12, FontColor: ColorRed, Font: &truetype.Font{}},
},
expected: FontStyle{
FontSize: 12,
FontColor: ColorRed,
Font: &truetype.Font{},
},
},
{
name: "primary_values_take_precedence",
primary: FontStyle{
FontSize: 16,
FontColor: ColorBlue,
},
defaults: []FontStyle{
{FontSize: 12, FontColor: ColorRed, Font: &truetype.Font{}},
},
expected: FontStyle{
FontSize: 16, // from primary
FontColor: ColorBlue, // from primary
Font: &truetype.Font{}, // from default
},
},
{
name: "uses_first_available_value_from_multiple_defaults",
primary: FontStyle{},
defaults: []FontStyle{
{}, // empty first default
{FontSize: 14}, // has size
{FontSize: 18, FontColor: ColorGreen}, // has size and color
},
expected: FontStyle{
FontSize: 14, // from second default (first with size)
FontColor: ColorGreen, // from third default (first with color)
Font: nil, // no defaults have font
},
},
{
name: "partial_primary_fills_from_defaults",
primary: FontStyle{
FontSize: 20, // has size
// missing color and font
},
defaults: []FontStyle{
{FontColor: ColorPurple}, // has color
{Font: &truetype.Font{}}, // has font
},
expected: FontStyle{
FontSize: 20, // from primary
FontColor: ColorPurple, // from first default
Font: &truetype.Font{}, // from second default
},
},
{
name: "zero_values_in_primary_use_defaults",
primary: FontStyle{
FontSize: 0, // zero value
FontColor: Color{}, // zero color
Font: nil, // nil font
},
defaults: []FontStyle{
{FontSize: 15, FontColor: ColorYellow, Font: &truetype.Font{}},
},
expected: FontStyle{
FontSize: 15,
FontColor: ColorYellow,
Font: &truetype.Font{},
},
},
{
name: "no_defaults_primary_unchanged",
primary: FontStyle{
FontSize: 24,
FontColor: ColorBlack,
},
defaults: []FontStyle{},
expected: FontStyle{
FontSize: 24,
FontColor: ColorBlack,
Font: nil,
},
},
{
name: "complex_precedence_scenario",
primary: FontStyle{
FontColor: ColorRed, // only color set
},
defaults: []FontStyle{
{FontSize: 10}, // first default has size
{}, // empty default
{FontSize: 12, Font: &truetype.Font{}}, // third has size and font
{FontSize: 14, FontColor: ColorBlue, Font: &truetype.Font{}}, // fourth has all
},
expected: FontStyle{
FontSize: 10, // from first default with size
FontColor: ColorRed, // from primary
Font: &truetype.Font{}, // from third default with font
},
},
{
name: "all_defaults_empty_primary_unchanged",
primary: FontStyle{
FontSize: 18,
FontColor: ColorGreen,
Font: &truetype.Font{},
},
defaults: []FontStyle{
{}, {}, {}, // all empty
},
expected: FontStyle{
FontSize: 18,
FontColor: ColorGreen,
Font: &truetype.Font{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := mergeFontStyles(tt.primary, tt.defaults...)
assert.InDelta(t, tt.expected.FontSize, result.FontSize, matrix.DefaultEpsilon)
assert.Equal(t, tt.expected.FontColor, result.FontColor)
// Handle font comparison carefully since their pointers
if tt.expected.Font != nil && result.Font != nil {
// Both should be non-nil, check if they match expected source
if tt.primary.Font != nil {
assert.Equal(t, tt.primary.Font, result.Font, "should preserve primary font")
} else {
// Should come from defaults, just verify it's not nil
assert.NotNil(t, result.Font, "should get font from defaults")
}
} else if tt.expected.Font == nil {
assert.Nil(t, result.Font, "font should be nil")
}
})
}
}
func TestMergeFontStylesEdgeCases(t *testing.T) {
t.Parallel()
t.Run("nil_font_handling", func(t *testing.T) {
primary := FontStyle{}
defaults := []FontStyle{
{Font: nil}, // explicit nil
{Font: &truetype.Font{}}, // has font
}
result := mergeFontStyles(primary, defaults...)
assert.NotNil(t, result.Font, "should get font from second default")
})
t.Run("zero_vs_non_zero_color", func(t *testing.T) {
primary := FontStyle{
FontColor: Color{R: 0, G: 0, B: 0, A: 0}, // transparent/zero
}
defaults := []FontStyle{
{FontColor: ColorRed},
}
result := mergeFontStyles(primary, defaults...)
assert.Equal(t, ColorRed, result.FontColor, "zero color should be replaced")
})
t.Run("partial_alpha_color", func(t *testing.T) {
partialColor := Color{R: 100, G: 100, B: 100, A: 0} // has RGB but no alpha
primary := FontStyle{
FontColor: partialColor,
}
defaults := []FontStyle{
{FontColor: ColorBlue},
}
result := mergeFontStyles(primary, defaults...)
// Should keep the primary color even with zero alpha since IsZero() checks all components
if partialColor.IsZero() {
assert.Equal(t, ColorBlue, result.FontColor)
} else {
assert.Equal(t, partialColor, result.FontColor)
}
})
}