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