add tests for proposal-duplicate-named-capturing-groups

This commit is contained in:
Kevin Gibbons 2022-07-31 19:14:10 -07:00 committed by Philip Chimento
parent b42d184876
commit 52284ba4bb
5 changed files with 79 additions and 0 deletions

View File

@ -276,6 +276,10 @@ regexp-v-flag
# https://github.com/tc39/proposal-decorators
decorators
# Duplicate named capturing groups
# https://github.com/tc39/proposal-duplicate-named-capturing-groups
regexp-duplicate-named-groups
## Standard language features
#
# Language features that have been included in a published version of the

View File

@ -0,0 +1,24 @@
// Copyright 2022 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Enumeration order of the groups object with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
includes: [compareArray.js]
---*/
let regexp = /(?<y>a)(?<x>a)|(?<x>b)(?<y>b)/;
assert.compareArray(
Object.keys(regexp.exec("aa").groups),
["y", "x"],
"property enumeration order of the groups object is based on source order, not match order"
);
assert.compareArray(
Object.keys(regexp.exec("bb").groups),
["y", "x"],
"property enumeration order of the groups object is based on source order, not match order"
);

View File

@ -0,0 +1,15 @@
// Copyright 2022 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: match indices with duplicate named capture groups
esid: sec-makematchindicesindexpairarray
features: [regexp-duplicate-named-groups, regexp-match-indices]
includes: [compareArray.js]
---*/
let indices = "..ab".match(/(?<x>a)|(?<x>b)/d).indices;
assert.compareArray(indices.groups.x, [2, 3]);
indices = "..ba".match(/(?<x>a)|(?<x>b)/d).indices;
assert.compareArray(indices.groups.x, [2, 3]);

View File

@ -0,0 +1,17 @@
// Copyright 2022 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: String.prototype.replace behavior with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
---*/
assert.sameValue("ab".replace(/(?<x>a)|(?<x>b)/, "[$<x>]"), "[a]b");
assert.sameValue("ba".replace(/(?<x>a)|(?<x>b)/, "[$<x>]"), "[b]a");
assert.sameValue("ab".replace(/(?<x>a)|(?<x>b)/, "[$<x>][$1][$2]"), "[a][a][]b");
assert.sameValue("ba".replace(/(?<x>a)|(?<x>b)/, "[$<x>][$1][$2]"), "[b][][b]a");
assert.sameValue("ab".replace(/(?<x>a)|(?<x>b)/g, "[$<x>]"), "[a][b]");
assert.sameValue("ba".replace(/(?<x>a)|(?<x>b)/g, "[$<x>]"), "[b][a]");

View File

@ -0,0 +1,19 @@
// Copyright 2022 Kevin Gibbons. 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(["b", "b"], "bab".match(/(?<x>a)|(?<x>b)/));
assert.compareArray(["b", "b"], "bab".match(/(?<x>b)|(?<x>a)/));
assert.compareArray(["aa", "aa", undefined], "aa".match(/(?:(?<x>a)|(?<x>b))\k<x>/));
assert.compareArray(["bb", undefined, "bb"], "bb".match(/(?:(?<x>a)|(?<x>b))\k<x>/));
let matchResult = "aabb".match(/(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/);
assert.compareArray(["aabb", undefined, "bb"], matchResult);
assert.sameValue(matchResult.groups.x, "bb");