Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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?

like image 952
Djordje Zivanovic Avatar asked Mar 24 '26 05:03

Djordje Zivanovic


1 Answers

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]);
}
like image 108
PiotrNycz Avatar answered Mar 25 '26 21:03

PiotrNycz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!