QTestPrivate property tests: don't try to create abstract types

Amends 5743837a26, after which Qt
Positioning and Qt SCXML failed to build as some properties operate on
abstract classes.

Check whether we can instantiate the tested class before trying to do so,
otherwise return a default-constructed unique_ptr.

Pick-to: 6.6 6.5
Change-Id: Ida9d4375197a93438062b1e1473b4a2a22cc7054
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-09-28 09:10:25 +02:00
parent a1bdee4697
commit f791570b86

View File

@ -108,7 +108,12 @@ void testReadWritePropertyBasics(
std::function<char *(const PropertyType &)> represent =
[](const PropertyType &val) { return QTest::toString(val); },
std::function<std::unique_ptr<TestedClass>(void)> helperConstructor =
[]() { return std::make_unique<TestedClass>(); })
[]() {
if constexpr (std::is_default_constructible_v<TestedClass>)
return std::make_unique<TestedClass>();
else
return std::unique_ptr<TestedClass>();
})
{
// get the property
const QMetaObject *metaObject = instance.metaObject();
@ -281,7 +286,12 @@ void testWriteOncePropertyBasics(
std::function<char *(const PropertyType &)> represent =
[](const PropertyType &val) { return QTest::toString(val); },
std::function<std::unique_ptr<TestedClass>(void)> helperConstructor =
[]() { return std::make_unique<TestedClass>(); })
[]() {
if constexpr (std::is_default_constructible_v<TestedClass>)
return std::make_unique<TestedClass>();
else
return std::unique_ptr<TestedClass>();
})
{
Q_UNUSED(helperConstructor);