From e55305faded1440d23c1cd8edbf7dff2f377b57e Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 3 Nov 2022 15:45:18 +0100 Subject: [PATCH] Add more tests for duplicated named capture groups. Fixes #3704. --- .../named-groups/duplicate-names-exec.js | 35 +++++++++++++++++++ .../named-groups/duplicate-names-match.js | 27 ++++++++++---- .../named-groups/duplicate-names-matchall.js | 30 ++++++++++++++++ .../duplicate-names-replaceall.js | 15 ++++++++ .../named-groups/duplicate-names-search.js | 11 ++++++ .../named-groups/duplicate-names-split.js | 12 +++++++ .../named-groups/duplicate-names-test.js | 21 +++++++++++ 7 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 test/built-ins/RegExp/named-groups/duplicate-names-exec.js create mode 100644 test/built-ins/RegExp/named-groups/duplicate-names-matchall.js create mode 100644 test/built-ins/RegExp/named-groups/duplicate-names-replaceall.js create mode 100644 test/built-ins/RegExp/named-groups/duplicate-names-search.js create mode 100644 test/built-ins/RegExp/named-groups/duplicate-names-split.js create mode 100644 test/built-ins/RegExp/named-groups/duplicate-names-test.js diff --git a/test/built-ins/RegExp/named-groups/duplicate-names-exec.js b/test/built-ins/RegExp/named-groups/duplicate-names-exec.js new file mode 100644 index 00000000000..a4a15817c50 --- /dev/null +++ b/test/built-ins/RegExp/named-groups/duplicate-names-exec.js @@ -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(/(?a)|(?b)/.exec("bab"), ["b", undefined, "b"]); +assert.compareArray(/(?b)|(?a)/.exec("bab"), ["b", "b", undefined]); + +assert.compareArray(/(?:(?a)|(?b))\k/.exec("aa"), ["aa", "a", undefined]); +assert.compareArray(/(?:(?a)|(?b))\k/.exec("bb"), ["bb", undefined, "b"]); + +let matchResult = /(?:(?:(?a)|(?b))\k){2}/.exec("aabb"); +assert.compareArray(matchResult, ["aabb", undefined, "b"]); +assert.sameValue(matchResult.groups.x, "b"); + +assert.sameValue(/(?:(?:(?a)|(?b))\k){2}/.exec("abab"), null); + +assert.sameValue(/(?:(?a)|(?b))\k/.exec("abab"), null); + +assert.sameValue(/(?:(?a)|(?b))\k/.exec("cdef"), null); + +assert.compareArray(/^(?:(?x)|(?y)|z)\k$/.exec("xx"), ["xx", "x", undefined]); +assert.compareArray(/^(?:(?x)|(?y)|z)\k$/.exec("z"), ["z", undefined, undefined]); +assert.sameValue(/^(?:(?x)|(?y)|z)\k$/.exec("zz"), null); +assert.compareArray(/(?x)|(?:zy\k)/.exec("zy"), ["zy", undefined]); + +assert.compareArray(/^(?:(?x)|(?y)|z){2}\k$/.exec("xz"), ["xz", undefined, undefined]); +assert.compareArray(/^(?:(?x)|(?y)|z){2}\k$/.exec("yz"), ["yz", undefined, undefined]); +assert.sameValue(/^(?:(?x)|(?y)|z){2}\k$/.exec("xzx"), null); +assert.sameValue(/^(?:(?x)|(?y)|z){2}\k$/.exec("yzy"), null); diff --git a/test/built-ins/RegExp/named-groups/duplicate-names-match.js b/test/built-ins/RegExp/named-groups/duplicate-names-match.js index ac801ed3982..65892d59549 100644 --- a/test/built-ins/RegExp/named-groups/duplicate-names-match.js +++ b/test/built-ins/RegExp/named-groups/duplicate-names-match.js @@ -8,15 +8,28 @@ features: [regexp-duplicate-named-groups] includes: [compareArray.js] ---*/ -assert.compareArray(["b", undefined, "b"], "bab".match(/(?a)|(?b)/)); -assert.compareArray(["b", "b", undefined], "bab".match(/(?b)|(?a)/)); +assert.compareArray("bab".match(/(?a)|(?b)/), ["b", undefined, "b"]); +assert.compareArray("bab".match(/(?b)|(?a)/), ["b", "b", undefined]); -assert.compareArray(["aa", "a", undefined], "aa".match(/(?:(?a)|(?b))\k/)); -assert.compareArray(["bb", undefined, "b"], "bb".match(/(?:(?a)|(?b))\k/)); +assert.compareArray("aa".match(/(?:(?a)|(?b))\k/), ["aa", "a", undefined]); +assert.compareArray("bb".match(/(?:(?a)|(?b))\k/), ["bb", undefined, "b"]); let matchResult = "aabb".match(/(?:(?:(?a)|(?b))\k){2}/); -assert.compareArray(["aabb", undefined, "b"], matchResult); +assert.compareArray(matchResult, ["aabb", undefined, "b"]); assert.sameValue(matchResult.groups.x, "b"); -let notMatched = "abab".match(/(?:(?:(?a)|(?b))\k){2}/); -assert.sameValue(notMatched, null); +assert.sameValue("abab".match(/(?:(?:(?a)|(?b))\k){2}/), null); + +assert.sameValue("abab".match(/(?:(?a)|(?b))\k/), null); + +assert.sameValue("cdef".match(/(?:(?a)|(?b))\k/), null); + +assert.compareArray("xx".match(/^(?:(?x)|(?y)|z)\k$/), ["xx", "x", undefined]); +assert.compareArray("z".match(/^(?:(?x)|(?y)|z)\k$/), ["z", undefined, undefined]); +assert.sameValue("zz".match(/^(?:(?x)|(?y)|z)\k$/), null); +assert.compareArray("zy".match(/(?x)|(?:zy\k)/), ["zy", undefined]); + +assert.compareArray("xz".match(/^(?:(?x)|(?y)|z){2}\k$/), ["xz", undefined, undefined]); +assert.compareArray("yz".match(/^(?:(?x)|(?y)|z){2}\k$/), ["yz", undefined, undefined]); +assert.sameValue("xzx".match(/^(?:(?x)|(?y)|z){2}\k$/), null); +assert.sameValue("yzy".match(/^(?:(?x)|(?y)|z){2}\k$/), null); diff --git a/test/built-ins/RegExp/named-groups/duplicate-names-matchall.js b/test/built-ins/RegExp/named-groups/duplicate-names-matchall.js new file mode 100644 index 00000000000..761b32d8074 --- /dev/null +++ b/test/built-ins/RegExp/named-groups/duplicate-names-matchall.js @@ -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(/(?a)|(?b)/g), + [ + ["b", undefined, "b"], + ["a", "a", undefined], + ["b", undefined, "b"], + ]); +matchesIterator("bab".matchAll(/(?b)|(?a)/g), + [ + ["b", "b", undefined], + ["a", undefined, "a"], + ["b", "b", undefined], + ]); diff --git a/test/built-ins/RegExp/named-groups/duplicate-names-replaceall.js b/test/built-ins/RegExp/named-groups/duplicate-names-replaceall.js new file mode 100644 index 00000000000..4cd892c35b7 --- /dev/null +++ b/test/built-ins/RegExp/named-groups/duplicate-names-replaceall.js @@ -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(/(?a)|(?b)/g, "[$]"), "[a][b]x[a][b]"); +assert.sameValue("baxba".replaceAll(/(?a)|(?b)/g, "[$]"), "[b][a]x[b][a]"); + +assert.sameValue("abxab".replaceAll(/(?a)|(?b)/g, "[$][$1][$2]"), "[a][a][][b][][b]x[a][a][][b][][b]"); +assert.sameValue("baxba".replaceAll(/(?a)|(?b)/g, "[$][$1][$2]"), "[b][][b][a][a][]x[b][][b][a][a][]"); + diff --git a/test/built-ins/RegExp/named-groups/duplicate-names-search.js b/test/built-ins/RegExp/named-groups/duplicate-names-search.js new file mode 100644 index 00000000000..18c484e5abd --- /dev/null +++ b/test/built-ins/RegExp/named-groups/duplicate-names-search.js @@ -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(/(?a)|(?b)/), 1); +assert.sameValue("xba".search(/(?a)|(?b)/), 1); diff --git a/test/built-ins/RegExp/named-groups/duplicate-names-split.js b/test/built-ins/RegExp/named-groups/duplicate-names-split.js new file mode 100644 index 00000000000..ebc03cafd86 --- /dev/null +++ b/test/built-ins/RegExp/named-groups/duplicate-names-split.js @@ -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(/(?a)|(?b)/), ["x", "a", undefined, "", undefined, "b", ""]); +assert.compareArray("xba".split(/(?a)|(?b)/), ["x", undefined, "b", "", "a", undefined, ""]); diff --git a/test/built-ins/RegExp/named-groups/duplicate-names-test.js b/test/built-ins/RegExp/named-groups/duplicate-names-test.js new file mode 100644 index 00000000000..ddf1c5e0698 --- /dev/null +++ b/test/built-ins/RegExp/named-groups/duplicate-names-test.js @@ -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(/(?a)|(?b)/.test("bab")); +assert(/(?b)|(?a)/.test("bab")); + +assert(/(?:(?a)|(?b))\k/.test("aa")); +assert(/(?:(?a)|(?b))\k/.test("bb")); + +let matchResult = /(?:(?:(?a)|(?b))\k){2}/.test("aabb"); +assert(matchResult); + +let notMatched = /(?:(?:(?a)|(?b))\k){2}/.test("abab"); +assert.sameValue(notMatched, false);