Skip to content

Commit

Permalink
add tests for proposal-duplicate-named-capturing-groups
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored and ptomato committed Aug 9, 2022
1 parent b42d184 commit 52284ba
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
);
Original file line number Diff line number Diff line change
@@ -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]);
17 changes: 17 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names-replace.js
Original file line number Diff line number Diff line change
@@ -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]");
19 changes: 19 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names.js
Original file line number Diff line number Diff line change
@@ -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");

0 comments on commit 52284ba

Please sign in to comment.