From d3d395ab3dd378d26ce5060c10f97a1db4393e1d Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 11 Nov 2024 22:29:47 +0200 Subject: [PATCH] tst_QString: refactor one test 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 --- .../auto/corelib/text/qstring/tst_qstring.cpp | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index d54f40f114d..8e93d2153e6 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -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)); + QTest::ThrowOnFailEnabler throwOnFail; - // 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"); + 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()