Expect a value within a given range using Google Test

Drew Noakes picture Drew Noakes · Feb 23, 2014 · Viewed 12.9k times · Source

I want to specify an expectation that a value is between an upper and lower bound, inclusively.

Google Test provides LT,LE,GT,GE, but no way of testing a range that I can see. You could use EXPECT_NEAR and juggle the operands, but in many cases this isn't as clear as explicitly setting upper and lower bounds.

Usage should resemble:

EXPECT_WITHIN_INCLUSIVE(1, 3, 2); // 2 is in range [1,3]

How would one add this expectation?

Answer

Billy Donahue picture Billy Donahue · Feb 23, 2014

Google mock has richer composable matchers:

EXPECT_THAT(x, AllOf(Ge(1),Le(3)));

Maybe that would work for you.