-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_test.go
More file actions
224 lines (200 loc) · 5.65 KB
/
Copy pathdevice_test.go
File metadata and controls
224 lines (200 loc) · 5.65 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
//go:build !wasm
package css
import (
"strconv"
"strings"
"testing"
)
func parseMaxWidth(query string) (float64, bool) {
_, after, ok := strings.Cut(query, "max-width: ")
if !ok {
return 0, false
}
end := strings.Index(after, "px")
if end == -1 {
return 0, false
}
v, err := strconv.ParseFloat(after[:end], 64)
return v, err == nil
}
func parseMinWidth(query string) (float64, bool) {
_, after, ok := strings.Cut(query, "min-width: ")
if !ok {
return 0, false
}
end := strings.Index(after, "px")
if end == -1 {
return 0, false
}
v, err := strconv.ParseFloat(after[:end], 64)
return v, err == nil
}
func parsePx(s string) (float64, bool) {
trimmed, ok := strings.CutSuffix(s, "px")
if !ok {
return 0, false
}
v, err := strconv.ParseFloat(trimmed, 64)
return v, err == nil
}
// The thresholds exist in two places: as literals inside Device.Query(), where a
// var() is invalid (SPECS §6), and as the --bp-* token values. Nothing in the
// language ties them together, so a change to one would drift silently past every
// consumer. This test is that tie: it is the reason the duplication is safe.
func TestDeviceThresholdsMatchBreakpointTokens(t *testing.T) {
sm, ok := parsePx(BpSm.Dark)
if !ok {
t.Fatalf("BpSm value %q is not a px length", BpSm.Dark)
}
lg, ok := parsePx(BpLg.Dark)
if !ok {
t.Fatalf("BpLg value %q is not a px length", BpLg.Dark)
}
// The gap a fractional viewport width could fall into. Kept in sync with the
// ".98" fractions in device.go.
const epsilon = 0.02
cases := []struct {
name string
got func() (float64, bool)
want float64
token string
}{
{"Tablet min-width", func() (float64, bool) { return parseMinWidth(Tablet.Query()) }, sm, "BpSm"},
{"Mobile max-width", func() (float64, bool) { return parseMaxWidth(Mobile.Query()) }, sm - epsilon, "BpSm"},
{"Desktop min-width", func() (float64, bool) { return parseMinWidth(Desktop.Query()) }, lg, "BpLg"},
{"Tablet max-width", func() (float64, bool) { return parseMaxWidth(Tablet.Query()) }, lg - epsilon, "BpLg"},
}
for _, c := range cases {
got, ok := c.got()
if !ok {
t.Errorf("%s: could not be parsed out of the query", c.name)
continue
}
if got != c.want {
t.Errorf("%s = %g, want %g (derived from %s = %q); device.go and catalog.go have drifted apart",
c.name, got, c.want, c.token, c.token)
}
}
}
func TestDeviceClassesPartition(t *testing.T) {
tests := []struct {
width float64
expected Device
}{
{320, Mobile},
{639, Mobile},
// Fractional widths are the whole reason for the ".98" fractions: at a
// plain 639px/1023px boundary these fall into a gap matching no class.
{639.5, Mobile},
{639.98, Mobile},
{640, Tablet},
{767, Tablet},
{1023, Tablet},
{1023.5, Tablet},
{1023.98, Tablet},
{1024, Desktop},
{1440, Desktop},
}
for _, tt := range tests {
var matching Device
var matched bool
for _, d := range []Device{Mobile, Tablet, Desktop} {
q := d.Query()
matches := true
if strings.Contains(q, "max-width: ") {
mw, ok := parseMaxWidth(q)
if ok && tt.width > mw {
matches = false
}
}
if strings.Contains(q, "min-width: ") {
mw, ok := parseMinWidth(q)
if ok && tt.width < mw {
matches = false
}
}
if matches {
if matched {
t.Errorf("width %.0f matches both %s and %s", tt.width, matching, d)
}
matching = d
matched = true
}
}
if !matched {
t.Errorf("width %.0f matches no device class", tt.width)
}
if matched && matching != tt.expected {
t.Errorf("width %.0f matched %s, expected %s", tt.width, matching, tt.expected)
}
}
}
func TestDeviceQueryHasNoVar(t *testing.T) {
for _, d := range []Device{Mobile, Tablet, Desktop} {
if strings.Contains(d.Query(), "var(") {
t.Errorf("%s.Query() contains var(): %s", d, d.Query())
}
}
}
func TestQueryJoinIsDeterministic(t *testing.T) {
a := Query(Desktop, Mobile)
b := Query(Mobile, Desktop)
if a != b {
t.Errorf("Query(Desktop, Mobile) != Query(Mobile, Desktop): %q vs %q", a, b)
}
want := Mobile.Query() + ", " + Desktop.Query()
if a != want {
t.Errorf("Query(Mobile, Desktop) = %q, want %q", a, want)
}
}
func TestQueryDeduplicates(t *testing.T) {
single := Query(Mobile)
dup := Query(Mobile, Mobile)
if single != dup {
t.Errorf("Query(Mobile, Mobile) = %q, want %q", dup, single)
}
}
func TestQueryEmpty(t *testing.T) {
if got := Query(); got != "" {
t.Errorf("Query() = %q, want empty", got)
}
}
func TestRailTokensDeclared(t *testing.T) {
css := RootCSS().String()
for _, name := range []string{"--rail-narrow", "--rail-wide"} {
if !strings.Contains(css, name+":") {
t.Errorf("RootCSS() missing declaration for %s", name)
}
}
}
func TestDeviceStringRoundTrip(t *testing.T) {
for _, d := range []Device{Mobile, Tablet, Desktop} {
if d.String() == "Unknown" {
t.Errorf("%d.String() = Unknown", d)
}
}
}
func TestQueryIgnoresUnknown(t *testing.T) {
got := Query(Mobile, 99, Desktop)
want := Query(Mobile, Desktop)
if got != want {
t.Errorf("Query with unknown = %q, want %q", got, want)
}
}
func TestQueryAllReturnsThree(t *testing.T) {
got := Query(Mobile, Tablet, Desktop)
parts := strings.Split(got, ", ")
if len(parts) != 3 {
t.Errorf("Query(all) has %d parts, want 3: %q", len(parts), got)
}
}
func TestHoverCapabilityQuery(t *testing.T) {
// The fine-pointer gate: hover reveals are scoped on this condition because
// a touch tap fires `:hover` and synthetic mouse events but never this.
if got := FinePointer.Query(); got != "(hover: hover)" {
t.Errorf("FinePointer.Query() = %q, want \"(hover: hover)\"", got)
}
if got := Capability(99).Query(); got != "" {
t.Errorf("unknown capability Query() = %q, want empty", got)
}
}