shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame^] | 1 | // Copyright 2008, 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 | // Google Mock - a framework for writing C++ mock classes. |
| 31 | // |
| 32 | // This file tests the built-in matchers generated by a script. |
| 33 | |
| 34 | #include <gmock/gmock-generated-matchers.h> |
| 35 | |
| 36 | #include <list> |
| 37 | #include <sstream> |
| 38 | #include <string> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include <gmock/gmock.h> |
| 42 | #include <gtest/gtest.h> |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | using std::list; |
| 47 | using std::stringstream; |
| 48 | using std::vector; |
| 49 | using testing::_; |
| 50 | using testing::ElementsAre; |
| 51 | using testing::ElementsAreArray; |
| 52 | using testing::Eq; |
| 53 | using testing::Ge; |
| 54 | using testing::Gt; |
| 55 | using testing::MakeMatcher; |
| 56 | using testing::Matcher; |
| 57 | using testing::MatcherInterface; |
| 58 | using testing::Ne; |
| 59 | using testing::Not; |
| 60 | using testing::Pointee; |
| 61 | using testing::Ref; |
| 62 | using testing::StrEq; |
| 63 | using testing::internal::string; |
| 64 | |
| 65 | // Returns the description of the given matcher. |
| 66 | template <typename T> |
| 67 | string Describe(const Matcher<T>& m) { |
| 68 | stringstream ss; |
| 69 | m.DescribeTo(&ss); |
| 70 | return ss.str(); |
| 71 | } |
| 72 | |
| 73 | // Returns the description of the negation of the given matcher. |
| 74 | template <typename T> |
| 75 | string DescribeNegation(const Matcher<T>& m) { |
| 76 | stringstream ss; |
| 77 | m.DescribeNegationTo(&ss); |
| 78 | return ss.str(); |
| 79 | } |
| 80 | |
| 81 | // Returns the reason why x matches, or doesn't match, m. |
| 82 | template <typename MatcherType, typename Value> |
| 83 | string Explain(const MatcherType& m, const Value& x) { |
| 84 | stringstream ss; |
| 85 | m.ExplainMatchResultTo(x, &ss); |
| 86 | return ss.str(); |
| 87 | } |
| 88 | |
| 89 | // For testing ExplainMatchResultTo(). |
| 90 | class GreaterThanMatcher : public MatcherInterface<int> { |
| 91 | public: |
| 92 | explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 93 | |
| 94 | virtual bool Matches(int lhs) const { return lhs > rhs_; } |
| 95 | |
| 96 | virtual void DescribeTo(::std::ostream* os) const { |
| 97 | *os << "is greater than " << rhs_; |
| 98 | } |
| 99 | |
| 100 | virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { |
| 101 | const int diff = lhs - rhs_; |
| 102 | if (diff > 0) { |
| 103 | *os << "is " << diff << " more than " << rhs_; |
| 104 | } else if (diff == 0) { |
| 105 | *os << "is the same as " << rhs_; |
| 106 | } else { |
| 107 | *os << "is " << -diff << " less than " << rhs_; |
| 108 | } |
| 109 | } |
| 110 | private: |
| 111 | const int rhs_; |
| 112 | }; |
| 113 | |
| 114 | Matcher<int> GreaterThan(int n) { |
| 115 | return MakeMatcher(new GreaterThanMatcher(n)); |
| 116 | } |
| 117 | |
| 118 | // Tests for ElementsAre(). |
| 119 | |
| 120 | // Evaluates to the number of elements in 'array'. |
| 121 | #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) |
| 122 | |
| 123 | TEST(ElementsAreTest, CanDescribeExpectingNoElement) { |
| 124 | Matcher<const vector<int>&> m = ElementsAre(); |
| 125 | EXPECT_EQ("is empty", Describe(m)); |
| 126 | } |
| 127 | |
| 128 | TEST(ElementsAreTest, CanDescribeExpectingOneElement) { |
| 129 | Matcher<vector<int> > m = ElementsAre(Gt(5)); |
| 130 | EXPECT_EQ("has 1 element that is greater than 5", Describe(m)); |
| 131 | } |
| 132 | |
| 133 | TEST(ElementsAreTest, CanDescribeExpectingManyElements) { |
| 134 | Matcher<list<string> > m = ElementsAre(StrEq("one"), "two"); |
| 135 | EXPECT_EQ("has 2 elements where\n" |
| 136 | "element 0 is equal to \"one\",\n" |
| 137 | "element 1 is equal to \"two\"", Describe(m)); |
| 138 | } |
| 139 | |
| 140 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { |
| 141 | Matcher<vector<int> > m = ElementsAre(); |
| 142 | EXPECT_EQ("is not empty", DescribeNegation(m)); |
| 143 | } |
| 144 | |
| 145 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) { |
| 146 | Matcher<const list<int>& > m = ElementsAre(Gt(5)); |
| 147 | EXPECT_EQ("does not have 1 element, or\n" |
| 148 | "element 0 is not greater than 5", DescribeNegation(m)); |
| 149 | } |
| 150 | |
| 151 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { |
| 152 | Matcher<const list<string>& > m = ElementsAre("one", "two"); |
| 153 | EXPECT_EQ("does not have 2 elements, or\n" |
| 154 | "element 0 is not equal to \"one\", or\n" |
| 155 | "element 1 is not equal to \"two\"", DescribeNegation(m)); |
| 156 | } |
| 157 | |
| 158 | TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { |
| 159 | Matcher<const list<int>& > m = ElementsAre(1, Ne(2)); |
| 160 | |
| 161 | list<int> test_list; |
| 162 | test_list.push_back(1); |
| 163 | test_list.push_back(3); |
| 164 | EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. |
| 165 | } |
| 166 | |
| 167 | TEST(ElementsAreTest, ExplainsNonTrivialMatch) { |
| 168 | Matcher<const vector<int>& > m = |
| 169 | ElementsAre(GreaterThan(1), 0, GreaterThan(2)); |
| 170 | |
| 171 | const int a[] = { 10, 0, 100 }; |
| 172 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 173 | EXPECT_EQ("element 0 is 9 more than 1,\n" |
| 174 | "element 2 is 98 more than 2", Explain(m, test_vector)); |
| 175 | } |
| 176 | |
| 177 | TEST(ElementsAreTest, CanExplainMismatchWrongSize) { |
| 178 | Matcher<const list<int>& > m = ElementsAre(1, 3); |
| 179 | |
| 180 | list<int> test_list; |
| 181 | // No need to explain when the container is empty. |
| 182 | EXPECT_EQ("", Explain(m, test_list)); |
| 183 | |
| 184 | test_list.push_back(1); |
| 185 | EXPECT_EQ("has 1 element", Explain(m, test_list)); |
| 186 | } |
| 187 | |
| 188 | TEST(ElementsAreTest, CanExplainMismatchRightSize) { |
| 189 | Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5)); |
| 190 | |
| 191 | vector<int> v; |
| 192 | v.push_back(2); |
| 193 | v.push_back(1); |
| 194 | EXPECT_EQ("element 0 doesn't match", Explain(m, v)); |
| 195 | |
| 196 | v[0] = 1; |
| 197 | EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v)); |
| 198 | } |
| 199 | |
| 200 | TEST(ElementsAreTest, MatchesOneElementVector) { |
| 201 | vector<string> test_vector; |
| 202 | test_vector.push_back("test string"); |
| 203 | |
| 204 | EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); |
| 205 | } |
| 206 | |
| 207 | TEST(ElementsAreTest, MatchesOneElementList) { |
| 208 | list<string> test_list; |
| 209 | test_list.push_back("test string"); |
| 210 | |
| 211 | EXPECT_THAT(test_list, ElementsAre("test string")); |
| 212 | } |
| 213 | |
| 214 | TEST(ElementsAreTest, MatchesThreeElementVector) { |
| 215 | vector<string> test_vector; |
| 216 | test_vector.push_back("one"); |
| 217 | test_vector.push_back("two"); |
| 218 | test_vector.push_back("three"); |
| 219 | |
| 220 | EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _)); |
| 221 | } |
| 222 | |
| 223 | TEST(ElementsAreTest, MatchesOneElementEqMatcher) { |
| 224 | vector<int> test_vector; |
| 225 | test_vector.push_back(4); |
| 226 | |
| 227 | EXPECT_THAT(test_vector, ElementsAre(Eq(4))); |
| 228 | } |
| 229 | |
| 230 | TEST(ElementsAreTest, MatchesOneElementAnyMatcher) { |
| 231 | vector<int> test_vector; |
| 232 | test_vector.push_back(4); |
| 233 | |
| 234 | EXPECT_THAT(test_vector, ElementsAre(_)); |
| 235 | } |
| 236 | |
| 237 | TEST(ElementsAreTest, MatchesOneElementValue) { |
| 238 | vector<int> test_vector; |
| 239 | test_vector.push_back(4); |
| 240 | |
| 241 | EXPECT_THAT(test_vector, ElementsAre(4)); |
| 242 | } |
| 243 | |
| 244 | TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { |
| 245 | vector<int> test_vector; |
| 246 | test_vector.push_back(1); |
| 247 | test_vector.push_back(2); |
| 248 | test_vector.push_back(3); |
| 249 | |
| 250 | EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _)); |
| 251 | } |
| 252 | |
| 253 | TEST(ElementsAreTest, MatchesTenElementVector) { |
| 254 | const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 255 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 256 | |
| 257 | EXPECT_THAT(test_vector, |
| 258 | // The element list can contain values and/or matchers |
| 259 | // of different types. |
| 260 | ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _)); |
| 261 | } |
| 262 | |
| 263 | TEST(ElementsAreTest, DoesNotMatchWrongSize) { |
| 264 | vector<string> test_vector; |
| 265 | test_vector.push_back("test string"); |
| 266 | test_vector.push_back("test string"); |
| 267 | |
| 268 | Matcher<vector<string> > m = ElementsAre(StrEq("test string")); |
| 269 | EXPECT_FALSE(m.Matches(test_vector)); |
| 270 | } |
| 271 | |
| 272 | TEST(ElementsAreTest, DoesNotMatchWrongValue) { |
| 273 | vector<string> test_vector; |
| 274 | test_vector.push_back("other string"); |
| 275 | |
| 276 | Matcher<vector<string> > m = ElementsAre(StrEq("test string")); |
| 277 | EXPECT_FALSE(m.Matches(test_vector)); |
| 278 | } |
| 279 | |
| 280 | TEST(ElementsAreTest, DoesNotMatchWrongOrder) { |
| 281 | vector<string> test_vector; |
| 282 | test_vector.push_back("one"); |
| 283 | test_vector.push_back("three"); |
| 284 | test_vector.push_back("two"); |
| 285 | |
| 286 | Matcher<vector<string> > m = ElementsAre( |
| 287 | StrEq("one"), StrEq("two"), StrEq("three")); |
| 288 | EXPECT_FALSE(m.Matches(test_vector)); |
| 289 | } |
| 290 | |
| 291 | TEST(ElementsAreTest, WorksForNestedContainer) { |
| 292 | const char* strings[] = { |
| 293 | "Hi", |
| 294 | "world" |
| 295 | }; |
| 296 | |
| 297 | vector<list<char> > nested; |
| 298 | for (int i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) { |
| 299 | nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); |
| 300 | } |
| 301 | |
| 302 | EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), |
| 303 | ElementsAre('w', 'o', _, _, 'd'))); |
| 304 | EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), |
| 305 | ElementsAre('w', 'o', _, _, 'd')))); |
| 306 | } |
| 307 | |
| 308 | TEST(ElementsAreTest, WorksWithByRefElementMatchers) { |
| 309 | int a[] = { 0, 1, 2 }; |
| 310 | vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 311 | |
| 312 | EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); |
| 313 | EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); |
| 314 | } |
| 315 | |
| 316 | TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { |
| 317 | int a[] = { 0, 1, 2 }; |
| 318 | vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 319 | |
| 320 | EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); |
| 321 | EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); |
| 322 | } |
| 323 | |
| 324 | // Tests for ElementsAreArray(). Since ElementsAreArray() shares most |
| 325 | // of the implementation with ElementsAre(), we don't test it as |
| 326 | // thoroughly here. |
| 327 | |
| 328 | TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { |
| 329 | const int a[] = { 1, 2, 3 }; |
| 330 | |
| 331 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 332 | EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 333 | |
| 334 | test_vector[2] = 0; |
| 335 | EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 336 | } |
| 337 | |
| 338 | TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { |
| 339 | const char* a[] = { "one", "two", "three" }; |
| 340 | |
| 341 | vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 342 | EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a))); |
| 343 | |
| 344 | const char** p = a; |
| 345 | test_vector[0] = "1"; |
| 346 | EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a)))); |
| 347 | } |
| 348 | |
| 349 | TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { |
| 350 | const char* a[] = { "one", "two", "three" }; |
| 351 | |
| 352 | vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 353 | EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 354 | |
| 355 | test_vector[0] = "1"; |
| 356 | EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 357 | } |
| 358 | |
| 359 | TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { |
| 360 | const Matcher<string> kMatcherArray[] = |
| 361 | { StrEq("one"), StrEq("two"), StrEq("three") }; |
| 362 | |
| 363 | vector<string> test_vector; |
| 364 | test_vector.push_back("one"); |
| 365 | test_vector.push_back("two"); |
| 366 | test_vector.push_back("three"); |
| 367 | EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); |
| 368 | |
| 369 | test_vector.push_back("three"); |
| 370 | EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); |
| 371 | } |
| 372 | |
| 373 | } // namespace |