Improves matcher messages across the board.
This commit is contained in:
@@ -211,6 +211,43 @@ TEST(ArgsTest, DescribesNegationCorrectly) {
|
||||
DescribeNegation(m));
|
||||
}
|
||||
|
||||
TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
|
||||
const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
|
||||
EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
|
||||
Explain(m, make_tuple(false, 42, 42)));
|
||||
EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
|
||||
Explain(m, make_tuple(false, 42, 43)));
|
||||
}
|
||||
|
||||
// For testing Args<>'s explanation.
|
||||
class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
|
||||
public:
|
||||
virtual void DescribeTo(::std::ostream* os) const {}
|
||||
|
||||
virtual bool MatchAndExplain(tuple<char, int> value,
|
||||
MatchResultListener* listener) const {
|
||||
const int diff = get<0>(value) - get<1>(value);
|
||||
if (diff > 0) {
|
||||
*listener << "where the first value is " << diff
|
||||
<< " more than the second";
|
||||
}
|
||||
return diff < 0;
|
||||
}
|
||||
};
|
||||
|
||||
Matcher<tuple<char, int> > LessThan() {
|
||||
return MakeMatcher(new LessThanMatcher);
|
||||
}
|
||||
|
||||
TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
|
||||
const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
|
||||
EXPECT_EQ("whose fields (#0, #2) are ('a' (97), 42), "
|
||||
"where the first value is 55 more than the second",
|
||||
Explain(m, make_tuple('a', 42, 42)));
|
||||
EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
|
||||
Explain(m, make_tuple('\0', 42, 43)));
|
||||
}
|
||||
|
||||
// For testing ExplainMatchResultTo().
|
||||
class GreaterThanMatcher : public MatcherInterface<int> {
|
||||
public:
|
||||
@@ -224,11 +261,11 @@ class GreaterThanMatcher : public MatcherInterface<int> {
|
||||
MatchResultListener* listener) const {
|
||||
const int diff = lhs - rhs_;
|
||||
if (diff > 0) {
|
||||
*listener << "is " << diff << " more than " << rhs_;
|
||||
*listener << "which is " << diff << " more than " << rhs_;
|
||||
} else if (diff == 0) {
|
||||
*listener << "is the same as " << rhs_;
|
||||
*listener << "which is the same as " << rhs_;
|
||||
} else {
|
||||
*listener << "is " << -diff << " less than " << rhs_;
|
||||
*listener << "which is " << -diff << " less than " << rhs_;
|
||||
}
|
||||
|
||||
return lhs > rhs_;
|
||||
@@ -254,32 +291,32 @@ TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
|
||||
|
||||
TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
|
||||
Matcher<vector<int> > m = ElementsAre(Gt(5));
|
||||
EXPECT_EQ("has 1 element that is greater than 5", Describe(m));
|
||||
EXPECT_EQ("has 1 element that is > 5", Describe(m));
|
||||
}
|
||||
|
||||
TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
|
||||
Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
|
||||
EXPECT_EQ("has 2 elements where\n"
|
||||
"element 0 is equal to \"one\",\n"
|
||||
"element 1 is equal to \"two\"", Describe(m));
|
||||
"element #0 is equal to \"one\",\n"
|
||||
"element #1 is equal to \"two\"", Describe(m));
|
||||
}
|
||||
|
||||
TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
|
||||
Matcher<vector<int> > m = ElementsAre();
|
||||
EXPECT_EQ("is not empty", DescribeNegation(m));
|
||||
EXPECT_EQ("isn't empty", DescribeNegation(m));
|
||||
}
|
||||
|
||||
TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
|
||||
Matcher<const list<int>& > m = ElementsAre(Gt(5));
|
||||
EXPECT_EQ("does not have 1 element, or\n"
|
||||
"element 0 is not greater than 5", DescribeNegation(m));
|
||||
EXPECT_EQ("doesn't have 1 element, or\n"
|
||||
"element #0 isn't > 5", DescribeNegation(m));
|
||||
}
|
||||
|
||||
TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
|
||||
Matcher<const list<string>& > m = ElementsAre("one", "two");
|
||||
EXPECT_EQ("does not have 2 elements, or\n"
|
||||
"element 0 is not equal to \"one\", or\n"
|
||||
"element 1 is not equal to \"two\"", DescribeNegation(m));
|
||||
EXPECT_EQ("doesn't have 2 elements, or\n"
|
||||
"element #0 isn't equal to \"one\", or\n"
|
||||
"element #1 isn't equal to \"two\"", DescribeNegation(m));
|
||||
}
|
||||
|
||||
TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
|
||||
@@ -297,8 +334,9 @@ TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
|
||||
|
||||
const int a[] = { 10, 0, 100 };
|
||||
vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
|
||||
EXPECT_EQ("element 0 is 9 more than 1,\n"
|
||||
"element 2 is 98 more than 2", Explain(m, test_vector));
|
||||
EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
|
||||
"and whose element #2 matches, which is 98 more than 2",
|
||||
Explain(m, test_vector));
|
||||
}
|
||||
|
||||
TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
|
||||
@@ -309,7 +347,7 @@ TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
|
||||
EXPECT_EQ("", Explain(m, test_list));
|
||||
|
||||
test_list.push_back(1);
|
||||
EXPECT_EQ("has 1 element", Explain(m, test_list));
|
||||
EXPECT_EQ("which has 1 element", Explain(m, test_list));
|
||||
}
|
||||
|
||||
TEST(ElementsAreTest, CanExplainMismatchRightSize) {
|
||||
@@ -318,10 +356,11 @@ TEST(ElementsAreTest, CanExplainMismatchRightSize) {
|
||||
vector<int> v;
|
||||
v.push_back(2);
|
||||
v.push_back(1);
|
||||
EXPECT_EQ("element 0 doesn't match", Explain(m, v));
|
||||
EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
|
||||
|
||||
v[0] = 1;
|
||||
EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v));
|
||||
EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
|
||||
Explain(m, v));
|
||||
}
|
||||
|
||||
TEST(ElementsAreTest, MatchesOneElementVector) {
|
||||
@@ -1030,16 +1069,22 @@ TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
|
||||
EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
|
||||
}
|
||||
|
||||
TEST(ContainsTest, DescribesItselfCorrectly) {
|
||||
TEST(ContainsTest, ExplainsMatchResultCorrectly) {
|
||||
const int a[2] = { 1, 2 };
|
||||
Matcher<const int(&)[2]> m = Contains(2);
|
||||
EXPECT_EQ("element 1 matches", Explain(m, a));
|
||||
EXPECT_EQ("whose element #1 matches", Explain(m, a));
|
||||
|
||||
m = Contains(3);
|
||||
EXPECT_EQ("", Explain(m, a));
|
||||
|
||||
m = Contains(GreaterThan(0));
|
||||
EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
|
||||
|
||||
m = Contains(GreaterThan(10));
|
||||
EXPECT_EQ("", Explain(m, a));
|
||||
}
|
||||
|
||||
TEST(ContainsTest, ExplainsMatchResultCorrectly) {
|
||||
TEST(ContainsTest, DescribesItselfCorrectly) {
|
||||
Matcher<vector<int> > m = Contains(1);
|
||||
EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user