OLD | NEW |
(Empty) | |
| 1 // Copyright 2007, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 // |
| 30 // Author: wan@google.com (Zhanyong Wan) |
| 31 |
| 32 // Google Mock - a framework for writing C++ mock classes. |
| 33 // |
| 34 // This file implements Matcher<const string&>, Matcher<string>, and |
| 35 // utilities for defining matchers. |
| 36 |
| 37 #include <gmock/gmock-matchers.h> |
| 38 #include <gmock/gmock-generated-matchers.h> |
| 39 |
| 40 #include <string.h> |
| 41 #include <sstream> |
| 42 #include <string> |
| 43 |
| 44 namespace testing { |
| 45 |
| 46 // Constructs a matcher that matches a const string& whose value is |
| 47 // equal to s. |
| 48 Matcher<const internal::string&>::Matcher(const internal::string& s) { |
| 49 *this = Eq(s); |
| 50 } |
| 51 |
| 52 // Constructs a matcher that matches a const string& whose value is |
| 53 // equal to s. |
| 54 Matcher<const internal::string&>::Matcher(const char* s) { |
| 55 *this = Eq(internal::string(s)); |
| 56 } |
| 57 |
| 58 // Constructs a matcher that matches a string whose value is equal to s. |
| 59 Matcher<internal::string>::Matcher(const internal::string& s) { *this = Eq(s); } |
| 60 |
| 61 // Constructs a matcher that matches a string whose value is equal to s. |
| 62 Matcher<internal::string>::Matcher(const char* s) { |
| 63 *this = Eq(internal::string(s)); |
| 64 } |
| 65 |
| 66 namespace internal { |
| 67 |
| 68 // Utilities for validating and formatting description strings in the |
| 69 // MATCHER*() macros. |
| 70 |
| 71 // Returns the 0-based index of the given parameter in the |
| 72 // NULL-terminated parameter array; if the parameter is "*", returns |
| 73 // kTupleInterpolation; if it's not found in the list, returns |
| 74 // kInvalidInterpolation. |
| 75 int GetParamIndex(const char* param_names[], const string& param_name) { |
| 76 if (param_name == "*") |
| 77 return kTupleInterpolation; |
| 78 |
| 79 for (int i = 0; param_names[i] != NULL; i++) { |
| 80 if (param_name == param_names[i]) |
| 81 return i; |
| 82 } |
| 83 return kInvalidInterpolation; |
| 84 } |
| 85 |
| 86 // If *pstr starts with the given prefix, modifies *pstr to be right |
| 87 // past the prefix and returns true; otherwise leaves *pstr unchanged |
| 88 // and returns false. None of pstr, *pstr, and prefix can be NULL. |
| 89 bool SkipPrefix(const char* prefix, const char** pstr) { |
| 90 const size_t prefix_len = strlen(prefix); |
| 91 if (strncmp(*pstr, prefix, prefix_len) == 0) { |
| 92 *pstr += prefix_len; |
| 93 return true; |
| 94 } |
| 95 return false; |
| 96 } |
| 97 |
| 98 // Helper function used by ValidateMatcherDescription() to format |
| 99 // error messages. |
| 100 string FormatMatcherDescriptionSyntaxError(const char* description, |
| 101 const char* error_pos) { |
| 102 ::std::stringstream ss; |
| 103 ss << "Syntax error at index " << (error_pos - description) |
| 104 << " in matcher description \"" << description << "\": "; |
| 105 return ss.str(); |
| 106 } |
| 107 |
| 108 // Parses a matcher description string and returns a vector of |
| 109 // interpolations that appear in the string; generates non-fatal |
| 110 // failures iff 'description' is an invalid matcher description. |
| 111 // 'param_names' is a NULL-terminated array of parameter names in the |
| 112 // order they appear in the MATCHER_P*() parameter list. |
| 113 Interpolations ValidateMatcherDescription( |
| 114 const char* param_names[], const char* description) { |
| 115 Interpolations interps; |
| 116 for (const char* p = description; *p != '\0';) { |
| 117 if (SkipPrefix("%%", &p)) { |
| 118 interps.push_back(Interpolation(p - 2, p, kPercentInterpolation)); |
| 119 } else if (SkipPrefix("%(", &p)) { |
| 120 const char* const q = strstr(p, ")s"); |
| 121 if (q == NULL) { |
| 122 // TODO(wan@google.com): change the source file location in |
| 123 // the failure to point to where the MATCHER*() macro is used. |
| 124 ADD_FAILURE() << FormatMatcherDescriptionSyntaxError(description, p - 2) |
| 125 << "an interpolation must end with \")s\", " |
| 126 << "but \"" << (p - 2) << "\" does not."; |
| 127 } else { |
| 128 const string param_name(p, q); |
| 129 const int param_index = GetParamIndex(param_names, param_name); |
| 130 if (param_index == kInvalidInterpolation) { |
| 131 ADD_FAILURE() << FormatMatcherDescriptionSyntaxError(description, p) |
| 132 << "\"" << param_name |
| 133 << "\" is an invalid parameter name."; |
| 134 } else { |
| 135 interps.push_back(Interpolation(p - 2, q + 2, param_index)); |
| 136 p = q + 2; |
| 137 } |
| 138 } |
| 139 } else { |
| 140 EXPECT_NE(*p, '%') << FormatMatcherDescriptionSyntaxError(description, p) |
| 141 << "use \"%%\" instead of \"%\" to print \"%\"."; |
| 142 ++p; |
| 143 } |
| 144 } |
| 145 return interps; |
| 146 } |
| 147 |
| 148 // Joins a vector of strings as if they are fields of a tuple; returns |
| 149 // the joined string. |
| 150 string JoinAsTuple(const Strings& fields) { |
| 151 switch (fields.size()) { |
| 152 case 0: |
| 153 return ""; |
| 154 case 1: |
| 155 return fields[0]; |
| 156 default: |
| 157 string result = "(" + fields[0]; |
| 158 for (size_t i = 1; i < fields.size(); i++) { |
| 159 result += ", "; |
| 160 result += fields[i]; |
| 161 } |
| 162 result += ")"; |
| 163 return result; |
| 164 } |
| 165 } |
| 166 |
| 167 // Returns the actual matcher description, given the matcher name, |
| 168 // user-supplied description template string, interpolations in the |
| 169 // string, and the printed values of the matcher parameters. |
| 170 string FormatMatcherDescription( |
| 171 const char* matcher_name, const char* description, |
| 172 const Interpolations& interp, const Strings& param_values) { |
| 173 string result; |
| 174 if (*description == '\0') { |
| 175 // When the user supplies an empty description, we calculate one |
| 176 // from the matcher name. |
| 177 result = ConvertIdentifierNameToWords(matcher_name); |
| 178 if (param_values.size() >= 1) |
| 179 result += " " + JoinAsTuple(param_values); |
| 180 } else { |
| 181 // The end position of the last interpolation. |
| 182 const char* last_interp_end = description; |
| 183 for (size_t i = 0; i < interp.size(); i++) { |
| 184 result.append(last_interp_end, interp[i].start_pos); |
| 185 const int param_index = interp[i].param_index; |
| 186 if (param_index == kTupleInterpolation) { |
| 187 result += JoinAsTuple(param_values); |
| 188 } else if (param_index == kPercentInterpolation) { |
| 189 result += '%'; |
| 190 } else if (param_index != kInvalidInterpolation) { |
| 191 result += param_values[param_index]; |
| 192 } |
| 193 last_interp_end = interp[i].end_pos; |
| 194 } |
| 195 result += last_interp_end; |
| 196 } |
| 197 |
| 198 return result; |
| 199 } |
| 200 |
| 201 } // namespace internal |
| 202 } // namespace testing |
OLD | NEW |