CDictionary unit tests

refs #281
This commit is contained in:
Roland Winklmeier
2014-06-30 00:15:54 +02:00
parent d46c5edb9f
commit 222a30eee9
4 changed files with 368 additions and 0 deletions

View File

@@ -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 <QList>
#include <QVector>
#include <QSet>
@@ -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<CTestValueObject> keyCollection = d1.keysCollection();
QVERIFY2(keyCollection.size() == 2, "keysCollection size wrong");
// keysSequence
CSequence<CTestValueObject> 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

View File

@@ -25,6 +25,7 @@ namespace BlackMiscTest
void sequenceBasics();
void joinAndSplit();
void findTests();
void dictionaryBasics();
};
} //namespace BlackMiscTest

View File

@@ -0,0 +1,167 @@
#include "testvalueobject.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include <tuple>
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<CTestValueObject>();
}
/*
* is a
*/
bool CTestValueObject::isA(int metaTypeId) const
{
if (metaTypeId == qMetaTypeId<CTestValueObject>()) { return true; }
return this->CValueObject::isA(metaTypeId);
}
/*
* Compare
*/
int CTestValueObject::compareImpl(const CValueObject &otherBase) const
{
const auto &other = static_cast<const CTestValueObject &>(otherBase);
return compare(TupleConverter<CTestValueObject>::toTuple(*this), TupleConverter<CTestValueObject>::toTuple(other));
}
/*
* Marshall to DBus
*/
void CTestValueObject::marshallToDbus(QDBusArgument &argument) const
{
argument << TupleConverter<CTestValueObject>::toTuple(*this);
}
/*
* Unmarshall from DBus
*/
void CTestValueObject::unmarshallFromDbus(const QDBusArgument &argument)
{
argument >> TupleConverter<CTestValueObject>::toTuple(*this);
}
/*
* Equal?
*/
bool CTestValueObject::operator ==(const CTestValueObject &other) const
{
if (this == &other) return true;
return TupleConverter<CTestValueObject>::toTuple(*this) == TupleConverter<CTestValueObject>::toTuple(other);
}
/*
* Unequal?
*/
bool CTestValueObject::operator !=(const CTestValueObject &other) const
{
return !((*this) == other);
}
/*
* Hash
*/
uint CTestValueObject::getValueHash() const
{
return qHash(TupleConverter<CTestValueObject>::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<QString>());
break;
case IndexName:
this->setName(variant.value<QString>());
break;
default:
Q_ASSERT_X(false, "CTestValueObject", "index unknown");
break;
}
}
/*
* Register metadata
*/
void CTestValueObject::registerMetadata()
{
qRegisterMetaType<CTestValueObject>();
qDBusRegisterMetaType<CTestValueObject>();
}
/*
* Members
*/
const QStringList &CTestValueObject::jsonMembers()
{
return TupleConverter<CTestValueObject>::jsonMembers();
}
/*
* To JSON
*/
QJsonObject CTestValueObject::toJson() const
{
return BlackMisc::serializeJson(CTestValueObject::jsonMembers(), TupleConverter<CTestValueObject>::toTuple(*this));
}
/*
* From Json
*/
void CTestValueObject::fromJson(const QJsonObject &json)
{
BlackMisc::deserializeJson(json, CTestValueObject::jsonMembers(), TupleConverter<CTestValueObject>::toTuple(*this));
}
} // namespace

View File

@@ -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<BlackMisc::CTestValueObject>)
Q_DECLARE_METATYPE(BlackMisc::CSequence<BlackMisc::CTestValueObject>)
// We need to typedef because 'commas' confuse the Q_DECLARE_METATYPE macro
// https://bugreports.qt-project.org/browse/QTBUG-11485
typedef BlackMisc::CDictionary<BlackMisc::CTestValueObject, BlackMisc::CTestValueObject, QMap> CValueObjectDictionary;
Q_DECLARE_METATYPE(CValueObjectDictionary)
#endif // guard