Skip to content

Commit 5d17c89

Browse files
authored
Add a few tests on consolidated CSS file (#1569)
This adds tests to guarantee that entries are unique in the consolidated CSS file (they should be by construction, tests are meant to capture bugs that we might miss if we update the consolidation logic in Reffy). Consolidation amends syntaxes of some of the entries. Tests also make sure that syntaxes can be parsed with CSSTree.
1 parent 40da077 commit 5d17c89

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/css/consolidated.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Test the consolidated css.json file in the curated view.
3+
*/
4+
5+
import { describe, it } from 'node:test';
6+
import { strict as assert } from 'node:assert';
7+
import consolidated from '../../curated/css.json' with { type: 'json' };
8+
import { definitionSyntax } from 'css-tree';
9+
10+
// Expected categories in the consolidated file
11+
const categories = {
12+
atrules: 'at-rule',
13+
functions: 'function',
14+
properties: 'property',
15+
selectors: 'selector',
16+
types: 'type'
17+
};
18+
19+
// Quick and dirty function to pluralize category labels
20+
function pluralize(word) {
21+
if (word === 'property') {
22+
return 'properties';
23+
}
24+
else {
25+
return word + 's';
26+
}
27+
}
28+
29+
describe(`The consolidated CSS file`, async () => {
30+
for (const [category, label] of Object.entries(categories)) {
31+
const list = consolidated[category];
32+
33+
it(`contains ${pluralize(label)}`, () => {
34+
assert(list);
35+
assert(list.length > 0);
36+
});
37+
38+
it(`does not contain duplicated ${pluralize(label)}`, () => {
39+
const keys = list.map(entry => entry.name +
40+
(entry.for ? ` for ${entry.for}` : ''));
41+
const duplicates = keys.filter((key, idx) =>
42+
keys.findIndex(k => k === key) !== idx);
43+
assert.deepEqual(duplicates, []);
44+
});
45+
46+
it(`contains valid ${label} syntaxes`, () => {
47+
const invalid = list
48+
.filter(entry => entry.value)
49+
.filter(entry => {
50+
const ast = definitionSyntax.parse(entry.value);
51+
return !ast.type;
52+
});
53+
assert.deepEqual(invalid, []);
54+
});
55+
}
56+
});

0 commit comments

Comments
 (0)