summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2021-08-03 11:35:58 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-09-18 18:04:36 +0000
commitb0e5ee4fcd49cebe66a941f35bcc3c878b016239 (patch)
tree716964ec903cf1c4821690b8336648750c74ee75 /tests
parent8910951a4500fdbdcd5118cde148347d1f1a2ceb (diff)
Rework tst_QString::remove_regexp() and its data table
The test was producing a warning about the invalid test, for which replace_regexp() had anticipated that warning; do the same in remove_regexp(). The two tests shared a date() method, but the remove test was a no-op on the tests with non-empty replacement text; move the column set-up and data rows with empty replacement to remove's data() function, from replace's, and reverse the direction of calling each other between data() functions, so each test gets the cases that are relevant to it and no spurious PASSes happen for no-op tests. In the process, give moved test-cases informative names; relocate the (entirely re-written) remove data function to beside its test; and eliminate a pointless local variable from both tests (it used to be needed when testing both QRegExp and QRegularExpression). Change-Id: I93dcfc444f984edf5c029f99306aff6bc95d554a Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 826e1963e3f3a821f31c2457242c4055f9820b0e) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/text/qstring/tst_qstring.cpp66
1 files changed, 36 insertions, 30 deletions
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp
index 970c6afb23..bc24e4dfd0 100644
--- a/tests/auto/corelib/text/qstring/tst_qstring.cpp
+++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp
@@ -680,13 +680,6 @@ void tst_QString::remove_string_data()
replace_string_data();
}
-#if QT_CONFIG(regularexpression)
-void tst_QString::remove_regexp_data()
-{
- replace_regexp_data();
-}
-#endif
-
void tst_QString::indexOf3_data()
{
indexOf2_data();
@@ -876,17 +869,9 @@ void tst_QString::replace_string_data()
#if QT_CONFIG(regularexpression)
void tst_QString::replace_regexp_data()
{
- QTest::addColumn<QString>("string" );
- QTest::addColumn<QString>("regexp" );
- QTest::addColumn<QString>("after" );
- QTest::addColumn<QString>("result" );
-
- QTest::newRow( "rem00" ) << QString("alpha") << QString("a+") << QString("") << QString("lph");
- QTest::newRow( "rem01" ) << QString("banana") << QString("^.a") << QString("") << QString("nana");
- QTest::newRow( "rem02" ) << QString("") << QString("^.a") << QString("") << QString("");
- QTest::newRow( "rem03" ) << QString("") << QString("^.a") << QString() << QString("");
- QTest::newRow( "rem04" ) << QString() << QString("^.a") << QString("") << QString();
- QTest::newRow( "rem05" ) << QString() << QString("^.a") << QString() << QString();
+ remove_regexp_data(); // Sets up the columns, adds rows with empty replacement text.
+ // Columns (all QString): string, regexp, after, result; string.replace(regexp, after) == result
+ // Test-cases with empty after (replacement text, third column) go in remove_regexp_data()
QTest::newRow( "rep00" ) << QString("A <i>bon mot</i>.") << QString("<i>([^<]*)</i>") << QString("\\emph{\\1}") << QString("A \\emph{bon mot}.");
QTest::newRow( "rep01" ) << QString("banana") << QString("^.a()") << QString("\\1") << QString("nana");
@@ -914,7 +899,6 @@ void tst_QString::replace_regexp_data()
<< QString("a9a8a7a6a5nmlkjii0hh0gg0ff0ee0dd0cc0bb0a");
QTest::newRow("backref10") << QString("abc") << QString("((((((((((((((abc))))))))))))))")
<< QString("\\0\\01\\011") << QString("\\0\\01\\011");
- QTest::newRow("invalid") << QString("") << QString("invalid regex\\") << QString("") << QString("");
}
#endif
@@ -3202,12 +3186,11 @@ void tst_QString::replace_regexp()
QFETCH( QString, regexp );
QFETCH( QString, after );
- QString s2 = string;
QRegularExpression regularExpression(regexp);
if (!regularExpression.isValid())
QTest::ignoreMessage(QtWarningMsg, "QString::replace: invalid QRegularExpression object");
- s2.replace( regularExpression, after );
- QTEST( s2, "result" );
+ string.replace(regularExpression, after);
+ QTEST(string, "result");
}
void tst_QString::replace_regexp_extra()
@@ -3294,19 +3277,42 @@ void tst_QString::remove_string()
}
#if QT_CONFIG(regularexpression)
+void tst_QString::remove_regexp_data()
+{
+ QTest::addColumn<QString>("string");
+ QTest::addColumn<QString>("regexp");
+ QTest::addColumn<QString>("after"); // For the benefit of replace_regexp; empty = remove.
+ QTest::addColumn<QString>("result");
+ // string.remove(regexp) == result
+
+ QTest::newRow("alpha:s/a+//")
+ << QString("alpha") << QString("a+") << QString("") << QString("lph");
+ QTest::newRow("banana:s/^.a//")
+ << QString("banana") << QString("^.a") << QString("") << QString("nana");
+ QTest::newRow("<empty>:s/^.a//")
+ << QString("") << QString("^.a") << QString("") << QString("");
+ // The null-vs-empty distinction in after is only relevant to repplace_regexp(), but
+ // include both cases here to keep after's "empty here, non-empty there" rule simple.
+ QTest::newRow("<empty>:s/^.a/<null>/")
+ << QString("") << QString("^.a") << QString() << QString("");
+ QTest::newRow("<null>:s/^.a//") << QString() << QString("^.a") << QString("") << QString();
+ QTest::newRow("<null>s/.a/<null>/") << QString() << QString("^.a") << QString() << QString();
+ QTest::newRow("invalid")
+ << QString("") << QString("invalid regex\\") << QString("") << QString("");
+}
+
void tst_QString::remove_regexp()
{
QFETCH( QString, string );
QFETCH( QString, regexp );
- QFETCH( QString, after );
+ QTEST(QString(), "after"); // non-empty replacement text tests should go in replace_regexp_data()
- if ( after.length() == 0 ) {
- QString s2 = string;
- s2.remove( QRegularExpression(regexp) );
- QTEST( s2, "result" );
- } else {
- QCOMPARE( 0, 0 ); // shut Qt Test
- }
+ QRegularExpression regularExpression(regexp);
+ // remove() delegates to replace(), which produces this warning:
+ if (!regularExpression.isValid())
+ QTest::ignoreMessage(QtWarningMsg, "QString::replace: invalid QRegularExpression object");
+ string.remove(regularExpression);
+ QTEST(string, "result");
}
#endif