Search code examples
c++unit-testingtddgoogletestassertions

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


Solution

  • 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)