From 222a30eee928177f482ff5851bc6f86f96026fd7 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Mon, 30 Jun 2014 00:15:54 +0200 Subject: [PATCH] CDictionary unit tests refs #281 --- tests/blackmisc/testcontainers.cpp | 79 +++++++++++++ tests/blackmisc/testcontainers.h | 1 + tests/blackmisc/testvalueobject.cpp | 167 ++++++++++++++++++++++++++++ tests/blackmisc/testvalueobject.h | 121 ++++++++++++++++++++ 4 files changed, 368 insertions(+) create mode 100644 tests/blackmisc/testvalueobject.cpp create mode 100644 tests/blackmisc/testvalueobject.h diff --git a/tests/blackmisc/testcontainers.cpp b/tests/blackmisc/testcontainers.cpp index c0068b013..e89bd05d0 100644 --- a/tests/blackmisc/testcontainers.cpp +++ b/tests/blackmisc/testcontainers.cpp @@ -4,10 +4,12 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "testcontainers.h" +#include "testvalueobject.h" #include "blackmisc/blackmiscfreefunctions.h" #include "blackmisc/collection.h" #include "blackmisc/sequence.h" #include "blackmisc/avcallsignlist.h" +#include "blackmisc/dictionary.h" #include #include #include @@ -124,4 +126,81 @@ namespace BlackMiscTest QVERIFY2(found.size() == 1, "found"); } + void CTestContainers::dictionaryBasics() + { + CTestValueObject key1("Key1", "This is key object 1"); + CTestValueObject key2("Key2", "This is key object 2"); + + CTestValueObject value1("Value1", "This is value object 1"); + CTestValueObject value2("Value2", "This is value object 2"); + + CValueObjectDictionary d1; + d1.insert(key1, value1); + d1.insert(key2, value2); + + CValueObjectDictionary d3; + d3.insert(key1, value1); + d3.insert(key2, value2); + + CValueObjectDictionary d2; + + // Operators + QVERIFY2(d1 != d2, "Inequality operator failed"); + QVERIFY2(d1 == d3, "Equality operator failed"); + + // size + QVERIFY2(d1.size() == 2, "size() wrong"); + QVERIFY2(d1.size() == d1.count(), "size() is different to count()"); + + // clear/empty + d1.clear(); + QVERIFY2(d1.isEmpty(), "clear failed"); + d1.insert(key1, value1); + d1.insert(key2, value2); + + // keysCollection + CCollection keyCollection = d1.keysCollection(); + QVERIFY2(keyCollection.size() == 2, "keysCollection size wrong"); + + // keysSequence + CSequence keySequence = d1.keysSequence(); + QVERIFY2(keySequence.size() == 2, "keysSequence size wrong"); + + // findKeyBy + d2 = d1.findKeyBy(&CTestValueObject::getName, QString("Key1")); + QVERIFY2(d2.size() == 1, "findKeyBy returned wrong container"); + CTestValueObject o1 = d2.value(key1); + QVERIFY2(o1.getName() == "Value1", "findKeyBy returned wrong container"); + + // findValueBy + d2 = d1.findValueBy(&CTestValueObject::getName, QString("Value1")); + QVERIFY2(d2.size() == 1, "findValueBy returned wrong container"); + o1 = d2.value(key1); + QVERIFY2(o1.getName() == "Value1", "findKeyBy returned wrong container"); + + // containsByKey + QVERIFY2(d1.containsByKey(&CTestValueObject::getName, QString("Key1")), "containsByKey failed"); + QVERIFY2(!d1.containsByKey(&CTestValueObject::getName, QString("Key5")), "containsByKey failed"); + + // containsByValue + QVERIFY2(d1.containsByValue(&CTestValueObject::getName, QString("Value1")), "containsByValue failed"); + QVERIFY2(!d1.containsByValue(&CTestValueObject::getName, QString("Value5")), "containsByValue failed"); + + // removeByKeyIf + d2 = d1; + d2.removeByKeyIf(&CTestValueObject::getName, "Key2"); + QVERIFY2(d2.size() == 1, "size() wrong"); + + // removeByValueIf + d2 = d1; + d2.removeByValueIf(&CTestValueObject::getName, "Value2"); + QVERIFY2(d2.size() == 1, "size() wrong"); + + // JSON + QJsonObject jsonObject = d1.toJson(); + CValueObjectDictionary d4; + d4.fromJson(jsonObject); + QVERIFY2(d1 == d4, "JSON serialization/deserialization failed"); + } + } //namespace BlackMiscTest diff --git a/tests/blackmisc/testcontainers.h b/tests/blackmisc/testcontainers.h index 12b51794b..2540827fe 100644 --- a/tests/blackmisc/testcontainers.h +++ b/tests/blackmisc/testcontainers.h @@ -25,6 +25,7 @@ namespace BlackMiscTest void sequenceBasics(); void joinAndSplit(); void findTests(); + void dictionaryBasics(); }; } //namespace BlackMiscTest diff --git a/tests/blackmisc/testvalueobject.cpp b/tests/blackmisc/testvalueobject.cpp new file mode 100644 index 000000000..4f6545722 --- /dev/null +++ b/tests/blackmisc/testvalueobject.cpp @@ -0,0 +1,167 @@ +#include "testvalueobject.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include + +namespace BlackMisc +{ + /* + * Convert to string + */ + QString CTestValueObject::convertToQString(bool /*i18n*/) const + { + QString s(this->m_name); + s.append(" ").append(this->m_description); + return s; + } + + /* + * metaTypeId + */ + int CTestValueObject::getMetaTypeId() const + { + return qMetaTypeId(); + } + + /* + * is a + */ + bool CTestValueObject::isA(int metaTypeId) const + { + if (metaTypeId == qMetaTypeId()) { return true; } + + return this->CValueObject::isA(metaTypeId); + } + + /* + * Compare + */ + int CTestValueObject::compareImpl(const CValueObject &otherBase) const + { + const auto &other = static_cast(otherBase); + + return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); + } + + /* + * Marshall to DBus + */ + void CTestValueObject::marshallToDbus(QDBusArgument &argument) const + { + argument << TupleConverter::toTuple(*this); + } + + /* + * Unmarshall from DBus + */ + void CTestValueObject::unmarshallFromDbus(const QDBusArgument &argument) + { + argument >> TupleConverter::toTuple(*this); + } + + /* + * Equal? + */ + bool CTestValueObject::operator ==(const CTestValueObject &other) const + { + if (this == &other) return true; + return TupleConverter::toTuple(*this) == TupleConverter::toTuple(other); + } + + /* + * Unequal? + */ + bool CTestValueObject::operator !=(const CTestValueObject &other) const + { + return !((*this) == other); + } + + /* + * Hash + */ + uint CTestValueObject::getValueHash() const + { + return qHash(TupleConverter::toTuple(*this)); + } + + /* + * Property by index + */ + QVariant CTestValueObject::propertyByIndex(int index) const + { + switch (index) + { + case IndexDescription: + return QVariant::fromValue(this->m_description); + case IndexName: + return QVariant::fromValue(this->m_name); + default: + break; + } + + Q_ASSERT_X(false, "CTestValueObject", "index unknown"); + QString m = QString("no property, index ").append(QString::number(index)); + return QVariant::fromValue(m); + } + + /* + * Property as string by index + */ + QString CTestValueObject::propertyByIndexAsString(int index, bool i18n) const + { + QVariant qv = this->propertyByIndex(index); + return BlackMisc::qVariantToString(qv, i18n); + } + + /* + * Property by index (setter) + */ + void CTestValueObject::setPropertyByIndex(const QVariant &variant, int index) + { + switch (index) + { + case IndexDescription: + this->setDescription(variant.value()); + break; + case IndexName: + this->setName(variant.value()); + break; + default: + Q_ASSERT_X(false, "CTestValueObject", "index unknown"); + break; + } + } + + /* + * Register metadata + */ + void CTestValueObject::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + /* + * Members + */ + const QStringList &CTestValueObject::jsonMembers() + { + return TupleConverter::jsonMembers(); + } + + /* + * To JSON + */ + QJsonObject CTestValueObject::toJson() const + { + return BlackMisc::serializeJson(CTestValueObject::jsonMembers(), TupleConverter::toTuple(*this)); + } + + /* + * From Json + */ + void CTestValueObject::fromJson(const QJsonObject &json) + { + BlackMisc::deserializeJson(json, CTestValueObject::jsonMembers(), TupleConverter::toTuple(*this)); + } + +} // namespace diff --git a/tests/blackmisc/testvalueobject.h b/tests/blackmisc/testvalueobject.h new file mode 100644 index 000000000..8849e7edf --- /dev/null +++ b/tests/blackmisc/testvalueobject.h @@ -0,0 +1,121 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BLACKMISC_SERVER_H +#define BLACKMISC_SERVER_H + +#include "blackmisc/valueobject.h" +#include "blackmisc/sequence.h" +#include "blackmisc/collection.h" +#include "blackmisc/dictionary.h" + +namespace BlackMisc +{ + /*! + * Value object encapsulating information of a server + */ + class CTestValueObject : public BlackMisc::CValueObject + { + public: + //! \brief Default constructor. + CTestValueObject() {} + + //! \brief Constructor. + CTestValueObject(const QString &name, const QString &description) + : m_name(name), m_description(description) {} + + //! \copydoc CValueObject::toQVariant + virtual QVariant toQVariant() const override + { + return QVariant::fromValue(*this); + } + + //! \brief Get name + const QString &getName() const { return m_name; } + + //! \brief Set name + void setName(const QString &name) { m_name = name; } + + //! \brief Get description + const QString &getDescription() const { return m_description; } + + //! \brief Set description + void setDescription(const QString &description) { m_description = description; } + + //! \brief Equal operator == + bool operator ==(const CTestValueObject &other) const; + + //! \brief Unequal operator != + bool operator !=(const CTestValueObject &other) const; + + //! \copydoc CValueObject::getValueHash() + virtual uint getValueHash() const override; + + //! \copydoc CValueObject::toJson + virtual QJsonObject toJson() const override; + + //! \copydoc CValueObject::fromJson + void fromJson(const QJsonObject &json) override; + + //! \brief Register metadata + static void registerMetadata(); + + //! \copydoc TupleConverter<>::jsonMembers() + static const QStringList &jsonMembers(); + + //! \brief Properties by index + enum ColumnIndex + { + IndexName = 0, + IndexDescription, + }; + + //! \copydoc CValueObject::propertyByIndex(int) + QVariant propertyByIndex(int index) const override; + + //! \copydoc CValueObject::setPropertyByIndex(const QVariant &, int index) + void setPropertyByIndex(const QVariant &variant, int index) override; + + //! \copydoc CValueObject::propertyByIndexAsString() + QString propertyByIndexAsString(int index, bool i18n) const; + + protected: + //! \copydoc CValueObject::convertToQString() + virtual QString convertToQString(bool i18n = false) const override; + + //! \copydoc CValueObject::getMetaTypeId + virtual int getMetaTypeId() const override; + + //! \copydoc CValueObject::isA + virtual bool isA(int metaTypeId) const override; + + //! \copydoc CValueObject::compareImpl + virtual int compareImpl(const CValueObject &other) const override; + + //! \copydoc CValueObject::marshallToDbus + virtual void marshallToDbus(QDBusArgument &argument) const override; + + //! \copydoc CValueObject::unmarshallFromDbus + virtual void unmarshallFromDbus(const QDBusArgument &argument) override; + + private: + BLACK_ENABLE_TUPLE_CONVERSION(CTestValueObject) + QString m_name; + QString m_description; + }; + +} // namespace + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CTestValueObject, (o.m_name, o.m_description)) +Q_DECLARE_METATYPE(BlackMisc::CTestValueObject) +Q_DECLARE_METATYPE(BlackMisc::CCollection) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +// We need to typedef because 'commas' confuse the Q_DECLARE_METATYPE macro +// https://bugreports.qt-project.org/browse/QTBUG-11485 +typedef BlackMisc::CDictionary CValueObjectDictionary; +Q_DECLARE_METATYPE(CValueObjectDictionary) + +#endif // guard