Googletest export
Add ::testing::FieldsAre matcher for objects that support get<> and structured bindings. PiperOrigin-RevId: 337165285
This commit is contained in:
@@ -1643,6 +1643,147 @@ TEST(PairTest, InsideContainsUsingMap) {
|
||||
EXPECT_THAT(container, Not(Contains(Pair(3, _))));
|
||||
}
|
||||
|
||||
TEST(FieldsAreTest, MatchesCorrectly) {
|
||||
std::tuple<int, std::string, double> p(25, "foo", .5);
|
||||
|
||||
// All fields match.
|
||||
EXPECT_THAT(p, FieldsAre(25, "foo", .5));
|
||||
EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr("o"), DoubleEq(.5)));
|
||||
|
||||
// Some don't match.
|
||||
EXPECT_THAT(p, Not(FieldsAre(26, "foo", .5)));
|
||||
EXPECT_THAT(p, Not(FieldsAre(25, "fo", .5)));
|
||||
EXPECT_THAT(p, Not(FieldsAre(25, "foo", .6)));
|
||||
}
|
||||
|
||||
TEST(FieldsAreTest, CanDescribeSelf) {
|
||||
Matcher<const pair<std::string, int>&> m1 = FieldsAre("foo", 42);
|
||||
EXPECT_EQ(
|
||||
"has field #0 that is equal to \"foo\""
|
||||
", and has field #1 that is equal to 42",
|
||||
Describe(m1));
|
||||
EXPECT_EQ(
|
||||
"has field #0 that isn't equal to \"foo\""
|
||||
", or has field #1 that isn't equal to 42",
|
||||
DescribeNegation(m1));
|
||||
}
|
||||
|
||||
TEST(FieldsAreTest, CanExplainMatchResultTo) {
|
||||
// The first one that fails is the one that gives the error.
|
||||
Matcher<std::tuple<int, int, int>> m =
|
||||
FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
|
||||
|
||||
EXPECT_EQ("whose field #0 does not match, which is 1 less than 0",
|
||||
Explain(m, std::make_tuple(-1, -2, -3)));
|
||||
EXPECT_EQ("whose field #1 does not match, which is 2 less than 0",
|
||||
Explain(m, std::make_tuple(1, -2, -3)));
|
||||
EXPECT_EQ("whose field #2 does not match, which is 3 less than 0",
|
||||
Explain(m, std::make_tuple(1, 2, -3)));
|
||||
|
||||
// If they all match, we get a long explanation of success.
|
||||
EXPECT_EQ(
|
||||
"whose all elements match, "
|
||||
"where field #0 is a value which is 1 more than 0"
|
||||
", and field #1 is a value which is 2 more than 0"
|
||||
", and field #2 is a value which is 3 more than 0",
|
||||
Explain(m, std::make_tuple(1, 2, 3)));
|
||||
|
||||
// Only print those that have an explanation.
|
||||
m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));
|
||||
EXPECT_EQ(
|
||||
"whose all elements match, "
|
||||
"where field #0 is a value which is 1 more than 0"
|
||||
", and field #2 is a value which is 3 more than 0",
|
||||
Explain(m, std::make_tuple(1, 0, 3)));
|
||||
|
||||
// If only one has an explanation, then print that one.
|
||||
m = FieldsAre(0, GreaterThan(0), 0);
|
||||
EXPECT_EQ(
|
||||
"whose all elements match, "
|
||||
"where field #1 is a value which is 1 more than 0",
|
||||
Explain(m, std::make_tuple(0, 1, 0)));
|
||||
}
|
||||
|
||||
#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
|
||||
TEST(FieldsAreTest, StructuredBindings) {
|
||||
// testing::FieldsAre can also match aggregates and such with C++17 and up.
|
||||
struct MyType {
|
||||
int i;
|
||||
std::string str;
|
||||
};
|
||||
EXPECT_THAT((MyType{17, "foo"}), FieldsAre(Eq(17), HasSubstr("oo")));
|
||||
|
||||
// Test all the supported arities.
|
||||
struct MyVarType1 {
|
||||
int a;
|
||||
};
|
||||
EXPECT_THAT(MyVarType1{}, FieldsAre(0));
|
||||
struct MyVarType2 {
|
||||
int a, b;
|
||||
};
|
||||
EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0));
|
||||
struct MyVarType3 {
|
||||
int a, b, c;
|
||||
};
|
||||
EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0));
|
||||
struct MyVarType4 {
|
||||
int a, b, c, d;
|
||||
};
|
||||
EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0));
|
||||
struct MyVarType5 {
|
||||
int a, b, c, d, e;
|
||||
};
|
||||
EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));
|
||||
struct MyVarType6 {
|
||||
int a, b, c, d, e, f;
|
||||
};
|
||||
EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType7 {
|
||||
int a, b, c, d, e, f, g;
|
||||
};
|
||||
EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType8 {
|
||||
int a, b, c, d, e, f, g, h;
|
||||
};
|
||||
EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType9 {
|
||||
int a, b, c, d, e, f, g, h, i;
|
||||
};
|
||||
EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType10 {
|
||||
int a, b, c, d, e, f, g, h, i, j;
|
||||
};
|
||||
EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType11 {
|
||||
int a, b, c, d, e, f, g, h, i, j, k;
|
||||
};
|
||||
EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType12 {
|
||||
int a, b, c, d, e, f, g, h, i, j, k, l;
|
||||
};
|
||||
EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType13 {
|
||||
int a, b, c, d, e, f, g, h, i, j, k, l, m;
|
||||
};
|
||||
EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType14 {
|
||||
int a, b, c, d, e, f, g, h, i, j, k, l, m, n;
|
||||
};
|
||||
EXPECT_THAT(MyVarType14{},
|
||||
FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType15 {
|
||||
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;
|
||||
};
|
||||
EXPECT_THAT(MyVarType15{},
|
||||
FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
struct MyVarType16 {
|
||||
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
|
||||
};
|
||||
EXPECT_THAT(MyVarType16{},
|
||||
FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(ContainsTest, WorksWithMoveOnly) {
|
||||
ContainerHelper helper;
|
||||
EXPECT_CALL(helper, Call(Contains(Pointee(2))));
|
||||
|
||||
Reference in New Issue
Block a user