PDA

View Full Version : Using QPair in QTest



hackerNovitiate
16th November 2010, 02:07
Hi, is it possible to use QPairs as test values? I'm running into some trouble:



void function_data()
{
QTest::addColumn<QDate>("testDate");
QTest::addColumn< QPair<QDate,QDate> >("expectedOutputs");

// newRow...
}

void function()
{
QFETCH(QDate, testDate);
QFETCH(QPair<QDate,QDate>, expectedOutputs);

// QCOMPARE...
}


My compiler seems to have issues with the comma in the QPair template and complains
error: macro "QFETCH" passed 3 arguments, but takes just 2

tbscope
16th November 2010, 04:27
You need to declare metatype properties for QPair<QDate, QDate>
http://doc.qt.nokia.com/4.7/qtest.html#addColumn
http://doc.qt.nokia.com/4.7/qmetatype.html#Q_DECLARE_METATYPE

hackerNovitiate
16th November 2010, 05:17
You need to declare metatype properties for QPair<QDate, QDate>
http://doc.qt.nokia.com/4.7/qtest.html#addColumn
http://doc.qt.nokia.com/4.7/qmetatype.html#Q_DECLARE_METATYPE

Thanks tbscope. Unfortunately,

Q_DECLARE_METATYPE(QPair<QDate,Ddate>)
leads to:

error: macro "Q_DECLARE_METATYPE" passed 2 arguments, but takes just 1

I'm guessing that the preprocessor (I'm using GCC 4.5.0 on Windows and Qt 4.6.2) doesn't like special usages of commas inside macros...? addColumn() seemed ok, but Q_DECLARE_METATYPE and QFETCH both balked.

My current workaround is to store pair.first and pair.second in separate columns, and do element-wise comparisons. :-/ (My container is actually QList< QPair<QDate,QDate> >, split such that each test row row contains two parallel QList<QDate>).

It's working fine so far, but if there's a neater way to do it, please let me know!

tbscope
16th November 2010, 05:27
You can try a typedef.

As far as I know, it should be possible to use QPair or QHash or QMap, but I'm not behind a development machine, so I can't test it.

hackerNovitiate
16th November 2010, 05:42
You can try a typedef.


typedef QList< QPair<QDate,QDate> > PairList;
Q_DECLARE_METATYPE(PairList)...worked like a charm. :) Thanks!