I have a compilation error related to the QMetaType and QtTest. I feel like my issue is rooted in some misunderstanding I have about a Qt concept. Skip below for the complication error, or start here for the full story.
I am trying to write a unit test for a class that I have written, CustomVector, which is a QVector over a QHash of QString to QVariant. I attach this to the metatyping system with the Q_DECLARE_METATYPE() macro after the class definiton. In the same header file, I have a TestCustomVector class which is intended to test the CustomVector. This class is encased in #ifdefs to only compile in debug configuration.
customvector.h
// ... necessary includes here ...
class CustomVector : public QVector< QHash<QString, QVariant> > { ... };
Q_DECLARE_METATYPE(CustomVector)
#ifdef QT_DEBUG
#include <QtTest>
class TestCustomVector
: public QObject{
Q_OBJECT
private slots:
void isNull_data();
void isNull();
};
#endif // QT_DEBUG
// ... necessary includes here ...
class CustomVector : public QVector< QHash<QString, QVariant> > { ... };
Q_DECLARE_METATYPE(CustomVector)
#ifdef QT_DEBUG
#include <QtTest>
class TestCustomVector : public QObject
{
Q_OBJECT
private slots:
void isNull_data();
void isNull();
};
#endif // QT_DEBUG
To copy to clipboard, switch view to plain text mode
The CustomVector and TestCustomVector classes are also implemented in the same cpp file. TestCustomVector has the one isNull() test which is implemented using the data-driven testing methodology. Again, the unit testing code is only compiled in on a debug build.
customvector.cpp
// ... CustomVector functions including an isNull() function ...
#ifdef QT_DEBUG
void TestCustomVector::isNull_data()
{
QTest::addColumn<CustomVector>("CustomVector");
QTest::addColumn<bool>("Result");
CustomVector cv_default(); // will be null
CustomVector cv_notNull(/* ... parameters giving a not null state... */);
CustomVector cv_null(/* ... parameters giving a null state... */)
QTest::newRow("Default Constructor")
<< &cv_default << true;
QTest::newRow("Basic Constructor, Fully Specified")
<< &cv_notNull << false;
QTest::newRow("Basic Constructor, Partially Specified")
<< &cv_nameNull << true;
}
void TestCustomVector::isNull()
{
QFETCH(CustomVector, customVector);
QFETCH(bool, result);
QCOMPARE(customVector, result);
}
#endif // QT_DEBUG
// ... CustomVector functions including an isNull() function ...
#ifdef QT_DEBUG
void TestCustomVector::isNull_data()
{
QTest::addColumn<CustomVector>("CustomVector");
QTest::addColumn<bool>("Result");
CustomVector cv_default(); // will be null
CustomVector cv_notNull(/* ... parameters giving a not null state... */);
CustomVector cv_null(/* ... parameters giving a null state... */)
QTest::newRow("Default Constructor")
<< &cv_default << true;
QTest::newRow("Basic Constructor, Fully Specified")
<< &cv_notNull << false;
QTest::newRow("Basic Constructor, Partially Specified")
<< &cv_nameNull << true;
}
void TestCustomVector::isNull()
{
QFETCH(CustomVector, customVector);
QFETCH(bool, result);
QCOMPARE(customVector, result);
}
#endif // QT_DEBUG
To copy to clipboard, switch view to plain text mode
These tests are defined in the files they refer to for ease of coding. I also intend to call QTest::qExec() in my main.cpp as a program command-line option of the main program.
When compiling, I get the following errors:
/opt/qt-4.5.2/include/QtCore/qmetatype.h: In static member function ‘static int QMetaTypeId2<T>::qt_metatype_id() [with T = CustomVector (*)()]’:
/opt/qt-4.5.2/include/QtCore/qmetatype.h:199: instantiated from ‘int qMetaTypeId(T*) [with T = CustomVector (*)()]’
/opt/qt-4.5.2/include/QtTest/qtestdata.h:82: instantiated from ‘QTestData& operator<<(QTestData&, const T&) [with T = CustomVector (*)()]’
customvector.cpp:15: instantiated from here
/opt/qt-4.5.2/include/QtCore/qmetatype.h:189: error: ‘qt_metatype_id’ is not a member of ‘QMetaTypeId<CustomVector (*)()>’
/opt/qt-4.5.2/include/QtCore/qmetatype.h: In static member function ‘static int QMetaTypeId2<T>::qt_metatype_id() [with T = CustomVector*]’:
/opt/qt-4.5.2/include/QtCore/qmetatype.h:199: instantiated from ‘int qMetaTypeId(T*) [with T = CustomVector*]’
/opt/qt-4.5.2/include/QtTest/qtestdata.h:82: instantiated from ‘QTestData& operator<<(QTestData&, const T&) [with T = CustomVector*]’
customvector.cpp:17: instantiated from here
/opt/qt-4.5.2/include/QtCore/qmetatype.h:189: error: ‘qt_metatype_id’ is not a member of ‘QMetaTypeId<CustomVector*>’
Based on other threads I have searched, I have tried:
- Separating the code into individual files
- Using Q_DECLARE_METATYPE(CustomVector *) and Q_DECLARE_METATYPE(CustomVector)
- Various permutations of streaming objects, pointers, and references into QTest::newRow()
Like I said before, I feel like whatever I am missing is a high-level Qt concept. Perhaps someone can shed some holy Qt wisdom on me?
Bookmarks