Commit b1c7f93c by zhanyong.wan

Improves matcher messages across the board.

parent 676e8cc6
// This file was GENERATED by a script. DO NOT EDIT BY HAND!!!
// This file was GENERATED by command:
// pump.py gmock-generated-matchers.h.pump
// DO NOT EDIT BY HAND!!!
// Copyright 2008, Google Inc.
// All rights reserved.
......@@ -231,15 +233,28 @@ class ArgsMatcherImpl : public MatcherInterface<ArgsTuple> {
virtual bool MatchAndExplain(ArgsTuple args,
MatchResultListener* listener) const {
return inner_matcher_.MatchAndExplain(GetSelectedArgs(args), listener);
const SelectedArgs& selected_args = GetSelectedArgs(args);
if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
PrintIndices(listener->stream());
*listener << "are " << PrintToString(selected_args);
StringMatchResultListener inner_listener;
const bool match = inner_matcher_.MatchAndExplain(selected_args,
&inner_listener);
PrintIfNotEmpty(inner_listener.str(), listener->stream());
return match;
}
virtual void DescribeTo(::std::ostream* os) const {
*os << "are a tuple ";
PrintIndices(os);
inner_matcher_.DescribeTo(os);
}
virtual void DescribeNegationTo(::std::ostream* os) const {
*os << "are a tuple ";
PrintIndices(os);
inner_matcher_.DescribeNegationTo(os);
}
......@@ -252,7 +267,7 @@ class ArgsMatcherImpl : public MatcherInterface<ArgsTuple> {
// Prints the indices of the selected fields.
static void PrintIndices(::std::ostream* os) {
*os << "are a tuple whose fields (";
*os << "whose fields (";
const int indices[10] = { k0, k1, k2, k3, k4, k5, k6, k7, k8, k9 };
for (int i = 0; i < 10; i++) {
if (indices[i] < 0)
......
......@@ -118,15 +118,28 @@ class ArgsMatcherImpl : public MatcherInterface<ArgsTuple> {
virtual bool MatchAndExplain(ArgsTuple args,
MatchResultListener* listener) const {
return inner_matcher_.MatchAndExplain(GetSelectedArgs(args), listener);
const SelectedArgs& selected_args = GetSelectedArgs(args);
if (!listener->IsInterested())
return inner_matcher_.Matches(selected_args);
PrintIndices(listener->stream());
*listener << "are " << PrintToString(selected_args);
StringMatchResultListener inner_listener;
const bool match = inner_matcher_.MatchAndExplain(selected_args,
&inner_listener);
PrintIfNotEmpty(inner_listener.str(), listener->stream());
return match;
}
virtual void DescribeTo(::std::ostream* os) const {
*os << "are a tuple ";
PrintIndices(os);
inner_matcher_.DescribeTo(os);
}
virtual void DescribeNegationTo(::std::ostream* os) const {
*os << "are a tuple ";
PrintIndices(os);
inner_matcher_.DescribeNegationTo(os);
}
......@@ -138,7 +151,7 @@ class ArgsMatcherImpl : public MatcherInterface<ArgsTuple> {
// Prints the indices of the selected fields.
static void PrintIndices(::std::ostream* os) {
*os << "are a tuple whose fields (";
*os << "whose fields (";
const int indices[$n] = { $ks };
for (int i = 0; i < $n; i++) {
if (indices[i] < 0)
......
......@@ -993,8 +993,8 @@ class TypedExpectation : public ExpectationBase {
// Describes the result of matching the arguments against this
// expectation to the given ostream.
// L >= g_gmock_mutex
void DescribeMatchResultTo(const ArgumentTuple& args,
::std::ostream* os) const {
void ExplainMatchResultTo(const ArgumentTuple& args,
::std::ostream* os) const {
g_gmock_mutex.AssertHeld();
if (is_retired()) {
......@@ -1002,7 +1002,7 @@ class TypedExpectation : public ExpectationBase {
<< " Actual: it is retired\n";
} else if (!Matches(args)) {
if (!TupleMatches(matchers_, args)) {
DescribeMatchFailureTupleTo(matchers_, args, os);
ExplainMatchFailureTupleTo(matchers_, args, os);
}
StringMatchResultListener listener;
if (!extra_matcher_.MatchAndExplain(args, &listener)) {
......@@ -1010,7 +1010,7 @@ class TypedExpectation : public ExpectationBase {
extra_matcher_.DescribeTo(os);
*os << "\n Actual: don't match";
internal::StreamInParensAsNeeded(listener.str(), os);
internal::PrintIfNotEmpty(listener.str(), os);
*os << "\n";
}
} else if (!AllPrerequisitesAreSatisfied()) {
......@@ -1028,7 +1028,7 @@ class TypedExpectation : public ExpectationBase {
*os << " (end of pre-requisites)\n";
} else {
// This line is here just for completeness' sake. It will never
// be executed as currently the DescribeMatchResultTo() function
// be executed as currently the ExplainMatchResultTo() function
// is called only when the mock function call does NOT match the
// expectation.
*os << "The call matches the expectation.\n";
......@@ -1618,7 +1618,7 @@ class FunctionMockerBase : public UntypedFunctionMockerBase {
*why << "tried expectation #" << i << ": ";
}
*why << expectations_[i]->source_text() << "...\n";
expectations_[i]->DescribeMatchResultTo(args, why);
expectations_[i]->ExplainMatchResultTo(args, why);
expectations_[i]->DescribeCallCountTo(why);
}
}
......
......@@ -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));
......
......@@ -175,8 +175,8 @@ Google Mock tried the following 1 expectation, but it didn't match:
FILE:#: EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)))...
Expected arg #0: references the variable @0x# "Hi"
Actual: "Ho" (is located @0x#)
Expected arg #2: is greater than or equal to 0
Actual: "Ho", which is located @0x#
Expected arg #2: is >= 0
Actual: -0.1
Expected: to be called once
Actual: never called - unsatisfied and active
......@@ -204,7 +204,7 @@ Unexpected mock function call - returning default value.
Google Mock tried the following 1 expectation, but it didn't match:
FILE:#: EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))...
Expected arg #0: is greater than or equal to 2
Expected arg #0: is >= 2
Actual: 1
Expected args: are a pair (x, y) where x >= y
Actual: don't match
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment