Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to negate the matcher in gtest assert or expect that?

I'd like to check if a vector is not empty in gtest but I'd like to understand more in general how to check the negation of a matcher.

I usually ckeck size is greater than zero

EXPECT_THAT( vector.size(), Gt( 0 ) );

and I know I could write my own matcher

MATCHER( IsNotEmpty, !negation ? "isn't empty" : "is empty" ) {
if ( !arg.empty() ) {
    return true;
}
*result_listener << "whose size is " << arg.size();
return false;
}

but I'm wondering if it's just simply possible to negate any matcher

like image 870
Paolo Vigori Avatar asked Mar 02 '17 09:03

Paolo Vigori


1 Answers

I find out that you can composite some matchers and do something like

EXPECT_THAT( vector, Not( IsEmpty() ) );

also other interesting composite matchers

AllOf(m1, m2, ..., mn)
AnyOf(m1, m2, ..., mn)
like image 143
Paolo Vigori Avatar answered Oct 13 '22 07:10

Paolo Vigori