/
definition-syntax.js
117 lines (100 loc) · 3.68 KB
/
definition-syntax.js
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
import fs from 'fs';
import path from 'path';
import { createLexer, lexer as defaultLexer } from 'css-tree';
import { JsonLocator } from '../helpers/JsonLocator.js';
const __dirname = 'fixtures/definition-syntax';
export function forEachTest(factory) {
for (const file of Object.values(tests)) {
for (const test of Object.values(file)) {
const syntax = test.syntax;
const lexer = test.lexer ? createLexer(test.lexer) : defaultLexer;
for (const field in test) {
if (field !== 'valid' && field !== 'invalid') {
continue;
}
test[field].forEach((value, idx) => {
factory(
field,
test.test,
test.name + ' ' + field + '#' + idx,
lexer,
test.property,
value,
typeof syntax === 'string' ? syntax : undefined
);
});
}
}
}
}
export function forEachAtrulePreludeTest(factory) {
for (const [atruleName, testset] of Object.entries(atruleTests)) {
if (testset.prelude) {
const test = testset.prelude;
for (const field in test) {
if (field !== 'valid' && field !== 'invalid') {
continue;
}
test[field].forEach((value, idx) => {
factory(
field,
testset.test,
testset.name + ' @' + atruleName + ' prelude ' + field + '#' + idx,
defaultLexer,
atruleName,
value
);
});
}
}
}
}
export function forEachAtruleDescriptorTest(factory) {
for (const [atruleName, testset] of Object.entries(atruleTests)) {
if (testset.descriptors) {
for (const [descriptorName, test] of Object.entries(testset.descriptors)) {
for (const field of Object.keys(test)) {
if (field !== 'valid' && field !== 'invalid') {
continue;
}
test[field].forEach((value, idx) => {
factory(
field,
testset.test,
`${testset.name} @${atruleName} descriptor ${descriptorName} ${field}#${idx}`,
defaultLexer,
atruleName,
descriptorName,
value
);
});
}
}
}
}
}
export const tests = fs.readdirSync(__dirname).reduce(function(result, fn) {
if (path.extname(fn) === '.json' && fn !== 'atrules.json') {
const filename = path.join(__dirname, fn);
const tests = JSON.parse(fs.readFileSync(filename));
const locator = new JsonLocator(filename);
for (const key of Object.keys(tests)) {
const test = tests[key];
test.name = locator.get(key);
if (!test.syntax && !test.property && !test.type) {
test.syntax = key;
}
}
result[filename] = tests;
}
return result;
}, {});
export const atruleTests = (() => {
const filename = path.join(__dirname, 'atrules.json');
const tests = JSON.parse(fs.readFileSync(filename));
const locator = new JsonLocator(filename);
for (const key of Object.keys(tests)) {
tests[key].name = locator.get(key);
}
return tests;
})();