-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparity_test.go
More file actions
398 lines (357 loc) · 10.4 KB
/
Copy pathparity_test.go
File metadata and controls
398 lines (357 loc) · 10.4 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package dicebear
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/dicebear/dicebear-go/v10/color"
"github.com/dicebear/dicebear-go/v10/internal/initials"
"github.com/dicebear/dicebear-go/v10/internal/num"
"github.com/dicebear/dicebear-go/v10/internal/prng"
)
// Cross-language parity: assert the primitives produce exactly the values in the
// shared fixtures under <repo>/tests/fixtures/parity/, the same the JS, PHP,
// Python, Rust and Dart suites run against. The tests skip gracefully when the
// fixtures are absent (e.g. in the split dicebear-go repo).
func fixturePath(t *testing.T, name string) string {
t.Helper()
p := filepath.Join("..", "..", "..", "tests", "fixtures", "parity", name)
if _, err := os.Stat(p); err != nil {
t.Skipf("parity fixtures not available (%s)", p)
}
return p
}
// fixtureStyleNames enumerates the parity style fixtures instead of repeating a
// hand-maintained list, so a fixture added to tests/fixtures/parity/styles is
// picked up here the way the PHP and Python suites already pick it up. Names are
// returned sorted, so the subtest order stays stable.
func fixtureStyleNames(t *testing.T) []string {
t.Helper()
paths, err := filepath.Glob(filepath.Join(fixturePath(t, "styles"), "*.json"))
if err != nil {
t.Fatalf("list style fixtures: %v", err)
}
if len(paths) == 0 {
t.Fatal("no style fixtures found")
}
names := make([]string, 0, len(paths))
for _, p := range paths {
names = append(names, strings.TrimSuffix(filepath.Base(p), ".json"))
}
sort.Strings(names)
return names
}
func readFixture(t *testing.T, name string, v any) {
t.Helper()
b, err := os.ReadFile(fixturePath(t, name))
if err != nil {
t.Fatalf("read %s: %v", name, err)
}
if err := json.Unmarshal(b, v); err != nil {
t.Fatalf("parse %s: %v", name, err)
}
}
func strID(s string) string { return s }
func TestFnv1aParity(t *testing.T) {
var cases []struct {
Input string `json:"input"`
Hash uint32 `json:"hash"`
Hex string `json:"hex"`
}
readFixture(t, "fnv1a.json", &cases)
for _, c := range cases {
if got := prng.Hash(c.Input); got != c.Hash {
t.Errorf("Hash(%q) = %d, want %d", c.Input, got, c.Hash)
}
if got := prng.Hex(c.Input); got != c.Hex {
t.Errorf("Hex(%q) = %s, want %s", c.Input, got, c.Hex)
}
}
}
func TestMulberry32Parity(t *testing.T) {
var cases []struct {
Seed uint32 `json:"seed"`
Sequence []struct {
Float float64 `json:"float"`
State int32 `json:"state"`
} `json:"sequence"`
}
readFixture(t, "mulberry32.json", &cases)
for _, c := range cases {
m := prng.NewMulberry32(c.Seed)
for i, step := range c.Sequence {
f := m.NextFloat()
s := m.SignedState()
if f != step.Float {
t.Errorf("seed %d step %d: float = %v, want %v", c.Seed, i, f, step.Float)
}
if s != step.State {
t.Errorf("seed %d step %d: state = %d, want %d", c.Seed, i, s, step.State)
}
}
}
}
type rangeFixture struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
Step *float64 `json:"step"`
}
func (r rangeFixture) toRange() *prng.Range {
return &prng.Range{Min: r.Min, Max: r.Max, Step: r.Step}
}
func TestPrngParity(t *testing.T) {
var f struct {
GetValue []struct {
Seed, Key string
Result float64
} `json:"getValue"`
Pick []struct {
Seed, Key string
Items []string
Result *string
} `json:"pick"`
WeightedPick []struct {
Seed, Key string
Weights map[string]float64
Result *string
} `json:"weightedPick"`
Bool []struct {
Seed, Key string
Likelihood float64
Result bool
} `json:"bool"`
Float []struct {
Seed, Key string
Range rangeFixture
Result float64
} `json:"float"`
Integer []struct {
Seed, Key string
Range rangeFixture
Result int
} `json:"integer"`
Shuffle []struct {
Seed, Key string
Items []string
Result []string
} `json:"shuffle"`
}
readFixture(t, "prng.json", &f)
for _, c := range f.GetValue {
if got := prng.New(c.Seed).GetValue(c.Key); got != c.Result {
t.Errorf("GetValue(%q, %q) = %v, want %v", c.Seed, c.Key, got, c.Result)
}
}
for _, c := range f.Pick {
got, ok := prng.Pick(prng.New(c.Seed), c.Key, c.Items, strID)
if !matchOptional(got, ok, c.Result) {
t.Errorf("Pick(%q, %q, %v) = (%q, %v), want %v", c.Seed, c.Key, c.Items, got, ok, c.Result)
}
}
for _, c := range f.WeightedPick {
got, ok := prng.New(c.Seed).WeightedPick(c.Key, c.Weights)
if !matchOptional(got, ok, c.Result) {
t.Errorf("WeightedPick(%q, %q, %v) = (%q, %v), want %v", c.Seed, c.Key, c.Weights, got, ok, c.Result)
}
}
for _, c := range f.Bool {
if got := prng.New(c.Seed).Bool(c.Key, c.Likelihood); got != c.Result {
t.Errorf("Bool(%q, %q, %v) = %v, want %v", c.Seed, c.Key, c.Likelihood, got, c.Result)
}
}
for _, c := range f.Float {
if got := prng.New(c.Seed).Float(c.Key, c.Range.toRange()); got != c.Result {
t.Errorf("Float(%q, %q) = %v, want %v", c.Seed, c.Key, got, c.Result)
}
}
for _, c := range f.Integer {
if got := prng.New(c.Seed).Integer(c.Key, c.Range.toRange()); got != c.Result {
t.Errorf("Integer(%q, %q) = %d, want %d", c.Seed, c.Key, got, c.Result)
}
}
for _, c := range f.Shuffle {
got := prng.New(c.Seed).Shuffle(c.Key, c.Items)
if !equalStrings(got, c.Result) {
t.Errorf("Shuffle(%q, %q, %v) = %v, want %v", c.Seed, c.Key, c.Items, got, c.Result)
}
}
}
func TestNumberParity(t *testing.T) {
var cases []struct {
Input float64 `json:"input"`
Output string `json:"output"`
}
readFixture(t, "numbers.json", &cases)
for _, c := range cases {
if got := num.Format(c.Input); got != c.Output {
t.Errorf("num.Format(%v) = %q, want %q", c.Input, got, c.Output)
}
}
}
func TestInitialsParity(t *testing.T) {
var cases []struct {
Seed string `json:"seed"`
Result string `json:"result"`
}
readFixture(t, "initials.json", &cases)
for _, c := range cases {
if got := initials.FromSeed(c.Seed); got != c.Result {
t.Errorf("initials.FromSeed(%q) = %q, want %q", c.Seed, got, c.Result)
}
}
}
func TestColorParity(t *testing.T) {
var f struct {
ToHex []struct {
Input string `json:"input"`
Result string `json:"result"`
} `json:"toHex"`
ToRGBHex []struct {
Input string `json:"input"`
Result string `json:"result"`
} `json:"toRgbHex"`
ParseHex []struct {
Input string `json:"input"`
Result []uint8 `json:"result"`
} `json:"parseHex"`
Luminance []struct {
Input string `json:"input"`
Result float64 `json:"result"`
} `json:"luminance"`
SortByContrast []struct {
Candidates []string `json:"candidates"`
RefColor string `json:"refColor"`
Result []string `json:"result"`
} `json:"sortByContrast"`
FilterNotEqualTo []struct {
Candidates []string `json:"candidates"`
Excluded []string `json:"excluded"`
Result []string `json:"result"`
} `json:"filterNotEqualTo"`
}
readFixture(t, "colors.json", &f)
for _, c := range f.ToHex {
if got := color.ToHex(c.Input); got != c.Result {
t.Errorf("ToHex(%q) = %q, want %q", c.Input, got, c.Result)
}
}
for _, c := range f.ToRGBHex {
if got := color.ToRGBHex(c.Input); got != c.Result {
t.Errorf("ToRGBHex(%q) = %q, want %q", c.Input, got, c.Result)
}
}
for _, c := range f.ParseHex {
r, g, b := color.ParseHex(c.Input)
if r != c.Result[0] || g != c.Result[1] || b != c.Result[2] {
t.Errorf("ParseHex(%q) = (%d, %d, %d), want %v", c.Input, r, g, b, c.Result)
}
}
for _, c := range f.Luminance {
if got := color.Luminance(c.Input); got != c.Result {
t.Errorf("Luminance(%q) = %v, want %v", c.Input, got, c.Result)
}
}
for _, c := range f.SortByContrast {
if got := color.SortByContrast(c.Candidates, c.RefColor); !equalStrings(got, c.Result) {
t.Errorf("SortByContrast(%v, %q) = %v, want %v", c.Candidates, c.RefColor, got, c.Result)
}
}
for _, c := range f.FilterNotEqualTo {
if got := color.FilterNotEqualTo(c.Candidates, c.Excluded); !equalStrings(got, c.Result) {
t.Errorf("FilterNotEqualTo(%v, %v) = %v, want %v", c.Candidates, c.Excluded, got, c.Result)
}
}
}
// Cross-language validation parity: every port must accept and reject the same
// inputs (error messages are language-specific and not compared). The circular
// color reference cases additionally pin the reported chain.
func TestValidationParity(t *testing.T) {
var f struct {
Styles []struct {
ID string `json:"id"`
Definition json.RawMessage `json:"definition"`
Valid bool `json:"valid"`
} `json:"styles"`
Options []struct {
ID string `json:"id"`
Options map[string]any `json:"options"`
Valid bool `json:"valid"`
} `json:"options"`
CircularColors []struct {
ID string `json:"id"`
Style json.RawMessage `json:"style"`
Options map[string]any `json:"options"`
Chain []string `json:"chain"`
} `json:"circularColors"`
}
readFixture(t, "validation.json", &f)
var minimal *Style
for _, c := range f.Styles {
s, err := NewStyle(c.Definition)
if c.Valid != (err == nil) {
t.Errorf("style %s: err = %v, want valid = %v", c.ID, err, c.Valid)
continue
}
if !c.Valid {
var ve *ValidationError
if !errors.As(err, &ve) {
t.Errorf("style %s: error %T is not a *ValidationError", c.ID, err)
}
}
if c.ID == "minimal" {
minimal = s
}
}
if minimal == nil {
t.Fatal("minimal style fixture missing or invalid")
}
for _, c := range f.Options {
_, err := NewAvatar(minimal, c.Options)
if c.Valid != (err == nil) {
t.Errorf("options %s: err = %v, want valid = %v", c.ID, err, c.Valid)
continue
}
if !c.Valid {
var ve *ValidationError
if !errors.As(err, &ve) {
t.Errorf("options %s: error %T is not a *ValidationError", c.ID, err)
}
}
}
for _, c := range f.CircularColors {
style, err := NewStyle(c.Style)
if err != nil {
t.Errorf("circular %s: style must parse: %v", c.ID, err)
continue
}
_, err = NewAvatar(style, c.Options)
var ce *CircularColorReferenceError
if !errors.As(err, &ce) {
t.Errorf("circular %s: err = %v, want *CircularColorReferenceError", c.ID, err)
continue
}
if !equalStrings(ce.Chain, c.Chain) {
t.Errorf("circular %s: chain = %v, want %v", c.ID, ce.Chain, c.Chain)
}
}
}
func matchOptional(got string, ok bool, want *string) bool {
if want == nil {
return !ok
}
return ok && got == *want
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}