mercredi 4 décembre 2019

Testing against values of an enum class in google test

I have a constructor of an object that can throw an exception if a parameter of an enum class type is wrong. This permits me to ensure that such an object, that will use that parameter for some decision, is never constructed if the value of this enum class parameter is illegal. To test this logic I would need two parameterized test: one checks that all the wrong values will generate an exception while the other checks that the good values are correcly accepted.

In my test (by the way it's google test) I have something like this:

TEST_P(MessageActionTest, askResMsgForbiddenActions){
    msgType action=GetParam();
    EXPECT_THROW(m= new askResMessage(action),messageException);
}
msgType askResForbidden[]={
        msgType::registration, msgType::login /*long list of wrong values*/
};
INSTANTIATE_TEST_CASE_P(askResThrowExceptionInConstruction, MessageActionTest, testing::ValuesIn(askResForbidden));

What I woud like to take advantage of is the fact that the "permitted" values are much less than the values of my enum. I would like to write something like: "test with all the values of this enum class, except this list of values". This would be much less error prone and much more readable. If I choose to use the current solution I will have to write every time a comment so that the reader can quickly understand.

Can you suggest a convenient way to do so?

Thanks in advance. For some reason StackOverflow cuts my "Hello everyone" even if I try to edit the post. I'm not rude.

Aucun commentaire:

Enregistrer un commentaire