forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-regexes.js
More file actions
68 lines (61 loc) · 1.71 KB
/
test-regexes.js
File metadata and controls
68 lines (61 loc) · 1.71 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
'use strict';
const assert = require('assert');
/**
* @typedef {import('../types').Identifier} Identifier
*
* @typedef {object} TestCase
* @property {string[]} features
* @property {string[]} matches
* @property {string[]} misses
*/
/** @type {Identifier} */
const bcd = require('..');
/**
* @param {string} dottedFeature
*/
function lookup(dottedFeature) {
const x = dottedFeature.split('.');
const feature = x.reduce((prev, current) => prev[current], bcd);
return feature;
}
/**
* @param {Identifier} feature
* @param {string[]} matches
* @param {string[]} misses
*/
function testToken(feature, matches, misses) {
const str =
feature.__compat.matches.regex_token ||
feature.__compat.matches.regex_value;
const regexp = new RegExp(str);
matches.forEach(match =>
assert.ok(regexp.test(match), `${regexp} did not match ${match}`),
);
misses.forEach(miss =>
assert.ok(!regexp.test(miss), `${regexp} erroneously matched ${miss}`),
);
}
/** @type {TestCase[]} */
const tests = [
{
features: ['css.properties.color.alpha_hexadecimal_notation'],
matches: ['#003399ff', '#0af9'],
misses: ['#00aaff', '#0af', 'green', '#greenish'],
},
{
features: ['css.properties.transform-origin.three_value_syntax'],
matches: [
'2px 30% 10px', // length, percentage, length
'right bottom -2cm', // two keywords and length
'calc(50px - 25%) 2px 1px', // lengths with calc
],
misses: [
'center', // one value syntax
'left 5px', // two value syntax
'left calc(10px - 50%)', // two value syntax with calc
],
},
];
tests.forEach(({ features, matches, misses }) => {
features.forEach(feature => testToken(lookup(feature), matches, misses));
});