Skip to content

Commit 31498ac

Browse files
test(css): cover the generator's dataset guards and spelling walk
Give the three collectors that read mdn-data their table as an argument, so a test can drive the guards that fire when an entry disappears, and export the spelling walk and slot readers the cases need.
1 parent 695598d commit 31498ac

2 files changed

Lines changed: 163 additions & 13 deletions

File tree

‎test/CssValueSyntax.unittest.js‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ const {
55
DATA_TARGET,
66
acceptedValues,
77
assertClassesArePrintable,
8+
checkStatedClassSpellings,
89
collectData,
10+
collectGradientFunctions,
11+
collectMergeableAtRules,
12+
isSpelledSyntax,
913
parseValueSyntax,
14+
shorthandSlots,
15+
slotSpellings,
1016
walkValueSyntax
1117
} = require("../tooling/generate-css-data");
1218

@@ -388,6 +394,140 @@ describe("CssValueSyntax", () => {
388394
});
389395
});
390396

397+
describe("isSpelledSyntax", () => {
398+
it("spells out a group's body through the multiplier over it", () => {
399+
expect(isSpelledSyntax("[ red | blue ]?", new Set())).toBe(true);
400+
});
401+
402+
it("stops at a class already on the walk", () => {
403+
// `<color>` reaching itself must terminate rather than recurse, and says
404+
// nothing new about the spelling when it does.
405+
expect(isSpelledSyntax("<color>", new Set(["color"]))).toBe(true);
406+
});
407+
408+
it("leaves a `<'property'>` reference unspelled", () => {
409+
// What a property accepts is not a list of values this can write out.
410+
expect(isSpelledSyntax("<'width'>", new Set())).toBe(false);
411+
});
412+
413+
it("leaves a class unspelled when a nested one is", () => {
414+
expect(isSpelledSyntax("red | [ <'width'> ]", new Set())).toBe(false);
415+
});
416+
417+
it("spells out nothing it cannot parse", () => {
418+
expect(isSpelledSyntax("<", new Set())).toBe(false);
419+
});
420+
});
421+
422+
describe("shorthandSlots", () => {
423+
it("reads the slots out of a group enclosing the whole definition", () => {
424+
expect(shorthandSlots("[ <'flex-grow'> || <'flex-shrink'> ]")).toEqual([
425+
"<'flex-grow'>",
426+
"<'flex-shrink'>"
427+
]);
428+
});
429+
430+
it("reads none out of brackets that enclose less than the whole", () => {
431+
// The first `[` is not the one the last `]` closes, so the group is a term
432+
// of the definition rather than the definition itself.
433+
expect(shorthandSlots("[[a]")).toBeNull();
434+
});
435+
});
436+
437+
describe("slotSpellings", () => {
438+
it("reports every spelling a property reference reaches", () => {
439+
expect([...slotSpellings("<'visibility'>")]).toEqual([
440+
"visible",
441+
"hidden",
442+
"collapse"
443+
]);
444+
});
445+
446+
it("reports none for a slot it cannot parse", () => {
447+
expect([...slotSpellings("<")]).toEqual([]);
448+
});
449+
});
450+
451+
describe("the guards on what those datasets still publish", () => {
452+
it("accepts a `syntaxes.json` that states no class the SUPPLEMENT does", () => {
453+
expect(() => checkStatedClassSpellings({})).not.toThrow();
454+
});
455+
456+
it("rejects one that has grown an entry the SUPPLEMENT states", () => {
457+
expect(() =>
458+
checkStatedClassSpellings({ url: { syntax: "<url()> | <src()>" } })
459+
).toThrow("`<url>` is spelled out by mdn-data now");
460+
});
461+
462+
it("reads each gradient's last position off the stop list it names", () => {
463+
expect(
464+
collectGradientFunctions(
465+
{
466+
gradient: { syntax: "<conic-gradient()> | <linear-gradient()>" },
467+
"conic-gradient-syntax": { syntax: "<angular-color-stop-list>" }
468+
},
469+
{
470+
"conic-gradient()": { syntax: "<conic-gradient-syntax>" },
471+
"linear-gradient()": { syntax: "<color-stop-list>" }
472+
}
473+
)
474+
).toEqual([
475+
["conic-gradient", ["100%", "360deg", "1turn"]],
476+
["linear-gradient", ["100%"]]
477+
]);
478+
});
479+
480+
it("reads no function out of a `<gradient>` that names none", () => {
481+
expect(
482+
collectGradientFunctions({ gradient: { syntax: "none" } }, {})
483+
).toEqual([]);
484+
});
485+
486+
it("rejects a `syntaxes.json` without `<gradient>`", () => {
487+
expect(() => collectGradientFunctions({}, {})).toThrow(
488+
"`<gradient>` is gone from mdn-data"
489+
);
490+
});
491+
492+
it("rejects a `functions.json` missing a call `<gradient>` names", () => {
493+
expect(() =>
494+
collectGradientFunctions(
495+
{ gradient: { syntax: "<linear-gradient()>" } },
496+
{}
497+
)
498+
).toThrow("`linear-gradient()` is gone from mdn-data");
499+
});
500+
501+
it("rejects one whose call is there with its syntax unwritten", () => {
502+
expect(() =>
503+
collectGradientFunctions(
504+
{ gradient: { syntax: "<linear-gradient()>" } },
505+
{
506+
"linear-gradient()": { syntax: null }
507+
}
508+
)
509+
).toThrow("`linear-gradient()` is gone from mdn-data");
510+
});
511+
512+
it("skips an at-rule whose syntax the dataset leaves unwritten", () => {
513+
expect(
514+
collectMergeableAtRules({
515+
"@font-face": { syntax: null },
516+
"@keyframes": {
517+
syntax: "@keyframes <keyframes-name> { <rule-list> }"
518+
},
519+
"@media": { syntax: "@media <media-query-list> { <rule-list> }" }
520+
})
521+
).toEqual(["media"]);
522+
});
523+
524+
it("rejects an `at-rules.json` without a rule replaced by name", () => {
525+
expect(() => collectMergeableAtRules({})).toThrow(
526+
"`@keyframes` is gone from mdn-data"
527+
);
528+
});
529+
});
530+
391531
describe("the file those tables are emitted into", () => {
392532
// Prettier reaches its ESM entry through a dynamic import, which the `vm`
393533
// shims Bun and Deno run jest on reject; `lint:special` makes the same

‎tooling/generate-css-data.js‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,12 @@ const isSpelledClass = (name) => {
713713
/**
714714
* A stated class is stated because no dataset writes it out; once one does, the
715715
* derived spelling is the one to read.
716+
* @param {EXPECTED_ANY} syntaxTable the `syntaxes.json` to read
716717
* @returns {void}
717718
*/
718-
const checkStatedClassSpellings = () => {
719+
const checkStatedClassSpellings = (syntaxTable) => {
719720
for (const [name] of SUPPLEMENT.classSpellings) {
720-
if (syntaxes[name] !== undefined) {
721+
if (syntaxTable[name] !== undefined) {
721722
throw new Error(`\`<${name}>\` is spelled out by mdn-data now`);
722723
}
723724
}
@@ -1343,26 +1344,28 @@ const RULE_HOLDING_BLOCKS = [
13431344
* The gradient functions, each with the last position its own stop list means:
13441345
* `<color-stop-length>` runs to `100%`, `<color-stop-angle>` to the same turn
13451346
* spelled either way. Read off `<gradient>`, so a seventh needs nothing here.
1347+
* @param {EXPECTED_ANY} syntaxTable the `syntaxes.json` to read
1348+
* @param {EXPECTED_ANY} functionTable the `functions.json` to read
13461349
* @returns {[string, string[]][]} `[function, last-position spellings]`, sorted
13471350
*/
1348-
const collectGradientFunctions = () => {
1349-
const entry = syntaxes.gradient;
1351+
const collectGradientFunctions = (syntaxTable, functionTable) => {
1352+
const entry = syntaxTable.gradient;
13501353
if (entry === undefined) {
13511354
throw new Error("`<gradient>` is gone from mdn-data");
13521355
}
13531356
/** @type {[string, string[]][]} */
13541357
const out = [];
13551358
for (const name of entry.syntax.match(/<([a-z-]+)\(\)>/g) || []) {
13561359
const bare = name.slice(1, -3);
1357-
const call = functions[`${bare}()`];
1360+
const call = functionTable[`${bare}()`];
13581361
if (call === undefined || typeof call.syntax !== "string") {
13591362
throw new Error(`\`${bare}()\` is gone from mdn-data`);
13601363
}
13611364
// The stop list is reached through the `<x-gradient-syntax>` the call names.
13621365
const named = /<([a-z-]+)>/.exec(call.syntax);
13631366
const body =
1364-
named !== null && syntaxes[named[1]] !== undefined
1365-
? syntaxes[named[1]].syntax
1367+
named !== null && syntaxTable[named[1]] !== undefined
1368+
? syntaxTable[named[1]].syntax
13661369
: call.syntax;
13671370
const angular = body.includes("<angular-color-stop-list>");
13681371
out.push([bare, angular ? ["100%", "360deg", "1turn"] : ["100%"]]);
@@ -1374,20 +1377,21 @@ const collectGradientFunctions = () => {
13741377
* The at-rules whose adjacent blocks merge into one: their block holds rules, and
13751378
* their prelude states a condition rather than naming the one thing the block
13761379
* belongs to (`@keyframes`, whose later block replaces the earlier).
1380+
* @param {EXPECTED_ANY} atRuleTable the `at-rules.json` to read
13771381
* @returns {string[]} the names without the `@`, sorted
13781382
*/
1379-
const collectMergeableAtRules = () => {
1383+
const collectMergeableAtRules = (atRuleTable) => {
13801384
const replaced = new Set(SUPPLEMENT.replacedByNameAtRules);
13811385
const out = [];
1382-
for (const [name, entry] of Object.entries(atRules)) {
1386+
for (const [name, entry] of Object.entries(atRuleTable)) {
13831387
const syntax = entry.syntax;
13841388
if (typeof syntax !== "string") continue;
13851389
if (!RULE_HOLDING_BLOCKS.some((one) => syntax.includes(one))) continue;
13861390
const bare = name.slice(1);
13871391
if (!replaced.has(bare)) out.push(bare);
13881392
}
13891393
for (const one of replaced) {
1390-
const entry = atRules[`@${one}`];
1394+
const entry = atRuleTable[`@${one}`];
13911395
if (entry === undefined) {
13921396
throw new Error(`\`@${one}\` is gone from mdn-data`);
13931397
}
@@ -3353,11 +3357,11 @@ const collectData = () => {
33533357
])
33543358
].sort();
33553359
const [positionXKeywords, positionYKeywords] = collectPositionKeywordAxes();
3356-
checkStatedClassSpellings();
3360+
checkStatedClassSpellings(syntaxes);
33573361
const shorthandInitialKeywords = collectShorthandInitialKeywords();
33583362
const shadowProperties = collectShadowProperties();
3359-
const mergeableAtRules = collectMergeableAtRules();
3360-
const gradientFunctions = collectGradientFunctions();
3363+
const mergeableAtRules = collectMergeableAtRules(atRules);
3364+
const gradientFunctions = collectGradientFunctions(syntaxes, functions);
33613365
const fontStretchPercentages = collectFontStretchPercentages();
33623366
const filterFunctionOmitted = collectFilterFunctionOmitted();
33633367
const shorterColorSpellings = collectShorterColorSpellings(colorNames);
@@ -3904,6 +3908,12 @@ if (require.main === module) generate();
39043908
module.exports.DATA_TARGET = TARGET;
39053909
module.exports.acceptedValues = acceptedValues;
39063910
module.exports.assertClassesArePrintable = assertClassesArePrintable;
3911+
module.exports.checkStatedClassSpellings = checkStatedClassSpellings;
39073912
module.exports.collectData = collectData;
3913+
module.exports.collectGradientFunctions = collectGradientFunctions;
3914+
module.exports.collectMergeableAtRules = collectMergeableAtRules;
3915+
module.exports.isSpelledSyntax = isSpelledSyntax;
39083916
module.exports.parseValueSyntax = parseValueSyntax;
3917+
module.exports.shorthandSlots = shorthandSlots;
3918+
module.exports.slotSpellings = slotSpellings;
39093919
module.exports.walkValueSyntax = walkValueSyntax;

0 commit comments

Comments
 (0)