Skip to content

Commit

Permalink
tst_QString: refactor one test
Browse files Browse the repository at this point in the history
By using a lambda and QTest::ThrowOnFailEnabler; this required changing
the custom QCOMPARE in those tests.

Drive-by, use the UTF16 character instead of the hexdecimal
representation, one of them is easier to read.

Change-Id: Ic319cc2afdda6a73e293ebf16eb4f8df469b6d72
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
ahmadsamir committed Nov 12, 2024
1 parent 68f970d commit d3d395a
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions tests/auto/corelib/text/qstring/tst_qstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5936,22 +5936,30 @@ void tst_QString::setRawData()

void tst_QString::setUnicode()
{
const QChar ptr[] = { QChar(0x1234), QChar(0x0000) };
const QChar ptr[] = { u'', QChar(0x0000) };

QString str;
QVERIFY(!str.isDetached());
str.setUnicode(ptr, 1);
// make sure that the data is copied
QVERIFY(str.constData() != ptr);
QVERIFY(str.isDetached());
QCOMPARE(str, QString(ptr, 1));

// make sure that the string is resized, even if the data is nullptr
str = u"test"_s;
QCOMPARE(str.size(), 4);
str.setUnicode(nullptr, 1);
QCOMPARE(str.size(), 1);
QCOMPARE(str, u"t");
QTest::ThrowOnFailEnabler throwOnFail;

auto doTest = [](const auto ptr, QString &str) mutable {
// make sure that the data is copied
QVERIFY(str.constData() != ptr);
QVERIFY(str.isDetached());
QCOMPARE(str, QString(ptr, 1));

// make sure that the string is resized, even if the data is nullptr
str = u"test"_s;
QCOMPARE(str.size(), 4);
str.setUnicode(nullptr, 1);
QCOMPARE(str.size(), 1);
QCOMPARE(str, u"t");
};

{
QString str;
QVERIFY(!str.isDetached());
str.setUnicode(ptr, 1);
doTest(ptr, str);
}
}

void tst_QString::fromStdString()
Expand Down

0 comments on commit d3d395a

Please sign in to comment.