1

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?

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
Zack
  • 117
  • 1
  • 11
  • 1
    Regarding your question, I'm not sure if that's possible in C++, unless your enum has values that can be easily calculated (like default values - incrementing by one, or values increasing by powers of 2). You can list all of the values and remove the "correct" ones, but that's also error prone (someone will have to remember to add value to vector when adding new value to enum). C++ doesn't support inspection of declared enum values. – Yksisarvinen Dec 06 '19 at 15:14
  • Ok, I solved trying to put group of related msgType(s) in vectors and for every test case removing the ones I exprect success from. – Zack Dec 13 '19 at 19:46

0 Answers0