-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more tests for duplicated named capture groups.
Fixes #3704.
- Loading branch information
Showing
7 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
test/built-ins/RegExp/named-groups/duplicate-names-exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/built-ins/RegExp/named-groups/duplicate-names-matchall.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
test/built-ins/RegExp/named-groups/duplicate-names-replaceall.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
test/built-ins/RegExp/named-groups/duplicate-names-search.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
test/built-ins/RegExp/named-groups/duplicate-names-split.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
test/built-ins/RegExp/named-groups/duplicate-names-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |