diff --git a/src/blackmisc/slot.h b/src/blackmisc/slot.h index 503f53d1a..6fd29159f 100644 --- a/src/blackmisc/slot.h +++ b/src/blackmisc/slot.h @@ -62,13 +62,13 @@ namespace BlackMisc //! True if the slot can be called, false if it is empty. operator bool() const { - return !this->isEmpty(); + return !this->isEmpty() && !this->hasNullObject(); } - //! True if the slot is empty, false if it can be called. + //! True if the slot is empty or object is null, false if it can be called. bool operator !() const { - return this->isEmpty(); + return this->isEmpty() || this->hasNullObject(); } //! True if the slot is empty, false if it can be called. @@ -77,6 +77,12 @@ namespace BlackMisc return ! m_function; } + //! True if the object is null + bool hasNullObject() const + { + return m_object.isNull(); + } + private: QPointer m_object; std::function m_function; diff --git a/tests/blackmisc/testblackmiscmain.cpp b/tests/blackmisc/testblackmiscmain.cpp index 73329e380..103bd8096 100644 --- a/tests/blackmisc/testblackmiscmain.cpp +++ b/tests/blackmisc/testblackmiscmain.cpp @@ -23,6 +23,7 @@ #include "testlibrarypath.h" #include "testmath.h" #include "testphysicalquantities.h" +#include "testslot.h" #include "testvaluecache.h" #include "testvariantandmap.h" #include "testweather.h" @@ -45,6 +46,7 @@ namespace BlackMiscTest CTestVariantAndMap variantAndMap; CTestInput inputTests; CTestIdentifier identifierTests; + CTestSlot slotTests; CTestValueCache valueCacheTests; CTestWeather weatherTests; CTestMath mathTests; @@ -56,6 +58,7 @@ namespace BlackMiscTest status |= QTest::qExec(&containerTests, argc, argv); status |= QTest::qExec(&variantAndMap, argc, argv); status |= QTest::qExec(&inputTests, argc, argv); + status |= QTest::qExec(&slotTests, argc, argv); status |= QTest::qExec(&valueCacheTests, argc, argv); status |= QTest::qExec(&weatherTests, argc, argv); status |= QTest::qExec(&mathTests, argc, argv); diff --git a/tests/blackmisc/testslot.cpp b/tests/blackmisc/testslot.cpp new file mode 100644 index 000000000..320beab50 --- /dev/null +++ b/tests/blackmisc/testslot.cpp @@ -0,0 +1,42 @@ +/* Copyright (C) 2016 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \cond PRIVATE_TESTS + +/*! + * \file + * \ingroup testblackmisc + */ + +#include "testslot.h" +#include "blackmisc/slot.h" + +#include + +using namespace BlackMisc; + +namespace BlackMiscTest +{ + + void CTestSlot::slotBasics() + { + QObject *obj = new QObject(this); + CSlot slot1 = { obj, &QObject::setObjectName }; + QVERIFY2(slot1, "Slot has valid object and function - can be called."); + + CSlot slot2 = { static_cast(nullptr), &QObject::setObjectName }; + QVERIFY2(!slot2, "Slot has an invalid pointer - cannot be called."); + + CSlot slot3; + QVERIFY2(!slot3, "Slot has an invalid pointer and invalid function - cannot be called."); + } + +} // namespace + +//! \endcond diff --git a/tests/blackmisc/testslot.h b/tests/blackmisc/testslot.h new file mode 100644 index 000000000..8a4d41eb8 --- /dev/null +++ b/tests/blackmisc/testslot.h @@ -0,0 +1,43 @@ +/* Copyright (C) 2016 + * swift project community / contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#ifndef BLACKMISCTEST_TESTSLOT_H +#define BLACKMISCTEST_TESTSLOT_H + +//! \cond PRIVATE_TESTS + +/*! + * \file + * \ingroup testblackmisc + */ + +#include + +namespace BlackMiscTest +{ + + //! CSlot tests + class CTestSlot : public QObject + { + Q_OBJECT + + public: + //! Standard test case constructor + explicit CTestSlot(QObject *parent = nullptr) : QObject(parent) {} + + private slots: + //! Basic unit tests for CSlot + void slotBasics(); + }; + +} // namespace + +//! \endcond + +#endif // guard