diff options
Diffstat (limited to 'tests/auto/corelib/io')
-rw-r--r-- | tests/auto/corelib/io/qdebug/CMakeLists.txt | 4 | ||||
-rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 73 |
2 files changed, 77 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qdebug/CMakeLists.txt b/tests/auto/corelib/io/qdebug/CMakeLists.txt index 39b92b9d2f..067e3be04b 100644 --- a/tests/auto/corelib/io/qdebug/CMakeLists.txt +++ b/tests/auto/corelib/io/qdebug/CMakeLists.txt @@ -10,3 +10,7 @@ qt_internal_add_test(tst_qdebug PUBLIC_LIBRARIES Qt::Concurrent ) + +if (APPLE) + target_compile_options(tst_qdebug PRIVATE -x objective-c++) +endif() diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 9b9696d7c1..a85d8328e0 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -52,6 +52,10 @@ struct ConvertsToQVariant { }; static_assert(!QTypeTraits::has_ostream_operator_v<QDebug, ConvertsToQVariant>); +#if defined(Q_OS_DARWIN) +#include <objc/runtime.h> +#include <Foundation/Foundation.h> +#endif class tst_QDebug: public QObject { @@ -88,6 +92,12 @@ private slots: void threadSafety() const; void toString() const; void noQVariantEndlessRecursion() const; +#if defined(Q_OS_DARWIN) + void objcInCppMode_data() const; + void objcInCppMode() const; + void objcInObjcMode_data() const; + void objcInObjcMode() const; +#endif }; void tst_QDebug::assignment() const @@ -873,6 +883,69 @@ void tst_QDebug::noQVariantEndlessRecursion() const qDebug() << var; } +#if defined(Q_OS_DARWIN) + +@interface MyObjcClass : NSObject +@end + +@implementation MyObjcClass : NSObject +- (NSString *)description +{ + return @"MyObjcClass is the best"; +} +@end + +void tst_QDebug::objcInCppMode_data() const +{ + QTest::addColumn<objc_object *>("object"); + QTest::addColumn<QString>("message"); + + QTest::newRow("nil") << static_cast<objc_object*>(nullptr) << QString::fromLatin1("(null)"); + + // Not an NSObject subclass + auto *nsproxy = reinterpret_cast<objc_object *>(class_createInstance(objc_getClass("NSProxy"), 0)); + QTest::newRow("NSProxy") << nsproxy << QString::fromLatin1("<NSProxy: 0x%1>").arg(uintptr_t(nsproxy), 1, 16); + + // Plain NSObject + auto *nsobject = reinterpret_cast<objc_object *>(class_createInstance(objc_getClass("NSObject"), 0)); + QTest::newRow("NSObject") << nsobject << QString::fromLatin1("<NSObject: 0x%1>").arg(uintptr_t(nsobject), 1, 16); + + auto str = QString::fromLatin1("foo"); + QTest::newRow("NSString") << reinterpret_cast<objc_object*>(str.toNSString()) << str; + + // Custom debug description + QTest::newRow("MyObjcClass") << reinterpret_cast<objc_object*>([[MyObjcClass alloc] init]) + << QString::fromLatin1("MyObjcClass is the best"); +} + +void tst_QDebug::objcInCppMode() const +{ + QFETCH(objc_object *, object); + QFETCH(QString, message); + + MessageHandlerSetter mhs(myMessageHandler); + { qDebug() << object; } + + QCOMPARE(s_msg, message); +} + +void tst_QDebug::objcInObjcMode_data() const +{ + objcInCppMode_data(); +} + +void tst_QDebug::objcInObjcMode() const +{ + QFETCH(objc_object *, object); + QFETCH(QString, message); + + MessageHandlerSetter mhs(myMessageHandler); + { qDebug() << static_cast<id>(object); } + + QCOMPARE(s_msg, message); +} +#endif + // Should compile: instentiation of unrelated operator<< should not cause cause compilation // error in QDebug operators (QTBUG-47375) class TestClassA {}; |