From e1bc9db85149b89feb73f1690fd218de498b8b27 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 2 Apr 2020 09:28:23 +0200 Subject: Improve conversion between JS RegExp and QRegularExpression Add support for InvertedGreedinessOption and MultilineOption. Change-Id: I19dce6e356a7ec406640bb8858885cd576b4aa2f Reviewed-by: Ulf Hermann --- tests/auto/qml/qjsengine/tst_qjsengine.cpp | 57 +++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'tests/auto/qml/qjsengine') diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index 8dd0f7b188..1d859021ad 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -142,6 +142,8 @@ private slots: void qRegExpInport(); void qRegularExpressionImport_data(); void qRegularExpressionImport(); + void qRegularExpressionExport_data(); + void qRegularExpressionExport(); void dateRoundtripJSQtJS(); void dateRoundtripQtJSQt(); void dateConversionJSQt(); @@ -3287,7 +3289,6 @@ void tst_QJSEngine::qRegularExpressionImport_data() { QTest::addColumn("rx"); QTest::addColumn("string"); - QTest::addColumn("matched"); QTest::newRow("normal") << QRegularExpression("(test|foo)") << "test _ foo _ test _ Foo"; QTest::newRow("normal2") << QRegularExpression("(Test|Foo)") << "test _ foo _ test _ Foo"; @@ -3311,6 +3312,14 @@ void tst_QJSEngine::qRegularExpressionImport_data() QTest::newRow(".+ minimal") << QRegularExpression("^.+$") << ".+"; QTest::newRow("[.?] minimal") << QRegularExpression("^[.?]$") << ".?"; QTest::newRow("[.+] minimal") << QRegularExpression("^[.+]$") << ".+"; + QTest::newRow("aaa inverted greedyness") << QRegularExpression("a{2,5}", QRegularExpression::InvertedGreedinessOption) << "aAaAaaaaaAa"; + QTest::newRow("inverted greedyness") << QRegularExpression(".*\\} [*8]", QRegularExpression::InvertedGreedinessOption) << "}?} ?} *"; + QTest::newRow(".? inverted greedyness") << QRegularExpression(".?", QRegularExpression::InvertedGreedinessOption) << ".?"; + QTest::newRow(".+ inverted greedyness") << QRegularExpression(".+", QRegularExpression::InvertedGreedinessOption) << ".+"; + QTest::newRow("[.?] inverted greedyness") << QRegularExpression("[.?]", QRegularExpression::InvertedGreedinessOption) << ".?"; + QTest::newRow("[.+] inverted greedyness") << QRegularExpression("[.+]", QRegularExpression::InvertedGreedinessOption) << ".+"; + QTest::newRow("two lines") << QRegularExpression("^.*$") << "abc\ndef"; + QTest::newRow("multiline") << QRegularExpression("^.*$", QRegularExpression::MultilineOption) << "abc\ndef"; } void tst_QJSEngine::qRegularExpressionImport() @@ -3333,6 +3342,52 @@ void tst_QJSEngine::qRegularExpressionImport() QCOMPARE(result.property(i).toString(), match.captured(i)); } +void tst_QJSEngine::qRegularExpressionExport_data() +{ + QTest::addColumn("js"); + QTest::addColumn("regularexpression"); + + QTest::newRow("normal") << "/(test|foo)/" << QRegularExpression("(test|foo)"); + QTest::newRow("normal2") << "/(Test|Foo)/" << QRegularExpression("(Test|Foo)"); + QTest::newRow("case insensitive") << "/(test|foo)/i" << QRegularExpression("(test|foo)", QRegularExpression::CaseInsensitiveOption); + QTest::newRow("case insensitive2") << "/(Test|Foo)/i" << QRegularExpression("(Test|Foo)", QRegularExpression::CaseInsensitiveOption); + QTest::newRow("b(a*)(b*)") << "/b(a*)(b*)/i" << QRegularExpression("b(a*)(b*)", QRegularExpression::CaseInsensitiveOption); + QTest::newRow("greedy") << "/a*(a*)/i" << QRegularExpression("a*(a*)", QRegularExpression::CaseInsensitiveOption); + QTest::newRow("wildcard") << "/.*\\.txt/" << QRegularExpression(".*\\.txt"); + QTest::newRow("wildcard 2") << "/a.b\\.txt/" << QRegularExpression("a.b\\.txt"); + QTest::newRow("slash") << "/g\\/.*\\/s/i" << QRegularExpression("g\\/.*\\/s", QRegularExpression::CaseInsensitiveOption); + QTest::newRow("slash2") << "/g \\/ .* \\/ s/i" << QRegularExpression("g \\/ .* \\/ s", QRegularExpression::CaseInsensitiveOption); + QTest::newRow("fixed") << "/a\\*aa\\.a\\(ba\\)\\*a\\\\ba/i" << QRegularExpression("a\\*aa\\.a\\(ba\\)\\*a\\\\ba", QRegularExpression::CaseInsensitiveOption); + QTest::newRow("fixed insensitive") << "/A\\*A/i" << QRegularExpression("A\\*A", QRegularExpression::CaseInsensitiveOption); + QTest::newRow("fixed sensitive") << "/A\\*A/" << QRegularExpression("A\\*A"); + QTest::newRow("html") << "/(.*)<\\/b>/" << QRegularExpression("(.*)<\\/b>"); + QTest::newRow("html minimal") << "/^(.*)<\\/b>$/" << QRegularExpression("^(.*)<\\/b>$"); + QTest::newRow("aaa") << "/a{2,5}/" << QRegularExpression("a{2,5}"); + QTest::newRow("aaa minimal") << "/^a{2,5}$/" << QRegularExpression("^a{2,5}$"); + QTest::newRow("minimal") << "/^.*\\} [*8]$/" << QRegularExpression("^.*\\} [*8]$"); + QTest::newRow(".? minimal") << "/^.?$/" << QRegularExpression("^.?$"); + QTest::newRow(".+ minimal") << "/^.+$/" << QRegularExpression("^.+$"); + QTest::newRow("[.?] minimal") << "/^[.?]$/" << QRegularExpression("^[.?]$"); + QTest::newRow("[.+] minimal") << "/^[.+]$/" << QRegularExpression("^[.+]$"); + QTest::newRow("multiline") << "/^.*$/m" << QRegularExpression("^.*$", QRegularExpression::MultilineOption); +} + +void tst_QJSEngine::qRegularExpressionExport() +{ + QFETCH(QString, js); + QFETCH(QRegularExpression, regularexpression); + + QJSEngine eng; + QJSValue rexp; + rexp = eng.evaluate(js); + + QCOMPARE(rexp.isRegExp(), true); + QCOMPARE(rexp.isCallable(), false); + + QRegularExpression rx = qjsvalue_cast(rexp); + QCOMPARE(rx, regularexpression); +} + // QScriptValue::toDateTime() returns a local time, whereas JS dates // are always stored as UTC. Qt Script must respect the current time // zone, and correctly adjust for daylight saving time that may be in -- cgit v1.2.3