aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2019-01-22 13:38:44 +0100
committerLars Knoll <lars.knoll@qt.io>2019-01-25 21:55:55 +0000
commitcf04d5ee18344d45da538810654690bd3936f46a (patch)
treef3d2f81f54865d2ce713407490d48bd0549c4e98 /tests
parentf4ab0ab62496e63db72592845b285bab3d8388b9 (diff)
Bring behavior of String.replace() in line with other engines
"x".replace("x", "$1") gives "$1" in both JSC and V8, as there are no captures that could be used as a replacement for $1. Implement the same behavior as it's the most logical thing to do (even though it's undefined according to the spec). Two digit captures ($nm) work in a way that they get applied if $nm captures exist. If there are less than nm but more than n captures available $n is replaced by the n'th capture and m is copied over verbatim. Change-Id: I8b5f576f2c42c8334859ab7854dcdf07104dd35b Fixes: QTBUG-73152 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index cf7b9c7224..42d8f37ca8 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -220,6 +220,8 @@ private slots:
void functionToString_data();
void functionToString();
+ void stringReplace();
+
void protoChanges_QTBUG68369();
void multilineStrings();
@@ -4367,6 +4369,63 @@ void tst_QJSEngine::functionToString()
QCOMPARE(evaluationResult.toString(), expectedString);
}
+void tst_QJSEngine::stringReplace()
+{
+ QJSEngine engine;
+
+ QJSValue val = engine.evaluate("'x'.replace('x', '$1')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$1"));
+
+ val = engine.evaluate("'x'.replace('x', '$10')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$10"));
+
+ val = engine.evaluate("'x'.replace('x', '$01')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$01"));
+
+ val = engine.evaluate("'x'.replace('x', '$0')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$0"));
+
+ val = engine.evaluate("'x'.replace('x', '$00')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$00"));
+
+ val = engine.evaluate("'x'.replace(/(x)/, '$1')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("x"));
+
+ val = engine.evaluate("'x'.replace(/(x)/, '$01')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("x"));
+
+ val = engine.evaluate("'x'.replace(/(x)/, '$2')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$2"));
+
+ val = engine.evaluate("'x'.replace(/(x)/, '$02')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$02"));
+
+ val = engine.evaluate("'x'.replace(/(x)/, '$0')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$0"));
+
+ val = engine.evaluate("'x'.replace(/(x)/, '$00')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("$00"));
+
+ val = engine.evaluate("'x'.replace(/()()()()()()()()()(x)/, '$11')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("1"));
+
+ val = engine.evaluate("'x'.replace(/()()()()()()()()()(x)/, '$10')");
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString("x"));
+}
+
void tst_QJSEngine::protoChanges_QTBUG68369()
{
QJSEngine engine;