Commit b4140808 by zhanyong.wan

Replaces Python-style interpolation with arbitrary C++ string expression in MATCHER* descriptions.

parent 0a781df3
...@@ -402,25 +402,29 @@ $$ // show up in the generated code. ...@@ -402,25 +402,29 @@ $$ // show up in the generated code.
// Describing Parameterized Matchers // Describing Parameterized Matchers
// ================================= // =================================
// //
// When defining a parameterized matcher, you can use Python-style // The last argument to MATCHER*() is a string-typed expression. The
// interpolations in the description string to refer to the parameter // expression can reference all of the matcher's parameters and a
// values. We support the following syntax currently: // special bool-typed variable named 'negation'. When 'negation' is
// // false, the expression should evaluate to the matcher's description;
// %% a single '%' character // otherwise it should evaluate to the description of the negation of
// %(*)s all parameters of the matcher printed as a tuple // the matcher. For example,
// %(foo)s value of the matcher parameter named 'foo' //
// // using testing::PrintToString;
// For example, //
// // MATCHER_P2(InClosedRange, low, hi,
// MATCHER_P2(InClosedRange, low, hi, "is in range [%(low)s, %(hi)s]") { // string(negation ? "is not" : "is") + " in range [" +
// PrintToString(low) + ", " + PrintToString(hi) + "]") {
// return low <= arg && arg <= hi; // return low <= arg && arg <= hi;
// } // }
// ... // ...
// EXPECT_THAT(3, InClosedRange(4, 6)); // EXPECT_THAT(3, InClosedRange(4, 6));
// EXPECT_THAT(3, Not(InClosedRange(2, 4)));
// //
// would generate a failure that contains the message: // would generate two failures that contain the text:
// //
// Expected: is in range [4, 6] // Expected: is in range [4, 6]
// ...
// Expected: is not in range [2, 4]
// //
// If you specify "" as the description, the failure message will // If you specify "" as the description, the failure message will
// contain the sequence of words in the matcher name followed by the // contain the sequence of words in the matcher name followed by the
...@@ -429,10 +433,13 @@ $$ // show up in the generated code. ...@@ -429,10 +433,13 @@ $$ // show up in the generated code.
// MATCHER_P2(InClosedRange, low, hi, "") { ... } // MATCHER_P2(InClosedRange, low, hi, "") { ... }
// ... // ...
// EXPECT_THAT(3, InClosedRange(4, 6)); // EXPECT_THAT(3, InClosedRange(4, 6));
// EXPECT_THAT(3, Not(InClosedRange(2, 4)));
// //
// would generate a failure that contains the text: // would generate two failures that contain the text:
// //
// Expected: in closed range (4, 6) // Expected: in closed range (4, 6)
// ...
// Expected: not (in closed range (2, 4))
// //
// Types of Matcher Parameters // Types of Matcher Parameters
// =========================== // ===========================
...@@ -529,11 +536,9 @@ $var template = [[$if i==0 [[]] $else [[ ...@@ -529,11 +536,9 @@ $var template = [[$if i==0 [[]] $else [[
template <$for j, [[typename p$j##_type]]>\ template <$for j, [[typename p$j##_type]]>\
]]]] ]]]]
$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]] $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
$var impl_ctor_param_list = [[$for j [[p$j##_type gmock_p$j, ]] $var impl_ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
const ::testing::internal::Interpolations& gmock_interp]] $var impl_inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
$var impl_inits = [[ : $for j [[p$j(gmock_p$j), ]]gmock_interp_(gmock_interp)]]
$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]] $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
$var params_and_interp = [[$for j [[p$j, ]]gmock_interp_]]
$var params = [[$for j, [[p$j]]]] $var params = [[$for j, [[p$j]]]]
$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]] $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]] $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
...@@ -559,28 +564,31 @@ $var param_field_decls2 = [[$for j ...@@ -559,28 +564,31 @@ $var param_field_decls2 = [[$for j
virtual bool MatchAndExplain(\ virtual bool MatchAndExplain(\
arg_type arg, ::testing::MatchResultListener* result_listener) const;\ arg_type arg, ::testing::MatchResultListener* result_listener) const;\
virtual void DescribeTo(::std::ostream* gmock_os) const {\ virtual void DescribeTo(::std::ostream* gmock_os) const {\
const ::testing::internal::Strings& gmock_printed_params = \ *gmock_os << FormatDescription(false);\
::testing::internal::UniversalTersePrintTupleFieldsToStrings(\ }\
::std::tr1::tuple<$for j, [[p$j##_type]]>($for j, [[p$j]]));\ virtual void DescribeNegationTo(::std::ostream* gmock_os) const {\
*gmock_os << ::testing::internal::FormatMatcherDescription(\ *gmock_os << FormatDescription(true);\
#name, description, gmock_interp_, gmock_printed_params);\
}\$param_field_decls }\$param_field_decls
const ::testing::internal::Interpolations gmock_interp_;\
private:\ private:\
::testing::internal::string FormatDescription(bool negation) const {\
const ::testing::internal::string gmock_description = (description);\
if (!gmock_description.empty())\
return gmock_description;\
return ::testing::internal::FormatMatcherDescription(\
negation, #name,\
::testing::internal::UniversalTersePrintTupleFieldsToStrings(\
::std::tr1::tuple<$for j, [[p$j##_type]]>($for j, [[p$j]])));\
}\
GTEST_DISALLOW_ASSIGN_(gmock_Impl);\ GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
};\ };\
template <typename arg_type>\ template <typename arg_type>\
operator ::testing::Matcher<arg_type>() const {\ operator ::testing::Matcher<arg_type>() const {\
return ::testing::Matcher<arg_type>(\ return ::testing::Matcher<arg_type>(\
new gmock_Impl<arg_type>($params_and_interp));\ new gmock_Impl<arg_type>($params));\
}\ }\
$class_name($ctor_param_list)$inits {\ $class_name($ctor_param_list)$inits {\
const char* gmock_param_names[] = { $for j [[#p$j, ]]NULL };\
gmock_interp_ = ::testing::internal::ValidateMatcherDescription(\
gmock_param_names, ("" description ""));\
}\$param_field_decls2 }\$param_field_decls2
private:\ private:\
::testing::internal::Interpolations gmock_interp_;\
GTEST_DISALLOW_ASSIGN_($class_name);\ GTEST_DISALLOW_ASSIGN_($class_name);\
};\$template };\$template
inline $class_name$param_types name($param_types_and_names) {\ inline $class_name$param_types name($param_types_and_names) {\
......
...@@ -2527,41 +2527,13 @@ class ElementsAreArrayMatcher { ...@@ -2527,41 +2527,13 @@ class ElementsAreArrayMatcher {
GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher); GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher);
}; };
// Constants denoting interpolations in a matcher description string. // Returns the description for a matcher defined using the MATCHER*()
const int kTupleInterpolation = -1; // "%(*)s" // macro where the user-supplied description string is "", if
const int kPercentInterpolation = -2; // "%%" // 'negation' is false; otherwise returns the description of the
const int kInvalidInterpolation = -3; // "%" followed by invalid text // negation of the matcher. 'param_values' contains a list of strings
// that are the print-out of the matcher's parameters.
// Records the location and content of an interpolation. string FormatMatcherDescription(bool negation, const char* matcher_name,
struct Interpolation { const Strings& param_values);
Interpolation(const char* start, const char* end, int param)
: start_pos(start), end_pos(end), param_index(param) {}
// Points to the start of the interpolation (the '%' character).
const char* start_pos;
// Points to the first character after the interpolation.
const char* end_pos;
// 0-based index of the interpolated matcher parameter;
// kTupleInterpolation for "%(*)s"; kPercentInterpolation for "%%".
int param_index;
};
typedef ::std::vector<Interpolation> Interpolations;
// Parses a matcher description string and returns a vector of
// interpolations that appear in the string; generates non-fatal
// failures iff 'description' is an invalid matcher description.
// 'param_names' is a NULL-terminated array of parameter names in the
// order they appear in the MATCHER_P*() parameter list.
Interpolations ValidateMatcherDescription(
const char* param_names[], const char* description);
// Returns the actual matcher description, given the matcher name,
// user-supplied description template string, interpolations in the
// string, and the printed values of the matcher parameters.
string FormatMatcherDescription(
const char* matcher_name, const char* description,
const Interpolations& interp, const Strings& param_values);
} // namespace internal } // namespace internal
......
...@@ -65,74 +65,6 @@ Matcher<internal::string>::Matcher(const char* s) { ...@@ -65,74 +65,6 @@ Matcher<internal::string>::Matcher(const char* s) {
namespace internal { namespace internal {
// Utilities for validating and formatting description strings in the
// MATCHER*() macros.
// Returns the 0-based index of the given parameter in the
// NULL-terminated parameter array; if the parameter is "*", returns
// kTupleInterpolation; if it's not found in the list, returns
// kInvalidInterpolation.
int GetParamIndex(const char* param_names[], const string& param_name) {
if (param_name == "*")
return kTupleInterpolation;
for (int i = 0; param_names[i] != NULL; i++) {
if (param_name == param_names[i])
return i;
}
return kInvalidInterpolation;
}
// Helper function used by ValidateMatcherDescription() to format
// error messages.
string FormatMatcherDescriptionSyntaxError(const char* description,
const char* error_pos) {
::std::stringstream ss;
ss << "Syntax error at index " << (error_pos - description)
<< " in matcher description \"" << description << "\": ";
return ss.str();
}
// Parses a matcher description string and returns a vector of
// interpolations that appear in the string; generates non-fatal
// failures iff 'description' is an invalid matcher description.
// 'param_names' is a NULL-terminated array of parameter names in the
// order they appear in the MATCHER_P*() parameter list.
Interpolations ValidateMatcherDescription(
const char* param_names[], const char* description) {
Interpolations interps;
for (const char* p = description; *p != '\0';) {
if (SkipPrefix("%%", &p)) {
interps.push_back(Interpolation(p - 2, p, kPercentInterpolation));
} else if (SkipPrefix("%(", &p)) {
const char* const q = strstr(p, ")s");
if (q == NULL) {
// TODO(wan@google.com): change the source file location in
// the failure to point to where the MATCHER*() macro is used.
ADD_FAILURE() << FormatMatcherDescriptionSyntaxError(description, p - 2)
<< "an interpolation must end with \")s\", "
<< "but \"" << (p - 2) << "\" does not.";
} else {
const string param_name(p, q);
const int param_index = GetParamIndex(param_names, param_name);
if (param_index == kInvalidInterpolation) {
ADD_FAILURE() << FormatMatcherDescriptionSyntaxError(description, p)
<< "\"" << param_name
<< "\" is an invalid parameter name.";
} else {
interps.push_back(Interpolation(p - 2, q + 2, param_index));
p = q + 2;
}
}
} else {
EXPECT_NE(*p, '%') << FormatMatcherDescriptionSyntaxError(description, p)
<< "use \"%%\" instead of \"%\" to print \"%\".";
++p;
}
}
return interps;
}
// Joins a vector of strings as if they are fields of a tuple; returns // Joins a vector of strings as if they are fields of a tuple; returns
// the joined string. // the joined string.
string JoinAsTuple(const Strings& fields) { string JoinAsTuple(const Strings& fields) {
...@@ -152,38 +84,17 @@ string JoinAsTuple(const Strings& fields) { ...@@ -152,38 +84,17 @@ string JoinAsTuple(const Strings& fields) {
} }
} }
// Returns the actual matcher description, given the matcher name, // Returns the description for a matcher defined using the MATCHER*()
// user-supplied description template string, interpolations in the // macro where the user-supplied description string is "", if
// string, and the printed values of the matcher parameters. // 'negation' is false; otherwise returns the description of the
string FormatMatcherDescription( // negation of the matcher. 'param_values' contains a list of strings
const char* matcher_name, const char* description, // that are the print-out of the matcher's parameters.
const Interpolations& interp, const Strings& param_values) { string FormatMatcherDescription(bool negation, const char* matcher_name,
string result; const Strings& param_values) {
if (*description == '\0') { string result = ConvertIdentifierNameToWords(matcher_name);
// When the user supplies an empty description, we calculate one
// from the matcher name.
result = ConvertIdentifierNameToWords(matcher_name);
if (param_values.size() >= 1) if (param_values.size() >= 1)
result += " " + JoinAsTuple(param_values); result += " " + JoinAsTuple(param_values);
} else { return negation ? "not (" + result + ")" : result;
// The end position of the last interpolation.
const char* last_interp_end = description;
for (size_t i = 0; i < interp.size(); i++) {
result.append(last_interp_end, interp[i].start_pos);
const int param_index = interp[i].param_index;
if (param_index == kTupleInterpolation) {
result += JoinAsTuple(param_values);
} else if (param_index == kPercentInterpolation) {
result += '%';
} else if (param_index != kInvalidInterpolation) {
result += param_values[param_index];
}
last_interp_end = interp[i].end_pos;
}
result += last_interp_end;
}
return result;
} }
} // namespace internal } // namespace internal
......
...@@ -72,6 +72,7 @@ using testing::MatchResultListener; ...@@ -72,6 +72,7 @@ using testing::MatchResultListener;
using testing::Ne; using testing::Ne;
using testing::Not; using testing::Not;
using testing::Pointee; using testing::Pointee;
using testing::PrintToString;
using testing::Ref; using testing::Ref;
using testing::StaticAssertTypeEq; using testing::StaticAssertTypeEq;
using testing::StrEq; using testing::StrEq;
...@@ -603,9 +604,8 @@ TEST(MatcherMacroTest, Works) { ...@@ -603,9 +604,8 @@ TEST(MatcherMacroTest, Works) {
EXPECT_EQ("", Explain(m, 7)); EXPECT_EQ("", Explain(m, 7));
} }
// Tests explaining match result in a MATCHER* macro. // This also tests that the description string can reference 'negation'.
MATCHER(IsEven2, negation ? "is odd" : "is even") {
MATCHER(IsEven2, "is even") {
if ((arg % 2) == 0) { if ((arg % 2) == 0) {
// Verifies that we can stream to result_listener, a listener // Verifies that we can stream to result_listener, a listener
// supplied by the MATCHER macro implicitly. // supplied by the MATCHER macro implicitly.
...@@ -617,7 +617,11 @@ MATCHER(IsEven2, "is even") { ...@@ -617,7 +617,11 @@ MATCHER(IsEven2, "is even") {
} }
} }
MATCHER_P2(EqSumOf, x, y, "") { // This also tests that the description string can reference matcher
// parameters.
MATCHER_P2(EqSumOf, x, y,
string(negation ? "doesn't equal" : "equals") + " the sum of " +
PrintToString(x) + " and " + PrintToString(y)) {
if (arg == (x + y)) { if (arg == (x + y)) {
*result_listener << "OK"; *result_listener << "OK";
return true; return true;
...@@ -631,6 +635,19 @@ MATCHER_P2(EqSumOf, x, y, "") { ...@@ -631,6 +635,19 @@ MATCHER_P2(EqSumOf, x, y, "") {
} }
} }
// Tests that the matcher description can reference 'negation' and the
// matcher parameters.
TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
const Matcher<int> m1 = IsEven2();
EXPECT_EQ("is even", Describe(m1));
EXPECT_EQ("is odd", DescribeNegation(m1));
const Matcher<int> m2 = EqSumOf(5, 9);
EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
}
// Tests explaining match result in a MATCHER* macro.
TEST(MatcherMacroTest, CanExplainMatchResult) { TEST(MatcherMacroTest, CanExplainMatchResult) {
const Matcher<int> m1 = IsEven2(); const Matcher<int> m1 = IsEven2();
EXPECT_EQ("OK", Explain(m1, 4)); EXPECT_EQ("OK", Explain(m1, 4));
...@@ -641,29 +658,6 @@ TEST(MatcherMacroTest, CanExplainMatchResult) { ...@@ -641,29 +658,6 @@ TEST(MatcherMacroTest, CanExplainMatchResult) {
EXPECT_EQ("diff == -1", Explain(m2, 4)); EXPECT_EQ("diff == -1", Explain(m2, 4));
} }
// Tests that the description string supplied to MATCHER() must be
// valid.
MATCHER(HasBadDescription, "Invalid%") {
// Uses arg to suppress "unused parameter" warning.
return arg==arg;
}
TEST(MatcherMacroTest,
CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
EXPECT_NONFATAL_FAILURE(
HasBadDescription(),
"Syntax error at index 7 in matcher description \"Invalid%\": "
"use \"%%\" instead of \"%\" to print \"%\".");
}
MATCHER(HasGoodDescription, "good") { return arg==arg; }
TEST(MatcherMacroTest, AcceptsValidDescription) {
const Matcher<int> m = HasGoodDescription();
EXPECT_EQ("good", Describe(m));
}
// Tests that the body of MATCHER() can reference the type of the // Tests that the body of MATCHER() can reference the type of the
// value being matched. // value being matched.
...@@ -723,29 +717,6 @@ TEST(MatcherPMacroTest, Works) { ...@@ -723,29 +717,6 @@ TEST(MatcherPMacroTest, Works) {
EXPECT_EQ("", Explain(m, 5)); EXPECT_EQ("", Explain(m, 5));
} }
// Tests that the description string supplied to MATCHER_P() must be
// valid.
MATCHER_P(HasBadDescription1, n, "not %(m)s good") {
return arg > n;
}
TEST(MatcherPMacroTest,
CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
EXPECT_NONFATAL_FAILURE(
HasBadDescription1(2),
"Syntax error at index 6 in matcher description \"not %(m)s good\": "
"\"m\" is an invalid parameter name.");
}
MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return arg==arg; }
TEST(MatcherPMacroTest, AcceptsValidDescription) {
const Matcher<int> m = HasGoodDescription1(5);
EXPECT_EQ("good 5", Describe(m));
}
// Tests that the description is calculated correctly from the matcher name. // Tests that the description is calculated correctly from the matcher name.
MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; } MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
...@@ -789,32 +760,6 @@ TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) { ...@@ -789,32 +760,6 @@ TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
} }
// Tests that the description string supplied to MATCHER_Pn() must be
// valid.
MATCHER_P2(HasBadDescription2, m, n, "not %(good") {
return arg > m + n;
}
TEST(MatcherPnMacroTest,
CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
EXPECT_NONFATAL_FAILURE(
HasBadDescription2(3, 4),
"Syntax error at index 4 in matcher description \"not %(good\": "
"an interpolation must end with \")s\", but \"%(good\" does not.");
}
MATCHER_P2(HasComplexDescription, foo, bar,
"is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") {
return arg==arg;
}
TEST(MatcherPnMacroTest, AcceptsValidDescription) {
Matcher<int> m = HasComplexDescription(100, "ducks");
EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)",
Describe(m));
}
// Tests that the body of MATCHER_Pn() can reference the parameter // Tests that the body of MATCHER_Pn() can reference the parameter
// types. // types.
......
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