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 // Helper function used by ValidateMatcherDescription() to format | |
87 // error messages. | |
88 string FormatMatcherDescriptionSyntaxError(const char* description, | |
89 const char* error_pos) { | |
90 ::std::stringstream ss; | |
91 ss << "Syntax error at index " << (error_pos - description) | |
92 << " in matcher description \"" << description << "\": "; | |
93 return ss.str(); | |
94 } | |
95 | |
96 // Parses a matcher description string and returns a vector of | |
97 // interpolations that appear in the string; generates non-fatal | |
98 // failures iff 'description' is an invalid matcher description. | |
99 // 'param_names' is a NULL-terminated array of parameter names in the | |
100 // order they appear in the MATCHER_P*() parameter list. | |
101 Interpolations ValidateMatcherDescription( | |
102 const char* param_names[], const char* description) { | |
103 Interpolations interps; | |
104 for (const char* p = description; *p != '\0';) { | |
105 if (SkipPrefix("%%", &p)) { | |
106 interps.push_back(Interpolation(p - 2, p, kPercentInterpolation)); | |
107 } else if (SkipPrefix("%(", &p)) { | |
108 const char* const q = strstr(p, ")s"); | |
109 if (q == NULL) { | |
110 // TODO(wan@google.com): change the source file location in | |
111 // the failure to point to where the MATCHER*() macro is used. | |
112 ADD_FAILURE() << FormatMatcherDescriptionSyntaxError(description, p - 2) | |
113 << "an interpolation must end with \")s\", " | |
114 << "but \"" << (p - 2) << "\" does not."; | |
115 } else { | |
116 const string param_name(p, q); | |
117 const int param_index = GetParamIndex(param_names, param_name); | |
118 if (param_index == kInvalidInterpolation) { | |
119 ADD_FAILURE() << FormatMatcherDescriptionSyntaxError(description, p) | |
120 << "\"" << param_name | |
121 << "\" is an invalid parameter name."; | |
122 } else { | |
123 interps.push_back(Interpolation(p - 2, q + 2, param_index)); | |
124 p = q + 2; | |
125 } | |
126 } | |
127 } else { | |
128 EXPECT_NE(*p, '%') << FormatMatcherDescriptionSyntaxError(description, p) | |
129 << "use \"%%\" instead of \"%\" to print \"%\"."; | |
130 ++p; | |
131 } | |
132 } | |
133 return interps; | |
134 } | |
135 | |
136 // Joins a vector of strings as if they are fields of a tuple; returns | |
137 // the joined string. | |
138 string JoinAsTuple(const Strings& fields) { | |
139 switch (fields.size()) { | |
140 case 0: | |
141 return ""; | |
142 case 1: | |
143 return fields[0]; | |
144 default: | |
145 string result = "(" + fields[0]; | |
146 for (size_t i = 1; i < fields.size(); i++) { | |
147 result += ", "; | |
148 result += fields[i]; | |
149 } | |
150 result += ")"; | |
151 return result; | |
152 } | |
153 } | |
154 | |
155 // Returns the actual matcher description, given the matcher name, | |
156 // user-supplied description template string, interpolations in the | |
157 // string, and the printed values of the matcher parameters. | |
158 string FormatMatcherDescription( | |
159 const char* matcher_name, const char* description, | |
160 const Interpolations& interp, const Strings& param_values) { | |
161 string result; | |
162 if (*description == '\0') { | |
163 // When the user supplies an empty description, we calculate one | |
164 // from the matcher name. | |
165 result = ConvertIdentifierNameToWords(matcher_name); | |
166 if (param_values.size() >= 1) | |
167 result += " " + JoinAsTuple(param_values); | |
168 } else { | |
169 // The end position of the last interpolation. | |
170 const char* last_interp_end = description; | |
171 for (size_t i = 0; i < interp.size(); i++) { | |
172 result.append(last_interp_end, interp[i].start_pos); | |
173 const int param_index = interp[i].param_index; | |
174 if (param_index == kTupleInterpolation) { | |
175 result += JoinAsTuple(param_values); | |
176 } else if (param_index == kPercentInterpolation) { | |
177 result += '%'; | |
178 } else if (param_index != kInvalidInterpolation) { | |
179 result += param_values[param_index]; | |
180 } | |
181 last_interp_end = interp[i].end_pos; | |
182 } | |
183 result += last_interp_end; | |
184 } | |
185 | |
186 return result; | |
187 } | |
188 | |
189 } // namespace internal | |
190 } // namespace testing | |
OLD | NEW |