oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package wisp
2
3import (
4 "bytes"
5 "regexp"
6 "strconv"
7 "strings"
8 "sync"
9 "testing"
10)
11
12// The sprite is the same bytes every time: its content version, and so
13// every browser's cached copy, must survive a restart and a release that
14// does not touch the wisps.
15func TestSpriteIsDeterministic(t *testing.T) {
16 a := string(Sprite())
17 spriteOnce, sprite = sync.Once{}, nil
18 if b := string(Sprite()); a != b {
19 t.Fatal("two draws of the sprite differ")
20 }
21}
22
23// Every stretch of every variant is in the sprite, drawn (a glow, a body
24// and at least four fibres), and finite: a NaN in path data draws nothing
25// and fails silently in the browser.
26func TestSpriteHasEveryStretch(t *testing.T) {
27 s := string(Sprite())
28 for v := 0; v < Count; v++ {
29 for i := 0; i < Stretches; i++ {
30 id := symbolID(v, i)
31 start := strings.Index(s, `<symbol id="`+id+`"`)
32 if start < 0 {
33 t.Fatalf("sprite lacks %s", id)
34 }
35 end := strings.Index(s[start:], "</symbol>")
36 sym := s[start : start+end]
37 if n := strings.Count(sym, "<path "); n < 6 {
38 t.Errorf("%s has %d shapes, want a glow, a body and 4+ fibres", id, n)
39 }
40 for _, f := range []string{"url(#wisp-glow)", "url(#wisp-body)", "url(#wisp-fibre)"} {
41 if !strings.Contains(sym, f) {
42 t.Errorf("%s draws nothing with %s", id, f)
43 }
44 }
45 }
46 }
47 if strings.Contains(s, "NaN") || strings.Contains(s, "Inf") {
48 t.Fatal("sprite has a non-finite number")
49 }
50 // Path data is whole numbers, so a NaN would arrive as whatever Go's
51 // float-to-int conversion makes of it (a huge negative, or 0), never
52 // as "NaN". Every step of every outline is small: bound them all.
53 for _, d := range regexp.MustCompile(` d="([^"]+)"`).FindAllStringSubmatch(s, -1) {
54 for _, n := range regexp.MustCompile(`-?\d+`).FindAllString(d[1], -1) {
55 if v, _ := strconv.Atoi(n); v < -3000 || v > 3000 {
56 t.Fatalf("a path coordinate or step of %d is out of any box: %.60s…", v, d[1])
57 }
58 }
59 }
60 for _, f := range []string{"glow", "body", "fibre"} {
61 if !strings.Contains(s, `<filter id="wisp-`+f+`"`) {
62 t.Errorf("sprite lacks the %s blur", f)
63 }
64 }
65}
66
67// The variants differ: the point of having five. Each stretch of each
68// variant draws something no other variant's same stretch does.
69func TestVariantsDiffer(t *testing.T) {
70 s := Sprite()
71 body := func(v, i int) []byte {
72 start := bytes.Index(s, []byte(`<symbol id="`+symbolID(v, i)+`"`))
73 end := bytes.Index(s[start:], []byte("</symbol>"))
74 return regexp.MustCompile(`id="[^"]*"`).ReplaceAll(s[start:start+end], nil)
75 }
76 for i := 0; i < Stretches; i++ {
77 for a := 0; a < Count; a++ {
78 for b := a + 1; b < Count; b++ {
79 if bytes.Equal(body(a, i), body(b, i)) {
80 t.Errorf("variants %d and %d draw stretch %d identically", a, b, i)
81 }
82 }
83 }
84 }
85}
86
87// Markup points at exactly the variant's seven symbols, in order.
88func TestMarkupPointsIntoTheSprite(t *testing.T) {
89 m := Markup(2)
90 uses := regexp.MustCompile(`<use href="([^"]+)"/>`).FindAllStringSubmatch(m, -1)
91 if len(uses) != Stretches {
92 t.Fatalf("%d stretches, want %d", len(uses), Stretches)
93 }
94 for i, u := range uses {
95 if want := "#" + symbolID(2, i); u[1] != want {
96 t.Errorf("stretch %d points at %q, want %q", i, u[1], want)
97 }
98 }
99 if strings.Count(m, `aria-hidden="true"`) != Stretches {
100 t.Error("a stretch is not hidden from assistive technology")
101 }
102}
103