Paolo Vigori
Paolo Vigori

Reputation: 1724

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

Upvotes: 8

Views: 4742

Answers (1)

Paolo Vigori
Paolo Vigori

Reputation: 1724

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)

Upvotes: 15

Related Questions