diff options
author | Marc Mutz <[email protected]> | 2022-03-03 07:53:30 +0100 |
---|---|---|
committer | Marc Mutz <[email protected]> | 2022-03-07 18:25:35 +0100 |
commit | 53890b5d3cd771d66d6b73940eb38e3c3fc6d60f (patch) | |
tree | 5b3dd6cbcb6e4ea86d9cc2a8f6d9cec96718e82b | |
parent | 944b5a8e3e1cb9552482a9abb2db8b6577c274dd (diff) |
qtestmouse.h: compile with QT_TYPESAFE_FLAGS
Also include the comprehensive tests for bool cast compilation which I
originally wrote to confirm that the QTEST_ASSERT() change should be™
correct.
Pick-to: 6.3
Task-number: QTBUG-101406
Change-Id: I9a2871bfd4be9999b7a720bec775bba7aeffbe24
Reviewed-by: Qt CI Bot <[email protected]>
Reviewed-by: Giuseppe D'Angelo <[email protected]>
-rw-r--r-- | src/testlib/qtestmouse.h | 8 | ||||
-rw-r--r-- | tests/auto/corelib/global/qflags/tst_qflags.cpp | 44 |
2 files changed, 48 insertions, 4 deletions
diff --git a/src/testlib/qtestmouse.h b/src/testlib/qtestmouse.h index 26d3b501450..62a9b5d9bf7 100644 --- a/src/testlib/qtestmouse.h +++ b/src/testlib/qtestmouse.h @@ -114,9 +114,9 @@ namespace QTest if (pos.isNull()) pos = QPoint(window->width() / 2, window->height() / 2); - QTEST_ASSERT(uint(stateKey) == 0 || stateKey & Qt::KeyboardModifierMask); + QTEST_ASSERT(!stateKey || stateKey & Qt::KeyboardModifierMask); - stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask); + stateKey &= Qt::KeyboardModifierMask; QPointF global = window->mapToGlobal(pos); QPointer<QWindow> w(window); @@ -201,9 +201,9 @@ namespace QTest return; } - QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask); + QTEST_ASSERT(!stateKey || stateKey & Qt::KeyboardModifierMask); - stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask); + stateKey &= Qt::KeyboardModifierMask; QEvent::Type meType; using namespace QTestPrivate; diff --git a/tests/auto/corelib/global/qflags/tst_qflags.cpp b/tests/auto/corelib/global/qflags/tst_qflags.cpp index 73a76ebcde5..e64ef3e1286 100644 --- a/tests/auto/corelib/global/qflags/tst_qflags.cpp +++ b/tests/auto/corelib/global/qflags/tst_qflags.cpp @@ -32,6 +32,7 @@ class tst_QFlags: public QObject { Q_OBJECT private slots: + void boolCasts() const; void operators() const; void testFlag() const; void testFlagZeroFlag() const; @@ -46,6 +47,49 @@ private slots: void adl(); }; +void tst_QFlags::boolCasts() const +{ + // This tests that the operator overloading is sufficient so that common + // idioms involving flags -> bool casts work as expected: + + const Qt::Alignment nonNull = Qt::AlignCenter; + const Qt::Alignment null = {}; + + // basic premiss: + QVERIFY(bool(nonNull)); + QVERIFY(!bool(null)); + + // The rest is just checking that stuff compiles: + + // QVERIFY should compile: + QVERIFY(nonNull); + QVERIFY(!null); + + // ifs should compile: + if (null) QFAIL("Can't contextually convert QFlags to bool!"); + if (!nonNull) QFAIL("Missing operator! on QFlags (shouldn't be necessary)."); + + // ternary should compile: + QVERIFY(nonNull ? true : false); + QVERIFY(!null ? true : false); + + // logical operators should compile: + QVERIFY(nonNull && true); + QVERIFY(nonNull || false); + QVERIFY(!null && true); + QVERIFY(!null || false); + + // ... in both directions: + QVERIFY(true && nonNull); + QVERIFY(false || nonNull); + QVERIFY(true && !null); + QVERIFY(false || !null); + + // ... and mixed: + QVERIFY(null || nonNull); + QVERIFY(!(null && nonNull)); +} + void tst_QFlags::operators() const { #define CHECK(op, LHS, RHS, RES) \ |