Skip to content

Commit

Permalink
Add more tests for v flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tannal authored and ptomato committed Sep 20, 2024
1 parent dc7a22d commit 474af83
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/built-ins/RegExp/prototype/exec/regexp-builtin-exec-v-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (C) 2024 Tan Meng. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-regexpbuiltinexec
description: RegExpBuiltinExec behavior with 'v' flag
features: [regexp-v-flag]
includes: [compareArray.js]
---*/

const text = '𠮷a𠮷b𠮷';

function doExec(regex) {
const result = regex.exec(text);
return result ? [result[0], result.index] : null;
}

assert.compareArray(doExec(/𠮷/), ["𠮷", 0], "Basic exec without v flag");
assert.compareArray(doExec(/𠮷/v), ["𠮷", 0], "Exec with v flag");
assert.compareArray(doExec(/\p{Script=Han}/v), ["𠮷", 0], "Unicode property escapes with v flag");
assert.compareArray(doExec(/./v), ["𠮷", 0], "Dot with v flag");
assert.sameValue(doExec(/x/v), null, "Non-matching regex");

const regexWithGroups = /(\p{Script=Han})(.)/v;
const resultWithGroups = regexWithGroups.exec(text);
assert.sameValue(resultWithGroups[1], "𠮷", "Capture group 1");
assert.sameValue(resultWithGroups[2], "a", "Capture group 2");
assert.sameValue(resultWithGroups.index, 0, "Match index for groups");
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2024 Tan Meng. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-regexp.prototype-@@match
description: RegExp.prototype[@@match] behavior with 'v' flag
features: [Symbol.match, regexp-v-flag]
includes: [compareArray.js]
---*/

const text = '𠮷a𠮷b𠮷';

function doMatch(regex) {
return RegExp.prototype[Symbol.match].call(regex, text);
}

assert.compareArray(doMatch(/𠮷/g), ["𠮷", "𠮷", "𠮷"], "Basic match with g flag");
assert.compareArray(doMatch(/𠮷/v), ["𠮷"], "Match with v flag");
assert.compareArray(doMatch(/\p{Script=Han}/gv), ["𠮷", "𠮷", "𠮷"], "Unicode property escapes with v flag");
assert.compareArray(doMatch(/./gv), ["𠮷", "a", "𠮷", "b", "𠮷"], "Dot with v flag");
assert.sameValue(doMatch(/x/v), null, "Non-matching regex");
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2024 Tan Meng. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-regexp.prototype-@@matchall
description: RegExp.prototype[@@matchAll] behavior with 'v' flag
features: [Symbol.matchAll, regexp-v-flag]
---*/

const text = '𠮷a𠮷b𠮷';

function doMatchAll(regex) {
return Array.from(RegExp.prototype[Symbol.matchAll].call(regex, text), m => [m[0], m.index]);
}

assert.sameValue(
doMatchAll(/𠮷/g).toString(),
"𠮷,0,𠮷,3,𠮷,6",
"Basic matchAll with g flag"
);

assert.sameValue(
doMatchAll(/𠮷/gv).toString(),
"𠮷,0,𠮷,3,𠮷,6",
"matchAll with v flag"
);

assert.sameValue(
doMatchAll(/\p{Script=Han}/gv).toString(),
"𠮷,0,𠮷,3,𠮷,6",
"Unicode property escapes with v flag"
);

assert.sameValue(
doMatchAll(/./gv).toString(),
"𠮷,0,a,2,𠮷,3,b,5,𠮷,6",
"Dot with v flag"
);

assert.sameValue(
doMatchAll(/(?:)/gv).length,
6,
"Empty matches with v flag"
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2024 Tan Meng. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-regexp.prototype-@@replace
description: RegExp.prototype[@@replace] behavior with 'v' flag
features: [Symbol.replace, regexp-v-flag]
---*/

const text = '𠮷a𠮷b𠮷';

function doReplace(regex, replacement) {
return RegExp.prototype[Symbol.replace].call(regex, text, replacement);
}

assert.sameValue(doReplace(/𠮷/g, '-'), "-a-b-", "Basic replace with g flag");
assert.sameValue(doReplace(/𠮷/v, '-'), "-a𠮷b𠮷", "Replace with v flag");
assert.sameValue(doReplace(/\p{Script=Han}/gv, 'X'), "XaXbX", "Unicode property escapes with v flag");
assert.sameValue(doReplace(/./gv, '$&$&'), "𠮷𠮷aa𠮷𠮷bb𠮷𠮷", "Dot with v flag");
assert.sameValue(
doReplace(/./gv, (match, index) => `[${match}:${index}]`),
"[𠮷:0][a:2][𠮷:3][b:5][𠮷:6]",
"Replace with function"
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2024 Tan Meng. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-regexp.prototype-@@search
description: RegExp.prototype[@@search] behavior with 'v' flag
features: [Symbol.search, regexp-v-flag]
---*/

const text = '𠮷a𠮷b𠮷';

function doSearch(regex) {
return RegExp.prototype[Symbol.search].call(regex, text);
}

assert.sameValue(doSearch(/a/), 2, "Basic search without v flag");
assert.sameValue(doSearch(/a/v), 2, "Search with v flag");
assert.sameValue(doSearch(/𠮷/), 0, "Search for surrogate pair without v flag");
assert.sameValue(doSearch(/𠮷/v), 0, "Search for surrogate pair with v flag");
assert.sameValue(doSearch(/\p{Script=Han}/v), 0, "Unicode property escapes with v flag");
assert.sameValue(doSearch(/b./v), 5, "Dot with v flag");

0 comments on commit 474af83

Please sign in to comment.