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