Skip to content

Commit

Permalink
Add more tests for duplicated named capture groups.
Browse files Browse the repository at this point in the history
Fixes #3704.
  • Loading branch information
Ms2ger committed Nov 3, 2022
1 parent 700f0a5 commit e55305f
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 7 deletions.
35 changes: 35 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names-exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2022 Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Matching behavior with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
includes: [compareArray.js]
---*/

assert.compareArray(/(?<x>a)|(?<x>b)/.exec("bab"), ["b", undefined, "b"]);
assert.compareArray(/(?<x>b)|(?<x>a)/.exec("bab"), ["b", "b", undefined]);

assert.compareArray(/(?:(?<x>a)|(?<x>b))\k<x>/.exec("aa"), ["aa", "a", undefined]);
assert.compareArray(/(?:(?<x>a)|(?<x>b))\k<x>/.exec("bb"), ["bb", undefined, "b"]);

let matchResult = /(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/.exec("aabb");
assert.compareArray(matchResult, ["aabb", undefined, "b"]);
assert.sameValue(matchResult.groups.x, "b");

assert.sameValue(/(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/.exec("abab"), null);

assert.sameValue(/(?:(?<x>a)|(?<x>b))\k<x>/.exec("abab"), null);

assert.sameValue(/(?:(?<x>a)|(?<x>b))\k<x>/.exec("cdef"), null);

assert.compareArray(/^(?:(?<a>x)|(?<a>y)|z)\k<a>$/.exec("xx"), ["xx", "x", undefined]);
assert.compareArray(/^(?:(?<a>x)|(?<a>y)|z)\k<a>$/.exec("z"), ["z", undefined, undefined]);
assert.sameValue(/^(?:(?<a>x)|(?<a>y)|z)\k<a>$/.exec("zz"), null);
assert.compareArray(/(?<a>x)|(?:zy\k<a>)/.exec("zy"), ["zy", undefined]);

assert.compareArray(/^(?:(?<a>x)|(?<a>y)|z){2}\k<a>$/.exec("xz"), ["xz", undefined, undefined]);
assert.compareArray(/^(?:(?<a>x)|(?<a>y)|z){2}\k<a>$/.exec("yz"), ["yz", undefined, undefined]);
assert.sameValue(/^(?:(?<a>x)|(?<a>y)|z){2}\k<a>$/.exec("xzx"), null);
assert.sameValue(/^(?:(?<a>x)|(?<a>y)|z){2}\k<a>$/.exec("yzy"), null);
27 changes: 20 additions & 7 deletions test/built-ins/RegExp/named-groups/duplicate-names-match.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@ features: [regexp-duplicate-named-groups]
includes: [compareArray.js]
---*/

assert.compareArray(["b", undefined, "b"], "bab".match(/(?<x>a)|(?<x>b)/));
assert.compareArray(["b", "b", undefined], "bab".match(/(?<x>b)|(?<x>a)/));
assert.compareArray("bab".match(/(?<x>a)|(?<x>b)/), ["b", undefined, "b"]);
assert.compareArray("bab".match(/(?<x>b)|(?<x>a)/), ["b", "b", undefined]);

assert.compareArray(["aa", "a", undefined], "aa".match(/(?:(?<x>a)|(?<x>b))\k<x>/));
assert.compareArray(["bb", undefined, "b"], "bb".match(/(?:(?<x>a)|(?<x>b))\k<x>/));
assert.compareArray("aa".match(/(?:(?<x>a)|(?<x>b))\k<x>/), ["aa", "a", undefined]);
assert.compareArray("bb".match(/(?:(?<x>a)|(?<x>b))\k<x>/), ["bb", undefined, "b"]);

let matchResult = "aabb".match(/(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/);
assert.compareArray(["aabb", undefined, "b"], matchResult);
assert.compareArray(matchResult, ["aabb", undefined, "b"]);
assert.sameValue(matchResult.groups.x, "b");

let notMatched = "abab".match(/(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/);
assert.sameValue(notMatched, null);
assert.sameValue("abab".match(/(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/), null);

assert.sameValue("abab".match(/(?:(?<x>a)|(?<x>b))\k<x>/), null);

assert.sameValue("cdef".match(/(?:(?<x>a)|(?<x>b))\k<x>/), null);

assert.compareArray("xx".match(/^(?:(?<a>x)|(?<a>y)|z)\k<a>$/), ["xx", "x", undefined]);
assert.compareArray("z".match(/^(?:(?<a>x)|(?<a>y)|z)\k<a>$/), ["z", undefined, undefined]);
assert.sameValue("zz".match(/^(?:(?<a>x)|(?<a>y)|z)\k<a>$/), null);
assert.compareArray("zy".match(/(?<a>x)|(?:zy\k<a>)/), ["zy", undefined]);

assert.compareArray("xz".match(/^(?:(?<a>x)|(?<a>y)|z){2}\k<a>$/), ["xz", undefined, undefined]);
assert.compareArray("yz".match(/^(?:(?<a>x)|(?<a>y)|z){2}\k<a>$/), ["yz", undefined, undefined]);
assert.sameValue("xzx".match(/^(?:(?<a>x)|(?<a>y)|z){2}\k<a>$/), null);
assert.sameValue("yzy".match(/^(?:(?<a>x)|(?<a>y)|z){2}\k<a>$/), null);
30 changes: 30 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names-matchall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: String.prototype.search behavior with duplicate named capture groups
esid: prod-GroupSpecifier
includes: [compareArray.js]
features: [regexp-duplicate-named-groups]
---*/

function matchesIterator(iterator, expected) {
const actual = [...iterator];
assert.sameValue(actual.length, expected.length, "lengths should be equal");
for (let i = 0; i < expected.length; ++i) {
assert.compareArray(actual[i], expected[i]);
}
}

matchesIterator("bab".matchAll(/(?<x>a)|(?<x>b)/g),
[
["b", undefined, "b"],
["a", "a", undefined],
["b", undefined, "b"],
]);
matchesIterator("bab".matchAll(/(?<x>b)|(?<x>a)/g),
[
["b", "b", undefined],
["a", undefined, "a"],
["b", "b", undefined],
]);
15 changes: 15 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names-replaceall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: String.prototype.replaceAll behavior with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
---*/

assert.sameValue("abxab".replaceAll(/(?<x>a)|(?<x>b)/g, "[$<x>]"), "[a][b]x[a][b]");
assert.sameValue("baxba".replaceAll(/(?<x>a)|(?<x>b)/g, "[$<x>]"), "[b][a]x[b][a]");

assert.sameValue("abxab".replaceAll(/(?<x>a)|(?<x>b)/g, "[$<x>][$1][$2]"), "[a][a][][b][][b]x[a][a][][b][][b]");
assert.sameValue("baxba".replaceAll(/(?<x>a)|(?<x>b)/g, "[$<x>][$1][$2]"), "[b][][b][a][a][]x[b][][b][a][a][]");

11 changes: 11 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2022 Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: String.prototype.search behavior with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
---*/

assert.sameValue("xab".search(/(?<x>a)|(?<x>b)/), 1);
assert.sameValue("xba".search(/(?<x>a)|(?<x>b)/), 1);
12 changes: 12 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names-split.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2022 Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: String.prototype.search behavior with duplicate named capture groups
esid: prod-GroupSpecifier
includes: [compareArray.js]
features: [regexp-duplicate-named-groups]
---*/

assert.compareArray("xab".split(/(?<x>a)|(?<x>b)/), ["x", "a", undefined, "", undefined, "b", ""]);
assert.compareArray("xba".split(/(?<x>a)|(?<x>b)/), ["x", undefined, "b", "", "a", undefined, ""]);
21 changes: 21 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2022 Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Matching behavior with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
includes: [compareArray.js]
---*/

assert(/(?<x>a)|(?<x>b)/.test("bab"));
assert(/(?<x>b)|(?<x>a)/.test("bab"));

assert(/(?:(?<x>a)|(?<x>b))\k<x>/.test("aa"));
assert(/(?:(?<x>a)|(?<x>b))\k<x>/.test("bb"));

let matchResult = /(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/.test("aabb");
assert(matchResult);

let notMatched = /(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/.test("abab");
assert.sameValue(notMatched, false);

0 comments on commit e55305f

Please sign in to comment.