From a6f3449cd87c28da7de826bc2c99402f808a9fae Mon Sep 17 00:00:00 2001 From: Alexey Shvayka Date: Thu, 2 Apr 2020 14:57:17 +0300 Subject: [PATCH] Add string replacement test --- .../prototype/Symbol.replace/named-groups.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test/built-ins/RegExp/prototype/Symbol.replace/named-groups.js diff --git a/test/built-ins/RegExp/prototype/Symbol.replace/named-groups.js b/test/built-ins/RegExp/prototype/Symbol.replace/named-groups.js new file mode 100644 index 0000000000..15ff9a80d9 --- /dev/null +++ b/test/built-ins/RegExp/prototype/Symbol.replace/named-groups.js @@ -0,0 +1,44 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-getsubstitution +description: > + RegExp.prototype[Symbol.replace] works with named capture references as expected. + (string replacement) +info: | + GetSubstitution ( matched, str, position, captures, namedCaptures, replacement ) + + Table: Replacement Text Symbol Substitutions + + Unicode Characters: $< + Replacement text: + 1. If namedCaptures is undefined, the replacement text is the literal string $<. + 2. Else, + a. Assert: Type(namedCaptures) is Object. + b. Scan until the next > U+003E (GREATER-THAN SIGN). + c. If none is found, the replacement text is the String "$<". + d. Else, + i. Let groupName be the enclosed substring. + ii. Let capture be ? Get(namedCaptures, groupName). + iii. If capture is undefined, replace the text through > with the empty String. + iv. Otherwise, replace the text through > with ? ToString(capture). +features: [Symbol.replace, regexp-named-groups] +---*/ + +assert.sameValue(/b/u[Symbol.replace]("abc", "$&$d"), "c$dc$d"); +assert.sameValue(/(b)./[Symbol.replace]("abc", "$$1"), "a$b"); + +assert.sameValue(/(?.)(?.)/[Symbol.replace]("abc", "$$"), "bac"); +assert.sameValue(/(?.)(?.)/gu[Symbol.replace]("abc", "$2$$1"), "baac"); +assert.sameValue(/(?b)/u[Symbol.replace]("abc", "c$d"), "acdc"); +assert.sameValue(/(?.)/g[Symbol.replace]("abc", "$<$1>"), ""); +assert.sameValue(/(?b)/[Symbol.replace]("abc", "$<>"), "ac"); +assert.sameValue(/(?.)(?.)/g[Symbol.replace]("abc", "$2$1"), "bac"); +assert.sameValue(/(?b)/u[Symbol.replace]("abc", "$.)/gu[Symbol.replace]("abc", "$"), ""); +assert.sameValue(/(?b)/[Symbol.replace]("abc", "$$$&"), "a$bc"); + +assert.sameValue(/(?<𝒜>b)/u[Symbol.replace]("abc", "d$<𝒜>$`"), "adbac"); +assert.sameValue(/(?<$𐒤>b)/gu[Symbol.replace]("abc", "$'$<$𐒤>d"), "acbdc");