Skip to content

Commit 439da85

Browse files
committed
test(core): every quoted at-rule key a few characters off a reference spelling is that spelling
From the review of this pull request: a capital or a digit in the at-rule name ('@media (pointer: coarse)') still passed every case, after two earlier reviews found one more spelling class each. Rather than one more case, the scan now reads every quoted string that starts with '@' and applies two rules, so no drift depends on which character moved: one that names the pointer, the touch callout or a compact size, in any case, is a reference spelling or a query the presentation does not decide; and one within three characters of any of those spellings, case aside, is that spelling exactly, wherever the change sits. Mutant evidence, each passing the scan before this change and failing it after: '@media (pointer: coarse)', '@m3dia (pointer: coarse)', '@media (POINTER: COARSE)' and '@media (pointr: corse)' in Switch. The earlier drifts ('@meda', '-webkit-touch-calout', 'widht') still fail.
1 parent 9d19f94 commit 439da85

1 file changed

Lines changed: 54 additions & 24 deletions

File tree

‎packages/core/src/utils/touchPresentation.test.ts‎

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ afterEach(() => {
9292
vi.unstubAllGlobals();
9393
});
9494

95+
/**
96+
* The Levenshtein distance between two strings, or more than `limit` as soon
97+
* as it must exceed it: how many characters a spelling is off another.
98+
*/
99+
function editDistance(a: string, b: string, limit: number): number {
100+
if (Math.abs(a.length - b.length) > limit) {
101+
return limit + 1;
102+
}
103+
let previous = Array.from({length: b.length + 1}, (_, j) => j);
104+
for (let i = 1; i <= a.length; i++) {
105+
const current = [i];
106+
for (let j = 1; j <= b.length; j++) {
107+
current[j] = Math.min(
108+
previous[j] + 1,
109+
current[j - 1] + 1,
110+
previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1),
111+
);
112+
}
113+
if (Math.min(...current) > limit) {
114+
return limit + 1;
115+
}
116+
previous = current;
117+
}
118+
return previous[b.length];
119+
}
120+
95121
describe('touch presentation', () => {
96122
it('keys touch on the primary pointer and compact on the width', () => {
97123
const {result} = renderHook(() => useTouchPresentation());
@@ -228,13 +254,17 @@ describe('touch presentation', () => {
228254
expect(computed).toEqual([]);
229255
});
230256

231-
it('keeps every literal pointer, touch-callout and compact at-rule key in core one of the reference spellings', () => {
232-
// Any quoted at-rule key, whatever its at-rule is spelled: one that names
233-
// the pointer or the touch callout, or the compact width and height, is
234-
// one of the resolver's own, or a query the presentation does not
235-
// decide (a second pointer, a fine pointer, Reduce Motion split by
236-
// pointer). Any word of a pointer or callout query classes it, so a typo
237-
// inside one word still counts.
257+
it('keeps every quoted at-rule key in core that names the pointer, the touch callout or a compact size, or that is a few characters off a reference spelling, one of the reference spellings', () => {
258+
// Every quoted string that starts with `@` is read, whatever its at-rule
259+
// is spelled. Two rules, so no drift depends on which character moved:
260+
// - one that names the pointer or the touch callout (any word of either
261+
// query, any case), or a compact size with its breakpoint, is one of
262+
// the resolver's own spellings or a query the presentation does not
263+
// decide (a second pointer, a fine pointer, Reduce Motion split by
264+
// pointer);
265+
// - one within three characters of any of those spellings, case aside,
266+
// is that spelling exactly: a capital, a digit or a typo anywhere, in
267+
// the at-rule, a feature or a value, fails.
238268
const pointerKeys = new Set<string>([
239269
touchKeys.touch,
240270
touchKeys.compactTouch,
@@ -244,10 +274,8 @@ describe('touch presentation', () => {
244274
reducedMotionKeys.fade,
245275
reducedMotionKeys.instant,
246276
]);
247-
const compactKeys = new Set<string>([
248-
touchKeys.compact,
249-
touchKeys.compactTouch,
250-
]);
277+
const references = new Set<string>([...pointerKeys, touchKeys.compact]);
278+
const lowered = [...references].map(reference => reference.toLowerCase());
251279
const root = path.resolve(__dirname, '..');
252280
const drifted: string[] = [];
253281
let pointerLiterals = 0;
@@ -263,21 +291,23 @@ describe('touch presentation', () => {
263291
!/\.(test|spec|stories)\./.test(entry.name)
264292
) {
265293
const source = fs.readFileSync(full, 'utf8');
266-
for (const match of source.matchAll(/'(@[a-z-]+ [^'\n]*)'/g)) {
294+
for (const match of source.matchAll(/'(@[^'\n]*)'/g)) {
267295
const key = match[1];
268-
const where = `${path.relative(root, full)}: ${key}`;
269-
if (/pointer|coarse|fine|touch|callout/.test(key)) {
270-
pointerLiterals++;
271-
if (!pointerKeys.has(key)) {
272-
drifted.push(where);
296+
if (references.has(key)) {
297+
if (pointerKeys.has(key)) {
298+
pointerLiterals++;
273299
}
274-
} else if (
275-
// The compact query: either half's size, with its breakpoint.
276-
/\b(width|height)\b/.test(key) &&
277-
/\b(768|500)px\b/.test(key) &&
278-
!compactKeys.has(key)
279-
) {
280-
drifted.push(where);
300+
continue;
301+
}
302+
const names =
303+
/pointer|coarse|fine|touch|callout/i.test(key) ||
304+
(/\b(width|height)\b/i.test(key) && /\b(768|500)px\b/.test(key));
305+
const lower = key.toLowerCase();
306+
const isNear = lowered.some(
307+
reference => editDistance(lower, reference, 3) <= 3,
308+
);
309+
if (names || isNear) {
310+
drifted.push(`${path.relative(root, full)}: ${key}`);
281311
}
282312
}
283313
}

0 commit comments

Comments
 (0)