Djordje Zivanovic
Djordje Zivanovic

Reputation: 304

"Assert and return" macro in Google Test?

Is there any ASSERT_AND_RETURN macro in Google Test that tests something, and if it is false, raises an assertion and returns a value?

Upvotes: 2

Views: 10272

Answers (1)

PiotrNycz
PiotrNycz

Reputation: 24420

Actually every ASSERT_XXX returns from function - but it does not return value - it is assumed that the function (in most cases functions created by TESTxx macros) are void function.

This is sometimes issue when you use ASSERT_XXX within function called from another function. To check if function failed on assert - you need to use ASSERT_NO_FATAL_FAILURE.

See example

void assertNotNull(int *p)
{
    ASSERT_THAT(p, NotNull(p));
}
void assertSizeIs(int actual, int expected)
{
    ASSERT_EQ(actual, expected);
}
TEST(A, B)
{
   std::pair<int*,int> p = createArray(7);

   ASSERT_NO_FATAL_FAILURE(assertNotNull(p.first));
   ASSERT_NO_FATAL_FAILURE(assertSizeIs(p.second, 7));
   
   for( int i = 0; i < 7; ++i) 
      ASSERT_EQ(0, p.first[i]);
}

Upvotes: 10

Related Questions