mardi 25 août 2020

C++ pass list of function calls as parameters

I am currently setting up a unit test helper class which in Qt with C++11 which verifies that a signal was emitted during the test agnostic of its order, example:

void TestUtils::verifyVolumeAdjustment(const QSignalSpy &vol_spy, uint percent)
{
    for(QList<QVariant> vol_call_args : vol_spy){
        if(vol_call_args.at(0).toInt() == percent)
        {
            return;
        }
    }
    QString err = QString("volume never set to %1").arg(QString::number(percent));
    QFAIL(err.toLocal8Bit().constData());
}

I have a good dozen of those functions all for checking if certain signals were emitted. Now I need to write tests where order is of importance. In those tests I would need to verify for example:

Volume set to 10
Volume set to 50

But exactly in that order. Now my question is if there is a way with variadic templates or something similar to pass a list of function calls to a function. I imagine the generalistic order checking function to be something like this:

void checkExecutionOrder(const QSignalSpy& spy, FunctionCallList call_list){
    
    for(int i = 0; i < call_list.length(), i++){
         QSignalSpy temp;
         temp.append(spy.at(i)); //create a temporary copy of that caught signal to ensure its the only thing validated
         call_list[i].call(temp, arguments); // call the function from the list with modified spy and remaining arguments
    }
}

Is there any nice way of doing this so I do not have to create a order sensitive test function for each function?

Aucun commentaire:

Enregistrer un commentaire