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> |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 37 | #include <map> |
| 38 | #include <set> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 39 | #include <sstream> |
| 40 | #include <string> |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 41 | #include <utility> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 42 | #include <vector> |
| 43 | |
| 44 | #include <gmock/gmock.h> |
| 45 | #include <gtest/gtest.h> |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 46 | #include <gtest/gtest-spi.h> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 47 | |
| 48 | namespace { |
| 49 | |
| 50 | using std::list; |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 51 | using std::map; |
| 52 | using std::pair; |
| 53 | using std::set; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 54 | using std::stringstream; |
| 55 | using std::vector; |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 56 | using std::tr1::get; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 57 | using std::tr1::make_tuple; |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 58 | using std::tr1::tuple; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 59 | using testing::_; |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 60 | using testing::Args; |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 61 | using testing::Contains; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 62 | using testing::ElementsAre; |
| 63 | using testing::ElementsAreArray; |
| 64 | using testing::Eq; |
| 65 | using testing::Ge; |
| 66 | using testing::Gt; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 67 | using testing::Lt; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 68 | using testing::MakeMatcher; |
| 69 | using testing::Matcher; |
| 70 | using testing::MatcherInterface; |
| 71 | using testing::Ne; |
| 72 | using testing::Not; |
| 73 | using testing::Pointee; |
| 74 | using testing::Ref; |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 75 | using testing::StaticAssertTypeEq; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 76 | using testing::StrEq; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 77 | using testing::Value; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 78 | using testing::internal::string; |
| 79 | |
| 80 | // Returns the description of the given matcher. |
| 81 | template <typename T> |
| 82 | string Describe(const Matcher<T>& m) { |
| 83 | stringstream ss; |
| 84 | m.DescribeTo(&ss); |
| 85 | return ss.str(); |
| 86 | } |
| 87 | |
| 88 | // Returns the description of the negation of the given matcher. |
| 89 | template <typename T> |
| 90 | string DescribeNegation(const Matcher<T>& m) { |
| 91 | stringstream ss; |
| 92 | m.DescribeNegationTo(&ss); |
| 93 | return ss.str(); |
| 94 | } |
| 95 | |
| 96 | // Returns the reason why x matches, or doesn't match, m. |
| 97 | template <typename MatcherType, typename Value> |
| 98 | string Explain(const MatcherType& m, const Value& x) { |
| 99 | stringstream ss; |
| 100 | m.ExplainMatchResultTo(x, &ss); |
| 101 | return ss.str(); |
| 102 | } |
| 103 | |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 104 | // Tests Args<k0, ..., kn>(m). |
| 105 | |
| 106 | TEST(ArgsTest, AcceptsZeroTemplateArg) { |
| 107 | const tuple<int, bool> t(5, true); |
| 108 | EXPECT_THAT(t, Args<>(Eq(tuple<>()))); |
| 109 | EXPECT_THAT(t, Not(Args<>(Ne(tuple<>())))); |
| 110 | } |
| 111 | |
| 112 | TEST(ArgsTest, AcceptsOneTemplateArg) { |
| 113 | const tuple<int, bool> t(5, true); |
| 114 | EXPECT_THAT(t, Args<0>(Eq(make_tuple(5)))); |
| 115 | EXPECT_THAT(t, Args<1>(Eq(make_tuple(true)))); |
| 116 | EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false))))); |
| 117 | } |
| 118 | |
| 119 | TEST(ArgsTest, AcceptsTwoTemplateArgs) { |
| 120 | const tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 121 | |
| 122 | EXPECT_THAT(t, (Args<0, 1>(Lt()))); |
| 123 | EXPECT_THAT(t, (Args<1, 2>(Lt()))); |
| 124 | EXPECT_THAT(t, Not(Args<0, 2>(Gt()))); |
| 125 | } |
| 126 | |
| 127 | TEST(ArgsTest, AcceptsRepeatedTemplateArgs) { |
| 128 | const tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 129 | EXPECT_THAT(t, (Args<0, 0>(Eq()))); |
| 130 | EXPECT_THAT(t, Not(Args<1, 1>(Ne()))); |
| 131 | } |
| 132 | |
| 133 | TEST(ArgsTest, AcceptsDecreasingTemplateArgs) { |
| 134 | const tuple<short, int, long> t(4, 5, 6L); // NOLINT |
| 135 | EXPECT_THAT(t, (Args<2, 0>(Gt()))); |
| 136 | EXPECT_THAT(t, Not(Args<2, 1>(Lt()))); |
| 137 | } |
| 138 | |
| 139 | MATCHER(SumIsZero, "") { |
| 140 | return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0; |
| 141 | } |
| 142 | |
| 143 | TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) { |
| 144 | EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero()))); |
| 145 | EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero()))); |
| 146 | } |
| 147 | |
| 148 | TEST(ArgsTest, CanBeNested) { |
| 149 | const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT |
| 150 | EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq())))); |
| 151 | EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt())))); |
| 152 | } |
| 153 | |
| 154 | TEST(ArgsTest, CanMatchTupleByValue) { |
| 155 | typedef tuple<char, int, int> Tuple3; |
| 156 | const Matcher<Tuple3> m = Args<1, 2>(Lt()); |
| 157 | EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2))); |
| 158 | EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2))); |
| 159 | } |
| 160 | |
| 161 | TEST(ArgsTest, CanMatchTupleByReference) { |
| 162 | typedef tuple<char, char, int> Tuple3; |
| 163 | const Matcher<const Tuple3&> m = Args<0, 1>(Lt()); |
| 164 | EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2))); |
| 165 | EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2))); |
| 166 | } |
| 167 | |
| 168 | // Validates that arg is printed as str. |
| 169 | MATCHER_P(PrintsAs, str, "") { |
| 170 | typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(arg_type)) RawTuple; |
| 171 | return |
| 172 | testing::internal::UniversalPrinter<RawTuple>::PrintToString(arg) == str; |
| 173 | } |
| 174 | |
| 175 | TEST(ArgsTest, AcceptsTenTemplateArgs) { |
| 176 | EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), |
| 177 | (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( |
| 178 | PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); |
| 179 | EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9), |
| 180 | Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>( |
| 181 | PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)")))); |
| 182 | } |
| 183 | |
| 184 | TEST(ArgsTest, DescirbesSelfCorrectly) { |
| 185 | const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt()); |
| 186 | EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair (x, y) where x < y", |
| 187 | Describe(m)); |
| 188 | } |
| 189 | |
| 190 | TEST(ArgsTest, DescirbesNestedArgsCorrectly) { |
| 191 | const Matcher<const tuple<int, bool, char, int>&> m = |
| 192 | Args<0, 2, 3>(Args<2, 0>(Lt())); |
| 193 | EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple " |
| 194 | "whose fields (#2, #0) are a pair (x, y) where x < y", |
| 195 | Describe(m)); |
| 196 | } |
| 197 | |
| 198 | TEST(ArgsTest, DescribesNegationCorrectly) { |
| 199 | const Matcher<tuple<int, char> > m = Args<1, 0>(Gt()); |
| 200 | EXPECT_EQ("are a tuple whose fields (#1, #0) are a pair (x, y) " |
| 201 | "where x > y is false", |
| 202 | DescribeNegation(m)); |
| 203 | } |
| 204 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 205 | // For testing ExplainMatchResultTo(). |
| 206 | class GreaterThanMatcher : public MatcherInterface<int> { |
| 207 | public: |
| 208 | explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 209 | |
| 210 | virtual bool Matches(int lhs) const { return lhs > rhs_; } |
| 211 | |
| 212 | virtual void DescribeTo(::std::ostream* os) const { |
| 213 | *os << "is greater than " << rhs_; |
| 214 | } |
| 215 | |
| 216 | virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { |
| 217 | const int diff = lhs - rhs_; |
| 218 | if (diff > 0) { |
| 219 | *os << "is " << diff << " more than " << rhs_; |
| 220 | } else if (diff == 0) { |
| 221 | *os << "is the same as " << rhs_; |
| 222 | } else { |
| 223 | *os << "is " << -diff << " less than " << rhs_; |
| 224 | } |
| 225 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 226 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 227 | private: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 228 | int rhs_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | Matcher<int> GreaterThan(int n) { |
| 232 | return MakeMatcher(new GreaterThanMatcher(n)); |
| 233 | } |
| 234 | |
| 235 | // Tests for ElementsAre(). |
| 236 | |
| 237 | // Evaluates to the number of elements in 'array'. |
| 238 | #define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0])) |
| 239 | |
| 240 | TEST(ElementsAreTest, CanDescribeExpectingNoElement) { |
| 241 | Matcher<const vector<int>&> m = ElementsAre(); |
| 242 | EXPECT_EQ("is empty", Describe(m)); |
| 243 | } |
| 244 | |
| 245 | TEST(ElementsAreTest, CanDescribeExpectingOneElement) { |
| 246 | Matcher<vector<int> > m = ElementsAre(Gt(5)); |
| 247 | EXPECT_EQ("has 1 element that is greater than 5", Describe(m)); |
| 248 | } |
| 249 | |
| 250 | TEST(ElementsAreTest, CanDescribeExpectingManyElements) { |
| 251 | Matcher<list<string> > m = ElementsAre(StrEq("one"), "two"); |
| 252 | EXPECT_EQ("has 2 elements where\n" |
| 253 | "element 0 is equal to \"one\",\n" |
| 254 | "element 1 is equal to \"two\"", Describe(m)); |
| 255 | } |
| 256 | |
| 257 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { |
| 258 | Matcher<vector<int> > m = ElementsAre(); |
| 259 | EXPECT_EQ("is not empty", DescribeNegation(m)); |
| 260 | } |
| 261 | |
| 262 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) { |
| 263 | Matcher<const list<int>& > m = ElementsAre(Gt(5)); |
| 264 | EXPECT_EQ("does not have 1 element, or\n" |
| 265 | "element 0 is not greater than 5", DescribeNegation(m)); |
| 266 | } |
| 267 | |
| 268 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { |
| 269 | Matcher<const list<string>& > m = ElementsAre("one", "two"); |
| 270 | EXPECT_EQ("does not have 2 elements, or\n" |
| 271 | "element 0 is not equal to \"one\", or\n" |
| 272 | "element 1 is not equal to \"two\"", DescribeNegation(m)); |
| 273 | } |
| 274 | |
| 275 | TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { |
| 276 | Matcher<const list<int>& > m = ElementsAre(1, Ne(2)); |
| 277 | |
| 278 | list<int> test_list; |
| 279 | test_list.push_back(1); |
| 280 | test_list.push_back(3); |
| 281 | EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything. |
| 282 | } |
| 283 | |
| 284 | TEST(ElementsAreTest, ExplainsNonTrivialMatch) { |
| 285 | Matcher<const vector<int>& > m = |
| 286 | ElementsAre(GreaterThan(1), 0, GreaterThan(2)); |
| 287 | |
| 288 | const int a[] = { 10, 0, 100 }; |
| 289 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 290 | EXPECT_EQ("element 0 is 9 more than 1,\n" |
| 291 | "element 2 is 98 more than 2", Explain(m, test_vector)); |
| 292 | } |
| 293 | |
| 294 | TEST(ElementsAreTest, CanExplainMismatchWrongSize) { |
| 295 | Matcher<const list<int>& > m = ElementsAre(1, 3); |
| 296 | |
| 297 | list<int> test_list; |
| 298 | // No need to explain when the container is empty. |
| 299 | EXPECT_EQ("", Explain(m, test_list)); |
| 300 | |
| 301 | test_list.push_back(1); |
| 302 | EXPECT_EQ("has 1 element", Explain(m, test_list)); |
| 303 | } |
| 304 | |
| 305 | TEST(ElementsAreTest, CanExplainMismatchRightSize) { |
| 306 | Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5)); |
| 307 | |
| 308 | vector<int> v; |
| 309 | v.push_back(2); |
| 310 | v.push_back(1); |
| 311 | EXPECT_EQ("element 0 doesn't match", Explain(m, v)); |
| 312 | |
| 313 | v[0] = 1; |
| 314 | EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v)); |
| 315 | } |
| 316 | |
| 317 | TEST(ElementsAreTest, MatchesOneElementVector) { |
| 318 | vector<string> test_vector; |
| 319 | test_vector.push_back("test string"); |
| 320 | |
| 321 | EXPECT_THAT(test_vector, ElementsAre(StrEq("test string"))); |
| 322 | } |
| 323 | |
| 324 | TEST(ElementsAreTest, MatchesOneElementList) { |
| 325 | list<string> test_list; |
| 326 | test_list.push_back("test string"); |
| 327 | |
| 328 | EXPECT_THAT(test_list, ElementsAre("test string")); |
| 329 | } |
| 330 | |
| 331 | TEST(ElementsAreTest, MatchesThreeElementVector) { |
| 332 | vector<string> test_vector; |
| 333 | test_vector.push_back("one"); |
| 334 | test_vector.push_back("two"); |
| 335 | test_vector.push_back("three"); |
| 336 | |
| 337 | EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _)); |
| 338 | } |
| 339 | |
| 340 | TEST(ElementsAreTest, MatchesOneElementEqMatcher) { |
| 341 | vector<int> test_vector; |
| 342 | test_vector.push_back(4); |
| 343 | |
| 344 | EXPECT_THAT(test_vector, ElementsAre(Eq(4))); |
| 345 | } |
| 346 | |
| 347 | TEST(ElementsAreTest, MatchesOneElementAnyMatcher) { |
| 348 | vector<int> test_vector; |
| 349 | test_vector.push_back(4); |
| 350 | |
| 351 | EXPECT_THAT(test_vector, ElementsAre(_)); |
| 352 | } |
| 353 | |
| 354 | TEST(ElementsAreTest, MatchesOneElementValue) { |
| 355 | vector<int> test_vector; |
| 356 | test_vector.push_back(4); |
| 357 | |
| 358 | EXPECT_THAT(test_vector, ElementsAre(4)); |
| 359 | } |
| 360 | |
| 361 | TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { |
| 362 | vector<int> test_vector; |
| 363 | test_vector.push_back(1); |
| 364 | test_vector.push_back(2); |
| 365 | test_vector.push_back(3); |
| 366 | |
| 367 | EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _)); |
| 368 | } |
| 369 | |
| 370 | TEST(ElementsAreTest, MatchesTenElementVector) { |
| 371 | const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 372 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 373 | |
| 374 | EXPECT_THAT(test_vector, |
| 375 | // The element list can contain values and/or matchers |
| 376 | // of different types. |
| 377 | ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _)); |
| 378 | } |
| 379 | |
| 380 | TEST(ElementsAreTest, DoesNotMatchWrongSize) { |
| 381 | vector<string> test_vector; |
| 382 | test_vector.push_back("test string"); |
| 383 | test_vector.push_back("test string"); |
| 384 | |
| 385 | Matcher<vector<string> > m = ElementsAre(StrEq("test string")); |
| 386 | EXPECT_FALSE(m.Matches(test_vector)); |
| 387 | } |
| 388 | |
| 389 | TEST(ElementsAreTest, DoesNotMatchWrongValue) { |
| 390 | vector<string> test_vector; |
| 391 | test_vector.push_back("other string"); |
| 392 | |
| 393 | Matcher<vector<string> > m = ElementsAre(StrEq("test string")); |
| 394 | EXPECT_FALSE(m.Matches(test_vector)); |
| 395 | } |
| 396 | |
| 397 | TEST(ElementsAreTest, DoesNotMatchWrongOrder) { |
| 398 | vector<string> test_vector; |
| 399 | test_vector.push_back("one"); |
| 400 | test_vector.push_back("three"); |
| 401 | test_vector.push_back("two"); |
| 402 | |
| 403 | Matcher<vector<string> > m = ElementsAre( |
| 404 | StrEq("one"), StrEq("two"), StrEq("three")); |
| 405 | EXPECT_FALSE(m.Matches(test_vector)); |
| 406 | } |
| 407 | |
| 408 | TEST(ElementsAreTest, WorksForNestedContainer) { |
| 409 | const char* strings[] = { |
| 410 | "Hi", |
| 411 | "world" |
| 412 | }; |
| 413 | |
| 414 | vector<list<char> > nested; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 415 | for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 416 | nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); |
| 417 | } |
| 418 | |
| 419 | EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), |
| 420 | ElementsAre('w', 'o', _, _, 'd'))); |
| 421 | EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), |
| 422 | ElementsAre('w', 'o', _, _, 'd')))); |
| 423 | } |
| 424 | |
| 425 | TEST(ElementsAreTest, WorksWithByRefElementMatchers) { |
| 426 | int a[] = { 0, 1, 2 }; |
| 427 | vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 428 | |
| 429 | EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); |
| 430 | EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); |
| 431 | } |
| 432 | |
| 433 | TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { |
| 434 | int a[] = { 0, 1, 2 }; |
| 435 | vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 436 | |
| 437 | EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); |
| 438 | EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); |
| 439 | } |
| 440 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 441 | TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { |
| 442 | int array[] = { 0, 1, 2 }; |
| 443 | EXPECT_THAT(array, ElementsAre(0, 1, _)); |
| 444 | EXPECT_THAT(array, Not(ElementsAre(1, _, _))); |
| 445 | EXPECT_THAT(array, Not(ElementsAre(0, _))); |
| 446 | } |
| 447 | |
| 448 | class NativeArrayPassedAsPointerAndSize { |
| 449 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 450 | NativeArrayPassedAsPointerAndSize() {} |
| 451 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 452 | MOCK_METHOD2(Helper, void(int* array, int size)); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 453 | |
| 454 | private: |
| 455 | GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { |
| 459 | int array[] = { 0, 1 }; |
| 460 | ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2); |
| 461 | EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); |
| 462 | EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); |
| 463 | |
| 464 | NativeArrayPassedAsPointerAndSize helper; |
| 465 | EXPECT_CALL(helper, Helper(_, _)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 466 | .With(ElementsAre(0, 1)); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 467 | helper.Helper(array, 2); |
| 468 | } |
| 469 | |
| 470 | TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) { |
| 471 | const char a2[][3] = { "hi", "lo" }; |
| 472 | EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'), |
| 473 | ElementsAre('l', 'o', '\0'))); |
| 474 | EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo"))); |
| 475 | EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')), |
| 476 | ElementsAre('l', 'o', '\0'))); |
| 477 | } |
| 478 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 479 | // Tests for ElementsAreArray(). Since ElementsAreArray() shares most |
| 480 | // of the implementation with ElementsAre(), we don't test it as |
| 481 | // thoroughly here. |
| 482 | |
| 483 | TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { |
| 484 | const int a[] = { 1, 2, 3 }; |
| 485 | |
| 486 | vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 487 | EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 488 | |
| 489 | test_vector[2] = 0; |
| 490 | EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 491 | } |
| 492 | |
| 493 | TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { |
| 494 | const char* a[] = { "one", "two", "three" }; |
| 495 | |
| 496 | vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 497 | EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a))); |
| 498 | |
| 499 | const char** p = a; |
| 500 | test_vector[0] = "1"; |
| 501 | EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a)))); |
| 502 | } |
| 503 | |
| 504 | TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { |
| 505 | const char* a[] = { "one", "two", "three" }; |
| 506 | |
| 507 | vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); |
| 508 | EXPECT_THAT(test_vector, ElementsAreArray(a)); |
| 509 | |
| 510 | test_vector[0] = "1"; |
| 511 | EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
| 512 | } |
| 513 | |
| 514 | TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { |
| 515 | const Matcher<string> kMatcherArray[] = |
| 516 | { StrEq("one"), StrEq("two"), StrEq("three") }; |
| 517 | |
| 518 | vector<string> test_vector; |
| 519 | test_vector.push_back("one"); |
| 520 | test_vector.push_back("two"); |
| 521 | test_vector.push_back("three"); |
| 522 | EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); |
| 523 | |
| 524 | test_vector.push_back("three"); |
| 525 | EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); |
| 526 | } |
| 527 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 528 | // Since ElementsAre() and ElementsAreArray() share much of the |
| 529 | // implementation, we only do a sanity test for native arrays here. |
| 530 | TEST(ElementsAreArrayTest, WorksWithNativeArray) { |
| 531 | ::std::string a[] = { "hi", "ho" }; |
| 532 | ::std::string b[] = { "hi", "ho" }; |
| 533 | |
| 534 | EXPECT_THAT(a, ElementsAreArray(b)); |
| 535 | EXPECT_THAT(a, ElementsAreArray(b, 2)); |
| 536 | EXPECT_THAT(a, Not(ElementsAreArray(b, 1))); |
| 537 | } |
| 538 | |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 539 | // Tests for the MATCHER*() macro family. |
| 540 | |
| 541 | // Tests that a simple MATCHER() definition works. |
| 542 | |
| 543 | MATCHER(IsEven, "") { return (arg % 2) == 0; } |
| 544 | |
| 545 | TEST(MatcherMacroTest, Works) { |
| 546 | const Matcher<int> m = IsEven(); |
| 547 | EXPECT_TRUE(m.Matches(6)); |
| 548 | EXPECT_FALSE(m.Matches(7)); |
| 549 | |
| 550 | EXPECT_EQ("is even", Describe(m)); |
| 551 | EXPECT_EQ("not (is even)", DescribeNegation(m)); |
| 552 | EXPECT_EQ("", Explain(m, 6)); |
| 553 | EXPECT_EQ("", Explain(m, 7)); |
| 554 | } |
| 555 | |
| 556 | // Tests that the description string supplied to MATCHER() must be |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 557 | // valid. |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 558 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 559 | MATCHER(HasBadDescription, "Invalid%") { |
| 560 | // Uses arg to suppress "unused parameter" warning. |
| 561 | return arg==arg; |
| 562 | } |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 563 | |
| 564 | TEST(MatcherMacroTest, |
| 565 | CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 566 | EXPECT_NONFATAL_FAILURE( |
| 567 | HasBadDescription(), |
| 568 | "Syntax error at index 7 in matcher description \"Invalid%\": " |
| 569 | "use \"%%\" instead of \"%\" to print \"%\"."); |
| 570 | } |
| 571 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 572 | MATCHER(HasGoodDescription, "good") { return arg==arg; } |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 573 | |
| 574 | TEST(MatcherMacroTest, AcceptsValidDescription) { |
| 575 | const Matcher<int> m = HasGoodDescription(); |
| 576 | EXPECT_EQ("good", Describe(m)); |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | // Tests that the body of MATCHER() can reference the type of the |
| 580 | // value being matched. |
| 581 | |
| 582 | MATCHER(IsEmptyString, "") { |
| 583 | StaticAssertTypeEq< ::std::string, arg_type>(); |
| 584 | return arg == ""; |
| 585 | } |
| 586 | |
| 587 | MATCHER(IsEmptyStringByRef, "") { |
| 588 | StaticAssertTypeEq<const ::std::string&, arg_type>(); |
| 589 | return arg == ""; |
| 590 | } |
| 591 | |
| 592 | TEST(MatcherMacroTest, CanReferenceArgType) { |
| 593 | const Matcher< ::std::string> m1 = IsEmptyString(); |
| 594 | EXPECT_TRUE(m1.Matches("")); |
| 595 | |
| 596 | const Matcher<const ::std::string&> m2 = IsEmptyStringByRef(); |
| 597 | EXPECT_TRUE(m2.Matches("")); |
| 598 | } |
| 599 | |
| 600 | // Tests that MATCHER() can be used in a namespace. |
| 601 | |
| 602 | namespace matcher_test { |
| 603 | MATCHER(IsOdd, "") { return (arg % 2) != 0; } |
| 604 | } // namespace matcher_test |
| 605 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 606 | TEST(MatcherMacroTest, WorksInNamespace) { |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 607 | Matcher<int> m = matcher_test::IsOdd(); |
| 608 | EXPECT_FALSE(m.Matches(4)); |
| 609 | EXPECT_TRUE(m.Matches(5)); |
| 610 | } |
| 611 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 612 | // Tests that Value() can be used to compose matchers. |
| 613 | MATCHER(IsPositiveOdd, "") { |
| 614 | return Value(arg, matcher_test::IsOdd()) && arg > 0; |
| 615 | } |
| 616 | |
| 617 | TEST(MatcherMacroTest, CanBeComposedUsingValue) { |
| 618 | EXPECT_THAT(3, IsPositiveOdd()); |
| 619 | EXPECT_THAT(4, Not(IsPositiveOdd())); |
| 620 | EXPECT_THAT(-1, Not(IsPositiveOdd())); |
| 621 | } |
| 622 | |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 623 | // Tests that a simple MATCHER_P() definition works. |
| 624 | |
| 625 | MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; } |
| 626 | |
| 627 | TEST(MatcherPMacroTest, Works) { |
| 628 | const Matcher<int> m = IsGreaterThan32And(5); |
| 629 | EXPECT_TRUE(m.Matches(36)); |
| 630 | EXPECT_FALSE(m.Matches(5)); |
| 631 | |
| 632 | EXPECT_EQ("is greater than 32 and 5", Describe(m)); |
| 633 | EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m)); |
| 634 | EXPECT_EQ("", Explain(m, 36)); |
| 635 | EXPECT_EQ("", Explain(m, 5)); |
| 636 | } |
| 637 | |
| 638 | // Tests that the description string supplied to MATCHER_P() must be |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 639 | // valid. |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 640 | |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 641 | MATCHER_P(HasBadDescription1, n, "not %(m)s good") { |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 642 | return arg > n; |
| 643 | } |
| 644 | |
| 645 | TEST(MatcherPMacroTest, |
| 646 | CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 647 | EXPECT_NONFATAL_FAILURE( |
| 648 | HasBadDescription1(2), |
| 649 | "Syntax error at index 6 in matcher description \"not %(m)s good\": " |
| 650 | "\"m\" is an invalid parameter name."); |
| 651 | } |
| 652 | |
| 653 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 654 | MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return arg==arg; } |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 655 | |
| 656 | TEST(MatcherPMacroTest, AcceptsValidDescription) { |
| 657 | const Matcher<int> m = HasGoodDescription1(5); |
| 658 | EXPECT_EQ("good 5", Describe(m)); |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // Tests that the description is calculated correctly from the matcher name. |
| 662 | MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; } |
| 663 | |
| 664 | TEST(MatcherPMacroTest, GeneratesCorrectDescription) { |
| 665 | const Matcher<int> m = _is_Greater_Than32and_(5); |
| 666 | |
| 667 | EXPECT_EQ("is greater than 32 and 5", Describe(m)); |
| 668 | EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m)); |
| 669 | EXPECT_EQ("", Explain(m, 36)); |
| 670 | EXPECT_EQ("", Explain(m, 5)); |
| 671 | } |
| 672 | |
| 673 | // Tests that a MATCHER_P matcher can be explicitly instantiated with |
| 674 | // a reference parameter type. |
| 675 | |
| 676 | class UncopyableFoo { |
| 677 | public: |
| 678 | explicit UncopyableFoo(char value) : value_(value) {} |
| 679 | private: |
| 680 | UncopyableFoo(const UncopyableFoo&); |
| 681 | void operator=(const UncopyableFoo&); |
| 682 | |
| 683 | char value_; |
| 684 | }; |
| 685 | |
| 686 | MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; } |
| 687 | |
| 688 | TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) { |
| 689 | UncopyableFoo foo1('1'), foo2('2'); |
| 690 | const Matcher<const UncopyableFoo&> m = |
| 691 | ReferencesUncopyable<const UncopyableFoo&>(foo1); |
| 692 | |
| 693 | EXPECT_TRUE(m.Matches(foo1)); |
| 694 | EXPECT_FALSE(m.Matches(foo2)); |
| 695 | |
| 696 | // We don't want the address of the parameter printed, as most |
| 697 | // likely it will just annoy the user. If the address is |
| 698 | // interesting, the user should consider passing the parameter by |
| 699 | // pointer instead. |
| 700 | EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m)); |
| 701 | } |
| 702 | |
| 703 | |
| 704 | // Tests that the description string supplied to MATCHER_Pn() must be |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 705 | // valid. |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 706 | |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 707 | MATCHER_P2(HasBadDescription2, m, n, "not %(good") { |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 708 | return arg > m + n; |
| 709 | } |
| 710 | |
| 711 | TEST(MatcherPnMacroTest, |
| 712 | CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) { |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 713 | EXPECT_NONFATAL_FAILURE( |
| 714 | HasBadDescription2(3, 4), |
| 715 | "Syntax error at index 4 in matcher description \"not %(good\": " |
| 716 | "an interpolation must end with \")s\", but \"%(good\" does not."); |
| 717 | } |
| 718 | |
| 719 | MATCHER_P2(HasComplexDescription, foo, bar, |
| 720 | "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") { |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 721 | return arg==arg; |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | TEST(MatcherPnMacroTest, AcceptsValidDescription) { |
| 725 | Matcher<int> m = HasComplexDescription(100, "ducks"); |
| 726 | EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)", |
| 727 | Describe(m)); |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // Tests that the body of MATCHER_Pn() can reference the parameter |
| 731 | // types. |
| 732 | |
| 733 | MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") { |
| 734 | StaticAssertTypeEq<int, foo_type>(); |
| 735 | StaticAssertTypeEq<long, bar_type>(); // NOLINT |
| 736 | StaticAssertTypeEq<char, baz_type>(); |
| 737 | return arg == 0; |
| 738 | } |
| 739 | |
| 740 | TEST(MatcherPnMacroTest, CanReferenceParamTypes) { |
| 741 | EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a')); |
| 742 | } |
| 743 | |
| 744 | // Tests that a MATCHER_Pn matcher can be explicitly instantiated with |
| 745 | // reference parameter types. |
| 746 | |
| 747 | MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") { |
| 748 | return &arg == &variable1 || &arg == &variable2; |
| 749 | } |
| 750 | |
| 751 | TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) { |
| 752 | UncopyableFoo foo1('1'), foo2('2'), foo3('3'); |
| 753 | const Matcher<const UncopyableFoo&> m = |
| 754 | ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
| 755 | |
| 756 | EXPECT_TRUE(m.Matches(foo1)); |
| 757 | EXPECT_TRUE(m.Matches(foo2)); |
| 758 | EXPECT_FALSE(m.Matches(foo3)); |
| 759 | } |
| 760 | |
| 761 | TEST(MatcherPnMacroTest, |
| 762 | GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) { |
| 763 | UncopyableFoo foo1('1'), foo2('2'); |
| 764 | const Matcher<const UncopyableFoo&> m = |
| 765 | ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
| 766 | |
| 767 | // We don't want the addresses of the parameters printed, as most |
| 768 | // likely they will just annoy the user. If the addresses are |
| 769 | // interesting, the user should consider passing the parameters by |
| 770 | // pointers instead. |
| 771 | EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)", |
| 772 | Describe(m)); |
| 773 | } |
| 774 | |
| 775 | // Tests that a simple MATCHER_P2() definition works. |
| 776 | |
| 777 | MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; } |
| 778 | |
| 779 | TEST(MatcherPnMacroTest, Works) { |
| 780 | const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT |
| 781 | EXPECT_TRUE(m.Matches(36L)); |
| 782 | EXPECT_FALSE(m.Matches(15L)); |
| 783 | |
| 784 | EXPECT_EQ("is not in closed range (10, 20)", Describe(m)); |
| 785 | EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m)); |
| 786 | EXPECT_EQ("", Explain(m, 36L)); |
| 787 | EXPECT_EQ("", Explain(m, 15L)); |
| 788 | } |
| 789 | |
| 790 | // Tests that MATCHER*() definitions can be overloaded on the number |
| 791 | // of parameters; also tests MATCHER_Pn() where n >= 3. |
| 792 | |
| 793 | MATCHER(EqualsSumOf, "") { return arg == 0; } |
| 794 | MATCHER_P(EqualsSumOf, a, "") { return arg == a; } |
| 795 | MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; } |
| 796 | MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; } |
| 797 | MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; } |
| 798 | MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; } |
| 799 | MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") { |
| 800 | return arg == a + b + c + d + e + f; |
| 801 | } |
| 802 | MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") { |
| 803 | return arg == a + b + c + d + e + f + g; |
| 804 | } |
| 805 | MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") { |
| 806 | return arg == a + b + c + d + e + f + g + h; |
| 807 | } |
| 808 | MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") { |
| 809 | return arg == a + b + c + d + e + f + g + h + i; |
| 810 | } |
| 811 | MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") { |
| 812 | return arg == a + b + c + d + e + f + g + h + i + j; |
| 813 | } |
| 814 | |
| 815 | TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) { |
| 816 | EXPECT_THAT(0, EqualsSumOf()); |
| 817 | EXPECT_THAT(1, EqualsSumOf(1)); |
| 818 | EXPECT_THAT(12, EqualsSumOf(10, 2)); |
| 819 | EXPECT_THAT(123, EqualsSumOf(100, 20, 3)); |
| 820 | EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4)); |
| 821 | EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5)); |
| 822 | EXPECT_THAT("abcdef", |
| 823 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')); |
| 824 | EXPECT_THAT("abcdefg", |
| 825 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g')); |
| 826 | EXPECT_THAT("abcdefgh", |
| 827 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 828 | "h")); |
| 829 | EXPECT_THAT("abcdefghi", |
| 830 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 831 | "h", 'i')); |
| 832 | EXPECT_THAT("abcdefghij", |
| 833 | EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 834 | "h", 'i', ::std::string("j"))); |
| 835 | |
| 836 | EXPECT_THAT(1, Not(EqualsSumOf())); |
| 837 | EXPECT_THAT(-1, Not(EqualsSumOf(1))); |
| 838 | EXPECT_THAT(-12, Not(EqualsSumOf(10, 2))); |
| 839 | EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3))); |
| 840 | EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4))); |
| 841 | EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5))); |
| 842 | EXPECT_THAT("abcdef ", |
| 843 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'))); |
| 844 | EXPECT_THAT("abcdefg ", |
| 845 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', |
| 846 | 'g'))); |
| 847 | EXPECT_THAT("abcdefgh ", |
| 848 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 849 | "h"))); |
| 850 | EXPECT_THAT("abcdefghi ", |
| 851 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 852 | "h", 'i'))); |
| 853 | EXPECT_THAT("abcdefghij ", |
| 854 | Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', |
| 855 | "h", 'i', ::std::string("j")))); |
| 856 | } |
| 857 | |
| 858 | // Tests that a MATCHER_Pn() definition can be instantiated with any |
| 859 | // compatible parameter types. |
| 860 | TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) { |
| 861 | EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3))); |
| 862 | EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d")); |
| 863 | |
| 864 | EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3)))); |
| 865 | EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d"))); |
| 866 | } |
| 867 | |
| 868 | // Tests that the matcher body can promote the parameter types. |
| 869 | |
| 870 | MATCHER_P2(EqConcat, prefix, suffix, "") { |
| 871 | // The following lines promote the two parameters to desired types. |
| 872 | std::string prefix_str(prefix); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame^] | 873 | char suffix_char = static_cast<char>(suffix); |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 874 | return arg == prefix_str + suffix_char; |
| 875 | } |
| 876 | |
| 877 | TEST(MatcherPnMacroTest, SimpleTypePromotion) { |
| 878 | Matcher<std::string> no_promo = |
| 879 | EqConcat(std::string("foo"), 't'); |
| 880 | Matcher<const std::string&> promo = |
| 881 | EqConcat("foo", static_cast<int>('t')); |
| 882 | EXPECT_FALSE(no_promo.Matches("fool")); |
| 883 | EXPECT_FALSE(promo.Matches("fool")); |
| 884 | EXPECT_TRUE(no_promo.Matches("foot")); |
| 885 | EXPECT_TRUE(promo.Matches("foot")); |
| 886 | } |
| 887 | |
| 888 | // Verifies the type of a MATCHER*. |
| 889 | |
| 890 | TEST(MatcherPnMacroTest, TypesAreCorrect) { |
| 891 | // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable. |
| 892 | EqualsSumOfMatcher a0 = EqualsSumOf(); |
| 893 | |
| 894 | // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable. |
| 895 | EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1); |
| 896 | |
| 897 | // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk |
| 898 | // variable, and so on. |
| 899 | EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2'); |
| 900 | EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3'); |
| 901 | EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4'); |
| 902 | EqualsSumOfMatcherP5<int, int, int, int, char> a5 = |
| 903 | EqualsSumOf(1, 2, 3, 4, '5'); |
| 904 | EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 = |
| 905 | EqualsSumOf(1, 2, 3, 4, 5, '6'); |
| 906 | EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 = |
| 907 | EqualsSumOf(1, 2, 3, 4, 5, 6, '7'); |
| 908 | EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 = |
| 909 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8'); |
| 910 | EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 = |
| 911 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9'); |
| 912 | EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 = |
| 913 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); |
| 914 | } |
| 915 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 916 | // Tests that matcher-typed parameters can be used in Value() inside a |
| 917 | // MATCHER_Pn definition. |
| 918 | |
| 919 | // Succeeds if arg matches exactly 2 of the 3 matchers. |
| 920 | MATCHER_P3(TwoOf, m1, m2, m3, "") { |
| 921 | const int count = static_cast<int>(Value(arg, m1)) |
| 922 | + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3)); |
| 923 | return count == 2; |
| 924 | } |
| 925 | |
| 926 | TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) { |
| 927 | EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10))); |
| 928 | EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0)))); |
| 929 | } |
| 930 | |
| 931 | // Tests Contains(). |
| 932 | |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 933 | TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { |
| 934 | list<int> some_list; |
| 935 | some_list.push_back(3); |
| 936 | some_list.push_back(1); |
| 937 | some_list.push_back(2); |
| 938 | EXPECT_THAT(some_list, Contains(1)); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 939 | EXPECT_THAT(some_list, Contains(Gt(2.5))); |
| 940 | EXPECT_THAT(some_list, Contains(Eq(2.0f))); |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 941 | |
| 942 | list<string> another_list; |
| 943 | another_list.push_back("fee"); |
| 944 | another_list.push_back("fie"); |
| 945 | another_list.push_back("foe"); |
| 946 | another_list.push_back("fum"); |
| 947 | EXPECT_THAT(another_list, Contains(string("fee"))); |
| 948 | } |
| 949 | |
| 950 | TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) { |
| 951 | list<int> some_list; |
| 952 | some_list.push_back(3); |
| 953 | some_list.push_back(1); |
| 954 | EXPECT_THAT(some_list, Not(Contains(4))); |
| 955 | } |
| 956 | |
| 957 | TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { |
| 958 | set<int> some_set; |
| 959 | some_set.insert(3); |
| 960 | some_set.insert(1); |
| 961 | some_set.insert(2); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 962 | EXPECT_THAT(some_set, Contains(Eq(1.0))); |
| 963 | EXPECT_THAT(some_set, Contains(Eq(3.0f))); |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 964 | EXPECT_THAT(some_set, Contains(2)); |
| 965 | |
| 966 | set<const char*> another_set; |
| 967 | another_set.insert("fee"); |
| 968 | another_set.insert("fie"); |
| 969 | another_set.insert("foe"); |
| 970 | another_set.insert("fum"); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 971 | EXPECT_THAT(another_set, Contains(Eq(string("fum")))); |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { |
| 975 | set<int> some_set; |
| 976 | some_set.insert(3); |
| 977 | some_set.insert(1); |
| 978 | EXPECT_THAT(some_set, Not(Contains(4))); |
| 979 | |
| 980 | set<const char*> c_string_set; |
| 981 | c_string_set.insert("hello"); |
| 982 | EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str()))); |
| 983 | } |
| 984 | |
| 985 | TEST(ContainsTest, DescribesItselfCorrectly) { |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 986 | const int a[2] = { 1, 2 }; |
| 987 | Matcher<const int(&)[2]> m = Contains(2); |
| 988 | EXPECT_EQ("element 1 matches", Explain(m, a)); |
| 989 | |
| 990 | m = Contains(3); |
| 991 | EXPECT_EQ("", Explain(m, a)); |
| 992 | } |
| 993 | |
| 994 | TEST(ContainsTest, ExplainsMatchResultCorrectly) { |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 995 | Matcher<vector<int> > m = Contains(1); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 996 | EXPECT_EQ("contains at least one element that is equal to 1", Describe(m)); |
| 997 | |
| 998 | Matcher<vector<int> > m2 = Not(m); |
| 999 | EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2)); |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { |
| 1003 | map<const char*, int> my_map; |
| 1004 | const char* bar = "a string"; |
| 1005 | my_map[bar] = 2; |
| 1006 | EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2))); |
| 1007 | |
| 1008 | map<string, int> another_map; |
| 1009 | another_map["fee"] = 1; |
| 1010 | another_map["fie"] = 2; |
| 1011 | another_map["foe"] = 3; |
| 1012 | another_map["fum"] = 4; |
| 1013 | EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1))); |
| 1014 | EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2))); |
| 1015 | } |
| 1016 | |
| 1017 | TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { |
| 1018 | map<int, int> some_map; |
| 1019 | some_map[1] = 11; |
| 1020 | some_map[2] = 22; |
| 1021 | EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23)))); |
| 1022 | } |
| 1023 | |
| 1024 | TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { |
| 1025 | const char* string_array[] = { "fee", "fie", "foe", "fum" }; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1026 | EXPECT_THAT(string_array, Contains(Eq(string("fum")))); |
zhanyong.wan | 1bee7b2 | 2009-02-20 18:31:04 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { |
| 1030 | int int_array[] = { 1, 2, 3, 4 }; |
| 1031 | EXPECT_THAT(int_array, Not(Contains(5))); |
| 1032 | } |
| 1033 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1034 | TEST(ContainsTest, AcceptsMatcher) { |
| 1035 | const int a[] = { 1, 2, 3 }; |
| 1036 | EXPECT_THAT(a, Contains(Gt(2))); |
| 1037 | EXPECT_THAT(a, Not(Contains(Gt(4)))); |
| 1038 | } |
| 1039 | |
| 1040 | TEST(ContainsTest, WorksForNativeArrayAsTuple) { |
| 1041 | const int a[] = { 1, 2 }; |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 1042 | const int* const pointer = a; |
| 1043 | EXPECT_THAT(make_tuple(pointer, 2), Contains(1)); |
| 1044 | EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3)))); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { |
| 1048 | int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 1049 | EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); |
| 1050 | EXPECT_THAT(a, Contains(Contains(5))); |
| 1051 | EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); |
| 1052 | EXPECT_THAT(a, Contains(Not(Contains(5)))); |
| 1053 | } |
| 1054 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1055 | } // namespace |