Skip to content

Commit

Permalink
Add exhaustive tests for RegExp Unicode property escapes (#971)
Browse files Browse the repository at this point in the history
Proposal: https://github.com/tc39/proposal-regexp-unicode-property-escapes

These tests have been generated by the script at https://github.com/mathiasbynens/unicode-property-escapes-tests. They check all the properties and values that should be supported by implementations against the symbols they’re supposed to match. False positives are detected as well.

Ref. #950.
Ref. tc39/proposal-regexp-unicode-property-escapes#4.
  • Loading branch information
mathiasbynens authored and leobalter committed Apr 13, 2017
1 parent 89e15ce commit 44b40e0
Show file tree
Hide file tree
Showing 367 changed files with 62,809 additions and 0 deletions.
32 changes: 32 additions & 0 deletions harness/regExpUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function buildString({ loneCodePoints, ranges }) {
const CHUNK_SIZE = 10000;
let result = String.fromCodePoint(...loneCodePoints);
for (const [start, end] of ranges) {
const codePoints = [];
for (let length = 0, codePoint = start; codePoint <= end; codePoint++) {
codePoints[length++] = codePoint;
if (length === CHUNK_SIZE) {
result += String.fromCodePoint(...codePoints);
codePoints.length = length = 0;
}
}
result += String.fromCodePoint(...codePoints);
}
return result;
}

function testPropertyEscapes(regex, string, expression) {
if (!regex.test(string)) {
for (const symbol of string) {
const hex = symbol
.codePointAt(0)
.toString(16)
.toUpperCase()
.padStart(6, "0");
assert(
regex.test(symbol),
`\`${ expression }\` should match U+${ hex } (\`${ symbol }\`)`
);
}
}
}
40 changes: 40 additions & 0 deletions test/built-ins/RegExp/property-escapes/ASCII.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2017 Mathias Bynens. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
author: Mathias Bynens
description: >
Unicode property escapes for `ASCII`
info: |
Generated by https://github.com/mathiasbynens/unicode-property-escapes-tests
Unicode v9.0.0
esid: sec-static-semantics-unicodematchproperty-p
features: [regexp-unicode-property-escapes]
includes: [regExpUtils.js]
---*/

const matchSymbols = buildString({
loneCodePoints: [],
ranges: [
[0x000000, 0x00007F]
]
});
testPropertyEscapes(
/^\p{ASCII}+$/u,
matchSymbols,
"\\p{ASCII}"
);

const nonMatchSymbols = buildString({
loneCodePoints: [],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000080, 0x00DBFF],
[0x00E000, 0x10FFFF]
]
});
testPropertyEscapes(
/^\P{ASCII}+$/u,
nonMatchSymbols,
"\\P{ASCII}"
);
55 changes: 55 additions & 0 deletions test/built-ins/RegExp/property-escapes/ASCII_Hex_Digit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2017 Mathias Bynens. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
author: Mathias Bynens
description: >
Unicode property escapes for `ASCII_Hex_Digit`
info: |
Generated by https://github.com/mathiasbynens/unicode-property-escapes-tests
Unicode v9.0.0
esid: sec-static-semantics-unicodematchproperty-p
features: [regexp-unicode-property-escapes]
includes: [regExpUtils.js]
---*/

const matchSymbols = buildString({
loneCodePoints: [],
ranges: [
[0x000030, 0x000039],
[0x000041, 0x000046],
[0x000061, 0x000066]
]
});
testPropertyEscapes(
/^\p{ASCII_Hex_Digit}+$/u,
matchSymbols,
"\\p{ASCII_Hex_Digit}"
);
testPropertyEscapes(
/^\p{AHex}+$/u,
matchSymbols,
"\\p{AHex}"
);

const nonMatchSymbols = buildString({
loneCodePoints: [],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x00002F],
[0x00003A, 0x000040],
[0x000047, 0x000060],
[0x000067, 0x00DBFF],
[0x00E000, 0x10FFFF]
]
});
testPropertyEscapes(
/^\P{ASCII_Hex_Digit}+$/u,
nonMatchSymbols,
"\\P{ASCII_Hex_Digit}"
);
testPropertyEscapes(
/^\P{AHex}+$/u,
nonMatchSymbols,
"\\P{AHex}"
);
Loading

0 comments on commit 44b40e0

Please sign in to comment.