shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2007, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file tests some commonly used argument matchers. |
| 35 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 36 | #include "gmock/gmock-matchers.h" |
zhanyong.wan | 320814a | 2013-03-01 00:20:30 +0000 | [diff] [blame] | 37 | #include "gmock/gmock-more-matchers.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 38 | |
| 39 | #include <string.h> |
| 40 | #include <functional> |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 41 | #include <iostream> |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 42 | #include <list> |
| 43 | #include <map> |
| 44 | #include <set> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 45 | #include <sstream> |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 46 | #include <string> |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 47 | #include <utility> |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 48 | #include <vector> |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 49 | #include "gmock/gmock.h" |
| 50 | #include "gtest/gtest.h" |
| 51 | #include "gtest/gtest-spi.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 52 | |
| 53 | namespace testing { |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 54 | |
| 55 | namespace internal { |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 56 | GTEST_API_ string JoinAsTuple(const Strings& fields); |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 57 | } // namespace internal |
| 58 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 59 | namespace gmock_matchers_test { |
| 60 | |
zhanyong.wan | 898725c | 2011-09-16 16:45:39 +0000 | [diff] [blame] | 61 | using std::greater; |
| 62 | using std::less; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 63 | using std::list; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 64 | using std::make_pair; |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 65 | using std::map; |
| 66 | using std::multimap; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 67 | using std::multiset; |
| 68 | using std::ostream; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 69 | using std::pair; |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 70 | using std::set; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 71 | using std::stringstream; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 72 | using std::tr1::get; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 73 | using std::tr1::make_tuple; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 74 | using std::tr1::tuple; |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 75 | using std::vector; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 76 | using testing::A; |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 77 | using testing::AllArgs; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 78 | using testing::AllOf; |
| 79 | using testing::An; |
| 80 | using testing::AnyOf; |
| 81 | using testing::ByRef; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 82 | using testing::ContainsRegex; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 83 | using testing::DoubleEq; |
| 84 | using testing::EndsWith; |
| 85 | using testing::Eq; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 86 | using testing::ExplainMatchResult; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 87 | using testing::Field; |
| 88 | using testing::FloatEq; |
| 89 | using testing::Ge; |
| 90 | using testing::Gt; |
| 91 | using testing::HasSubstr; |
zhanyong.wan | 320814a | 2013-03-01 00:20:30 +0000 | [diff] [blame] | 92 | using testing::IsEmpty; |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 93 | using testing::IsNull; |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 94 | using testing::Key; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 95 | using testing::Le; |
| 96 | using testing::Lt; |
| 97 | using testing::MakeMatcher; |
| 98 | using testing::MakePolymorphicMatcher; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 99 | using testing::MatchResultListener; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 100 | using testing::Matcher; |
| 101 | using testing::MatcherCast; |
| 102 | using testing::MatcherInterface; |
| 103 | using testing::Matches; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 104 | using testing::MatchesRegex; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 105 | using testing::NanSensitiveDoubleEq; |
| 106 | using testing::NanSensitiveFloatEq; |
| 107 | using testing::Ne; |
| 108 | using testing::Not; |
| 109 | using testing::NotNull; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 110 | using testing::Pair; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 111 | using testing::Pointee; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 112 | using testing::Pointwise; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 113 | using testing::PolymorphicMatcher; |
| 114 | using testing::Property; |
| 115 | using testing::Ref; |
| 116 | using testing::ResultOf; |
zhanyong.wan | a31d9ce | 2013-03-01 01:50:17 +0000 | [diff] [blame] | 117 | using testing::SizeIs; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 118 | using testing::StartsWith; |
| 119 | using testing::StrCaseEq; |
| 120 | using testing::StrCaseNe; |
| 121 | using testing::StrEq; |
| 122 | using testing::StrNe; |
| 123 | using testing::Truly; |
| 124 | using testing::TypedEq; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 125 | using testing::Value; |
zhanyong.wan | 898725c | 2011-09-16 16:45:39 +0000 | [diff] [blame] | 126 | using testing::WhenSorted; |
| 127 | using testing::WhenSortedBy; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 128 | using testing::_; |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 129 | using testing::internal::DummyMatchResultListener; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 130 | using testing::internal::ExplainMatchFailureTupleTo; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 131 | using testing::internal::FloatingEqMatcher; |
zhanyong.wan | b414080 | 2010-06-08 22:53:57 +0000 | [diff] [blame] | 132 | using testing::internal::FormatMatcherDescription; |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 133 | using testing::internal::IsReadableTypeName; |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 134 | using testing::internal::JoinAsTuple; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 135 | using testing::internal::RE; |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 136 | using testing::internal::StreamMatchResultListener; |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 137 | using testing::internal::StringMatchResultListener; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 138 | using testing::internal::Strings; |
vladlosev | 79b8350 | 2009-11-18 00:43:37 +0000 | [diff] [blame] | 139 | using testing::internal::linked_ptr; |
vladlosev | e56daa7 | 2009-11-18 01:08:08 +0000 | [diff] [blame] | 140 | using testing::internal::scoped_ptr; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 141 | using testing::internal::string; |
| 142 | |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 143 | // For testing ExplainMatchResultTo(). |
| 144 | class GreaterThanMatcher : public MatcherInterface<int> { |
| 145 | public: |
| 146 | explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 147 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 148 | virtual void DescribeTo(ostream* os) const { |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 149 | *os << "is > " << rhs_; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 150 | } |
| 151 | |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 152 | virtual bool MatchAndExplain(int lhs, |
| 153 | MatchResultListener* listener) const { |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 154 | const int diff = lhs - rhs_; |
| 155 | if (diff > 0) { |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 156 | *listener << "which is " << diff << " more than " << rhs_; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 157 | } else if (diff == 0) { |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 158 | *listener << "which is the same as " << rhs_; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 159 | } else { |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 160 | *listener << "which is " << -diff << " less than " << rhs_; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 161 | } |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 162 | |
| 163 | return lhs > rhs_; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 164 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 165 | |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 166 | private: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 167 | int rhs_; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | Matcher<int> GreaterThan(int n) { |
| 171 | return MakeMatcher(new GreaterThanMatcher(n)); |
| 172 | } |
| 173 | |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 174 | string OfType(const string& type_name) { |
| 175 | #if GTEST_HAS_RTTI |
| 176 | return " (of type " + type_name + ")"; |
| 177 | #else |
| 178 | return ""; |
| 179 | #endif |
| 180 | } |
| 181 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 182 | // Returns the description of the given matcher. |
| 183 | template <typename T> |
| 184 | string Describe(const Matcher<T>& m) { |
| 185 | stringstream ss; |
| 186 | m.DescribeTo(&ss); |
| 187 | return ss.str(); |
| 188 | } |
| 189 | |
| 190 | // Returns the description of the negation of the given matcher. |
| 191 | template <typename T> |
| 192 | string DescribeNegation(const Matcher<T>& m) { |
| 193 | stringstream ss; |
| 194 | m.DescribeNegationTo(&ss); |
| 195 | return ss.str(); |
| 196 | } |
| 197 | |
| 198 | // Returns the reason why x matches, or doesn't match, m. |
| 199 | template <typename MatcherType, typename Value> |
| 200 | string Explain(const MatcherType& m, const Value& x) { |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 201 | StringMatchResultListener listener; |
| 202 | ExplainMatchResult(m, x, &listener); |
| 203 | return listener.str(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 204 | } |
| 205 | |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 206 | TEST(MatchResultListenerTest, StreamingWorks) { |
| 207 | StringMatchResultListener listener; |
| 208 | listener << "hi" << 5; |
| 209 | EXPECT_EQ("hi5", listener.str()); |
| 210 | |
| 211 | // Streaming shouldn't crash when the underlying ostream is NULL. |
| 212 | DummyMatchResultListener dummy; |
| 213 | dummy << "hi" << 5; |
| 214 | } |
| 215 | |
| 216 | TEST(MatchResultListenerTest, CanAccessUnderlyingStream) { |
| 217 | EXPECT_TRUE(DummyMatchResultListener().stream() == NULL); |
| 218 | EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL); |
| 219 | |
| 220 | EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream()); |
| 221 | } |
| 222 | |
| 223 | TEST(MatchResultListenerTest, IsInterestedWorks) { |
| 224 | EXPECT_TRUE(StringMatchResultListener().IsInterested()); |
| 225 | EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested()); |
| 226 | |
| 227 | EXPECT_FALSE(DummyMatchResultListener().IsInterested()); |
| 228 | EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested()); |
| 229 | } |
| 230 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 231 | // Makes sure that the MatcherInterface<T> interface doesn't |
| 232 | // change. |
| 233 | class EvenMatcherImpl : public MatcherInterface<int> { |
| 234 | public: |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 235 | virtual bool MatchAndExplain(int x, |
| 236 | MatchResultListener* /* listener */) const { |
| 237 | return x % 2 == 0; |
| 238 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 239 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 240 | virtual void DescribeTo(ostream* os) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 241 | *os << "is an even number"; |
| 242 | } |
| 243 | |
| 244 | // We deliberately don't define DescribeNegationTo() and |
| 245 | // ExplainMatchResultTo() here, to make sure the definition of these |
| 246 | // two methods is optional. |
| 247 | }; |
| 248 | |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 249 | // Makes sure that the MatcherInterface API doesn't change. |
| 250 | TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 251 | EvenMatcherImpl m; |
| 252 | } |
| 253 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 254 | // Tests implementing a monomorphic matcher using MatchAndExplain(). |
| 255 | |
| 256 | class NewEvenMatcherImpl : public MatcherInterface<int> { |
| 257 | public: |
| 258 | virtual bool MatchAndExplain(int x, MatchResultListener* listener) const { |
| 259 | const bool match = x % 2 == 0; |
| 260 | // Verifies that we can stream to a listener directly. |
| 261 | *listener << "value % " << 2; |
| 262 | if (listener->stream() != NULL) { |
| 263 | // Verifies that we can stream to a listener's underlying stream |
| 264 | // too. |
| 265 | *listener->stream() << " == " << (x % 2); |
| 266 | } |
| 267 | return match; |
| 268 | } |
| 269 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 270 | virtual void DescribeTo(ostream* os) const { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 271 | *os << "is an even number"; |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) { |
| 276 | Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl); |
| 277 | EXPECT_TRUE(m.Matches(2)); |
| 278 | EXPECT_FALSE(m.Matches(3)); |
| 279 | EXPECT_EQ("value % 2 == 0", Explain(m, 2)); |
| 280 | EXPECT_EQ("value % 2 == 1", Explain(m, 3)); |
| 281 | } |
| 282 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 283 | // Tests default-constructing a matcher. |
| 284 | TEST(MatcherTest, CanBeDefaultConstructed) { |
| 285 | Matcher<double> m; |
| 286 | } |
| 287 | |
| 288 | // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*. |
| 289 | TEST(MatcherTest, CanBeConstructedFromMatcherInterface) { |
| 290 | const MatcherInterface<int>* impl = new EvenMatcherImpl; |
| 291 | Matcher<int> m(impl); |
| 292 | EXPECT_TRUE(m.Matches(4)); |
| 293 | EXPECT_FALSE(m.Matches(5)); |
| 294 | } |
| 295 | |
| 296 | // Tests that value can be used in place of Eq(value). |
| 297 | TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) { |
| 298 | Matcher<int> m1 = 5; |
| 299 | EXPECT_TRUE(m1.Matches(5)); |
| 300 | EXPECT_FALSE(m1.Matches(6)); |
| 301 | } |
| 302 | |
| 303 | // Tests that NULL can be used in place of Eq(NULL). |
| 304 | TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) { |
| 305 | Matcher<int*> m1 = NULL; |
| 306 | EXPECT_TRUE(m1.Matches(NULL)); |
| 307 | int n = 0; |
| 308 | EXPECT_FALSE(m1.Matches(&n)); |
| 309 | } |
| 310 | |
| 311 | // Tests that matchers are copyable. |
| 312 | TEST(MatcherTest, IsCopyable) { |
| 313 | // Tests the copy constructor. |
| 314 | Matcher<bool> m1 = Eq(false); |
| 315 | EXPECT_TRUE(m1.Matches(false)); |
| 316 | EXPECT_FALSE(m1.Matches(true)); |
| 317 | |
| 318 | // Tests the assignment operator. |
| 319 | m1 = Eq(true); |
| 320 | EXPECT_TRUE(m1.Matches(true)); |
| 321 | EXPECT_FALSE(m1.Matches(false)); |
| 322 | } |
| 323 | |
| 324 | // Tests that Matcher<T>::DescribeTo() calls |
| 325 | // MatcherInterface<T>::DescribeTo(). |
| 326 | TEST(MatcherTest, CanDescribeItself) { |
| 327 | EXPECT_EQ("is an even number", |
| 328 | Describe(Matcher<int>(new EvenMatcherImpl))); |
| 329 | } |
| 330 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 331 | // Tests Matcher<T>::MatchAndExplain(). |
| 332 | TEST(MatcherTest, MatchAndExplain) { |
| 333 | Matcher<int> m = GreaterThan(0); |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 334 | StringMatchResultListener listener1; |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 335 | EXPECT_TRUE(m.MatchAndExplain(42, &listener1)); |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 336 | EXPECT_EQ("which is 42 more than 0", listener1.str()); |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 337 | |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 338 | StringMatchResultListener listener2; |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 339 | EXPECT_FALSE(m.MatchAndExplain(-9, &listener2)); |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 340 | EXPECT_EQ("which is 9 less than 0", listener2.str()); |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 341 | } |
| 342 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 343 | // Tests that a C-string literal can be implicitly converted to a |
| 344 | // Matcher<string> or Matcher<const string&>. |
| 345 | TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { |
| 346 | Matcher<string> m1 = "hi"; |
| 347 | EXPECT_TRUE(m1.Matches("hi")); |
| 348 | EXPECT_FALSE(m1.Matches("hello")); |
| 349 | |
| 350 | Matcher<const string&> m2 = "hi"; |
| 351 | EXPECT_TRUE(m2.Matches("hi")); |
| 352 | EXPECT_FALSE(m2.Matches("hello")); |
| 353 | } |
| 354 | |
| 355 | // Tests that a string object can be implicitly converted to a |
| 356 | // Matcher<string> or Matcher<const string&>. |
| 357 | TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) { |
| 358 | Matcher<string> m1 = string("hi"); |
| 359 | EXPECT_TRUE(m1.Matches("hi")); |
| 360 | EXPECT_FALSE(m1.Matches("hello")); |
| 361 | |
| 362 | Matcher<const string&> m2 = string("hi"); |
| 363 | EXPECT_TRUE(m2.Matches("hi")); |
| 364 | EXPECT_FALSE(m2.Matches("hello")); |
| 365 | } |
| 366 | |
zhanyong.wan | 1f122a0 | 2013-03-25 16:27:03 +0000 | [diff] [blame^] | 367 | #if GTEST_HAS_STRING_PIECE_ |
| 368 | // Tests that a C-string literal can be implicitly converted to a |
| 369 | // Matcher<StringPiece> or Matcher<const StringPiece&>. |
| 370 | TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { |
| 371 | Matcher<StringPiece> m1 = "cats"; |
| 372 | EXPECT_TRUE(m1.Matches("cats")); |
| 373 | EXPECT_FALSE(m1.Matches("dogs")); |
| 374 | |
| 375 | Matcher<const StringPiece&> m2 = "cats"; |
| 376 | EXPECT_TRUE(m2.Matches("cats")); |
| 377 | EXPECT_FALSE(m2.Matches("dogs")); |
| 378 | } |
| 379 | |
| 380 | // Tests that a string object can be implicitly converted to a |
| 381 | // Matcher<StringPiece> or Matcher<const StringPiece&>. |
| 382 | TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromString) { |
| 383 | Matcher<StringPiece> m1 = string("cats"); |
| 384 | EXPECT_TRUE(m1.Matches("cats")); |
| 385 | EXPECT_FALSE(m1.Matches("dogs")); |
| 386 | |
| 387 | Matcher<const StringPiece&> m2 = string("cats"); |
| 388 | EXPECT_TRUE(m2.Matches("cats")); |
| 389 | EXPECT_FALSE(m2.Matches("dogs")); |
| 390 | } |
| 391 | |
| 392 | // Tests that a StringPiece object can be implicitly converted to a |
| 393 | // Matcher<StringPiece> or Matcher<const StringPiece&>. |
| 394 | TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromStringPiece) { |
| 395 | Matcher<StringPiece> m1 = StringPiece("cats"); |
| 396 | EXPECT_TRUE(m1.Matches("cats")); |
| 397 | EXPECT_FALSE(m1.Matches("dogs")); |
| 398 | |
| 399 | Matcher<const StringPiece&> m2 = StringPiece("cats"); |
| 400 | EXPECT_TRUE(m2.Matches("cats")); |
| 401 | EXPECT_FALSE(m2.Matches("dogs")); |
| 402 | } |
| 403 | #endif // GTEST_HAS_STRING_PIECE_ |
| 404 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 405 | // Tests that MakeMatcher() constructs a Matcher<T> from a |
| 406 | // MatcherInterface* without requiring the user to explicitly |
| 407 | // write the type. |
| 408 | TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { |
| 409 | const MatcherInterface<int>* dummy_impl = NULL; |
| 410 | Matcher<int> m = MakeMatcher(dummy_impl); |
| 411 | } |
| 412 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 413 | // Tests that MakePolymorphicMatcher() can construct a polymorphic |
| 414 | // matcher from its implementation using the old API. |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 415 | const int g_bar = 1; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 416 | class ReferencesBarOrIsZeroImpl { |
| 417 | public: |
| 418 | template <typename T> |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 419 | bool MatchAndExplain(const T& x, |
| 420 | MatchResultListener* /* listener */) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 421 | const void* p = &x; |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 422 | return p == &g_bar || x == 0; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 423 | } |
| 424 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 425 | void DescribeTo(ostream* os) const { *os << "g_bar or zero"; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 426 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 427 | void DescribeNegationTo(ostream* os) const { |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 428 | *os << "doesn't reference g_bar and is not zero"; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 429 | } |
| 430 | }; |
| 431 | |
| 432 | // This function verifies that MakePolymorphicMatcher() returns a |
| 433 | // PolymorphicMatcher<T> where T is the argument's type. |
| 434 | PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { |
| 435 | return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); |
| 436 | } |
| 437 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 438 | TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 439 | // Using a polymorphic matcher to match a reference type. |
| 440 | Matcher<const int&> m1 = ReferencesBarOrIsZero(); |
| 441 | EXPECT_TRUE(m1.Matches(0)); |
| 442 | // Verifies that the identity of a by-reference argument is preserved. |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 443 | EXPECT_TRUE(m1.Matches(g_bar)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 444 | EXPECT_FALSE(m1.Matches(1)); |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 445 | EXPECT_EQ("g_bar or zero", Describe(m1)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 446 | |
| 447 | // Using a polymorphic matcher to match a value type. |
| 448 | Matcher<double> m2 = ReferencesBarOrIsZero(); |
| 449 | EXPECT_TRUE(m2.Matches(0.0)); |
| 450 | EXPECT_FALSE(m2.Matches(0.1)); |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 451 | EXPECT_EQ("g_bar or zero", Describe(m2)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 452 | } |
| 453 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 454 | // Tests implementing a polymorphic matcher using MatchAndExplain(). |
| 455 | |
| 456 | class PolymorphicIsEvenImpl { |
| 457 | public: |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 458 | void DescribeTo(ostream* os) const { *os << "is even"; } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 459 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 460 | void DescribeNegationTo(ostream* os) const { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 461 | *os << "is odd"; |
| 462 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 463 | |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 464 | template <typename T> |
| 465 | bool MatchAndExplain(const T& x, MatchResultListener* listener) const { |
| 466 | // Verifies that we can stream to the listener directly. |
| 467 | *listener << "% " << 2; |
| 468 | if (listener->stream() != NULL) { |
| 469 | // Verifies that we can stream to the listener's underlying stream |
| 470 | // too. |
| 471 | *listener->stream() << " == " << (x % 2); |
| 472 | } |
| 473 | return (x % 2) == 0; |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 474 | } |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 475 | }; |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 476 | |
| 477 | PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() { |
| 478 | return MakePolymorphicMatcher(PolymorphicIsEvenImpl()); |
| 479 | } |
| 480 | |
| 481 | TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) { |
| 482 | // Using PolymorphicIsEven() as a Matcher<int>. |
| 483 | const Matcher<int> m1 = PolymorphicIsEven(); |
| 484 | EXPECT_TRUE(m1.Matches(42)); |
| 485 | EXPECT_FALSE(m1.Matches(43)); |
| 486 | EXPECT_EQ("is even", Describe(m1)); |
| 487 | |
| 488 | const Matcher<int> not_m1 = Not(m1); |
| 489 | EXPECT_EQ("is odd", Describe(not_m1)); |
| 490 | |
| 491 | EXPECT_EQ("% 2 == 0", Explain(m1, 42)); |
| 492 | |
| 493 | // Using PolymorphicIsEven() as a Matcher<char>. |
| 494 | const Matcher<char> m2 = PolymorphicIsEven(); |
| 495 | EXPECT_TRUE(m2.Matches('\x42')); |
| 496 | EXPECT_FALSE(m2.Matches('\x43')); |
| 497 | EXPECT_EQ("is even", Describe(m2)); |
| 498 | |
| 499 | const Matcher<char> not_m2 = Not(m2); |
| 500 | EXPECT_EQ("is odd", Describe(not_m2)); |
| 501 | |
| 502 | EXPECT_EQ("% 2 == 0", Explain(m2, '\x42')); |
| 503 | } |
| 504 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 505 | // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher. |
| 506 | TEST(MatcherCastTest, FromPolymorphicMatcher) { |
| 507 | Matcher<int> m = MatcherCast<int>(Eq(5)); |
| 508 | EXPECT_TRUE(m.Matches(5)); |
| 509 | EXPECT_FALSE(m.Matches(6)); |
| 510 | } |
| 511 | |
| 512 | // For testing casting matchers between compatible types. |
| 513 | class IntValue { |
| 514 | public: |
| 515 | // An int can be statically (although not implicitly) cast to a |
| 516 | // IntValue. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 517 | explicit IntValue(int a_value) : value_(a_value) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 518 | |
| 519 | int value() const { return value_; } |
| 520 | private: |
| 521 | int value_; |
| 522 | }; |
| 523 | |
| 524 | // For testing casting matchers between compatible types. |
| 525 | bool IsPositiveIntValue(const IntValue& foo) { |
| 526 | return foo.value() > 0; |
| 527 | } |
| 528 | |
| 529 | // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T |
| 530 | // can be statically converted to U. |
| 531 | TEST(MatcherCastTest, FromCompatibleType) { |
| 532 | Matcher<double> m1 = Eq(2.0); |
| 533 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 534 | EXPECT_TRUE(m2.Matches(2)); |
| 535 | EXPECT_FALSE(m2.Matches(3)); |
| 536 | |
| 537 | Matcher<IntValue> m3 = Truly(IsPositiveIntValue); |
| 538 | Matcher<int> m4 = MatcherCast<int>(m3); |
| 539 | // In the following, the arguments 1 and 0 are statically converted |
| 540 | // to IntValue objects, and then tested by the IsPositiveIntValue() |
| 541 | // predicate. |
| 542 | EXPECT_TRUE(m4.Matches(1)); |
| 543 | EXPECT_FALSE(m4.Matches(0)); |
| 544 | } |
| 545 | |
| 546 | // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>. |
| 547 | TEST(MatcherCastTest, FromConstReferenceToNonReference) { |
| 548 | Matcher<const int&> m1 = Eq(0); |
| 549 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 550 | EXPECT_TRUE(m2.Matches(0)); |
| 551 | EXPECT_FALSE(m2.Matches(1)); |
| 552 | } |
| 553 | |
| 554 | // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>. |
| 555 | TEST(MatcherCastTest, FromReferenceToNonReference) { |
| 556 | Matcher<int&> m1 = Eq(0); |
| 557 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 558 | EXPECT_TRUE(m2.Matches(0)); |
| 559 | EXPECT_FALSE(m2.Matches(1)); |
| 560 | } |
| 561 | |
| 562 | // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. |
| 563 | TEST(MatcherCastTest, FromNonReferenceToConstReference) { |
| 564 | Matcher<int> m1 = Eq(0); |
| 565 | Matcher<const int&> m2 = MatcherCast<const int&>(m1); |
| 566 | EXPECT_TRUE(m2.Matches(0)); |
| 567 | EXPECT_FALSE(m2.Matches(1)); |
| 568 | } |
| 569 | |
| 570 | // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>. |
| 571 | TEST(MatcherCastTest, FromNonReferenceToReference) { |
| 572 | Matcher<int> m1 = Eq(0); |
| 573 | Matcher<int&> m2 = MatcherCast<int&>(m1); |
| 574 | int n = 0; |
| 575 | EXPECT_TRUE(m2.Matches(n)); |
| 576 | n = 1; |
| 577 | EXPECT_FALSE(m2.Matches(n)); |
| 578 | } |
| 579 | |
| 580 | // Tests that MatcherCast<T>(m) works when m is a Matcher<T>. |
| 581 | TEST(MatcherCastTest, FromSameType) { |
| 582 | Matcher<int> m1 = Eq(0); |
| 583 | Matcher<int> m2 = MatcherCast<int>(m1); |
| 584 | EXPECT_TRUE(m2.Matches(0)); |
| 585 | EXPECT_FALSE(m2.Matches(1)); |
| 586 | } |
| 587 | |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 588 | // Implicitly convertible form any type. |
| 589 | struct ConvertibleFromAny { |
| 590 | ConvertibleFromAny(int a_value) : value(a_value) {} |
| 591 | template <typename T> |
| 592 | ConvertibleFromAny(const T& a_value) : value(-1) { |
| 593 | ADD_FAILURE() << "Conversion constructor called"; |
| 594 | } |
| 595 | int value; |
| 596 | }; |
| 597 | |
| 598 | bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) { |
| 599 | return a.value == b.value; |
| 600 | } |
| 601 | |
| 602 | ostream& operator<<(ostream& os, const ConvertibleFromAny& a) { |
| 603 | return os << a.value; |
| 604 | } |
| 605 | |
| 606 | TEST(MatcherCastTest, ConversionConstructorIsUsed) { |
| 607 | Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1); |
| 608 | EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 609 | EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 610 | } |
| 611 | |
| 612 | TEST(MatcherCastTest, FromConvertibleFromAny) { |
| 613 | Matcher<ConvertibleFromAny> m = |
| 614 | MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1))); |
| 615 | EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 616 | EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 617 | } |
| 618 | |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 619 | class Base {}; |
| 620 | class Derived : public Base {}; |
| 621 | |
| 622 | // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher. |
| 623 | TEST(SafeMatcherCastTest, FromPolymorphicMatcher) { |
| 624 | Matcher<char> m2 = SafeMatcherCast<char>(Eq(32)); |
| 625 | EXPECT_TRUE(m2.Matches(' ')); |
| 626 | EXPECT_FALSE(m2.Matches('\n')); |
| 627 | } |
| 628 | |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 629 | // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where |
| 630 | // T and U are arithmetic types and T can be losslessly converted to |
| 631 | // U. |
| 632 | TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) { |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 633 | Matcher<double> m1 = DoubleEq(1.0); |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 634 | Matcher<float> m2 = SafeMatcherCast<float>(m1); |
| 635 | EXPECT_TRUE(m2.Matches(1.0f)); |
| 636 | EXPECT_FALSE(m2.Matches(2.0f)); |
| 637 | |
| 638 | Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a')); |
| 639 | EXPECT_TRUE(m3.Matches('a')); |
| 640 | EXPECT_FALSE(m3.Matches('b')); |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U |
| 644 | // are pointers or references to a derived and a base class, correspondingly. |
| 645 | TEST(SafeMatcherCastTest, FromBaseClass) { |
| 646 | Derived d, d2; |
| 647 | Matcher<Base*> m1 = Eq(&d); |
| 648 | Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1); |
| 649 | EXPECT_TRUE(m2.Matches(&d)); |
| 650 | EXPECT_FALSE(m2.Matches(&d2)); |
| 651 | |
| 652 | Matcher<Base&> m3 = Ref(d); |
| 653 | Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3); |
| 654 | EXPECT_TRUE(m4.Matches(d)); |
| 655 | EXPECT_FALSE(m4.Matches(d2)); |
| 656 | } |
| 657 | |
| 658 | // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>. |
| 659 | TEST(SafeMatcherCastTest, FromConstReferenceToReference) { |
| 660 | int n = 0; |
| 661 | Matcher<const int&> m1 = Ref(n); |
| 662 | Matcher<int&> m2 = SafeMatcherCast<int&>(m1); |
| 663 | int n1 = 0; |
| 664 | EXPECT_TRUE(m2.Matches(n)); |
| 665 | EXPECT_FALSE(m2.Matches(n1)); |
| 666 | } |
| 667 | |
| 668 | // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. |
| 669 | TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) { |
| 670 | Matcher<int> m1 = Eq(0); |
| 671 | Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1); |
| 672 | EXPECT_TRUE(m2.Matches(0)); |
| 673 | EXPECT_FALSE(m2.Matches(1)); |
| 674 | } |
| 675 | |
| 676 | // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>. |
| 677 | TEST(SafeMatcherCastTest, FromNonReferenceToReference) { |
| 678 | Matcher<int> m1 = Eq(0); |
| 679 | Matcher<int&> m2 = SafeMatcherCast<int&>(m1); |
| 680 | int n = 0; |
| 681 | EXPECT_TRUE(m2.Matches(n)); |
| 682 | n = 1; |
| 683 | EXPECT_FALSE(m2.Matches(n)); |
| 684 | } |
| 685 | |
| 686 | // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>. |
| 687 | TEST(SafeMatcherCastTest, FromSameType) { |
| 688 | Matcher<int> m1 = Eq(0); |
| 689 | Matcher<int> m2 = SafeMatcherCast<int>(m1); |
| 690 | EXPECT_TRUE(m2.Matches(0)); |
| 691 | EXPECT_FALSE(m2.Matches(1)); |
| 692 | } |
| 693 | |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 694 | TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) { |
| 695 | Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1); |
| 696 | EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 697 | EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 698 | } |
| 699 | |
| 700 | TEST(SafeMatcherCastTest, FromConvertibleFromAny) { |
| 701 | Matcher<ConvertibleFromAny> m = |
| 702 | SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1))); |
| 703 | EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); |
| 704 | EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); |
| 705 | } |
| 706 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 707 | // Tests that A<T>() matches any value of type T. |
| 708 | TEST(ATest, MatchesAnyValue) { |
| 709 | // Tests a matcher for a value type. |
| 710 | Matcher<double> m1 = A<double>(); |
| 711 | EXPECT_TRUE(m1.Matches(91.43)); |
| 712 | EXPECT_TRUE(m1.Matches(-15.32)); |
| 713 | |
| 714 | // Tests a matcher for a reference type. |
| 715 | int a = 2; |
| 716 | int b = -6; |
| 717 | Matcher<int&> m2 = A<int&>(); |
| 718 | EXPECT_TRUE(m2.Matches(a)); |
| 719 | EXPECT_TRUE(m2.Matches(b)); |
| 720 | } |
| 721 | |
| 722 | // Tests that A<T>() describes itself properly. |
| 723 | TEST(ATest, CanDescribeSelf) { |
| 724 | EXPECT_EQ("is anything", Describe(A<bool>())); |
| 725 | } |
| 726 | |
| 727 | // Tests that An<T>() matches any value of type T. |
| 728 | TEST(AnTest, MatchesAnyValue) { |
| 729 | // Tests a matcher for a value type. |
| 730 | Matcher<int> m1 = An<int>(); |
| 731 | EXPECT_TRUE(m1.Matches(9143)); |
| 732 | EXPECT_TRUE(m1.Matches(-1532)); |
| 733 | |
| 734 | // Tests a matcher for a reference type. |
| 735 | int a = 2; |
| 736 | int b = -6; |
| 737 | Matcher<int&> m2 = An<int&>(); |
| 738 | EXPECT_TRUE(m2.Matches(a)); |
| 739 | EXPECT_TRUE(m2.Matches(b)); |
| 740 | } |
| 741 | |
| 742 | // Tests that An<T>() describes itself properly. |
| 743 | TEST(AnTest, CanDescribeSelf) { |
| 744 | EXPECT_EQ("is anything", Describe(An<int>())); |
| 745 | } |
| 746 | |
| 747 | // Tests that _ can be used as a matcher for any type and matches any |
| 748 | // value of that type. |
| 749 | TEST(UnderscoreTest, MatchesAnyValue) { |
| 750 | // Uses _ as a matcher for a value type. |
| 751 | Matcher<int> m1 = _; |
| 752 | EXPECT_TRUE(m1.Matches(123)); |
| 753 | EXPECT_TRUE(m1.Matches(-242)); |
| 754 | |
| 755 | // Uses _ as a matcher for a reference type. |
| 756 | bool a = false; |
| 757 | const bool b = true; |
| 758 | Matcher<const bool&> m2 = _; |
| 759 | EXPECT_TRUE(m2.Matches(a)); |
| 760 | EXPECT_TRUE(m2.Matches(b)); |
| 761 | } |
| 762 | |
| 763 | // Tests that _ describes itself properly. |
| 764 | TEST(UnderscoreTest, CanDescribeSelf) { |
| 765 | Matcher<int> m = _; |
| 766 | EXPECT_EQ("is anything", Describe(m)); |
| 767 | } |
| 768 | |
| 769 | // Tests that Eq(x) matches any value equal to x. |
| 770 | TEST(EqTest, MatchesEqualValue) { |
| 771 | // 2 C-strings with same content but different addresses. |
| 772 | const char a1[] = "hi"; |
| 773 | const char a2[] = "hi"; |
| 774 | |
| 775 | Matcher<const char*> m1 = Eq(a1); |
| 776 | EXPECT_TRUE(m1.Matches(a1)); |
| 777 | EXPECT_FALSE(m1.Matches(a2)); |
| 778 | } |
| 779 | |
| 780 | // Tests that Eq(v) describes itself properly. |
| 781 | |
| 782 | class Unprintable { |
| 783 | public: |
| 784 | Unprintable() : c_('a') {} |
| 785 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 786 | bool operator==(const Unprintable& /* rhs */) { return true; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 787 | private: |
| 788 | char c_; |
| 789 | }; |
| 790 | |
| 791 | TEST(EqTest, CanDescribeSelf) { |
| 792 | Matcher<Unprintable> m = Eq(Unprintable()); |
| 793 | EXPECT_EQ("is equal to 1-byte object <61>", Describe(m)); |
| 794 | } |
| 795 | |
| 796 | // Tests that Eq(v) can be used to match any type that supports |
| 797 | // comparing with type T, where T is v's type. |
| 798 | TEST(EqTest, IsPolymorphic) { |
| 799 | Matcher<int> m1 = Eq(1); |
| 800 | EXPECT_TRUE(m1.Matches(1)); |
| 801 | EXPECT_FALSE(m1.Matches(2)); |
| 802 | |
| 803 | Matcher<char> m2 = Eq(1); |
| 804 | EXPECT_TRUE(m2.Matches('\1')); |
| 805 | EXPECT_FALSE(m2.Matches('a')); |
| 806 | } |
| 807 | |
| 808 | // Tests that TypedEq<T>(v) matches values of type T that's equal to v. |
| 809 | TEST(TypedEqTest, ChecksEqualityForGivenType) { |
| 810 | Matcher<char> m1 = TypedEq<char>('a'); |
| 811 | EXPECT_TRUE(m1.Matches('a')); |
| 812 | EXPECT_FALSE(m1.Matches('b')); |
| 813 | |
| 814 | Matcher<int> m2 = TypedEq<int>(6); |
| 815 | EXPECT_TRUE(m2.Matches(6)); |
| 816 | EXPECT_FALSE(m2.Matches(7)); |
| 817 | } |
| 818 | |
| 819 | // Tests that TypedEq(v) describes itself properly. |
| 820 | TEST(TypedEqTest, CanDescribeSelf) { |
| 821 | EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2))); |
| 822 | } |
| 823 | |
| 824 | // Tests that TypedEq<T>(v) has type Matcher<T>. |
| 825 | |
| 826 | // Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T |
| 827 | // is a "bare" type (i.e. not in the form of const U or U&). If v's |
| 828 | // type is not T, the compiler will generate a message about |
| 829 | // "undefined referece". |
| 830 | template <typename T> |
| 831 | struct Type { |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 832 | static bool IsTypeOf(const T& /* v */) { return true; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 833 | |
| 834 | template <typename T2> |
| 835 | static void IsTypeOf(T2 v); |
| 836 | }; |
| 837 | |
| 838 | TEST(TypedEqTest, HasSpecifiedType) { |
| 839 | // Verfies that the type of TypedEq<T>(v) is Matcher<T>. |
| 840 | Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5)); |
| 841 | Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5)); |
| 842 | } |
| 843 | |
| 844 | // Tests that Ge(v) matches anything >= v. |
| 845 | TEST(GeTest, ImplementsGreaterThanOrEqual) { |
| 846 | Matcher<int> m1 = Ge(0); |
| 847 | EXPECT_TRUE(m1.Matches(1)); |
| 848 | EXPECT_TRUE(m1.Matches(0)); |
| 849 | EXPECT_FALSE(m1.Matches(-1)); |
| 850 | } |
| 851 | |
| 852 | // Tests that Ge(v) describes itself properly. |
| 853 | TEST(GeTest, CanDescribeSelf) { |
| 854 | Matcher<int> m = Ge(5); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 855 | EXPECT_EQ("is >= 5", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | // Tests that Gt(v) matches anything > v. |
| 859 | TEST(GtTest, ImplementsGreaterThan) { |
| 860 | Matcher<double> m1 = Gt(0); |
| 861 | EXPECT_TRUE(m1.Matches(1.0)); |
| 862 | EXPECT_FALSE(m1.Matches(0.0)); |
| 863 | EXPECT_FALSE(m1.Matches(-1.0)); |
| 864 | } |
| 865 | |
| 866 | // Tests that Gt(v) describes itself properly. |
| 867 | TEST(GtTest, CanDescribeSelf) { |
| 868 | Matcher<int> m = Gt(5); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 869 | EXPECT_EQ("is > 5", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | // Tests that Le(v) matches anything <= v. |
| 873 | TEST(LeTest, ImplementsLessThanOrEqual) { |
| 874 | Matcher<char> m1 = Le('b'); |
| 875 | EXPECT_TRUE(m1.Matches('a')); |
| 876 | EXPECT_TRUE(m1.Matches('b')); |
| 877 | EXPECT_FALSE(m1.Matches('c')); |
| 878 | } |
| 879 | |
| 880 | // Tests that Le(v) describes itself properly. |
| 881 | TEST(LeTest, CanDescribeSelf) { |
| 882 | Matcher<int> m = Le(5); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 883 | EXPECT_EQ("is <= 5", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | // Tests that Lt(v) matches anything < v. |
| 887 | TEST(LtTest, ImplementsLessThan) { |
| 888 | Matcher<const string&> m1 = Lt("Hello"); |
| 889 | EXPECT_TRUE(m1.Matches("Abc")); |
| 890 | EXPECT_FALSE(m1.Matches("Hello")); |
| 891 | EXPECT_FALSE(m1.Matches("Hello, world!")); |
| 892 | } |
| 893 | |
| 894 | // Tests that Lt(v) describes itself properly. |
| 895 | TEST(LtTest, CanDescribeSelf) { |
| 896 | Matcher<int> m = Lt(5); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 897 | EXPECT_EQ("is < 5", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | // Tests that Ne(v) matches anything != v. |
| 901 | TEST(NeTest, ImplementsNotEqual) { |
| 902 | Matcher<int> m1 = Ne(0); |
| 903 | EXPECT_TRUE(m1.Matches(1)); |
| 904 | EXPECT_TRUE(m1.Matches(-1)); |
| 905 | EXPECT_FALSE(m1.Matches(0)); |
| 906 | } |
| 907 | |
| 908 | // Tests that Ne(v) describes itself properly. |
| 909 | TEST(NeTest, CanDescribeSelf) { |
| 910 | Matcher<int> m = Ne(5); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 911 | EXPECT_EQ("isn't equal to 5", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 912 | } |
| 913 | |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 914 | // Tests that IsNull() matches any NULL pointer of any type. |
| 915 | TEST(IsNullTest, MatchesNullPointer) { |
| 916 | Matcher<int*> m1 = IsNull(); |
| 917 | int* p1 = NULL; |
| 918 | int n = 0; |
| 919 | EXPECT_TRUE(m1.Matches(p1)); |
| 920 | EXPECT_FALSE(m1.Matches(&n)); |
| 921 | |
| 922 | Matcher<const char*> m2 = IsNull(); |
| 923 | const char* p2 = NULL; |
| 924 | EXPECT_TRUE(m2.Matches(p2)); |
| 925 | EXPECT_FALSE(m2.Matches("hi")); |
| 926 | |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 927 | #if !GTEST_OS_SYMBIAN |
| 928 | // Nokia's Symbian compiler generates: |
| 929 | // gmock-matchers.h: ambiguous access to overloaded function |
| 930 | // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)' |
| 931 | // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing:: |
| 932 | // MatcherInterface<void *> *)' |
| 933 | // gmock-matchers.h: (point of instantiation: 'testing:: |
| 934 | // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()') |
| 935 | // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 936 | Matcher<void*> m3 = IsNull(); |
| 937 | void* p3 = NULL; |
| 938 | EXPECT_TRUE(m3.Matches(p3)); |
| 939 | EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef))); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 940 | #endif |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 941 | } |
| 942 | |
vladlosev | 79b8350 | 2009-11-18 00:43:37 +0000 | [diff] [blame] | 943 | TEST(IsNullTest, LinkedPtr) { |
| 944 | const Matcher<linked_ptr<int> > m = IsNull(); |
| 945 | const linked_ptr<int> null_p; |
| 946 | const linked_ptr<int> non_null_p(new int); |
| 947 | |
| 948 | EXPECT_TRUE(m.Matches(null_p)); |
| 949 | EXPECT_FALSE(m.Matches(non_null_p)); |
| 950 | } |
| 951 | |
| 952 | TEST(IsNullTest, ReferenceToConstLinkedPtr) { |
| 953 | const Matcher<const linked_ptr<double>&> m = IsNull(); |
| 954 | const linked_ptr<double> null_p; |
| 955 | const linked_ptr<double> non_null_p(new double); |
| 956 | |
| 957 | EXPECT_TRUE(m.Matches(null_p)); |
| 958 | EXPECT_FALSE(m.Matches(non_null_p)); |
| 959 | } |
| 960 | |
vladlosev | e56daa7 | 2009-11-18 01:08:08 +0000 | [diff] [blame] | 961 | TEST(IsNullTest, ReferenceToConstScopedPtr) { |
| 962 | const Matcher<const scoped_ptr<double>&> m = IsNull(); |
| 963 | const scoped_ptr<double> null_p; |
| 964 | const scoped_ptr<double> non_null_p(new double); |
| 965 | |
| 966 | EXPECT_TRUE(m.Matches(null_p)); |
| 967 | EXPECT_FALSE(m.Matches(non_null_p)); |
| 968 | } |
| 969 | |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 970 | // Tests that IsNull() describes itself properly. |
| 971 | TEST(IsNullTest, CanDescribeSelf) { |
| 972 | Matcher<int*> m = IsNull(); |
| 973 | EXPECT_EQ("is NULL", Describe(m)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 974 | EXPECT_EQ("isn't NULL", DescribeNegation(m)); |
zhanyong.wan | 2d970ee | 2009-09-24 21:41:36 +0000 | [diff] [blame] | 975 | } |
| 976 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 977 | // Tests that NotNull() matches any non-NULL pointer of any type. |
| 978 | TEST(NotNullTest, MatchesNonNullPointer) { |
| 979 | Matcher<int*> m1 = NotNull(); |
| 980 | int* p1 = NULL; |
| 981 | int n = 0; |
| 982 | EXPECT_FALSE(m1.Matches(p1)); |
| 983 | EXPECT_TRUE(m1.Matches(&n)); |
| 984 | |
| 985 | Matcher<const char*> m2 = NotNull(); |
| 986 | const char* p2 = NULL; |
| 987 | EXPECT_FALSE(m2.Matches(p2)); |
| 988 | EXPECT_TRUE(m2.Matches("hi")); |
| 989 | } |
| 990 | |
vladlosev | 79b8350 | 2009-11-18 00:43:37 +0000 | [diff] [blame] | 991 | TEST(NotNullTest, LinkedPtr) { |
| 992 | const Matcher<linked_ptr<int> > m = NotNull(); |
| 993 | const linked_ptr<int> null_p; |
| 994 | const linked_ptr<int> non_null_p(new int); |
| 995 | |
| 996 | EXPECT_FALSE(m.Matches(null_p)); |
| 997 | EXPECT_TRUE(m.Matches(non_null_p)); |
| 998 | } |
| 999 | |
| 1000 | TEST(NotNullTest, ReferenceToConstLinkedPtr) { |
| 1001 | const Matcher<const linked_ptr<double>&> m = NotNull(); |
| 1002 | const linked_ptr<double> null_p; |
| 1003 | const linked_ptr<double> non_null_p(new double); |
| 1004 | |
| 1005 | EXPECT_FALSE(m.Matches(null_p)); |
| 1006 | EXPECT_TRUE(m.Matches(non_null_p)); |
| 1007 | } |
| 1008 | |
vladlosev | e56daa7 | 2009-11-18 01:08:08 +0000 | [diff] [blame] | 1009 | TEST(NotNullTest, ReferenceToConstScopedPtr) { |
| 1010 | const Matcher<const scoped_ptr<double>&> m = NotNull(); |
| 1011 | const scoped_ptr<double> null_p; |
| 1012 | const scoped_ptr<double> non_null_p(new double); |
| 1013 | |
| 1014 | EXPECT_FALSE(m.Matches(null_p)); |
| 1015 | EXPECT_TRUE(m.Matches(non_null_p)); |
| 1016 | } |
| 1017 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1018 | // Tests that NotNull() describes itself properly. |
| 1019 | TEST(NotNullTest, CanDescribeSelf) { |
| 1020 | Matcher<int*> m = NotNull(); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1021 | EXPECT_EQ("isn't NULL", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | // Tests that Ref(variable) matches an argument that references |
| 1025 | // 'variable'. |
| 1026 | TEST(RefTest, MatchesSameVariable) { |
| 1027 | int a = 0; |
| 1028 | int b = 0; |
| 1029 | Matcher<int&> m = Ref(a); |
| 1030 | EXPECT_TRUE(m.Matches(a)); |
| 1031 | EXPECT_FALSE(m.Matches(b)); |
| 1032 | } |
| 1033 | |
| 1034 | // Tests that Ref(variable) describes itself properly. |
| 1035 | TEST(RefTest, CanDescribeSelf) { |
| 1036 | int n = 5; |
| 1037 | Matcher<int&> m = Ref(n); |
| 1038 | stringstream ss; |
| 1039 | ss << "references the variable @" << &n << " 5"; |
| 1040 | EXPECT_EQ(string(ss.str()), Describe(m)); |
| 1041 | } |
| 1042 | |
| 1043 | // Test that Ref(non_const_varialbe) can be used as a matcher for a |
| 1044 | // const reference. |
| 1045 | TEST(RefTest, CanBeUsedAsMatcherForConstReference) { |
| 1046 | int a = 0; |
| 1047 | int b = 0; |
| 1048 | Matcher<const int&> m = Ref(a); |
| 1049 | EXPECT_TRUE(m.Matches(a)); |
| 1050 | EXPECT_FALSE(m.Matches(b)); |
| 1051 | } |
| 1052 | |
| 1053 | // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be |
| 1054 | // used wherever Ref(base) can be used (Ref(derived) is a sub-type |
| 1055 | // of Ref(base), but not vice versa. |
| 1056 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1057 | TEST(RefTest, IsCovariant) { |
| 1058 | Base base, base2; |
| 1059 | Derived derived; |
| 1060 | Matcher<const Base&> m1 = Ref(base); |
| 1061 | EXPECT_TRUE(m1.Matches(base)); |
| 1062 | EXPECT_FALSE(m1.Matches(base2)); |
| 1063 | EXPECT_FALSE(m1.Matches(derived)); |
| 1064 | |
| 1065 | m1 = Ref(derived); |
| 1066 | EXPECT_TRUE(m1.Matches(derived)); |
| 1067 | EXPECT_FALSE(m1.Matches(base)); |
| 1068 | EXPECT_FALSE(m1.Matches(base2)); |
| 1069 | } |
| 1070 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1071 | TEST(RefTest, ExplainsResult) { |
| 1072 | int n = 0; |
| 1073 | EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n), |
| 1074 | StartsWith("which is located @")); |
| 1075 | |
| 1076 | int m = 0; |
| 1077 | EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m), |
| 1078 | StartsWith("which is located @")); |
| 1079 | } |
| 1080 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1081 | // Tests string comparison matchers. |
| 1082 | |
| 1083 | TEST(StrEqTest, MatchesEqualString) { |
| 1084 | Matcher<const char*> m = StrEq(string("Hello")); |
| 1085 | EXPECT_TRUE(m.Matches("Hello")); |
| 1086 | EXPECT_FALSE(m.Matches("hello")); |
| 1087 | EXPECT_FALSE(m.Matches(NULL)); |
| 1088 | |
| 1089 | Matcher<const string&> m2 = StrEq("Hello"); |
| 1090 | EXPECT_TRUE(m2.Matches("Hello")); |
| 1091 | EXPECT_FALSE(m2.Matches("Hi")); |
| 1092 | } |
| 1093 | |
| 1094 | TEST(StrEqTest, CanDescribeSelf) { |
vladlosev | aa43220 | 2011-04-01 21:58:42 +0000 | [diff] [blame] | 1095 | Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3"); |
| 1096 | EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1097 | Describe(m)); |
| 1098 | |
| 1099 | string str("01204500800"); |
| 1100 | str[3] = '\0'; |
| 1101 | Matcher<string> m2 = StrEq(str); |
| 1102 | EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2)); |
| 1103 | str[0] = str[6] = str[7] = str[9] = str[10] = '\0'; |
| 1104 | Matcher<string> m3 = StrEq(str); |
| 1105 | EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3)); |
| 1106 | } |
| 1107 | |
| 1108 | TEST(StrNeTest, MatchesUnequalString) { |
| 1109 | Matcher<const char*> m = StrNe("Hello"); |
| 1110 | EXPECT_TRUE(m.Matches("")); |
| 1111 | EXPECT_TRUE(m.Matches(NULL)); |
| 1112 | EXPECT_FALSE(m.Matches("Hello")); |
| 1113 | |
| 1114 | Matcher<string> m2 = StrNe(string("Hello")); |
| 1115 | EXPECT_TRUE(m2.Matches("hello")); |
| 1116 | EXPECT_FALSE(m2.Matches("Hello")); |
| 1117 | } |
| 1118 | |
| 1119 | TEST(StrNeTest, CanDescribeSelf) { |
| 1120 | Matcher<const char*> m = StrNe("Hi"); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1121 | EXPECT_EQ("isn't equal to \"Hi\"", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 1125 | Matcher<const char*> m = StrCaseEq(string("Hello")); |
| 1126 | EXPECT_TRUE(m.Matches("Hello")); |
| 1127 | EXPECT_TRUE(m.Matches("hello")); |
| 1128 | EXPECT_FALSE(m.Matches("Hi")); |
| 1129 | EXPECT_FALSE(m.Matches(NULL)); |
| 1130 | |
| 1131 | Matcher<const string&> m2 = StrCaseEq("Hello"); |
| 1132 | EXPECT_TRUE(m2.Matches("hello")); |
| 1133 | EXPECT_FALSE(m2.Matches("Hi")); |
| 1134 | } |
| 1135 | |
| 1136 | TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 1137 | string str1("oabocdooeoo"); |
| 1138 | string str2("OABOCDOOEOO"); |
| 1139 | Matcher<const string&> m0 = StrCaseEq(str1); |
| 1140 | EXPECT_FALSE(m0.Matches(str2 + string(1, '\0'))); |
| 1141 | |
| 1142 | str1[3] = str2[3] = '\0'; |
| 1143 | Matcher<const string&> m1 = StrCaseEq(str1); |
| 1144 | EXPECT_TRUE(m1.Matches(str2)); |
| 1145 | |
| 1146 | str1[0] = str1[6] = str1[7] = str1[10] = '\0'; |
| 1147 | str2[0] = str2[6] = str2[7] = str2[10] = '\0'; |
| 1148 | Matcher<const string&> m2 = StrCaseEq(str1); |
| 1149 | str1[9] = str2[9] = '\0'; |
| 1150 | EXPECT_FALSE(m2.Matches(str2)); |
| 1151 | |
| 1152 | Matcher<const string&> m3 = StrCaseEq(str1); |
| 1153 | EXPECT_TRUE(m3.Matches(str2)); |
| 1154 | |
| 1155 | EXPECT_FALSE(m3.Matches(str2 + "x")); |
| 1156 | str2.append(1, '\0'); |
| 1157 | EXPECT_FALSE(m3.Matches(str2)); |
| 1158 | EXPECT_FALSE(m3.Matches(string(str2, 0, 9))); |
| 1159 | } |
| 1160 | |
| 1161 | TEST(StrCaseEqTest, CanDescribeSelf) { |
| 1162 | Matcher<string> m = StrCaseEq("Hi"); |
| 1163 | EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m)); |
| 1164 | } |
| 1165 | |
| 1166 | TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 1167 | Matcher<const char*> m = StrCaseNe("Hello"); |
| 1168 | EXPECT_TRUE(m.Matches("Hi")); |
| 1169 | EXPECT_TRUE(m.Matches(NULL)); |
| 1170 | EXPECT_FALSE(m.Matches("Hello")); |
| 1171 | EXPECT_FALSE(m.Matches("hello")); |
| 1172 | |
| 1173 | Matcher<string> m2 = StrCaseNe(string("Hello")); |
| 1174 | EXPECT_TRUE(m2.Matches("")); |
| 1175 | EXPECT_FALSE(m2.Matches("Hello")); |
| 1176 | } |
| 1177 | |
| 1178 | TEST(StrCaseNeTest, CanDescribeSelf) { |
| 1179 | Matcher<const char*> m = StrCaseNe("Hi"); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1180 | EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | // Tests that HasSubstr() works for matching string-typed values. |
| 1184 | TEST(HasSubstrTest, WorksForStringClasses) { |
| 1185 | const Matcher<string> m1 = HasSubstr("foo"); |
| 1186 | EXPECT_TRUE(m1.Matches(string("I love food."))); |
| 1187 | EXPECT_FALSE(m1.Matches(string("tofo"))); |
| 1188 | |
| 1189 | const Matcher<const std::string&> m2 = HasSubstr("foo"); |
| 1190 | EXPECT_TRUE(m2.Matches(std::string("I love food."))); |
| 1191 | EXPECT_FALSE(m2.Matches(std::string("tofo"))); |
| 1192 | } |
| 1193 | |
| 1194 | // Tests that HasSubstr() works for matching C-string-typed values. |
| 1195 | TEST(HasSubstrTest, WorksForCStrings) { |
| 1196 | const Matcher<char*> m1 = HasSubstr("foo"); |
| 1197 | EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food."))); |
| 1198 | EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo"))); |
| 1199 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1200 | |
| 1201 | const Matcher<const char*> m2 = HasSubstr("foo"); |
| 1202 | EXPECT_TRUE(m2.Matches("I love food.")); |
| 1203 | EXPECT_FALSE(m2.Matches("tofo")); |
| 1204 | EXPECT_FALSE(m2.Matches(NULL)); |
| 1205 | } |
| 1206 | |
| 1207 | // Tests that HasSubstr(s) describes itself properly. |
| 1208 | TEST(HasSubstrTest, CanDescribeSelf) { |
| 1209 | Matcher<string> m = HasSubstr("foo\n\""); |
| 1210 | EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); |
| 1211 | } |
| 1212 | |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1213 | TEST(KeyTest, CanDescribeSelf) { |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1214 | Matcher<const pair<std::string, int>&> m = Key("foo"); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1215 | EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1216 | EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m)); |
| 1217 | } |
| 1218 | |
| 1219 | TEST(KeyTest, ExplainsResult) { |
| 1220 | Matcher<pair<int, bool> > m = Key(GreaterThan(10)); |
| 1221 | EXPECT_EQ("whose first field is a value which is 5 less than 10", |
| 1222 | Explain(m, make_pair(5, true))); |
| 1223 | EXPECT_EQ("whose first field is a value which is 5 more than 10", |
| 1224 | Explain(m, make_pair(15, true))); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | TEST(KeyTest, MatchesCorrectly) { |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1228 | pair<int, std::string> p(25, "foo"); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1229 | EXPECT_THAT(p, Key(25)); |
| 1230 | EXPECT_THAT(p, Not(Key(42))); |
| 1231 | EXPECT_THAT(p, Key(Ge(20))); |
| 1232 | EXPECT_THAT(p, Not(Key(Lt(25)))); |
| 1233 | } |
| 1234 | |
| 1235 | TEST(KeyTest, SafelyCastsInnerMatcher) { |
| 1236 | Matcher<int> is_positive = Gt(0); |
| 1237 | Matcher<int> is_negative = Lt(0); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1238 | pair<char, bool> p('a', true); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1239 | EXPECT_THAT(p, Key(is_positive)); |
| 1240 | EXPECT_THAT(p, Not(Key(is_negative))); |
| 1241 | } |
| 1242 | |
| 1243 | TEST(KeyTest, InsideContainsUsingMap) { |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1244 | map<int, char> container; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1245 | container.insert(make_pair(1, 'a')); |
| 1246 | container.insert(make_pair(2, 'b')); |
| 1247 | container.insert(make_pair(4, 'c')); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1248 | EXPECT_THAT(container, Contains(Key(1))); |
| 1249 | EXPECT_THAT(container, Not(Contains(Key(3)))); |
| 1250 | } |
| 1251 | |
| 1252 | TEST(KeyTest, InsideContainsUsingMultimap) { |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1253 | multimap<int, char> container; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1254 | container.insert(make_pair(1, 'a')); |
| 1255 | container.insert(make_pair(2, 'b')); |
| 1256 | container.insert(make_pair(4, 'c')); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1257 | |
| 1258 | EXPECT_THAT(container, Not(Contains(Key(25)))); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1259 | container.insert(make_pair(25, 'd')); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1260 | EXPECT_THAT(container, Contains(Key(25))); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1261 | container.insert(make_pair(25, 'e')); |
zhanyong.wan | b5937da | 2009-07-16 20:26:41 +0000 | [diff] [blame] | 1262 | EXPECT_THAT(container, Contains(Key(25))); |
| 1263 | |
| 1264 | EXPECT_THAT(container, Contains(Key(1))); |
| 1265 | EXPECT_THAT(container, Not(Contains(Key(3)))); |
| 1266 | } |
| 1267 | |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1268 | TEST(PairTest, Typing) { |
| 1269 | // Test verifies the following type conversions can be compiled. |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1270 | Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42); |
| 1271 | Matcher<const pair<const char*, int> > m2 = Pair("foo", 42); |
| 1272 | Matcher<pair<const char*, int> > m3 = Pair("foo", 42); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1273 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1274 | Matcher<pair<int, const std::string> > m4 = Pair(25, "42"); |
| 1275 | Matcher<pair<const std::string, int> > m5 = Pair("25", 42); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | TEST(PairTest, CanDescribeSelf) { |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1279 | Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1280 | EXPECT_EQ("has a first field that is equal to \"foo\"" |
| 1281 | ", and has a second field that is equal to 42", |
| 1282 | Describe(m1)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1283 | EXPECT_EQ("has a first field that isn't equal to \"foo\"" |
| 1284 | ", or has a second field that isn't equal to 42", |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1285 | DescribeNegation(m1)); |
| 1286 | // Double and triple negation (1 or 2 times not and description of negation). |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1287 | Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42)); |
| 1288 | EXPECT_EQ("has a first field that isn't equal to 13" |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1289 | ", and has a second field that is equal to 42", |
| 1290 | DescribeNegation(m2)); |
| 1291 | } |
| 1292 | |
| 1293 | TEST(PairTest, CanExplainMatchResultTo) { |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 1294 | // If neither field matches, Pair() should explain about the first |
| 1295 | // field. |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1296 | const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0)); |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 1297 | EXPECT_EQ("whose first field does not match, which is 1 less than 0", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1298 | Explain(m, make_pair(-1, -2))); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1299 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 1300 | // If the first field matches but the second doesn't, Pair() should |
| 1301 | // explain about the second field. |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 1302 | EXPECT_EQ("whose second field does not match, which is 2 less than 0", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1303 | Explain(m, make_pair(1, -2))); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1304 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 1305 | // If the first field doesn't match but the second does, Pair() |
| 1306 | // should explain about the first field. |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 1307 | EXPECT_EQ("whose first field does not match, which is 1 less than 0", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1308 | Explain(m, make_pair(-1, 2))); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1309 | |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 1310 | // If both fields match, Pair() should explain about them both. |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 1311 | EXPECT_EQ("whose both fields match, where the first field is a value " |
| 1312 | "which is 1 more than 0, and the second field is a value " |
| 1313 | "which is 2 more than 0", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1314 | Explain(m, make_pair(1, 2))); |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 1315 | |
| 1316 | // If only the first match has an explanation, only this explanation should |
| 1317 | // be printed. |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1318 | const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0); |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 1319 | EXPECT_EQ("whose both fields match, where the first field is a value " |
| 1320 | "which is 1 more than 0", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1321 | Explain(explain_first, make_pair(1, 0))); |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 1322 | |
| 1323 | // If only the second match has an explanation, only this explanation should |
| 1324 | // be printed. |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1325 | const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0)); |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 1326 | EXPECT_EQ("whose both fields match, where the second field is a value " |
| 1327 | "which is 1 more than 0", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1328 | Explain(explain_second, make_pair(0, 1))); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | TEST(PairTest, MatchesCorrectly) { |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1332 | pair<int, std::string> p(25, "foo"); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1333 | |
| 1334 | // Both fields match. |
| 1335 | EXPECT_THAT(p, Pair(25, "foo")); |
| 1336 | EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); |
| 1337 | |
| 1338 | // 'first' doesnt' match, but 'second' matches. |
| 1339 | EXPECT_THAT(p, Not(Pair(42, "foo"))); |
| 1340 | EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); |
| 1341 | |
| 1342 | // 'first' matches, but 'second' doesn't match. |
| 1343 | EXPECT_THAT(p, Not(Pair(25, "bar"))); |
| 1344 | EXPECT_THAT(p, Not(Pair(25, Not("foo")))); |
| 1345 | |
| 1346 | // Neither field matches. |
| 1347 | EXPECT_THAT(p, Not(Pair(13, "bar"))); |
| 1348 | EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); |
| 1349 | } |
| 1350 | |
| 1351 | TEST(PairTest, SafelyCastsInnerMatchers) { |
| 1352 | Matcher<int> is_positive = Gt(0); |
| 1353 | Matcher<int> is_negative = Lt(0); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1354 | pair<char, bool> p('a', true); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1355 | EXPECT_THAT(p, Pair(is_positive, _)); |
| 1356 | EXPECT_THAT(p, Not(Pair(is_negative, _))); |
| 1357 | EXPECT_THAT(p, Pair(_, is_positive)); |
| 1358 | EXPECT_THAT(p, Not(Pair(_, is_negative))); |
| 1359 | } |
| 1360 | |
| 1361 | TEST(PairTest, InsideContainsUsingMap) { |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1362 | map<int, char> container; |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1363 | container.insert(make_pair(1, 'a')); |
| 1364 | container.insert(make_pair(2, 'b')); |
| 1365 | container.insert(make_pair(4, 'c')); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 1366 | EXPECT_THAT(container, Contains(Pair(1, 'a'))); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1367 | EXPECT_THAT(container, Contains(Pair(1, _))); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 1368 | EXPECT_THAT(container, Contains(Pair(_, 'a'))); |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 1369 | EXPECT_THAT(container, Not(Contains(Pair(3, _)))); |
| 1370 | } |
| 1371 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1372 | // Tests StartsWith(s). |
| 1373 | |
| 1374 | TEST(StartsWithTest, MatchesStringWithGivenPrefix) { |
| 1375 | const Matcher<const char*> m1 = StartsWith(string("")); |
| 1376 | EXPECT_TRUE(m1.Matches("Hi")); |
| 1377 | EXPECT_TRUE(m1.Matches("")); |
| 1378 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1379 | |
| 1380 | const Matcher<const string&> m2 = StartsWith("Hi"); |
| 1381 | EXPECT_TRUE(m2.Matches("Hi")); |
| 1382 | EXPECT_TRUE(m2.Matches("Hi Hi!")); |
| 1383 | EXPECT_TRUE(m2.Matches("High")); |
| 1384 | EXPECT_FALSE(m2.Matches("H")); |
| 1385 | EXPECT_FALSE(m2.Matches(" Hi")); |
| 1386 | } |
| 1387 | |
| 1388 | TEST(StartsWithTest, CanDescribeSelf) { |
| 1389 | Matcher<const std::string> m = StartsWith("Hi"); |
| 1390 | EXPECT_EQ("starts with \"Hi\"", Describe(m)); |
| 1391 | } |
| 1392 | |
| 1393 | // Tests EndsWith(s). |
| 1394 | |
| 1395 | TEST(EndsWithTest, MatchesStringWithGivenSuffix) { |
| 1396 | const Matcher<const char*> m1 = EndsWith(""); |
| 1397 | EXPECT_TRUE(m1.Matches("Hi")); |
| 1398 | EXPECT_TRUE(m1.Matches("")); |
| 1399 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1400 | |
| 1401 | const Matcher<const string&> m2 = EndsWith(string("Hi")); |
| 1402 | EXPECT_TRUE(m2.Matches("Hi")); |
| 1403 | EXPECT_TRUE(m2.Matches("Wow Hi Hi")); |
| 1404 | EXPECT_TRUE(m2.Matches("Super Hi")); |
| 1405 | EXPECT_FALSE(m2.Matches("i")); |
| 1406 | EXPECT_FALSE(m2.Matches("Hi ")); |
| 1407 | } |
| 1408 | |
| 1409 | TEST(EndsWithTest, CanDescribeSelf) { |
| 1410 | Matcher<const std::string> m = EndsWith("Hi"); |
| 1411 | EXPECT_EQ("ends with \"Hi\"", Describe(m)); |
| 1412 | } |
| 1413 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1414 | // Tests MatchesRegex(). |
| 1415 | |
| 1416 | TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) { |
| 1417 | const Matcher<const char*> m1 = MatchesRegex("a.*z"); |
| 1418 | EXPECT_TRUE(m1.Matches("az")); |
| 1419 | EXPECT_TRUE(m1.Matches("abcz")); |
| 1420 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1421 | |
| 1422 | const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z")); |
| 1423 | EXPECT_TRUE(m2.Matches("azbz")); |
| 1424 | EXPECT_FALSE(m2.Matches("az1")); |
| 1425 | EXPECT_FALSE(m2.Matches("1az")); |
| 1426 | } |
| 1427 | |
| 1428 | TEST(MatchesRegexTest, CanDescribeSelf) { |
| 1429 | Matcher<const std::string> m1 = MatchesRegex(string("Hi.*")); |
| 1430 | EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1)); |
| 1431 | |
zhanyong.wan | d14aaed | 2010-01-14 05:36:32 +0000 | [diff] [blame] | 1432 | Matcher<const char*> m2 = MatchesRegex(new RE("a.*")); |
| 1433 | EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | // Tests ContainsRegex(). |
| 1437 | |
| 1438 | TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) { |
| 1439 | const Matcher<const char*> m1 = ContainsRegex(string("a.*z")); |
| 1440 | EXPECT_TRUE(m1.Matches("az")); |
| 1441 | EXPECT_TRUE(m1.Matches("0abcz1")); |
| 1442 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1443 | |
| 1444 | const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z")); |
| 1445 | EXPECT_TRUE(m2.Matches("azbz")); |
| 1446 | EXPECT_TRUE(m2.Matches("az1")); |
| 1447 | EXPECT_FALSE(m2.Matches("1a")); |
| 1448 | } |
| 1449 | |
| 1450 | TEST(ContainsRegexTest, CanDescribeSelf) { |
| 1451 | Matcher<const std::string> m1 = ContainsRegex("Hi.*"); |
| 1452 | EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1)); |
| 1453 | |
zhanyong.wan | d14aaed | 2010-01-14 05:36:32 +0000 | [diff] [blame] | 1454 | Matcher<const char*> m2 = ContainsRegex(new RE("a.*")); |
| 1455 | EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1456 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1457 | |
| 1458 | // Tests for wide strings. |
| 1459 | #if GTEST_HAS_STD_WSTRING |
| 1460 | TEST(StdWideStrEqTest, MatchesEqual) { |
| 1461 | Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello")); |
| 1462 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1463 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1464 | EXPECT_FALSE(m.Matches(NULL)); |
| 1465 | |
| 1466 | Matcher<const ::std::wstring&> m2 = StrEq(L"Hello"); |
| 1467 | EXPECT_TRUE(m2.Matches(L"Hello")); |
| 1468 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1469 | |
| 1470 | Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1471 | EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); |
| 1472 | EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); |
| 1473 | |
| 1474 | ::std::wstring str(L"01204500800"); |
| 1475 | str[3] = L'\0'; |
| 1476 | Matcher<const ::std::wstring&> m4 = StrEq(str); |
| 1477 | EXPECT_TRUE(m4.Matches(str)); |
| 1478 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1479 | Matcher<const ::std::wstring&> m5 = StrEq(str); |
| 1480 | EXPECT_TRUE(m5.Matches(str)); |
| 1481 | } |
| 1482 | |
| 1483 | TEST(StdWideStrEqTest, CanDescribeSelf) { |
vladlosev | aa43220 | 2011-04-01 21:58:42 +0000 | [diff] [blame] | 1484 | Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v"); |
| 1485 | EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1486 | Describe(m)); |
| 1487 | |
| 1488 | Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1489 | EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", |
| 1490 | Describe(m2)); |
| 1491 | |
| 1492 | ::std::wstring str(L"01204500800"); |
| 1493 | str[3] = L'\0'; |
| 1494 | Matcher<const ::std::wstring&> m4 = StrEq(str); |
| 1495 | EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); |
| 1496 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1497 | Matcher<const ::std::wstring&> m5 = StrEq(str); |
| 1498 | EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); |
| 1499 | } |
| 1500 | |
| 1501 | TEST(StdWideStrNeTest, MatchesUnequalString) { |
| 1502 | Matcher<const wchar_t*> m = StrNe(L"Hello"); |
| 1503 | EXPECT_TRUE(m.Matches(L"")); |
| 1504 | EXPECT_TRUE(m.Matches(NULL)); |
| 1505 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1506 | |
| 1507 | Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello")); |
| 1508 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1509 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1510 | } |
| 1511 | |
| 1512 | TEST(StdWideStrNeTest, CanDescribeSelf) { |
| 1513 | Matcher<const wchar_t*> m = StrNe(L"Hi"); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1514 | EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 1518 | Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello")); |
| 1519 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1520 | EXPECT_TRUE(m.Matches(L"hello")); |
| 1521 | EXPECT_FALSE(m.Matches(L"Hi")); |
| 1522 | EXPECT_FALSE(m.Matches(NULL)); |
| 1523 | |
| 1524 | Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello"); |
| 1525 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1526 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1527 | } |
| 1528 | |
| 1529 | TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 1530 | ::std::wstring str1(L"oabocdooeoo"); |
| 1531 | ::std::wstring str2(L"OABOCDOOEOO"); |
| 1532 | Matcher<const ::std::wstring&> m0 = StrCaseEq(str1); |
| 1533 | EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0'))); |
| 1534 | |
| 1535 | str1[3] = str2[3] = L'\0'; |
| 1536 | Matcher<const ::std::wstring&> m1 = StrCaseEq(str1); |
| 1537 | EXPECT_TRUE(m1.Matches(str2)); |
| 1538 | |
| 1539 | str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; |
| 1540 | str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; |
| 1541 | Matcher<const ::std::wstring&> m2 = StrCaseEq(str1); |
| 1542 | str1[9] = str2[9] = L'\0'; |
| 1543 | EXPECT_FALSE(m2.Matches(str2)); |
| 1544 | |
| 1545 | Matcher<const ::std::wstring&> m3 = StrCaseEq(str1); |
| 1546 | EXPECT_TRUE(m3.Matches(str2)); |
| 1547 | |
| 1548 | EXPECT_FALSE(m3.Matches(str2 + L"x")); |
| 1549 | str2.append(1, L'\0'); |
| 1550 | EXPECT_FALSE(m3.Matches(str2)); |
| 1551 | EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9))); |
| 1552 | } |
| 1553 | |
| 1554 | TEST(StdWideStrCaseEqTest, CanDescribeSelf) { |
| 1555 | Matcher< ::std::wstring> m = StrCaseEq(L"Hi"); |
| 1556 | EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 1557 | } |
| 1558 | |
| 1559 | TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 1560 | Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); |
| 1561 | EXPECT_TRUE(m.Matches(L"Hi")); |
| 1562 | EXPECT_TRUE(m.Matches(NULL)); |
| 1563 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1564 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1565 | |
| 1566 | Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello")); |
| 1567 | EXPECT_TRUE(m2.Matches(L"")); |
| 1568 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1569 | } |
| 1570 | |
| 1571 | TEST(StdWideStrCaseNeTest, CanDescribeSelf) { |
| 1572 | Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1573 | EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | // Tests that HasSubstr() works for matching wstring-typed values. |
| 1577 | TEST(StdWideHasSubstrTest, WorksForStringClasses) { |
| 1578 | const Matcher< ::std::wstring> m1 = HasSubstr(L"foo"); |
| 1579 | EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food."))); |
| 1580 | EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo"))); |
| 1581 | |
| 1582 | const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo"); |
| 1583 | EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food."))); |
| 1584 | EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo"))); |
| 1585 | } |
| 1586 | |
| 1587 | // Tests that HasSubstr() works for matching C-wide-string-typed values. |
| 1588 | TEST(StdWideHasSubstrTest, WorksForCStrings) { |
| 1589 | const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); |
| 1590 | EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); |
| 1591 | EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); |
| 1592 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1593 | |
| 1594 | const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); |
| 1595 | EXPECT_TRUE(m2.Matches(L"I love food.")); |
| 1596 | EXPECT_FALSE(m2.Matches(L"tofo")); |
| 1597 | EXPECT_FALSE(m2.Matches(NULL)); |
| 1598 | } |
| 1599 | |
| 1600 | // Tests that HasSubstr(s) describes itself properly. |
| 1601 | TEST(StdWideHasSubstrTest, CanDescribeSelf) { |
| 1602 | Matcher< ::std::wstring> m = HasSubstr(L"foo\n\""); |
| 1603 | EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); |
| 1604 | } |
| 1605 | |
| 1606 | // Tests StartsWith(s). |
| 1607 | |
| 1608 | TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) { |
| 1609 | const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L"")); |
| 1610 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1611 | EXPECT_TRUE(m1.Matches(L"")); |
| 1612 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1613 | |
| 1614 | const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi"); |
| 1615 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1616 | EXPECT_TRUE(m2.Matches(L"Hi Hi!")); |
| 1617 | EXPECT_TRUE(m2.Matches(L"High")); |
| 1618 | EXPECT_FALSE(m2.Matches(L"H")); |
| 1619 | EXPECT_FALSE(m2.Matches(L" Hi")); |
| 1620 | } |
| 1621 | |
| 1622 | TEST(StdWideStartsWithTest, CanDescribeSelf) { |
| 1623 | Matcher<const ::std::wstring> m = StartsWith(L"Hi"); |
| 1624 | EXPECT_EQ("starts with L\"Hi\"", Describe(m)); |
| 1625 | } |
| 1626 | |
| 1627 | // Tests EndsWith(s). |
| 1628 | |
| 1629 | TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) { |
| 1630 | const Matcher<const wchar_t*> m1 = EndsWith(L""); |
| 1631 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1632 | EXPECT_TRUE(m1.Matches(L"")); |
| 1633 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1634 | |
| 1635 | const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi")); |
| 1636 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1637 | EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); |
| 1638 | EXPECT_TRUE(m2.Matches(L"Super Hi")); |
| 1639 | EXPECT_FALSE(m2.Matches(L"i")); |
| 1640 | EXPECT_FALSE(m2.Matches(L"Hi ")); |
| 1641 | } |
| 1642 | |
| 1643 | TEST(StdWideEndsWithTest, CanDescribeSelf) { |
| 1644 | Matcher<const ::std::wstring> m = EndsWith(L"Hi"); |
| 1645 | EXPECT_EQ("ends with L\"Hi\"", Describe(m)); |
| 1646 | } |
| 1647 | |
| 1648 | #endif // GTEST_HAS_STD_WSTRING |
| 1649 | |
| 1650 | #if GTEST_HAS_GLOBAL_WSTRING |
| 1651 | TEST(GlobalWideStrEqTest, MatchesEqual) { |
| 1652 | Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello")); |
| 1653 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1654 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1655 | EXPECT_FALSE(m.Matches(NULL)); |
| 1656 | |
| 1657 | Matcher<const ::wstring&> m2 = StrEq(L"Hello"); |
| 1658 | EXPECT_TRUE(m2.Matches(L"Hello")); |
| 1659 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1660 | |
| 1661 | Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1662 | EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); |
| 1663 | EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); |
| 1664 | |
| 1665 | ::wstring str(L"01204500800"); |
| 1666 | str[3] = L'\0'; |
| 1667 | Matcher<const ::wstring&> m4 = StrEq(str); |
| 1668 | EXPECT_TRUE(m4.Matches(str)); |
| 1669 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1670 | Matcher<const ::wstring&> m5 = StrEq(str); |
| 1671 | EXPECT_TRUE(m5.Matches(str)); |
| 1672 | } |
| 1673 | |
| 1674 | TEST(GlobalWideStrEqTest, CanDescribeSelf) { |
vladlosev | aa43220 | 2011-04-01 21:58:42 +0000 | [diff] [blame] | 1675 | Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v"); |
| 1676 | EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1677 | Describe(m)); |
| 1678 | |
| 1679 | Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); |
| 1680 | EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", |
| 1681 | Describe(m2)); |
| 1682 | |
| 1683 | ::wstring str(L"01204500800"); |
| 1684 | str[3] = L'\0'; |
| 1685 | Matcher<const ::wstring&> m4 = StrEq(str); |
| 1686 | EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); |
| 1687 | str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; |
| 1688 | Matcher<const ::wstring&> m5 = StrEq(str); |
| 1689 | EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); |
| 1690 | } |
| 1691 | |
| 1692 | TEST(GlobalWideStrNeTest, MatchesUnequalString) { |
| 1693 | Matcher<const wchar_t*> m = StrNe(L"Hello"); |
| 1694 | EXPECT_TRUE(m.Matches(L"")); |
| 1695 | EXPECT_TRUE(m.Matches(NULL)); |
| 1696 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1697 | |
| 1698 | Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello")); |
| 1699 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1700 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1701 | } |
| 1702 | |
| 1703 | TEST(GlobalWideStrNeTest, CanDescribeSelf) { |
| 1704 | Matcher<const wchar_t*> m = StrNe(L"Hi"); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1705 | EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
| 1709 | Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello")); |
| 1710 | EXPECT_TRUE(m.Matches(L"Hello")); |
| 1711 | EXPECT_TRUE(m.Matches(L"hello")); |
| 1712 | EXPECT_FALSE(m.Matches(L"Hi")); |
| 1713 | EXPECT_FALSE(m.Matches(NULL)); |
| 1714 | |
| 1715 | Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello"); |
| 1716 | EXPECT_TRUE(m2.Matches(L"hello")); |
| 1717 | EXPECT_FALSE(m2.Matches(L"Hi")); |
| 1718 | } |
| 1719 | |
| 1720 | TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { |
| 1721 | ::wstring str1(L"oabocdooeoo"); |
| 1722 | ::wstring str2(L"OABOCDOOEOO"); |
| 1723 | Matcher<const ::wstring&> m0 = StrCaseEq(str1); |
| 1724 | EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0'))); |
| 1725 | |
| 1726 | str1[3] = str2[3] = L'\0'; |
| 1727 | Matcher<const ::wstring&> m1 = StrCaseEq(str1); |
| 1728 | EXPECT_TRUE(m1.Matches(str2)); |
| 1729 | |
| 1730 | str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; |
| 1731 | str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; |
| 1732 | Matcher<const ::wstring&> m2 = StrCaseEq(str1); |
| 1733 | str1[9] = str2[9] = L'\0'; |
| 1734 | EXPECT_FALSE(m2.Matches(str2)); |
| 1735 | |
| 1736 | Matcher<const ::wstring&> m3 = StrCaseEq(str1); |
| 1737 | EXPECT_TRUE(m3.Matches(str2)); |
| 1738 | |
| 1739 | EXPECT_FALSE(m3.Matches(str2 + L"x")); |
| 1740 | str2.append(1, L'\0'); |
| 1741 | EXPECT_FALSE(m3.Matches(str2)); |
| 1742 | EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9))); |
| 1743 | } |
| 1744 | |
| 1745 | TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) { |
| 1746 | Matcher< ::wstring> m = StrCaseEq(L"Hi"); |
| 1747 | EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); |
| 1748 | } |
| 1749 | |
| 1750 | TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { |
| 1751 | Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); |
| 1752 | EXPECT_TRUE(m.Matches(L"Hi")); |
| 1753 | EXPECT_TRUE(m.Matches(NULL)); |
| 1754 | EXPECT_FALSE(m.Matches(L"Hello")); |
| 1755 | EXPECT_FALSE(m.Matches(L"hello")); |
| 1756 | |
| 1757 | Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello")); |
| 1758 | EXPECT_TRUE(m2.Matches(L"")); |
| 1759 | EXPECT_FALSE(m2.Matches(L"Hello")); |
| 1760 | } |
| 1761 | |
| 1762 | TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) { |
| 1763 | Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1764 | EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | // Tests that HasSubstr() works for matching wstring-typed values. |
| 1768 | TEST(GlobalWideHasSubstrTest, WorksForStringClasses) { |
| 1769 | const Matcher< ::wstring> m1 = HasSubstr(L"foo"); |
| 1770 | EXPECT_TRUE(m1.Matches(::wstring(L"I love food."))); |
| 1771 | EXPECT_FALSE(m1.Matches(::wstring(L"tofo"))); |
| 1772 | |
| 1773 | const Matcher<const ::wstring&> m2 = HasSubstr(L"foo"); |
| 1774 | EXPECT_TRUE(m2.Matches(::wstring(L"I love food."))); |
| 1775 | EXPECT_FALSE(m2.Matches(::wstring(L"tofo"))); |
| 1776 | } |
| 1777 | |
| 1778 | // Tests that HasSubstr() works for matching C-wide-string-typed values. |
| 1779 | TEST(GlobalWideHasSubstrTest, WorksForCStrings) { |
| 1780 | const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); |
| 1781 | EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); |
| 1782 | EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); |
| 1783 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1784 | |
| 1785 | const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); |
| 1786 | EXPECT_TRUE(m2.Matches(L"I love food.")); |
| 1787 | EXPECT_FALSE(m2.Matches(L"tofo")); |
| 1788 | EXPECT_FALSE(m2.Matches(NULL)); |
| 1789 | } |
| 1790 | |
| 1791 | // Tests that HasSubstr(s) describes itself properly. |
| 1792 | TEST(GlobalWideHasSubstrTest, CanDescribeSelf) { |
| 1793 | Matcher< ::wstring> m = HasSubstr(L"foo\n\""); |
| 1794 | EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); |
| 1795 | } |
| 1796 | |
| 1797 | // Tests StartsWith(s). |
| 1798 | |
| 1799 | TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) { |
| 1800 | const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L"")); |
| 1801 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1802 | EXPECT_TRUE(m1.Matches(L"")); |
| 1803 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1804 | |
| 1805 | const Matcher<const ::wstring&> m2 = StartsWith(L"Hi"); |
| 1806 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1807 | EXPECT_TRUE(m2.Matches(L"Hi Hi!")); |
| 1808 | EXPECT_TRUE(m2.Matches(L"High")); |
| 1809 | EXPECT_FALSE(m2.Matches(L"H")); |
| 1810 | EXPECT_FALSE(m2.Matches(L" Hi")); |
| 1811 | } |
| 1812 | |
| 1813 | TEST(GlobalWideStartsWithTest, CanDescribeSelf) { |
| 1814 | Matcher<const ::wstring> m = StartsWith(L"Hi"); |
| 1815 | EXPECT_EQ("starts with L\"Hi\"", Describe(m)); |
| 1816 | } |
| 1817 | |
| 1818 | // Tests EndsWith(s). |
| 1819 | |
| 1820 | TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) { |
| 1821 | const Matcher<const wchar_t*> m1 = EndsWith(L""); |
| 1822 | EXPECT_TRUE(m1.Matches(L"Hi")); |
| 1823 | EXPECT_TRUE(m1.Matches(L"")); |
| 1824 | EXPECT_FALSE(m1.Matches(NULL)); |
| 1825 | |
| 1826 | const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi")); |
| 1827 | EXPECT_TRUE(m2.Matches(L"Hi")); |
| 1828 | EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); |
| 1829 | EXPECT_TRUE(m2.Matches(L"Super Hi")); |
| 1830 | EXPECT_FALSE(m2.Matches(L"i")); |
| 1831 | EXPECT_FALSE(m2.Matches(L"Hi ")); |
| 1832 | } |
| 1833 | |
| 1834 | TEST(GlobalWideEndsWithTest, CanDescribeSelf) { |
| 1835 | Matcher<const ::wstring> m = EndsWith(L"Hi"); |
| 1836 | EXPECT_EQ("ends with L\"Hi\"", Describe(m)); |
| 1837 | } |
| 1838 | |
| 1839 | #endif // GTEST_HAS_GLOBAL_WSTRING |
| 1840 | |
| 1841 | |
| 1842 | typedef ::std::tr1::tuple<long, int> Tuple2; // NOLINT |
| 1843 | |
| 1844 | // Tests that Eq() matches a 2-tuple where the first field == the |
| 1845 | // second field. |
| 1846 | TEST(Eq2Test, MatchesEqualArguments) { |
| 1847 | Matcher<const Tuple2&> m = Eq(); |
| 1848 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1849 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1850 | } |
| 1851 | |
| 1852 | // Tests that Eq() describes itself properly. |
| 1853 | TEST(Eq2Test, CanDescribeSelf) { |
| 1854 | Matcher<const Tuple2&> m = Eq(); |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1855 | EXPECT_EQ("are an equal pair", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | // Tests that Ge() matches a 2-tuple where the first field >= the |
| 1859 | // second field. |
| 1860 | TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { |
| 1861 | Matcher<const Tuple2&> m = Ge(); |
| 1862 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 1863 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1864 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1865 | } |
| 1866 | |
| 1867 | // Tests that Ge() describes itself properly. |
| 1868 | TEST(Ge2Test, CanDescribeSelf) { |
| 1869 | Matcher<const Tuple2&> m = Ge(); |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1870 | EXPECT_EQ("are a pair where the first >= the second", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | // Tests that Gt() matches a 2-tuple where the first field > the |
| 1874 | // second field. |
| 1875 | TEST(Gt2Test, MatchesGreaterThanArguments) { |
| 1876 | Matcher<const Tuple2&> m = Gt(); |
| 1877 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 1878 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 1879 | EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 1880 | } |
| 1881 | |
| 1882 | // Tests that Gt() describes itself properly. |
| 1883 | TEST(Gt2Test, CanDescribeSelf) { |
| 1884 | Matcher<const Tuple2&> m = Gt(); |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1885 | EXPECT_EQ("are a pair where the first > the second", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | // Tests that Le() matches a 2-tuple where the first field <= the |
| 1889 | // second field. |
| 1890 | TEST(Le2Test, MatchesLessThanOrEqualArguments) { |
| 1891 | Matcher<const Tuple2&> m = Le(); |
| 1892 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 1893 | EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 1894 | EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 1895 | } |
| 1896 | |
| 1897 | // Tests that Le() describes itself properly. |
| 1898 | TEST(Le2Test, CanDescribeSelf) { |
| 1899 | Matcher<const Tuple2&> m = Le(); |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1900 | EXPECT_EQ("are a pair where the first <= the second", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | // Tests that Lt() matches a 2-tuple where the first field < the |
| 1904 | // second field. |
| 1905 | TEST(Lt2Test, MatchesLessThanArguments) { |
| 1906 | Matcher<const Tuple2&> m = Lt(); |
| 1907 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 1908 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 1909 | EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 1910 | } |
| 1911 | |
| 1912 | // Tests that Lt() describes itself properly. |
| 1913 | TEST(Lt2Test, CanDescribeSelf) { |
| 1914 | Matcher<const Tuple2&> m = Lt(); |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1915 | EXPECT_EQ("are a pair where the first < the second", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | // Tests that Ne() matches a 2-tuple where the first field != the |
| 1919 | // second field. |
| 1920 | TEST(Ne2Test, MatchesUnequalArguments) { |
| 1921 | Matcher<const Tuple2&> m = Ne(); |
| 1922 | EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 1923 | EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 1924 | EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 1925 | } |
| 1926 | |
| 1927 | // Tests that Ne() describes itself properly. |
| 1928 | TEST(Ne2Test, CanDescribeSelf) { |
| 1929 | Matcher<const Tuple2&> m = Ne(); |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 1930 | EXPECT_EQ("are an unequal pair", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | // Tests that Not(m) matches any value that doesn't match m. |
| 1934 | TEST(NotTest, NegatesMatcher) { |
| 1935 | Matcher<int> m; |
| 1936 | m = Not(Eq(2)); |
| 1937 | EXPECT_TRUE(m.Matches(3)); |
| 1938 | EXPECT_FALSE(m.Matches(2)); |
| 1939 | } |
| 1940 | |
| 1941 | // Tests that Not(m) describes itself properly. |
| 1942 | TEST(NotTest, CanDescribeSelf) { |
| 1943 | Matcher<int> m = Not(Eq(5)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1944 | EXPECT_EQ("isn't equal to 5", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 1947 | // Tests that monomorphic matchers are safely cast by the Not matcher. |
| 1948 | TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { |
| 1949 | // greater_than_5 is a monomorphic matcher. |
| 1950 | Matcher<int> greater_than_5 = Gt(5); |
| 1951 | |
| 1952 | Matcher<const int&> m = Not(greater_than_5); |
| 1953 | Matcher<int&> m2 = Not(greater_than_5); |
| 1954 | Matcher<int&> m3 = Not(m); |
| 1955 | } |
| 1956 | |
zhanyong.wan | 02c1505 | 2010-06-09 19:21:30 +0000 | [diff] [blame] | 1957 | // Helper to allow easy testing of AllOf matchers with num parameters. |
| 1958 | void AllOfMatches(int num, const Matcher<int>& m) { |
| 1959 | SCOPED_TRACE(Describe(m)); |
| 1960 | EXPECT_TRUE(m.Matches(0)); |
| 1961 | for (int i = 1; i <= num; ++i) { |
| 1962 | EXPECT_FALSE(m.Matches(i)); |
| 1963 | } |
| 1964 | EXPECT_TRUE(m.Matches(num + 1)); |
| 1965 | } |
| 1966 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1967 | // Tests that AllOf(m1, ..., mn) matches any value that matches all of |
| 1968 | // the given matchers. |
| 1969 | TEST(AllOfTest, MatchesWhenAllMatch) { |
| 1970 | Matcher<int> m; |
| 1971 | m = AllOf(Le(2), Ge(1)); |
| 1972 | EXPECT_TRUE(m.Matches(1)); |
| 1973 | EXPECT_TRUE(m.Matches(2)); |
| 1974 | EXPECT_FALSE(m.Matches(0)); |
| 1975 | EXPECT_FALSE(m.Matches(3)); |
| 1976 | |
| 1977 | m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 1978 | EXPECT_TRUE(m.Matches(3)); |
| 1979 | EXPECT_FALSE(m.Matches(2)); |
| 1980 | EXPECT_FALSE(m.Matches(1)); |
| 1981 | EXPECT_FALSE(m.Matches(0)); |
| 1982 | |
| 1983 | m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 1984 | EXPECT_TRUE(m.Matches(4)); |
| 1985 | EXPECT_FALSE(m.Matches(3)); |
| 1986 | EXPECT_FALSE(m.Matches(2)); |
| 1987 | EXPECT_FALSE(m.Matches(1)); |
| 1988 | EXPECT_FALSE(m.Matches(0)); |
| 1989 | |
| 1990 | m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 1991 | EXPECT_TRUE(m.Matches(0)); |
| 1992 | EXPECT_TRUE(m.Matches(1)); |
| 1993 | EXPECT_FALSE(m.Matches(3)); |
zhanyong.wan | 02c1505 | 2010-06-09 19:21:30 +0000 | [diff] [blame] | 1994 | |
| 1995 | // The following tests for varying number of sub-matchers. Due to the way |
| 1996 | // the sub-matchers are handled it is enough to test every sub-matcher once |
| 1997 | // with sub-matchers using the same matcher type. Varying matcher types are |
| 1998 | // checked for above. |
| 1999 | AllOfMatches(2, AllOf(Ne(1), Ne(2))); |
| 2000 | AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3))); |
| 2001 | AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4))); |
| 2002 | AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5))); |
| 2003 | AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6))); |
| 2004 | AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7))); |
| 2005 | AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), |
| 2006 | Ne(8))); |
| 2007 | AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), |
| 2008 | Ne(8), Ne(9))); |
| 2009 | AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), |
| 2010 | Ne(9), Ne(10))); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | // Tests that AllOf(m1, ..., mn) describes itself properly. |
| 2014 | TEST(AllOfTest, CanDescribeSelf) { |
| 2015 | Matcher<int> m; |
| 2016 | m = AllOf(Le(2), Ge(1)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2017 | EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2018 | |
| 2019 | m = AllOf(Gt(0), Ne(1), Ne(2)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2020 | EXPECT_EQ("(is > 0) and " |
| 2021 | "((isn't equal to 1) and " |
| 2022 | "(isn't equal to 2))", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2023 | Describe(m)); |
| 2024 | |
| 2025 | |
| 2026 | m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2027 | EXPECT_EQ("((is > 0) and " |
| 2028 | "(isn't equal to 1)) and " |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2029 | "((isn't equal to 2) and " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2030 | "(isn't equal to 3))", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2031 | Describe(m)); |
| 2032 | |
| 2033 | |
| 2034 | m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2035 | EXPECT_EQ("((is >= 0) and " |
| 2036 | "(is < 10)) and " |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2037 | "((isn't equal to 3) and " |
| 2038 | "((isn't equal to 5) and " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2039 | "(isn't equal to 7)))", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2040 | Describe(m)); |
| 2041 | } |
| 2042 | |
| 2043 | // Tests that AllOf(m1, ..., mn) describes its negation properly. |
| 2044 | TEST(AllOfTest, CanDescribeNegation) { |
| 2045 | Matcher<int> m; |
| 2046 | m = AllOf(Le(2), Ge(1)); |
| 2047 | EXPECT_EQ("(isn't <= 2) or " |
| 2048 | "(isn't >= 1)", |
| 2049 | DescribeNegation(m)); |
| 2050 | |
| 2051 | m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 2052 | EXPECT_EQ("(isn't > 0) or " |
| 2053 | "((is equal to 1) or " |
| 2054 | "(is equal to 2))", |
| 2055 | DescribeNegation(m)); |
| 2056 | |
| 2057 | |
| 2058 | m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2059 | EXPECT_EQ("((isn't > 0) or " |
| 2060 | "(is equal to 1)) or " |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2061 | "((is equal to 2) or " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2062 | "(is equal to 3))", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2063 | DescribeNegation(m)); |
| 2064 | |
| 2065 | |
| 2066 | m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2067 | EXPECT_EQ("((isn't >= 0) or " |
| 2068 | "(isn't < 10)) or " |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2069 | "((is equal to 3) or " |
| 2070 | "((is equal to 5) or " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2071 | "(is equal to 7)))", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2072 | DescribeNegation(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 2075 | // Tests that monomorphic matchers are safely cast by the AllOf matcher. |
| 2076 | TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) { |
| 2077 | // greater_than_5 and less_than_10 are monomorphic matchers. |
| 2078 | Matcher<int> greater_than_5 = Gt(5); |
| 2079 | Matcher<int> less_than_10 = Lt(10); |
| 2080 | |
| 2081 | Matcher<const int&> m = AllOf(greater_than_5, less_than_10); |
| 2082 | Matcher<int&> m2 = AllOf(greater_than_5, less_than_10); |
| 2083 | Matcher<int&> m3 = AllOf(greater_than_5, m2); |
| 2084 | |
| 2085 | // Tests that BothOf works when composing itself. |
| 2086 | Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10); |
| 2087 | Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10); |
| 2088 | } |
| 2089 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2090 | TEST(AllOfTest, ExplainsResult) { |
| 2091 | Matcher<int> m; |
| 2092 | |
| 2093 | // Successful match. Both matchers need to explain. The second |
| 2094 | // matcher doesn't give an explanation, so only the first matcher's |
| 2095 | // explanation is printed. |
| 2096 | m = AllOf(GreaterThan(10), Lt(30)); |
| 2097 | EXPECT_EQ("which is 15 more than 10", Explain(m, 25)); |
| 2098 | |
| 2099 | // Successful match. Both matchers need to explain. |
| 2100 | m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2101 | EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20", |
| 2102 | Explain(m, 30)); |
| 2103 | |
| 2104 | // Successful match. All matchers need to explain. The second |
| 2105 | // matcher doesn't given an explanation. |
| 2106 | m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20)); |
| 2107 | EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20", |
| 2108 | Explain(m, 25)); |
| 2109 | |
| 2110 | // Successful match. All matchers need to explain. |
| 2111 | m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 2112 | EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, " |
| 2113 | "and which is 10 more than 30", |
| 2114 | Explain(m, 40)); |
| 2115 | |
| 2116 | // Failed match. The first matcher, which failed, needs to |
| 2117 | // explain. |
| 2118 | m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2119 | EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 2120 | |
| 2121 | // Failed match. The second matcher, which failed, needs to |
| 2122 | // explain. Since it doesn't given an explanation, nothing is |
| 2123 | // printed. |
| 2124 | m = AllOf(GreaterThan(10), Lt(30)); |
| 2125 | EXPECT_EQ("", Explain(m, 40)); |
| 2126 | |
| 2127 | // Failed match. The second matcher, which failed, needs to |
| 2128 | // explain. |
| 2129 | m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2130 | EXPECT_EQ("which is 5 less than 20", Explain(m, 15)); |
| 2131 | } |
| 2132 | |
zhanyong.wan | 02c1505 | 2010-06-09 19:21:30 +0000 | [diff] [blame] | 2133 | // Helper to allow easy testing of AnyOf matchers with num parameters. |
| 2134 | void AnyOfMatches(int num, const Matcher<int>& m) { |
| 2135 | SCOPED_TRACE(Describe(m)); |
| 2136 | EXPECT_FALSE(m.Matches(0)); |
| 2137 | for (int i = 1; i <= num; ++i) { |
| 2138 | EXPECT_TRUE(m.Matches(i)); |
| 2139 | } |
| 2140 | EXPECT_FALSE(m.Matches(num + 1)); |
| 2141 | } |
| 2142 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2143 | // Tests that AnyOf(m1, ..., mn) matches any value that matches at |
| 2144 | // least one of the given matchers. |
| 2145 | TEST(AnyOfTest, MatchesWhenAnyMatches) { |
| 2146 | Matcher<int> m; |
| 2147 | m = AnyOf(Le(1), Ge(3)); |
| 2148 | EXPECT_TRUE(m.Matches(1)); |
| 2149 | EXPECT_TRUE(m.Matches(4)); |
| 2150 | EXPECT_FALSE(m.Matches(2)); |
| 2151 | |
| 2152 | m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2153 | EXPECT_TRUE(m.Matches(-1)); |
| 2154 | EXPECT_TRUE(m.Matches(1)); |
| 2155 | EXPECT_TRUE(m.Matches(2)); |
| 2156 | EXPECT_FALSE(m.Matches(0)); |
| 2157 | |
| 2158 | m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 2159 | EXPECT_TRUE(m.Matches(-1)); |
| 2160 | EXPECT_TRUE(m.Matches(1)); |
| 2161 | EXPECT_TRUE(m.Matches(2)); |
| 2162 | EXPECT_TRUE(m.Matches(3)); |
| 2163 | EXPECT_FALSE(m.Matches(0)); |
| 2164 | |
| 2165 | m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 2166 | EXPECT_TRUE(m.Matches(0)); |
| 2167 | EXPECT_TRUE(m.Matches(11)); |
| 2168 | EXPECT_TRUE(m.Matches(3)); |
| 2169 | EXPECT_FALSE(m.Matches(2)); |
zhanyong.wan | 02c1505 | 2010-06-09 19:21:30 +0000 | [diff] [blame] | 2170 | |
| 2171 | // The following tests for varying number of sub-matchers. Due to the way |
| 2172 | // the sub-matchers are handled it is enough to test every sub-matcher once |
| 2173 | // with sub-matchers using the same matcher type. Varying matcher types are |
| 2174 | // checked for above. |
| 2175 | AnyOfMatches(2, AnyOf(1, 2)); |
| 2176 | AnyOfMatches(3, AnyOf(1, 2, 3)); |
| 2177 | AnyOfMatches(4, AnyOf(1, 2, 3, 4)); |
| 2178 | AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5)); |
| 2179 | AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6)); |
| 2180 | AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7)); |
| 2181 | AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8)); |
| 2182 | AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9)); |
| 2183 | AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | // Tests that AnyOf(m1, ..., mn) describes itself properly. |
| 2187 | TEST(AnyOfTest, CanDescribeSelf) { |
| 2188 | Matcher<int> m; |
| 2189 | m = AnyOf(Le(1), Ge(3)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2190 | EXPECT_EQ("(is <= 1) or (is >= 3)", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2191 | Describe(m)); |
| 2192 | |
| 2193 | m = AnyOf(Lt(0), Eq(1), Eq(2)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2194 | EXPECT_EQ("(is < 0) or " |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2195 | "((is equal to 1) or (is equal to 2))", |
| 2196 | Describe(m)); |
| 2197 | |
| 2198 | m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2199 | EXPECT_EQ("((is < 0) or " |
| 2200 | "(is equal to 1)) or " |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2201 | "((is equal to 2) or " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2202 | "(is equal to 3))", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2203 | Describe(m)); |
| 2204 | |
| 2205 | m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2206 | EXPECT_EQ("((is <= 0) or " |
| 2207 | "(is > 10)) or " |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2208 | "((is equal to 3) or " |
| 2209 | "((is equal to 5) or " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2210 | "(is equal to 7)))", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2211 | Describe(m)); |
| 2212 | } |
| 2213 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2214 | // Tests that AnyOf(m1, ..., mn) describes its negation properly. |
| 2215 | TEST(AnyOfTest, CanDescribeNegation) { |
| 2216 | Matcher<int> m; |
| 2217 | m = AnyOf(Le(1), Ge(3)); |
| 2218 | EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", |
| 2219 | DescribeNegation(m)); |
| 2220 | |
| 2221 | m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2222 | EXPECT_EQ("(isn't < 0) and " |
| 2223 | "((isn't equal to 1) and (isn't equal to 2))", |
| 2224 | DescribeNegation(m)); |
| 2225 | |
| 2226 | m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2227 | EXPECT_EQ("((isn't < 0) and " |
| 2228 | "(isn't equal to 1)) and " |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2229 | "((isn't equal to 2) and " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2230 | "(isn't equal to 3))", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2231 | DescribeNegation(m)); |
| 2232 | |
| 2233 | m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2234 | EXPECT_EQ("((isn't <= 0) and " |
| 2235 | "(isn't > 10)) and " |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2236 | "((isn't equal to 3) and " |
| 2237 | "((isn't equal to 5) and " |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 2238 | "(isn't equal to 7)))", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2239 | DescribeNegation(m)); |
| 2240 | } |
| 2241 | |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 2242 | // Tests that monomorphic matchers are safely cast by the AnyOf matcher. |
| 2243 | TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) { |
| 2244 | // greater_than_5 and less_than_10 are monomorphic matchers. |
| 2245 | Matcher<int> greater_than_5 = Gt(5); |
| 2246 | Matcher<int> less_than_10 = Lt(10); |
| 2247 | |
| 2248 | Matcher<const int&> m = AnyOf(greater_than_5, less_than_10); |
| 2249 | Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10); |
| 2250 | Matcher<int&> m3 = AnyOf(greater_than_5, m2); |
| 2251 | |
| 2252 | // Tests that EitherOf works when composing itself. |
| 2253 | Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10); |
| 2254 | Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10); |
| 2255 | } |
| 2256 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2257 | TEST(AnyOfTest, ExplainsResult) { |
| 2258 | Matcher<int> m; |
| 2259 | |
| 2260 | // Failed match. Both matchers need to explain. The second |
| 2261 | // matcher doesn't give an explanation, so only the first matcher's |
| 2262 | // explanation is printed. |
| 2263 | m = AnyOf(GreaterThan(10), Lt(0)); |
| 2264 | EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 2265 | |
| 2266 | // Failed match. Both matchers need to explain. |
| 2267 | m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 2268 | EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20", |
| 2269 | Explain(m, 5)); |
| 2270 | |
| 2271 | // Failed match. All matchers need to explain. The second |
| 2272 | // matcher doesn't given an explanation. |
| 2273 | m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30)); |
| 2274 | EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30", |
| 2275 | Explain(m, 5)); |
| 2276 | |
| 2277 | // Failed match. All matchers need to explain. |
| 2278 | m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 2279 | EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, " |
| 2280 | "and which is 25 less than 30", |
| 2281 | Explain(m, 5)); |
| 2282 | |
| 2283 | // Successful match. The first matcher, which succeeded, needs to |
| 2284 | // explain. |
| 2285 | m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 2286 | EXPECT_EQ("which is 5 more than 10", Explain(m, 15)); |
| 2287 | |
| 2288 | // Successful match. The second matcher, which succeeded, needs to |
| 2289 | // explain. Since it doesn't given an explanation, nothing is |
| 2290 | // printed. |
| 2291 | m = AnyOf(GreaterThan(10), Lt(30)); |
| 2292 | EXPECT_EQ("", Explain(m, 0)); |
| 2293 | |
| 2294 | // Successful match. The second matcher, which succeeded, needs to |
| 2295 | // explain. |
| 2296 | m = AnyOf(GreaterThan(30), GreaterThan(20)); |
| 2297 | EXPECT_EQ("which is 5 more than 20", Explain(m, 25)); |
| 2298 | } |
| 2299 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2300 | // The following predicate function and predicate functor are for |
| 2301 | // testing the Truly(predicate) matcher. |
| 2302 | |
| 2303 | // Returns non-zero if the input is positive. Note that the return |
| 2304 | // type of this function is not bool. It's OK as Truly() accepts any |
| 2305 | // unary function or functor whose return type can be implicitly |
| 2306 | // converted to bool. |
| 2307 | int IsPositive(double x) { |
| 2308 | return x > 0 ? 1 : 0; |
| 2309 | } |
| 2310 | |
| 2311 | // This functor returns true if the input is greater than the given |
| 2312 | // number. |
| 2313 | class IsGreaterThan { |
| 2314 | public: |
| 2315 | explicit IsGreaterThan(int threshold) : threshold_(threshold) {} |
| 2316 | |
| 2317 | bool operator()(int n) const { return n > threshold_; } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2318 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2319 | private: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2320 | int threshold_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2321 | }; |
| 2322 | |
| 2323 | // For testing Truly(). |
| 2324 | const int foo = 0; |
| 2325 | |
| 2326 | // This predicate returns true iff the argument references foo and has |
| 2327 | // a zero value. |
| 2328 | bool ReferencesFooAndIsZero(const int& n) { |
| 2329 | return (&n == &foo) && (n == 0); |
| 2330 | } |
| 2331 | |
| 2332 | // Tests that Truly(predicate) matches what satisfies the given |
| 2333 | // predicate. |
| 2334 | TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) { |
| 2335 | Matcher<double> m = Truly(IsPositive); |
| 2336 | EXPECT_TRUE(m.Matches(2.0)); |
| 2337 | EXPECT_FALSE(m.Matches(-1.5)); |
| 2338 | } |
| 2339 | |
| 2340 | // Tests that Truly(predicate_functor) works too. |
| 2341 | TEST(TrulyTest, CanBeUsedWithFunctor) { |
| 2342 | Matcher<int> m = Truly(IsGreaterThan(5)); |
| 2343 | EXPECT_TRUE(m.Matches(6)); |
| 2344 | EXPECT_FALSE(m.Matches(4)); |
| 2345 | } |
| 2346 | |
zhanyong.wan | 8d3dc0c | 2011-04-14 19:37:06 +0000 | [diff] [blame] | 2347 | // A class that can be implicitly converted to bool. |
| 2348 | class ConvertibleToBool { |
| 2349 | public: |
| 2350 | explicit ConvertibleToBool(int number) : number_(number) {} |
| 2351 | operator bool() const { return number_ != 0; } |
| 2352 | |
| 2353 | private: |
| 2354 | int number_; |
| 2355 | }; |
| 2356 | |
| 2357 | ConvertibleToBool IsNotZero(int number) { |
| 2358 | return ConvertibleToBool(number); |
| 2359 | } |
| 2360 | |
| 2361 | // Tests that the predicate used in Truly() may return a class that's |
| 2362 | // implicitly convertible to bool, even when the class has no |
| 2363 | // operator!(). |
| 2364 | TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) { |
| 2365 | Matcher<int> m = Truly(IsNotZero); |
| 2366 | EXPECT_TRUE(m.Matches(1)); |
| 2367 | EXPECT_FALSE(m.Matches(0)); |
| 2368 | } |
| 2369 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2370 | // Tests that Truly(predicate) can describe itself properly. |
| 2371 | TEST(TrulyTest, CanDescribeSelf) { |
| 2372 | Matcher<double> m = Truly(IsPositive); |
| 2373 | EXPECT_EQ("satisfies the given predicate", |
| 2374 | Describe(m)); |
| 2375 | } |
| 2376 | |
| 2377 | // Tests that Truly(predicate) works when the matcher takes its |
| 2378 | // argument by reference. |
| 2379 | TEST(TrulyTest, WorksForByRefArguments) { |
| 2380 | Matcher<const int&> m = Truly(ReferencesFooAndIsZero); |
| 2381 | EXPECT_TRUE(m.Matches(foo)); |
| 2382 | int n = 0; |
| 2383 | EXPECT_FALSE(m.Matches(n)); |
| 2384 | } |
| 2385 | |
| 2386 | // Tests that Matches(m) is a predicate satisfied by whatever that |
| 2387 | // matches matcher m. |
| 2388 | TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) { |
| 2389 | EXPECT_TRUE(Matches(Ge(0))(1)); |
| 2390 | EXPECT_FALSE(Matches(Eq('a'))('b')); |
| 2391 | } |
| 2392 | |
| 2393 | // Tests that Matches(m) works when the matcher takes its argument by |
| 2394 | // reference. |
| 2395 | TEST(MatchesTest, WorksOnByRefArguments) { |
| 2396 | int m = 0, n = 0; |
| 2397 | EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n)); |
| 2398 | EXPECT_FALSE(Matches(Ref(m))(n)); |
| 2399 | } |
| 2400 | |
| 2401 | // Tests that a Matcher on non-reference type can be used in |
| 2402 | // Matches(). |
| 2403 | TEST(MatchesTest, WorksWithMatcherOnNonRefType) { |
| 2404 | Matcher<int> eq5 = Eq(5); |
| 2405 | EXPECT_TRUE(Matches(eq5)(5)); |
| 2406 | EXPECT_FALSE(Matches(eq5)(2)); |
| 2407 | } |
| 2408 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 2409 | // Tests Value(value, matcher). Since Value() is a simple wrapper for |
| 2410 | // Matches(), which has been tested already, we don't spend a lot of |
| 2411 | // effort on testing Value(). |
| 2412 | TEST(ValueTest, WorksWithPolymorphicMatcher) { |
| 2413 | EXPECT_TRUE(Value("hi", StartsWith("h"))); |
| 2414 | EXPECT_FALSE(Value(5, Gt(10))); |
| 2415 | } |
| 2416 | |
| 2417 | TEST(ValueTest, WorksWithMonomorphicMatcher) { |
| 2418 | const Matcher<int> is_zero = Eq(0); |
| 2419 | EXPECT_TRUE(Value(0, is_zero)); |
| 2420 | EXPECT_FALSE(Value('a', is_zero)); |
| 2421 | |
| 2422 | int n = 0; |
| 2423 | const Matcher<const int&> ref_n = Ref(n); |
| 2424 | EXPECT_TRUE(Value(n, ref_n)); |
| 2425 | EXPECT_FALSE(Value(1, ref_n)); |
| 2426 | } |
| 2427 | |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 2428 | TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) { |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 2429 | StringMatchResultListener listener1; |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 2430 | EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1)); |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 2431 | EXPECT_EQ("% 2 == 0", listener1.str()); |
| 2432 | |
| 2433 | StringMatchResultListener listener2; |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 2434 | EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2)); |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 2435 | EXPECT_EQ("", listener2.str()); |
| 2436 | } |
| 2437 | |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 2438 | TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) { |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 2439 | const Matcher<int> is_even = PolymorphicIsEven(); |
| 2440 | StringMatchResultListener listener1; |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 2441 | EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1)); |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 2442 | EXPECT_EQ("% 2 == 0", listener1.str()); |
| 2443 | |
| 2444 | const Matcher<const double&> is_zero = Eq(0); |
| 2445 | StringMatchResultListener listener2; |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 2446 | EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2)); |
zhanyong.wan | 34b034c | 2010-03-05 21:23:23 +0000 | [diff] [blame] | 2447 | EXPECT_EQ("", listener2.str()); |
| 2448 | } |
| 2449 | |
zhanyong.wan | a862f1d | 2010-03-15 21:23:04 +0000 | [diff] [blame] | 2450 | MATCHER_P(Really, inner_matcher, "") { |
| 2451 | return ExplainMatchResult(inner_matcher, arg, result_listener); |
| 2452 | } |
| 2453 | |
| 2454 | TEST(ExplainMatchResultTest, WorksInsideMATCHER) { |
| 2455 | EXPECT_THAT(0, Really(Eq(0))); |
| 2456 | } |
| 2457 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 2458 | TEST(AllArgsTest, WorksForTuple) { |
| 2459 | EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt())); |
| 2460 | EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt()))); |
| 2461 | } |
| 2462 | |
| 2463 | TEST(AllArgsTest, WorksForNonTuple) { |
| 2464 | EXPECT_THAT(42, AllArgs(Gt(0))); |
| 2465 | EXPECT_THAT('a', Not(AllArgs(Eq('b')))); |
| 2466 | } |
| 2467 | |
| 2468 | class AllArgsHelper { |
| 2469 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2470 | AllArgsHelper() {} |
| 2471 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 2472 | MOCK_METHOD2(Helper, int(char x, int y)); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2473 | |
| 2474 | private: |
| 2475 | GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 2476 | }; |
| 2477 | |
| 2478 | TEST(AllArgsTest, WorksInWithClause) { |
| 2479 | AllArgsHelper helper; |
| 2480 | ON_CALL(helper, Helper(_, _)) |
| 2481 | .With(AllArgs(Lt())) |
| 2482 | .WillByDefault(Return(1)); |
| 2483 | EXPECT_CALL(helper, Helper(_, _)); |
| 2484 | EXPECT_CALL(helper, Helper(_, _)) |
| 2485 | .With(AllArgs(Gt())) |
| 2486 | .WillOnce(Return(2)); |
| 2487 | |
| 2488 | EXPECT_EQ(1, helper.Helper('\1', 2)); |
| 2489 | EXPECT_EQ(2, helper.Helper('a', 1)); |
| 2490 | } |
| 2491 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2492 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value |
| 2493 | // matches the matcher. |
| 2494 | TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) { |
| 2495 | ASSERT_THAT(5, Ge(2)) << "This should succeed."; |
| 2496 | ASSERT_THAT("Foo", EndsWith("oo")); |
| 2497 | EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too."; |
| 2498 | EXPECT_THAT("Hello", StartsWith("Hell")); |
| 2499 | } |
| 2500 | |
| 2501 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value |
| 2502 | // doesn't match the matcher. |
| 2503 | TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) { |
| 2504 | // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(), |
| 2505 | // which cannot reference auto variables. |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 2506 | static unsigned short n; // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2507 | n = 5; |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 2508 | |
| 2509 | // VC++ prior to version 8.0 SP1 has a bug where it will not see any |
| 2510 | // functions declared in the namespace scope from within nested classes. |
| 2511 | // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all |
| 2512 | // namespace-level functions invoked inside them need to be explicitly |
| 2513 | // resolved. |
| 2514 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2515 | "Value of: n\n" |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2516 | "Expected: is > 10\n" |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 2517 | " Actual: 5" + OfType("unsigned short")); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2518 | n = 0; |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 2519 | EXPECT_NONFATAL_FAILURE( |
| 2520 | EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))), |
| 2521 | "Value of: n\n" |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2522 | "Expected: (is <= 7) and (is >= 5)\n" |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 2523 | " Actual: 0" + OfType("unsigned short")); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument |
| 2527 | // has a reference type. |
| 2528 | TEST(MatcherAssertionTest, WorksForByRefArguments) { |
| 2529 | // We use a static variable here as EXPECT_FATAL_FAILURE() cannot |
| 2530 | // reference auto variables. |
| 2531 | static int n; |
| 2532 | n = 0; |
| 2533 | EXPECT_THAT(n, AllOf(Le(7), Ref(n))); |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 2534 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2535 | "Value of: n\n" |
| 2536 | "Expected: does not reference the variable @"); |
| 2537 | // Tests the "Actual" part. |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 2538 | EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 2539 | "Actual: 0" + OfType("int") + ", which is located @"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 2542 | #if !GTEST_OS_SYMBIAN |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2543 | // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is |
| 2544 | // monomorphic. |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 2545 | |
| 2546 | // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's |
| 2547 | // Symbian compiler: it tries to compile |
| 2548 | // template<T, U> class MatcherCastImpl { ... |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 2549 | // virtual bool MatchAndExplain(T x, ...) const { |
| 2550 | // return source_matcher_.MatchAndExplain(static_cast<U>(x), ...); |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 2551 | // with U == string and T == const char* |
| 2552 | // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) |
| 2553 | // the compiler silently crashes with no output. |
| 2554 | // If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x) |
| 2555 | // the code compiles but the converted string is bogus. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2556 | TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { |
| 2557 | Matcher<const char*> starts_with_he = StartsWith("he"); |
| 2558 | ASSERT_THAT("hello", starts_with_he); |
| 2559 | |
| 2560 | Matcher<const string&> ends_with_ok = EndsWith("ok"); |
| 2561 | ASSERT_THAT("book", ends_with_ok); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 2562 | const string bad = "bad"; |
| 2563 | EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok), |
| 2564 | "Value of: bad\n" |
| 2565 | "Expected: ends with \"ok\"\n" |
| 2566 | " Actual: \"bad\""); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2567 | Matcher<int> is_greater_than_5 = Gt(5); |
| 2568 | EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), |
| 2569 | "Value of: 5\n" |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2570 | "Expected: is > 5\n" |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 2571 | " Actual: 5" + OfType("int")); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2572 | } |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 2573 | #endif // !GTEST_OS_SYMBIAN |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2574 | |
| 2575 | // Tests floating-point matchers. |
| 2576 | template <typename RawType> |
| 2577 | class FloatingPointTest : public testing::Test { |
| 2578 | protected: |
| 2579 | typedef typename testing::internal::FloatingPoint<RawType> Floating; |
| 2580 | typedef typename Floating::Bits Bits; |
| 2581 | |
| 2582 | virtual void SetUp() { |
| 2583 | const size_t max_ulps = Floating::kMaxUlps; |
| 2584 | |
| 2585 | // The bits that represent 0.0. |
| 2586 | const Bits zero_bits = Floating(0).bits(); |
| 2587 | |
| 2588 | // Makes some numbers close to 0.0. |
| 2589 | close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2); |
| 2590 | close_to_negative_zero_ = -Floating::ReinterpretBits( |
| 2591 | zero_bits + max_ulps - max_ulps/2); |
| 2592 | further_from_negative_zero_ = -Floating::ReinterpretBits( |
| 2593 | zero_bits + max_ulps + 1 - max_ulps/2); |
| 2594 | |
| 2595 | // The bits that represent 1.0. |
| 2596 | const Bits one_bits = Floating(1).bits(); |
| 2597 | |
| 2598 | // Makes some numbers close to 1.0. |
| 2599 | close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps); |
| 2600 | further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1); |
| 2601 | |
| 2602 | // +infinity. |
| 2603 | infinity_ = Floating::Infinity(); |
| 2604 | |
| 2605 | // The bits that represent +infinity. |
| 2606 | const Bits infinity_bits = Floating(infinity_).bits(); |
| 2607 | |
| 2608 | // Makes some numbers close to infinity. |
| 2609 | close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps); |
| 2610 | further_from_infinity_ = Floating::ReinterpretBits( |
| 2611 | infinity_bits - max_ulps - 1); |
| 2612 | |
| 2613 | // Makes some NAN's. |
| 2614 | nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1); |
| 2615 | nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200); |
| 2616 | } |
| 2617 | |
| 2618 | void TestSize() { |
| 2619 | EXPECT_EQ(sizeof(RawType), sizeof(Bits)); |
| 2620 | } |
| 2621 | |
| 2622 | // A battery of tests for FloatingEqMatcher::Matches. |
| 2623 | // matcher_maker is a pointer to a function which creates a FloatingEqMatcher. |
| 2624 | void TestMatches( |
| 2625 | testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) { |
| 2626 | Matcher<RawType> m1 = matcher_maker(0.0); |
| 2627 | EXPECT_TRUE(m1.Matches(-0.0)); |
| 2628 | EXPECT_TRUE(m1.Matches(close_to_positive_zero_)); |
| 2629 | EXPECT_TRUE(m1.Matches(close_to_negative_zero_)); |
| 2630 | EXPECT_FALSE(m1.Matches(1.0)); |
| 2631 | |
| 2632 | Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_); |
| 2633 | EXPECT_FALSE(m2.Matches(further_from_negative_zero_)); |
| 2634 | |
| 2635 | Matcher<RawType> m3 = matcher_maker(1.0); |
| 2636 | EXPECT_TRUE(m3.Matches(close_to_one_)); |
| 2637 | EXPECT_FALSE(m3.Matches(further_from_one_)); |
| 2638 | |
| 2639 | // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above. |
| 2640 | EXPECT_FALSE(m3.Matches(0.0)); |
| 2641 | |
| 2642 | Matcher<RawType> m4 = matcher_maker(-infinity_); |
| 2643 | EXPECT_TRUE(m4.Matches(-close_to_infinity_)); |
| 2644 | |
| 2645 | Matcher<RawType> m5 = matcher_maker(infinity_); |
| 2646 | EXPECT_TRUE(m5.Matches(close_to_infinity_)); |
| 2647 | |
| 2648 | // This is interesting as the representations of infinity_ and nan1_ |
| 2649 | // are only 1 DLP apart. |
| 2650 | EXPECT_FALSE(m5.Matches(nan1_)); |
| 2651 | |
| 2652 | // matcher_maker can produce a Matcher<const RawType&>, which is needed in |
| 2653 | // some cases. |
| 2654 | Matcher<const RawType&> m6 = matcher_maker(0.0); |
| 2655 | EXPECT_TRUE(m6.Matches(-0.0)); |
| 2656 | EXPECT_TRUE(m6.Matches(close_to_positive_zero_)); |
| 2657 | EXPECT_FALSE(m6.Matches(1.0)); |
| 2658 | |
| 2659 | // matcher_maker can produce a Matcher<RawType&>, which is needed in some |
| 2660 | // cases. |
| 2661 | Matcher<RawType&> m7 = matcher_maker(0.0); |
| 2662 | RawType x = 0.0; |
| 2663 | EXPECT_TRUE(m7.Matches(x)); |
| 2664 | x = 0.01f; |
| 2665 | EXPECT_FALSE(m7.Matches(x)); |
| 2666 | } |
| 2667 | |
| 2668 | // Pre-calculated numbers to be used by the tests. |
| 2669 | |
| 2670 | static RawType close_to_positive_zero_; |
| 2671 | static RawType close_to_negative_zero_; |
| 2672 | static RawType further_from_negative_zero_; |
| 2673 | |
| 2674 | static RawType close_to_one_; |
| 2675 | static RawType further_from_one_; |
| 2676 | |
| 2677 | static RawType infinity_; |
| 2678 | static RawType close_to_infinity_; |
| 2679 | static RawType further_from_infinity_; |
| 2680 | |
| 2681 | static RawType nan1_; |
| 2682 | static RawType nan2_; |
| 2683 | }; |
| 2684 | |
| 2685 | template <typename RawType> |
| 2686 | RawType FloatingPointTest<RawType>::close_to_positive_zero_; |
| 2687 | |
| 2688 | template <typename RawType> |
| 2689 | RawType FloatingPointTest<RawType>::close_to_negative_zero_; |
| 2690 | |
| 2691 | template <typename RawType> |
| 2692 | RawType FloatingPointTest<RawType>::further_from_negative_zero_; |
| 2693 | |
| 2694 | template <typename RawType> |
| 2695 | RawType FloatingPointTest<RawType>::close_to_one_; |
| 2696 | |
| 2697 | template <typename RawType> |
| 2698 | RawType FloatingPointTest<RawType>::further_from_one_; |
| 2699 | |
| 2700 | template <typename RawType> |
| 2701 | RawType FloatingPointTest<RawType>::infinity_; |
| 2702 | |
| 2703 | template <typename RawType> |
| 2704 | RawType FloatingPointTest<RawType>::close_to_infinity_; |
| 2705 | |
| 2706 | template <typename RawType> |
| 2707 | RawType FloatingPointTest<RawType>::further_from_infinity_; |
| 2708 | |
| 2709 | template <typename RawType> |
| 2710 | RawType FloatingPointTest<RawType>::nan1_; |
| 2711 | |
| 2712 | template <typename RawType> |
| 2713 | RawType FloatingPointTest<RawType>::nan2_; |
| 2714 | |
| 2715 | // Instantiate FloatingPointTest for testing floats. |
| 2716 | typedef FloatingPointTest<float> FloatTest; |
| 2717 | |
| 2718 | TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { |
| 2719 | TestMatches(&FloatEq); |
| 2720 | } |
| 2721 | |
| 2722 | TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) { |
| 2723 | TestMatches(&NanSensitiveFloatEq); |
| 2724 | } |
| 2725 | |
| 2726 | TEST_F(FloatTest, FloatEqCannotMatchNaN) { |
| 2727 | // FloatEq never matches NaN. |
| 2728 | Matcher<float> m = FloatEq(nan1_); |
| 2729 | EXPECT_FALSE(m.Matches(nan1_)); |
| 2730 | EXPECT_FALSE(m.Matches(nan2_)); |
| 2731 | EXPECT_FALSE(m.Matches(1.0)); |
| 2732 | } |
| 2733 | |
| 2734 | TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) { |
| 2735 | // NanSensitiveFloatEq will match NaN. |
| 2736 | Matcher<float> m = NanSensitiveFloatEq(nan1_); |
| 2737 | EXPECT_TRUE(m.Matches(nan1_)); |
| 2738 | EXPECT_TRUE(m.Matches(nan2_)); |
| 2739 | EXPECT_FALSE(m.Matches(1.0)); |
| 2740 | } |
| 2741 | |
| 2742 | TEST_F(FloatTest, FloatEqCanDescribeSelf) { |
| 2743 | Matcher<float> m1 = FloatEq(2.0f); |
| 2744 | EXPECT_EQ("is approximately 2", Describe(m1)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2745 | EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2746 | |
| 2747 | Matcher<float> m2 = FloatEq(0.5f); |
| 2748 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2749 | EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2750 | |
| 2751 | Matcher<float> m3 = FloatEq(nan1_); |
| 2752 | EXPECT_EQ("never matches", Describe(m3)); |
| 2753 | EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 2754 | } |
| 2755 | |
| 2756 | TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { |
| 2757 | Matcher<float> m1 = NanSensitiveFloatEq(2.0f); |
| 2758 | EXPECT_EQ("is approximately 2", Describe(m1)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2759 | EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2760 | |
| 2761 | Matcher<float> m2 = NanSensitiveFloatEq(0.5f); |
| 2762 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2763 | EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2764 | |
| 2765 | Matcher<float> m3 = NanSensitiveFloatEq(nan1_); |
| 2766 | EXPECT_EQ("is NaN", Describe(m3)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2767 | EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | // Instantiate FloatingPointTest for testing doubles. |
| 2771 | typedef FloatingPointTest<double> DoubleTest; |
| 2772 | |
| 2773 | TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { |
| 2774 | TestMatches(&DoubleEq); |
| 2775 | } |
| 2776 | |
| 2777 | TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { |
| 2778 | TestMatches(&NanSensitiveDoubleEq); |
| 2779 | } |
| 2780 | |
| 2781 | TEST_F(DoubleTest, DoubleEqCannotMatchNaN) { |
| 2782 | // DoubleEq never matches NaN. |
| 2783 | Matcher<double> m = DoubleEq(nan1_); |
| 2784 | EXPECT_FALSE(m.Matches(nan1_)); |
| 2785 | EXPECT_FALSE(m.Matches(nan2_)); |
| 2786 | EXPECT_FALSE(m.Matches(1.0)); |
| 2787 | } |
| 2788 | |
| 2789 | TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) { |
| 2790 | // NanSensitiveDoubleEq will match NaN. |
| 2791 | Matcher<double> m = NanSensitiveDoubleEq(nan1_); |
| 2792 | EXPECT_TRUE(m.Matches(nan1_)); |
| 2793 | EXPECT_TRUE(m.Matches(nan2_)); |
| 2794 | EXPECT_FALSE(m.Matches(1.0)); |
| 2795 | } |
| 2796 | |
| 2797 | TEST_F(DoubleTest, DoubleEqCanDescribeSelf) { |
| 2798 | Matcher<double> m1 = DoubleEq(2.0); |
| 2799 | EXPECT_EQ("is approximately 2", Describe(m1)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2800 | EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2801 | |
| 2802 | Matcher<double> m2 = DoubleEq(0.5); |
| 2803 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2804 | EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2805 | |
| 2806 | Matcher<double> m3 = DoubleEq(nan1_); |
| 2807 | EXPECT_EQ("never matches", Describe(m3)); |
| 2808 | EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 2809 | } |
| 2810 | |
| 2811 | TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { |
| 2812 | Matcher<double> m1 = NanSensitiveDoubleEq(2.0); |
| 2813 | EXPECT_EQ("is approximately 2", Describe(m1)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2814 | EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2815 | |
| 2816 | Matcher<double> m2 = NanSensitiveDoubleEq(0.5); |
| 2817 | EXPECT_EQ("is approximately 0.5", Describe(m2)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2818 | EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2819 | |
| 2820 | Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); |
| 2821 | EXPECT_EQ("is NaN", Describe(m3)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2822 | EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | TEST(PointeeTest, RawPointer) { |
| 2826 | const Matcher<int*> m = Pointee(Ge(0)); |
| 2827 | |
| 2828 | int n = 1; |
| 2829 | EXPECT_TRUE(m.Matches(&n)); |
| 2830 | n = -1; |
| 2831 | EXPECT_FALSE(m.Matches(&n)); |
| 2832 | EXPECT_FALSE(m.Matches(NULL)); |
| 2833 | } |
| 2834 | |
| 2835 | TEST(PointeeTest, RawPointerToConst) { |
| 2836 | const Matcher<const double*> m = Pointee(Ge(0)); |
| 2837 | |
| 2838 | double x = 1; |
| 2839 | EXPECT_TRUE(m.Matches(&x)); |
| 2840 | x = -1; |
| 2841 | EXPECT_FALSE(m.Matches(&x)); |
| 2842 | EXPECT_FALSE(m.Matches(NULL)); |
| 2843 | } |
| 2844 | |
| 2845 | TEST(PointeeTest, ReferenceToConstRawPointer) { |
| 2846 | const Matcher<int* const &> m = Pointee(Ge(0)); |
| 2847 | |
| 2848 | int n = 1; |
| 2849 | EXPECT_TRUE(m.Matches(&n)); |
| 2850 | n = -1; |
| 2851 | EXPECT_FALSE(m.Matches(&n)); |
| 2852 | EXPECT_FALSE(m.Matches(NULL)); |
| 2853 | } |
| 2854 | |
| 2855 | TEST(PointeeTest, ReferenceToNonConstRawPointer) { |
| 2856 | const Matcher<double* &> m = Pointee(Ge(0)); |
| 2857 | |
| 2858 | double x = 1.0; |
| 2859 | double* p = &x; |
| 2860 | EXPECT_TRUE(m.Matches(p)); |
| 2861 | x = -1; |
| 2862 | EXPECT_FALSE(m.Matches(p)); |
| 2863 | p = NULL; |
| 2864 | EXPECT_FALSE(m.Matches(p)); |
| 2865 | } |
| 2866 | |
vladlosev | ada2347 | 2012-08-14 15:38:49 +0000 | [diff] [blame] | 2867 | // Minimal const-propagating pointer. |
| 2868 | template <typename T> |
| 2869 | class ConstPropagatingPtr { |
| 2870 | public: |
| 2871 | typedef T element_type; |
| 2872 | |
| 2873 | ConstPropagatingPtr() : val_() {} |
| 2874 | explicit ConstPropagatingPtr(T* t) : val_(t) {} |
| 2875 | ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {} |
| 2876 | |
| 2877 | T* get() { return val_; } |
| 2878 | T& operator*() { return *val_; } |
| 2879 | // Most smart pointers return non-const T* and T& from the next methods. |
| 2880 | const T* get() const { return val_; } |
| 2881 | const T& operator*() const { return *val_; } |
| 2882 | |
| 2883 | private: |
| 2884 | T* val_; |
| 2885 | }; |
| 2886 | |
| 2887 | TEST(PointeeTest, WorksWithConstPropagatingPointers) { |
| 2888 | const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5)); |
| 2889 | int three = 3; |
| 2890 | const ConstPropagatingPtr<int> co(&three); |
| 2891 | ConstPropagatingPtr<int> o(&three); |
| 2892 | EXPECT_TRUE(m.Matches(o)); |
| 2893 | EXPECT_TRUE(m.Matches(co)); |
| 2894 | *o = 6; |
| 2895 | EXPECT_FALSE(m.Matches(o)); |
| 2896 | EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>())); |
| 2897 | } |
| 2898 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2899 | TEST(PointeeTest, NeverMatchesNull) { |
| 2900 | const Matcher<const char*> m = Pointee(_); |
| 2901 | EXPECT_FALSE(m.Matches(NULL)); |
| 2902 | } |
| 2903 | |
| 2904 | // Tests that we can write Pointee(value) instead of Pointee(Eq(value)). |
| 2905 | TEST(PointeeTest, MatchesAgainstAValue) { |
| 2906 | const Matcher<int*> m = Pointee(5); |
| 2907 | |
| 2908 | int n = 5; |
| 2909 | EXPECT_TRUE(m.Matches(&n)); |
| 2910 | n = -1; |
| 2911 | EXPECT_FALSE(m.Matches(&n)); |
| 2912 | EXPECT_FALSE(m.Matches(NULL)); |
| 2913 | } |
| 2914 | |
| 2915 | TEST(PointeeTest, CanDescribeSelf) { |
| 2916 | const Matcher<int*> m = Pointee(Gt(3)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 2917 | EXPECT_EQ("points to a value that is > 3", Describe(m)); |
| 2918 | EXPECT_EQ("does not point to a value that is > 3", |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2919 | DescribeNegation(m)); |
| 2920 | } |
| 2921 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2922 | TEST(PointeeTest, CanExplainMatchResult) { |
| 2923 | const Matcher<const string*> m = Pointee(StartsWith("Hi")); |
| 2924 | |
| 2925 | EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); |
| 2926 | |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 2927 | const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT |
| 2928 | long n = 3; // NOLINT |
| 2929 | EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1", |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 2930 | Explain(m2, &n)); |
| 2931 | } |
| 2932 | |
| 2933 | TEST(PointeeTest, AlwaysExplainsPointee) { |
| 2934 | const Matcher<int*> m = Pointee(0); |
| 2935 | int n = 42; |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 2936 | EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | // An uncopyable class. |
| 2940 | class Uncopyable { |
| 2941 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2942 | explicit Uncopyable(int a_value) : value_(a_value) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2943 | |
| 2944 | int value() const { return value_; } |
| 2945 | private: |
| 2946 | const int value_; |
| 2947 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable); |
| 2948 | }; |
| 2949 | |
| 2950 | // Returns true iff x.value() is positive. |
| 2951 | bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; } |
| 2952 | |
| 2953 | // A user-defined struct for testing Field(). |
| 2954 | struct AStruct { |
| 2955 | AStruct() : x(0), y(1.0), z(5), p(NULL) {} |
| 2956 | AStruct(const AStruct& rhs) |
| 2957 | : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {} |
| 2958 | |
| 2959 | int x; // A non-const field. |
| 2960 | const double y; // A const field. |
| 2961 | Uncopyable z; // An uncopyable field. |
| 2962 | const char* p; // A pointer field. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2963 | |
| 2964 | private: |
| 2965 | GTEST_DISALLOW_ASSIGN_(AStruct); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2966 | }; |
| 2967 | |
| 2968 | // A derived struct for testing Field(). |
| 2969 | struct DerivedStruct : public AStruct { |
| 2970 | char ch; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 2971 | |
| 2972 | private: |
| 2973 | GTEST_DISALLOW_ASSIGN_(DerivedStruct); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2974 | }; |
| 2975 | |
| 2976 | // Tests that Field(&Foo::field, ...) works when field is non-const. |
| 2977 | TEST(FieldTest, WorksForNonConstField) { |
| 2978 | Matcher<AStruct> m = Field(&AStruct::x, Ge(0)); |
| 2979 | |
| 2980 | AStruct a; |
| 2981 | EXPECT_TRUE(m.Matches(a)); |
| 2982 | a.x = -1; |
| 2983 | EXPECT_FALSE(m.Matches(a)); |
| 2984 | } |
| 2985 | |
| 2986 | // Tests that Field(&Foo::field, ...) works when field is const. |
| 2987 | TEST(FieldTest, WorksForConstField) { |
| 2988 | AStruct a; |
| 2989 | |
| 2990 | Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0)); |
| 2991 | EXPECT_TRUE(m.Matches(a)); |
| 2992 | m = Field(&AStruct::y, Le(0.0)); |
| 2993 | EXPECT_FALSE(m.Matches(a)); |
| 2994 | } |
| 2995 | |
| 2996 | // Tests that Field(&Foo::field, ...) works when field is not copyable. |
| 2997 | TEST(FieldTest, WorksForUncopyableField) { |
| 2998 | AStruct a; |
| 2999 | |
| 3000 | Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive)); |
| 3001 | EXPECT_TRUE(m.Matches(a)); |
| 3002 | m = Field(&AStruct::z, Not(Truly(ValueIsPositive))); |
| 3003 | EXPECT_FALSE(m.Matches(a)); |
| 3004 | } |
| 3005 | |
| 3006 | // Tests that Field(&Foo::field, ...) works when field is a pointer. |
| 3007 | TEST(FieldTest, WorksForPointerField) { |
| 3008 | // Matching against NULL. |
| 3009 | Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL)); |
| 3010 | AStruct a; |
| 3011 | EXPECT_TRUE(m.Matches(a)); |
| 3012 | a.p = "hi"; |
| 3013 | EXPECT_FALSE(m.Matches(a)); |
| 3014 | |
| 3015 | // Matching a pointer that is not NULL. |
| 3016 | m = Field(&AStruct::p, StartsWith("hi")); |
| 3017 | a.p = "hill"; |
| 3018 | EXPECT_TRUE(m.Matches(a)); |
| 3019 | a.p = "hole"; |
| 3020 | EXPECT_FALSE(m.Matches(a)); |
| 3021 | } |
| 3022 | |
| 3023 | // Tests that Field() works when the object is passed by reference. |
| 3024 | TEST(FieldTest, WorksForByRefArgument) { |
| 3025 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 3026 | |
| 3027 | AStruct a; |
| 3028 | EXPECT_TRUE(m.Matches(a)); |
| 3029 | a.x = -1; |
| 3030 | EXPECT_FALSE(m.Matches(a)); |
| 3031 | } |
| 3032 | |
| 3033 | // Tests that Field(&Foo::field, ...) works when the argument's type |
| 3034 | // is a sub-type of Foo. |
| 3035 | TEST(FieldTest, WorksForArgumentOfSubType) { |
| 3036 | // Note that the matcher expects DerivedStruct but we say AStruct |
| 3037 | // inside Field(). |
| 3038 | Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0)); |
| 3039 | |
| 3040 | DerivedStruct d; |
| 3041 | EXPECT_TRUE(m.Matches(d)); |
| 3042 | d.x = -1; |
| 3043 | EXPECT_FALSE(m.Matches(d)); |
| 3044 | } |
| 3045 | |
| 3046 | // Tests that Field(&Foo::field, m) works when field's type and m's |
| 3047 | // argument type are compatible but not the same. |
| 3048 | TEST(FieldTest, WorksForCompatibleMatcherType) { |
| 3049 | // The field is an int, but the inner matcher expects a signed char. |
| 3050 | Matcher<const AStruct&> m = Field(&AStruct::x, |
| 3051 | Matcher<signed char>(Ge(0))); |
| 3052 | |
| 3053 | AStruct a; |
| 3054 | EXPECT_TRUE(m.Matches(a)); |
| 3055 | a.x = -1; |
| 3056 | EXPECT_FALSE(m.Matches(a)); |
| 3057 | } |
| 3058 | |
| 3059 | // Tests that Field() can describe itself. |
| 3060 | TEST(FieldTest, CanDescribeSelf) { |
| 3061 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 3062 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3063 | EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); |
| 3064 | EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | // Tests that Field() can explain the match result. |
| 3068 | TEST(FieldTest, CanExplainMatchResult) { |
| 3069 | Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
| 3070 | |
| 3071 | AStruct a; |
| 3072 | a.x = 1; |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3073 | EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3074 | |
| 3075 | m = Field(&AStruct::x, GreaterThan(0)); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3076 | EXPECT_EQ( |
| 3077 | "whose given field is 1" + OfType("int") + ", which is 1 more than 0", |
| 3078 | Explain(m, a)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | // Tests that Field() works when the argument is a pointer to const. |
| 3082 | TEST(FieldForPointerTest, WorksForPointerToConst) { |
| 3083 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3084 | |
| 3085 | AStruct a; |
| 3086 | EXPECT_TRUE(m.Matches(&a)); |
| 3087 | a.x = -1; |
| 3088 | EXPECT_FALSE(m.Matches(&a)); |
| 3089 | } |
| 3090 | |
| 3091 | // Tests that Field() works when the argument is a pointer to non-const. |
| 3092 | TEST(FieldForPointerTest, WorksForPointerToNonConst) { |
| 3093 | Matcher<AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3094 | |
| 3095 | AStruct a; |
| 3096 | EXPECT_TRUE(m.Matches(&a)); |
| 3097 | a.x = -1; |
| 3098 | EXPECT_FALSE(m.Matches(&a)); |
| 3099 | } |
| 3100 | |
zhanyong.wan | 6953a72 | 2010-01-13 05:15:07 +0000 | [diff] [blame] | 3101 | // Tests that Field() works when the argument is a reference to a const pointer. |
| 3102 | TEST(FieldForPointerTest, WorksForReferenceToConstPointer) { |
| 3103 | Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0)); |
| 3104 | |
| 3105 | AStruct a; |
| 3106 | EXPECT_TRUE(m.Matches(&a)); |
| 3107 | a.x = -1; |
| 3108 | EXPECT_FALSE(m.Matches(&a)); |
| 3109 | } |
| 3110 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3111 | // Tests that Field() does not match the NULL pointer. |
| 3112 | TEST(FieldForPointerTest, DoesNotMatchNull) { |
| 3113 | Matcher<const AStruct*> m = Field(&AStruct::x, _); |
| 3114 | EXPECT_FALSE(m.Matches(NULL)); |
| 3115 | } |
| 3116 | |
| 3117 | // Tests that Field(&Foo::field, ...) works when the argument's type |
| 3118 | // is a sub-type of const Foo*. |
| 3119 | TEST(FieldForPointerTest, WorksForArgumentOfSubType) { |
| 3120 | // Note that the matcher expects DerivedStruct but we say AStruct |
| 3121 | // inside Field(). |
| 3122 | Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3123 | |
| 3124 | DerivedStruct d; |
| 3125 | EXPECT_TRUE(m.Matches(&d)); |
| 3126 | d.x = -1; |
| 3127 | EXPECT_FALSE(m.Matches(&d)); |
| 3128 | } |
| 3129 | |
| 3130 | // Tests that Field() can describe itself when used to match a pointer. |
| 3131 | TEST(FieldForPointerTest, CanDescribeSelf) { |
| 3132 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3133 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3134 | EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); |
| 3135 | EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | // Tests that Field() can explain the result of matching a pointer. |
| 3139 | TEST(FieldForPointerTest, CanExplainMatchResult) { |
| 3140 | Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
| 3141 | |
| 3142 | AStruct a; |
| 3143 | a.x = 1; |
| 3144 | EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL))); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3145 | EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"), |
| 3146 | Explain(m, &a)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3147 | |
| 3148 | m = Field(&AStruct::x, GreaterThan(0)); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3149 | EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") + |
| 3150 | ", which is 1 more than 0", Explain(m, &a)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | // A user-defined class for testing Property(). |
| 3154 | class AClass { |
| 3155 | public: |
| 3156 | AClass() : n_(0) {} |
| 3157 | |
| 3158 | // A getter that returns a non-reference. |
| 3159 | int n() const { return n_; } |
| 3160 | |
| 3161 | void set_n(int new_n) { n_ = new_n; } |
| 3162 | |
| 3163 | // A getter that returns a reference to const. |
| 3164 | const string& s() const { return s_; } |
| 3165 | |
| 3166 | void set_s(const string& new_s) { s_ = new_s; } |
| 3167 | |
| 3168 | // A getter that returns a reference to non-const. |
| 3169 | double& x() const { return x_; } |
| 3170 | private: |
| 3171 | int n_; |
| 3172 | string s_; |
| 3173 | |
| 3174 | static double x_; |
| 3175 | }; |
| 3176 | |
| 3177 | double AClass::x_ = 0.0; |
| 3178 | |
| 3179 | // A derived class for testing Property(). |
| 3180 | class DerivedClass : public AClass { |
| 3181 | private: |
| 3182 | int k_; |
| 3183 | }; |
| 3184 | |
| 3185 | // Tests that Property(&Foo::property, ...) works when property() |
| 3186 | // returns a non-reference. |
| 3187 | TEST(PropertyTest, WorksForNonReferenceProperty) { |
| 3188 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 3189 | |
| 3190 | AClass a; |
| 3191 | a.set_n(1); |
| 3192 | EXPECT_TRUE(m.Matches(a)); |
| 3193 | |
| 3194 | a.set_n(-1); |
| 3195 | EXPECT_FALSE(m.Matches(a)); |
| 3196 | } |
| 3197 | |
| 3198 | // Tests that Property(&Foo::property, ...) works when property() |
| 3199 | // returns a reference to const. |
| 3200 | TEST(PropertyTest, WorksForReferenceToConstProperty) { |
| 3201 | Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi")); |
| 3202 | |
| 3203 | AClass a; |
| 3204 | a.set_s("hill"); |
| 3205 | EXPECT_TRUE(m.Matches(a)); |
| 3206 | |
| 3207 | a.set_s("hole"); |
| 3208 | EXPECT_FALSE(m.Matches(a)); |
| 3209 | } |
| 3210 | |
| 3211 | // Tests that Property(&Foo::property, ...) works when property() |
| 3212 | // returns a reference to non-const. |
| 3213 | TEST(PropertyTest, WorksForReferenceToNonConstProperty) { |
| 3214 | double x = 0.0; |
| 3215 | AClass a; |
| 3216 | |
| 3217 | Matcher<const AClass&> m = Property(&AClass::x, Ref(x)); |
| 3218 | EXPECT_FALSE(m.Matches(a)); |
| 3219 | |
| 3220 | m = Property(&AClass::x, Not(Ref(x))); |
| 3221 | EXPECT_TRUE(m.Matches(a)); |
| 3222 | } |
| 3223 | |
| 3224 | // Tests that Property(&Foo::property, ...) works when the argument is |
| 3225 | // passed by value. |
| 3226 | TEST(PropertyTest, WorksForByValueArgument) { |
| 3227 | Matcher<AClass> m = Property(&AClass::s, StartsWith("hi")); |
| 3228 | |
| 3229 | AClass a; |
| 3230 | a.set_s("hill"); |
| 3231 | EXPECT_TRUE(m.Matches(a)); |
| 3232 | |
| 3233 | a.set_s("hole"); |
| 3234 | EXPECT_FALSE(m.Matches(a)); |
| 3235 | } |
| 3236 | |
| 3237 | // Tests that Property(&Foo::property, ...) works when the argument's |
| 3238 | // type is a sub-type of Foo. |
| 3239 | TEST(PropertyTest, WorksForArgumentOfSubType) { |
| 3240 | // The matcher expects a DerivedClass, but inside the Property() we |
| 3241 | // say AClass. |
| 3242 | Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0)); |
| 3243 | |
| 3244 | DerivedClass d; |
| 3245 | d.set_n(1); |
| 3246 | EXPECT_TRUE(m.Matches(d)); |
| 3247 | |
| 3248 | d.set_n(-1); |
| 3249 | EXPECT_FALSE(m.Matches(d)); |
| 3250 | } |
| 3251 | |
| 3252 | // Tests that Property(&Foo::property, m) works when property()'s type |
| 3253 | // and m's argument type are compatible but different. |
| 3254 | TEST(PropertyTest, WorksForCompatibleMatcherType) { |
| 3255 | // n() returns an int but the inner matcher expects a signed char. |
| 3256 | Matcher<const AClass&> m = Property(&AClass::n, |
| 3257 | Matcher<signed char>(Ge(0))); |
| 3258 | |
| 3259 | AClass a; |
| 3260 | EXPECT_TRUE(m.Matches(a)); |
| 3261 | a.set_n(-1); |
| 3262 | EXPECT_FALSE(m.Matches(a)); |
| 3263 | } |
| 3264 | |
| 3265 | // Tests that Property() can describe itself. |
| 3266 | TEST(PropertyTest, CanDescribeSelf) { |
| 3267 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 3268 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3269 | EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); |
| 3270 | EXPECT_EQ("is an object whose given property isn't >= 0", |
| 3271 | DescribeNegation(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | // Tests that Property() can explain the match result. |
| 3275 | TEST(PropertyTest, CanExplainMatchResult) { |
| 3276 | Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
| 3277 | |
| 3278 | AClass a; |
| 3279 | a.set_n(1); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3280 | EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3281 | |
| 3282 | m = Property(&AClass::n, GreaterThan(0)); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3283 | EXPECT_EQ( |
| 3284 | "whose given property is 1" + OfType("int") + ", which is 1 more than 0", |
| 3285 | Explain(m, a)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | // Tests that Property() works when the argument is a pointer to const. |
| 3289 | TEST(PropertyForPointerTest, WorksForPointerToConst) { |
| 3290 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 3291 | |
| 3292 | AClass a; |
| 3293 | a.set_n(1); |
| 3294 | EXPECT_TRUE(m.Matches(&a)); |
| 3295 | |
| 3296 | a.set_n(-1); |
| 3297 | EXPECT_FALSE(m.Matches(&a)); |
| 3298 | } |
| 3299 | |
| 3300 | // Tests that Property() works when the argument is a pointer to non-const. |
| 3301 | TEST(PropertyForPointerTest, WorksForPointerToNonConst) { |
| 3302 | Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi")); |
| 3303 | |
| 3304 | AClass a; |
| 3305 | a.set_s("hill"); |
| 3306 | EXPECT_TRUE(m.Matches(&a)); |
| 3307 | |
| 3308 | a.set_s("hole"); |
| 3309 | EXPECT_FALSE(m.Matches(&a)); |
| 3310 | } |
| 3311 | |
zhanyong.wan | 6953a72 | 2010-01-13 05:15:07 +0000 | [diff] [blame] | 3312 | // Tests that Property() works when the argument is a reference to a |
| 3313 | // const pointer. |
| 3314 | TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) { |
| 3315 | Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi")); |
| 3316 | |
| 3317 | AClass a; |
| 3318 | a.set_s("hill"); |
| 3319 | EXPECT_TRUE(m.Matches(&a)); |
| 3320 | |
| 3321 | a.set_s("hole"); |
| 3322 | EXPECT_FALSE(m.Matches(&a)); |
| 3323 | } |
| 3324 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3325 | // Tests that Property() does not match the NULL pointer. |
| 3326 | TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) { |
| 3327 | Matcher<const AClass*> m = Property(&AClass::x, _); |
| 3328 | EXPECT_FALSE(m.Matches(NULL)); |
| 3329 | } |
| 3330 | |
| 3331 | // Tests that Property(&Foo::property, ...) works when the argument's |
| 3332 | // type is a sub-type of const Foo*. |
| 3333 | TEST(PropertyForPointerTest, WorksForArgumentOfSubType) { |
| 3334 | // The matcher expects a DerivedClass, but inside the Property() we |
| 3335 | // say AClass. |
| 3336 | Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0)); |
| 3337 | |
| 3338 | DerivedClass d; |
| 3339 | d.set_n(1); |
| 3340 | EXPECT_TRUE(m.Matches(&d)); |
| 3341 | |
| 3342 | d.set_n(-1); |
| 3343 | EXPECT_FALSE(m.Matches(&d)); |
| 3344 | } |
| 3345 | |
| 3346 | // Tests that Property() can describe itself when used to match a pointer. |
| 3347 | TEST(PropertyForPointerTest, CanDescribeSelf) { |
| 3348 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 3349 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3350 | EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); |
| 3351 | EXPECT_EQ("is an object whose given property isn't >= 0", |
| 3352 | DescribeNegation(m)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | // Tests that Property() can explain the result of matching a pointer. |
| 3356 | TEST(PropertyForPointerTest, CanExplainMatchResult) { |
| 3357 | Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
| 3358 | |
| 3359 | AClass a; |
| 3360 | a.set_n(1); |
| 3361 | EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL))); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3362 | EXPECT_EQ( |
| 3363 | "which points to an object whose given property is 1" + OfType("int"), |
| 3364 | Explain(m, &a)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3365 | |
| 3366 | m = Property(&AClass::n, GreaterThan(0)); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3367 | EXPECT_EQ("which points to an object whose given property is 1" + |
| 3368 | OfType("int") + ", which is 1 more than 0", |
| 3369 | Explain(m, &a)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | // Tests ResultOf. |
| 3373 | |
| 3374 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 3375 | // function pointer. |
| 3376 | string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; } |
| 3377 | |
| 3378 | TEST(ResultOfTest, WorksForFunctionPointers) { |
| 3379 | Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo"))); |
| 3380 | |
| 3381 | EXPECT_TRUE(matcher.Matches(1)); |
| 3382 | EXPECT_FALSE(matcher.Matches(2)); |
| 3383 | } |
| 3384 | |
| 3385 | // Tests that ResultOf() can describe itself. |
| 3386 | TEST(ResultOfTest, CanDescribeItself) { |
| 3387 | Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo")); |
| 3388 | |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 3389 | EXPECT_EQ("is mapped by the given callable to a value that " |
| 3390 | "is equal to \"foo\"", Describe(matcher)); |
| 3391 | EXPECT_EQ("is mapped by the given callable to a value that " |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3392 | "isn't equal to \"foo\"", DescribeNegation(matcher)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | // Tests that ResultOf() can explain the match result. |
| 3396 | int IntFunction(int input) { return input == 42 ? 80 : 90; } |
| 3397 | |
| 3398 | TEST(ResultOfTest, CanExplainMatchResult) { |
| 3399 | Matcher<int> matcher = ResultOf(&IntFunction, Ge(85)); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3400 | EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"), |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 3401 | Explain(matcher, 36)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3402 | |
| 3403 | matcher = ResultOf(&IntFunction, GreaterThan(85)); |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3404 | EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") + |
| 3405 | ", which is 5 more than 85", Explain(matcher, 36)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 3409 | // returns a non-reference. |
| 3410 | TEST(ResultOfTest, WorksForNonReferenceResults) { |
| 3411 | Matcher<int> matcher = ResultOf(&IntFunction, Eq(80)); |
| 3412 | |
| 3413 | EXPECT_TRUE(matcher.Matches(42)); |
| 3414 | EXPECT_FALSE(matcher.Matches(36)); |
| 3415 | } |
| 3416 | |
| 3417 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 3418 | // returns a reference to non-const. |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3419 | double& DoubleFunction(double& input) { return input; } // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3420 | |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3421 | Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3422 | return obj; |
| 3423 | } |
| 3424 | |
| 3425 | TEST(ResultOfTest, WorksForReferenceToNonConstResults) { |
| 3426 | double x = 3.14; |
| 3427 | double x2 = x; |
| 3428 | Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x)); |
| 3429 | |
| 3430 | EXPECT_TRUE(matcher.Matches(x)); |
| 3431 | EXPECT_FALSE(matcher.Matches(x2)); |
| 3432 | |
| 3433 | // Test that ResultOf works with uncopyable objects |
| 3434 | Uncopyable obj(0); |
| 3435 | Uncopyable obj2(0); |
| 3436 | Matcher<Uncopyable&> matcher2 = |
| 3437 | ResultOf(&RefUncopyableFunction, Ref(obj)); |
| 3438 | |
| 3439 | EXPECT_TRUE(matcher2.Matches(obj)); |
| 3440 | EXPECT_FALSE(matcher2.Matches(obj2)); |
| 3441 | } |
| 3442 | |
| 3443 | // Tests that ResultOf(f, ...) compiles and works as expected when f(x) |
| 3444 | // returns a reference to const. |
| 3445 | const string& StringFunction(const string& input) { return input; } |
| 3446 | |
| 3447 | TEST(ResultOfTest, WorksForReferenceToConstResults) { |
| 3448 | string s = "foo"; |
| 3449 | string s2 = s; |
| 3450 | Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s)); |
| 3451 | |
| 3452 | EXPECT_TRUE(matcher.Matches(s)); |
| 3453 | EXPECT_FALSE(matcher.Matches(s2)); |
| 3454 | } |
| 3455 | |
| 3456 | // Tests that ResultOf(f, m) works when f(x) and m's |
| 3457 | // argument types are compatible but different. |
| 3458 | TEST(ResultOfTest, WorksForCompatibleMatcherTypes) { |
| 3459 | // IntFunction() returns int but the inner matcher expects a signed char. |
| 3460 | Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85))); |
| 3461 | |
| 3462 | EXPECT_TRUE(matcher.Matches(36)); |
| 3463 | EXPECT_FALSE(matcher.Matches(42)); |
| 3464 | } |
| 3465 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3466 | // Tests that the program aborts when ResultOf is passed |
| 3467 | // a NULL function pointer. |
| 3468 | TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) { |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 3469 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 3470 | ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3471 | "NULL function pointer is passed into ResultOf\\(\\)\\."); |
| 3472 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3473 | |
| 3474 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 3475 | // function reference. |
| 3476 | TEST(ResultOfTest, WorksForFunctionReferences) { |
| 3477 | Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo")); |
| 3478 | EXPECT_TRUE(matcher.Matches(1)); |
| 3479 | EXPECT_FALSE(matcher.Matches(2)); |
| 3480 | } |
| 3481 | |
| 3482 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 3483 | // function object. |
| 3484 | struct Functor : public ::std::unary_function<int, string> { |
| 3485 | result_type operator()(argument_type input) const { |
| 3486 | return IntToStringFunction(input); |
| 3487 | } |
| 3488 | }; |
| 3489 | |
| 3490 | TEST(ResultOfTest, WorksForFunctors) { |
| 3491 | Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo"))); |
| 3492 | |
| 3493 | EXPECT_TRUE(matcher.Matches(1)); |
| 3494 | EXPECT_FALSE(matcher.Matches(2)); |
| 3495 | } |
| 3496 | |
| 3497 | // Tests that ResultOf(f, ...) compiles and works as expected when f is a |
| 3498 | // functor with more then one operator() defined. ResultOf() must work |
| 3499 | // for each defined operator(). |
| 3500 | struct PolymorphicFunctor { |
| 3501 | typedef int result_type; |
| 3502 | int operator()(int n) { return n; } |
| 3503 | int operator()(const char* s) { return static_cast<int>(strlen(s)); } |
| 3504 | }; |
| 3505 | |
| 3506 | TEST(ResultOfTest, WorksForPolymorphicFunctors) { |
| 3507 | Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5)); |
| 3508 | |
| 3509 | EXPECT_TRUE(matcher_int.Matches(10)); |
| 3510 | EXPECT_FALSE(matcher_int.Matches(2)); |
| 3511 | |
| 3512 | Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5)); |
| 3513 | |
| 3514 | EXPECT_TRUE(matcher_string.Matches("long string")); |
| 3515 | EXPECT_FALSE(matcher_string.Matches("shrt")); |
| 3516 | } |
| 3517 | |
| 3518 | const int* ReferencingFunction(const int& n) { return &n; } |
| 3519 | |
| 3520 | struct ReferencingFunctor { |
| 3521 | typedef const int* result_type; |
| 3522 | result_type operator()(const int& n) { return &n; } |
| 3523 | }; |
| 3524 | |
| 3525 | TEST(ResultOfTest, WorksForReferencingCallables) { |
| 3526 | const int n = 1; |
| 3527 | const int n2 = 1; |
| 3528 | Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n)); |
| 3529 | EXPECT_TRUE(matcher2.Matches(n)); |
| 3530 | EXPECT_FALSE(matcher2.Matches(n2)); |
| 3531 | |
| 3532 | Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n)); |
| 3533 | EXPECT_TRUE(matcher3.Matches(n)); |
| 3534 | EXPECT_FALSE(matcher3.Matches(n2)); |
| 3535 | } |
| 3536 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3537 | class DivisibleByImpl { |
| 3538 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 3539 | explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3540 | |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 3541 | // For testing using ExplainMatchResultTo() with polymorphic matchers. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3542 | template <typename T> |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 3543 | bool MatchAndExplain(const T& n, MatchResultListener* listener) const { |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3544 | *listener << "which is " << (n % divider_) << " modulo " |
zhanyong.wan | db22c22 | 2010-01-28 21:52:29 +0000 | [diff] [blame] | 3545 | << divider_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3546 | return (n % divider_) == 0; |
| 3547 | } |
| 3548 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3549 | void DescribeTo(ostream* os) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3550 | *os << "is divisible by " << divider_; |
| 3551 | } |
| 3552 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3553 | void DescribeNegationTo(ostream* os) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3554 | *os << "is not divisible by " << divider_; |
| 3555 | } |
| 3556 | |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 3557 | void set_divider(int a_divider) { divider_ = a_divider; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3558 | int divider() const { return divider_; } |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 3559 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3560 | private: |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 3561 | int divider_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3562 | }; |
| 3563 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3564 | PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { |
| 3565 | return MakePolymorphicMatcher(DivisibleByImpl(n)); |
| 3566 | } |
| 3567 | |
| 3568 | // Tests that when AllOf() fails, only the first failing matcher is |
| 3569 | // asked to explain why. |
| 3570 | TEST(ExplainMatchResultTest, AllOf_False_False) { |
| 3571 | const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3572 | EXPECT_EQ("which is 1 modulo 4", Explain(m, 5)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | // Tests that when AllOf() fails, only the first failing matcher is |
| 3576 | // asked to explain why. |
| 3577 | TEST(ExplainMatchResultTest, AllOf_False_True) { |
| 3578 | const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3579 | EXPECT_EQ("which is 2 modulo 4", Explain(m, 6)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | // Tests that when AllOf() fails, only the first failing matcher is |
| 3583 | // asked to explain why. |
| 3584 | TEST(ExplainMatchResultTest, AllOf_True_False) { |
| 3585 | const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3586 | EXPECT_EQ("which is 2 modulo 3", Explain(m, 5)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | // Tests that when AllOf() succeeds, all matchers are asked to explain |
| 3590 | // why. |
| 3591 | TEST(ExplainMatchResultTest, AllOf_True_True) { |
| 3592 | const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3593 | EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | TEST(ExplainMatchResultTest, AllOf_True_True_2) { |
| 3597 | const Matcher<int> m = AllOf(Ge(2), Le(3)); |
| 3598 | EXPECT_EQ("", Explain(m, 2)); |
| 3599 | } |
| 3600 | |
| 3601 | TEST(ExplainmatcherResultTest, MonomorphicMatcher) { |
| 3602 | const Matcher<int> m = GreaterThan(5); |
zhanyong.wan | 676e8cc | 2010-03-16 20:01:51 +0000 | [diff] [blame] | 3603 | EXPECT_EQ("which is 1 more than 5", Explain(m, 6)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | // The following two tests verify that values without a public copy |
| 3607 | // ctor can be used as arguments to matchers like Eq(), Ge(), and etc |
| 3608 | // with the help of ByRef(). |
| 3609 | |
| 3610 | class NotCopyable { |
| 3611 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 3612 | explicit NotCopyable(int a_value) : value_(a_value) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 3613 | |
| 3614 | int value() const { return value_; } |
| 3615 | |
| 3616 | bool operator==(const NotCopyable& rhs) const { |
| 3617 | return value() == rhs.value(); |
| 3618 | } |
| 3619 | |
| 3620 | bool operator>=(const NotCopyable& rhs) const { |
| 3621 | return value() >= rhs.value(); |
| 3622 | } |
| 3623 | private: |
| 3624 | int value_; |
| 3625 | |
| 3626 | GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable); |
| 3627 | }; |
| 3628 | |
| 3629 | TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) { |
| 3630 | const NotCopyable const_value1(1); |
| 3631 | const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1)); |
| 3632 | |
| 3633 | const NotCopyable n1(1), n2(2); |
| 3634 | EXPECT_TRUE(m.Matches(n1)); |
| 3635 | EXPECT_FALSE(m.Matches(n2)); |
| 3636 | } |
| 3637 | |
| 3638 | TEST(ByRefTest, AllowsNotCopyableValueInMatchers) { |
| 3639 | NotCopyable value2(2); |
| 3640 | const Matcher<NotCopyable&> m = Ge(ByRef(value2)); |
| 3641 | |
| 3642 | NotCopyable n1(1), n2(2); |
| 3643 | EXPECT_FALSE(m.Matches(n1)); |
| 3644 | EXPECT_TRUE(m.Matches(n2)); |
| 3645 | } |
| 3646 | |
zhanyong.wan | 320814a | 2013-03-01 00:20:30 +0000 | [diff] [blame] | 3647 | TEST(IsEmptyTest, ImplementsIsEmpty) { |
| 3648 | vector<int> container; |
| 3649 | EXPECT_THAT(container, IsEmpty()); |
| 3650 | container.push_back(0); |
| 3651 | EXPECT_THAT(container, Not(IsEmpty())); |
| 3652 | container.push_back(1); |
| 3653 | EXPECT_THAT(container, Not(IsEmpty())); |
| 3654 | } |
| 3655 | |
| 3656 | TEST(IsEmptyTest, WorksWithString) { |
| 3657 | string text; |
| 3658 | EXPECT_THAT(text, IsEmpty()); |
| 3659 | text = "foo"; |
| 3660 | EXPECT_THAT(text, Not(IsEmpty())); |
| 3661 | text = string("\0", 1); |
| 3662 | EXPECT_THAT(text, Not(IsEmpty())); |
| 3663 | } |
| 3664 | |
| 3665 | TEST(IsEmptyTest, CanDescribeSelf) { |
| 3666 | Matcher<vector<int> > m = IsEmpty(); |
| 3667 | EXPECT_EQ("is empty", Describe(m)); |
| 3668 | EXPECT_EQ("isn't empty", DescribeNegation(m)); |
| 3669 | } |
| 3670 | |
| 3671 | TEST(IsEmptyTest, ExplainsResult) { |
| 3672 | Matcher<vector<int> > m = IsEmpty(); |
| 3673 | vector<int> container; |
| 3674 | EXPECT_EQ("", Explain(m, container)); |
| 3675 | container.push_back(0); |
| 3676 | EXPECT_EQ("whose size is 1", Explain(m, container)); |
| 3677 | } |
| 3678 | |
zhanyong.wan | a31d9ce | 2013-03-01 01:50:17 +0000 | [diff] [blame] | 3679 | TEST(SizeIsTest, ImplementsSizeIs) { |
| 3680 | vector<int> container; |
| 3681 | EXPECT_THAT(container, SizeIs(0)); |
| 3682 | EXPECT_THAT(container, Not(SizeIs(1))); |
| 3683 | container.push_back(0); |
| 3684 | EXPECT_THAT(container, Not(SizeIs(0))); |
| 3685 | EXPECT_THAT(container, SizeIs(1)); |
| 3686 | container.push_back(0); |
| 3687 | EXPECT_THAT(container, Not(SizeIs(0))); |
| 3688 | EXPECT_THAT(container, SizeIs(2)); |
| 3689 | } |
| 3690 | |
| 3691 | TEST(SizeIsTest, WorksWithMap) { |
| 3692 | map<string, int> container; |
| 3693 | EXPECT_THAT(container, SizeIs(0)); |
| 3694 | EXPECT_THAT(container, Not(SizeIs(1))); |
| 3695 | container.insert(make_pair("foo", 1)); |
| 3696 | EXPECT_THAT(container, Not(SizeIs(0))); |
| 3697 | EXPECT_THAT(container, SizeIs(1)); |
| 3698 | container.insert(make_pair("bar", 2)); |
| 3699 | EXPECT_THAT(container, Not(SizeIs(0))); |
| 3700 | EXPECT_THAT(container, SizeIs(2)); |
| 3701 | } |
| 3702 | |
| 3703 | TEST(SizeIsTest, WorksWithReferences) { |
| 3704 | vector<int> container; |
| 3705 | Matcher<const vector<int>&> m = SizeIs(1); |
| 3706 | EXPECT_THAT(container, Not(m)); |
| 3707 | container.push_back(0); |
| 3708 | EXPECT_THAT(container, m); |
| 3709 | } |
| 3710 | |
| 3711 | TEST(SizeIsTest, CanDescribeSelf) { |
| 3712 | Matcher<vector<int> > m = SizeIs(2); |
| 3713 | EXPECT_EQ("size is equal to 2", Describe(m)); |
| 3714 | EXPECT_EQ("size isn't equal to 2", DescribeNegation(m)); |
| 3715 | } |
| 3716 | |
| 3717 | TEST(SizeIsTest, ExplainsResult) { |
| 3718 | Matcher<vector<int> > m1 = SizeIs(2); |
| 3719 | Matcher<vector<int> > m2 = SizeIs(Lt(2u)); |
| 3720 | Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3)); |
| 3721 | Matcher<vector<int> > m4 = SizeIs(GreaterThan(1)); |
| 3722 | vector<int> container; |
| 3723 | EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container)); |
| 3724 | EXPECT_EQ("whose size 0 matches", Explain(m2, container)); |
| 3725 | EXPECT_EQ("whose size 0 matches", Explain(m3, container)); |
| 3726 | EXPECT_EQ("whose size 0 doesn't match, which is 1 less than 1", |
| 3727 | Explain(m4, container)); |
| 3728 | container.push_back(0); |
| 3729 | container.push_back(0); |
| 3730 | EXPECT_EQ("whose size 2 matches", Explain(m1, container)); |
| 3731 | EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container)); |
| 3732 | EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container)); |
| 3733 | EXPECT_EQ("whose size 2 matches, which is 1 more than 1", |
| 3734 | Explain(m4, container)); |
| 3735 | } |
| 3736 | |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 3737 | #if GTEST_HAS_TYPED_TEST |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3738 | // Tests ContainerEq with different container types, and |
| 3739 | // different element types. |
| 3740 | |
| 3741 | template <typename T> |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 3742 | class ContainerEqTest : public testing::Test {}; |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3743 | |
| 3744 | typedef testing::Types< |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3745 | set<int>, |
| 3746 | vector<size_t>, |
| 3747 | multiset<size_t>, |
| 3748 | list<int> > |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3749 | ContainerEqTestTypes; |
| 3750 | |
| 3751 | TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes); |
| 3752 | |
| 3753 | // Tests that the filled container is equal to itself. |
| 3754 | TYPED_TEST(ContainerEqTest, EqualsSelf) { |
| 3755 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3756 | TypeParam my_set(vals, vals + 6); |
| 3757 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 3758 | EXPECT_TRUE(m.Matches(my_set)); |
| 3759 | EXPECT_EQ("", Explain(m, my_set)); |
| 3760 | } |
| 3761 | |
| 3762 | // Tests that missing values are reported. |
| 3763 | TYPED_TEST(ContainerEqTest, ValueMissing) { |
| 3764 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3765 | static const int test_vals[] = {2, 1, 8, 5}; |
| 3766 | TypeParam my_set(vals, vals + 6); |
| 3767 | TypeParam test_set(test_vals, test_vals + 4); |
| 3768 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 3769 | EXPECT_FALSE(m.Matches(test_set)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3770 | EXPECT_EQ("which doesn't have these expected elements: 3", |
| 3771 | Explain(m, test_set)); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | // Tests that added values are reported. |
| 3775 | TYPED_TEST(ContainerEqTest, ValueAdded) { |
| 3776 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3777 | static const int test_vals[] = {1, 2, 3, 5, 8, 46}; |
| 3778 | TypeParam my_set(vals, vals + 6); |
| 3779 | TypeParam test_set(test_vals, test_vals + 6); |
| 3780 | const Matcher<const TypeParam&> m = ContainerEq(my_set); |
| 3781 | EXPECT_FALSE(m.Matches(test_set)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3782 | EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set)); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | // Tests that added and missing values are reported together. |
| 3786 | TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) { |
| 3787 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3788 | static const int test_vals[] = {1, 2, 3, 8, 46}; |
| 3789 | TypeParam my_set(vals, vals + 6); |
| 3790 | TypeParam test_set(test_vals, test_vals + 5); |
| 3791 | const Matcher<TypeParam> m = ContainerEq(my_set); |
| 3792 | EXPECT_FALSE(m.Matches(test_set)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3793 | EXPECT_EQ("which has these unexpected elements: 46,\n" |
| 3794 | "and doesn't have these expected elements: 5", |
| 3795 | Explain(m, test_set)); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | // Tests duplicated value -- expect no explanation. |
| 3799 | TYPED_TEST(ContainerEqTest, DuplicateDifference) { |
| 3800 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3801 | static const int test_vals[] = {1, 2, 3, 5, 8}; |
| 3802 | TypeParam my_set(vals, vals + 6); |
| 3803 | TypeParam test_set(test_vals, test_vals + 5); |
| 3804 | const Matcher<const TypeParam&> m = ContainerEq(my_set); |
| 3805 | // Depending on the container, match may be true or false |
| 3806 | // But in any case there should be no explanation. |
| 3807 | EXPECT_EQ("", Explain(m, test_set)); |
| 3808 | } |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 3809 | #endif // GTEST_HAS_TYPED_TEST |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3810 | |
| 3811 | // Tests that mutliple missing values are reported. |
| 3812 | // Using just vector here, so order is predicatble. |
| 3813 | TEST(ContainerEqExtraTest, MultipleValuesMissing) { |
| 3814 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3815 | static const int test_vals[] = {2, 1, 5}; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3816 | vector<int> my_set(vals, vals + 6); |
| 3817 | vector<int> test_set(test_vals, test_vals + 3); |
| 3818 | const Matcher<vector<int> > m = ContainerEq(my_set); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3819 | EXPECT_FALSE(m.Matches(test_set)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3820 | EXPECT_EQ("which doesn't have these expected elements: 3, 8", |
| 3821 | Explain(m, test_set)); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | // Tests that added values are reported. |
| 3825 | // Using just vector here, so order is predicatble. |
| 3826 | TEST(ContainerEqExtraTest, MultipleValuesAdded) { |
| 3827 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3828 | static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3829 | list<size_t> my_set(vals, vals + 6); |
| 3830 | list<size_t> test_set(test_vals, test_vals + 7); |
| 3831 | const Matcher<const list<size_t>&> m = ContainerEq(my_set); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3832 | EXPECT_FALSE(m.Matches(test_set)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3833 | EXPECT_EQ("which has these unexpected elements: 92, 46", |
| 3834 | Explain(m, test_set)); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | // Tests that added and missing values are reported together. |
| 3838 | TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { |
| 3839 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3840 | static const int test_vals[] = {1, 2, 3, 92, 46}; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3841 | list<size_t> my_set(vals, vals + 6); |
| 3842 | list<size_t> test_set(test_vals, test_vals + 5); |
| 3843 | const Matcher<const list<size_t> > m = ContainerEq(my_set); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3844 | EXPECT_FALSE(m.Matches(test_set)); |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3845 | EXPECT_EQ("which has these unexpected elements: 92, 46,\n" |
| 3846 | "and doesn't have these expected elements: 5, 8", |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3847 | Explain(m, test_set)); |
| 3848 | } |
| 3849 | |
| 3850 | // Tests to see that duplicate elements are detected, |
| 3851 | // but (as above) not reported in the explanation. |
| 3852 | TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { |
| 3853 | static const int vals[] = {1, 1, 2, 3, 5, 8}; |
| 3854 | static const int test_vals[] = {1, 2, 3, 5, 8}; |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3855 | vector<int> my_set(vals, vals + 6); |
| 3856 | vector<int> test_set(test_vals, test_vals + 5); |
| 3857 | const Matcher<vector<int> > m = ContainerEq(my_set); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3858 | EXPECT_TRUE(m.Matches(my_set)); |
| 3859 | EXPECT_FALSE(m.Matches(test_set)); |
| 3860 | // There is nothing to report when both sets contain all the same values. |
| 3861 | EXPECT_EQ("", Explain(m, test_set)); |
| 3862 | } |
| 3863 | |
| 3864 | // Tests that ContainerEq works for non-trivial associative containers, |
| 3865 | // like maps. |
| 3866 | TEST(ContainerEqExtraTest, WorksForMaps) { |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3867 | map<int, std::string> my_map; |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3868 | my_map[0] = "a"; |
| 3869 | my_map[1] = "b"; |
| 3870 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3871 | map<int, std::string> test_map; |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3872 | test_map[0] = "aa"; |
| 3873 | test_map[1] = "b"; |
| 3874 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 3875 | const Matcher<const map<int, std::string>&> m = ContainerEq(my_map); |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3876 | EXPECT_TRUE(m.Matches(my_map)); |
| 3877 | EXPECT_FALSE(m.Matches(test_map)); |
| 3878 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 3879 | EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n" |
| 3880 | "and doesn't have these expected elements: (0, \"a\")", |
zhanyong.wan | 6a896b5 | 2009-01-16 01:13:50 +0000 | [diff] [blame] | 3881 | Explain(m, test_map)); |
| 3882 | } |
| 3883 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 3884 | TEST(ContainerEqExtraTest, WorksForNativeArray) { |
| 3885 | int a1[] = { 1, 2, 3 }; |
| 3886 | int a2[] = { 1, 2, 3 }; |
| 3887 | int b[] = { 1, 2, 4 }; |
| 3888 | |
| 3889 | EXPECT_THAT(a1, ContainerEq(a2)); |
| 3890 | EXPECT_THAT(a1, Not(ContainerEq(b))); |
| 3891 | } |
| 3892 | |
| 3893 | TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) { |
| 3894 | const char a1[][3] = { "hi", "lo" }; |
| 3895 | const char a2[][3] = { "hi", "lo" }; |
| 3896 | const char b[][3] = { "lo", "hi" }; |
| 3897 | |
| 3898 | // Tests using ContainerEq() in the first dimension. |
| 3899 | EXPECT_THAT(a1, ContainerEq(a2)); |
| 3900 | EXPECT_THAT(a1, Not(ContainerEq(b))); |
| 3901 | |
| 3902 | // Tests using ContainerEq() in the second dimension. |
| 3903 | EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1]))); |
| 3904 | EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1]))); |
| 3905 | } |
| 3906 | |
| 3907 | TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) { |
| 3908 | const int a1[] = { 1, 2, 3 }; |
| 3909 | const int a2[] = { 1, 2, 3 }; |
| 3910 | const int b[] = { 1, 2, 3, 4 }; |
| 3911 | |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 3912 | const int* const p1 = a1; |
| 3913 | EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2)); |
| 3914 | EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b))); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 3915 | |
| 3916 | const int c[] = { 1, 3, 2 }; |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 3917 | EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c))); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) { |
| 3921 | std::string a1[][3] = { |
| 3922 | { "hi", "hello", "ciao" }, |
| 3923 | { "bye", "see you", "ciao" } |
| 3924 | }; |
| 3925 | |
| 3926 | std::string a2[][3] = { |
| 3927 | { "hi", "hello", "ciao" }, |
| 3928 | { "bye", "see you", "ciao" } |
| 3929 | }; |
| 3930 | |
| 3931 | const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2); |
| 3932 | EXPECT_THAT(a1, m); |
| 3933 | |
| 3934 | a2[0][0] = "ha"; |
| 3935 | EXPECT_THAT(a1, m); |
| 3936 | } |
| 3937 | |
zhanyong.wan | 898725c | 2011-09-16 16:45:39 +0000 | [diff] [blame] | 3938 | TEST(WhenSortedByTest, WorksForEmptyContainer) { |
| 3939 | const vector<int> numbers; |
| 3940 | EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre())); |
| 3941 | EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1)))); |
| 3942 | } |
| 3943 | |
| 3944 | TEST(WhenSortedByTest, WorksForNonEmptyContainer) { |
| 3945 | vector<unsigned> numbers; |
| 3946 | numbers.push_back(3); |
| 3947 | numbers.push_back(1); |
| 3948 | numbers.push_back(2); |
| 3949 | numbers.push_back(2); |
| 3950 | EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(), |
| 3951 | ElementsAre(3, 2, 2, 1))); |
| 3952 | EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(), |
| 3953 | ElementsAre(1, 2, 2, 3)))); |
| 3954 | } |
| 3955 | |
| 3956 | TEST(WhenSortedByTest, WorksForNonVectorContainer) { |
| 3957 | list<string> words; |
| 3958 | words.push_back("say"); |
| 3959 | words.push_back("hello"); |
| 3960 | words.push_back("world"); |
| 3961 | EXPECT_THAT(words, WhenSortedBy(less<string>(), |
| 3962 | ElementsAre("hello", "say", "world"))); |
| 3963 | EXPECT_THAT(words, Not(WhenSortedBy(less<string>(), |
| 3964 | ElementsAre("say", "hello", "world")))); |
| 3965 | } |
| 3966 | |
| 3967 | TEST(WhenSortedByTest, WorksForNativeArray) { |
| 3968 | const int numbers[] = { 1, 3, 2, 4 }; |
| 3969 | const int sorted_numbers[] = { 1, 2, 3, 4 }; |
| 3970 | EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4))); |
| 3971 | EXPECT_THAT(numbers, WhenSortedBy(less<int>(), |
| 3972 | ElementsAreArray(sorted_numbers))); |
| 3973 | EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4)))); |
| 3974 | } |
| 3975 | |
| 3976 | TEST(WhenSortedByTest, CanDescribeSelf) { |
| 3977 | const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2)); |
| 3978 | EXPECT_EQ("(when sorted) has 2 elements where\n" |
| 3979 | "element #0 is equal to 1,\n" |
| 3980 | "element #1 is equal to 2", |
| 3981 | Describe(m)); |
| 3982 | EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n" |
| 3983 | "element #0 isn't equal to 1, or\n" |
| 3984 | "element #1 isn't equal to 2", |
| 3985 | DescribeNegation(m)); |
| 3986 | } |
| 3987 | |
| 3988 | TEST(WhenSortedByTest, ExplainsMatchResult) { |
| 3989 | const int a[] = { 2, 1 }; |
| 3990 | EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match", |
| 3991 | Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a)); |
| 3992 | EXPECT_EQ("which is { 1, 2 } when sorted", |
| 3993 | Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a)); |
| 3994 | } |
| 3995 | |
| 3996 | // WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't |
| 3997 | // need to test it as exhaustively as we test the latter. |
| 3998 | |
| 3999 | TEST(WhenSortedTest, WorksForEmptyContainer) { |
| 4000 | const vector<int> numbers; |
| 4001 | EXPECT_THAT(numbers, WhenSorted(ElementsAre())); |
| 4002 | EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1)))); |
| 4003 | } |
| 4004 | |
| 4005 | TEST(WhenSortedTest, WorksForNonEmptyContainer) { |
| 4006 | list<string> words; |
| 4007 | words.push_back("3"); |
| 4008 | words.push_back("1"); |
| 4009 | words.push_back("2"); |
| 4010 | words.push_back("2"); |
| 4011 | EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3"))); |
| 4012 | EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2")))); |
| 4013 | } |
| 4014 | |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 4015 | // Tests IsReadableTypeName(). |
| 4016 | |
| 4017 | TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) { |
| 4018 | EXPECT_TRUE(IsReadableTypeName("int")); |
| 4019 | EXPECT_TRUE(IsReadableTypeName("const unsigned char*")); |
| 4020 | EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>")); |
| 4021 | EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)")); |
| 4022 | } |
| 4023 | |
| 4024 | TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) { |
| 4025 | EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName")); |
| 4026 | EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]")); |
| 4027 | EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass")); |
| 4028 | } |
| 4029 | |
| 4030 | TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) { |
| 4031 | EXPECT_FALSE( |
| 4032 | IsReadableTypeName("basic_string<char, std::char_traits<char> >")); |
| 4033 | EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >")); |
| 4034 | } |
| 4035 | |
| 4036 | TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) { |
| 4037 | EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)")); |
| 4038 | } |
| 4039 | |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 4040 | // Tests JoinAsTuple(). |
| 4041 | |
| 4042 | TEST(JoinAsTupleTest, JoinsEmptyTuple) { |
| 4043 | EXPECT_EQ("", JoinAsTuple(Strings())); |
| 4044 | } |
| 4045 | |
| 4046 | TEST(JoinAsTupleTest, JoinsOneTuple) { |
| 4047 | const char* fields[] = { "1" }; |
| 4048 | EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1))); |
| 4049 | } |
| 4050 | |
| 4051 | TEST(JoinAsTupleTest, JoinsTwoTuple) { |
| 4052 | const char* fields[] = { "1", "a" }; |
| 4053 | EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2))); |
| 4054 | } |
| 4055 | |
| 4056 | TEST(JoinAsTupleTest, JoinsTenTuple) { |
| 4057 | const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; |
| 4058 | EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)", |
| 4059 | JoinAsTuple(Strings(fields, fields + 10))); |
| 4060 | } |
| 4061 | |
| 4062 | // Tests FormatMatcherDescription(). |
| 4063 | |
| 4064 | TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) { |
| 4065 | EXPECT_EQ("is even", |
zhanyong.wan | b414080 | 2010-06-08 22:53:57 +0000 | [diff] [blame] | 4066 | FormatMatcherDescription(false, "IsEven", Strings())); |
| 4067 | EXPECT_EQ("not (is even)", |
| 4068 | FormatMatcherDescription(true, "IsEven", Strings())); |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 4069 | |
| 4070 | const char* params[] = { "5" }; |
| 4071 | EXPECT_EQ("equals 5", |
zhanyong.wan | b414080 | 2010-06-08 22:53:57 +0000 | [diff] [blame] | 4072 | FormatMatcherDescription(false, "Equals", |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 4073 | Strings(params, params + 1))); |
| 4074 | |
| 4075 | const char* params2[] = { "5", "8" }; |
| 4076 | EXPECT_EQ("is in range (5, 8)", |
zhanyong.wan | b414080 | 2010-06-08 22:53:57 +0000 | [diff] [blame] | 4077 | FormatMatcherDescription(false, "IsInRange", |
zhanyong.wan | 4a5330d | 2009-02-19 00:36:44 +0000 | [diff] [blame] | 4078 | Strings(params2, params2 + 2))); |
| 4079 | } |
| 4080 | |
zhanyong.wan | 2b43a9e | 2009-08-31 23:51:23 +0000 | [diff] [blame] | 4081 | // Tests PolymorphicMatcher::mutable_impl(). |
| 4082 | TEST(PolymorphicMatcherTest, CanAccessMutableImpl) { |
| 4083 | PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
| 4084 | DivisibleByImpl& impl = m.mutable_impl(); |
| 4085 | EXPECT_EQ(42, impl.divider()); |
| 4086 | |
| 4087 | impl.set_divider(0); |
| 4088 | EXPECT_EQ(0, m.mutable_impl().divider()); |
| 4089 | } |
| 4090 | |
| 4091 | // Tests PolymorphicMatcher::impl(). |
| 4092 | TEST(PolymorphicMatcherTest, CanAccessImpl) { |
| 4093 | const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
| 4094 | const DivisibleByImpl& impl = m.impl(); |
| 4095 | EXPECT_EQ(42, impl.divider()); |
| 4096 | } |
| 4097 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 4098 | TEST(MatcherTupleTest, ExplainsMatchFailure) { |
| 4099 | stringstream ss1; |
| 4100 | ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)), |
| 4101 | make_tuple('a', 10), &ss1); |
| 4102 | EXPECT_EQ("", ss1.str()); // Successful match. |
| 4103 | |
| 4104 | stringstream ss2; |
| 4105 | ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
| 4106 | make_tuple(2, 'b'), &ss2); |
| 4107 | EXPECT_EQ(" Expected arg #0: is > 5\n" |
| 4108 | " Actual: 2, which is 3 less than 5\n" |
zhanyong.wan | d60c5f4 | 2010-07-21 22:21:07 +0000 | [diff] [blame] | 4109 | " Expected arg #1: is equal to 'a' (97, 0x61)\n" |
| 4110 | " Actual: 'b' (98, 0x62)\n", |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 4111 | ss2.str()); // Failed match where both arguments need explanation. |
| 4112 | |
| 4113 | stringstream ss3; |
| 4114 | ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
| 4115 | make_tuple(2, 'a'), &ss3); |
| 4116 | EXPECT_EQ(" Expected arg #0: is > 5\n" |
| 4117 | " Actual: 2, which is 3 less than 5\n", |
| 4118 | ss3.str()); // Failed match where only one argument needs |
| 4119 | // explanation. |
| 4120 | } |
| 4121 | |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 4122 | // Tests Each(). |
| 4123 | |
| 4124 | TEST(EachTest, ExplainsMatchResultCorrectly) { |
| 4125 | set<int> a; // empty |
| 4126 | |
| 4127 | Matcher<set<int> > m = Each(2); |
| 4128 | EXPECT_EQ("", Explain(m, a)); |
| 4129 | |
zhanyong.wan | 736baa8 | 2010-09-27 17:44:16 +0000 | [diff] [blame] | 4130 | Matcher<const int(&)[1]> n = Each(1); // NOLINT |
zhanyong.wan | 33605ba | 2010-04-22 23:37:47 +0000 | [diff] [blame] | 4131 | |
| 4132 | const int b[1] = { 1 }; |
| 4133 | EXPECT_EQ("", Explain(n, b)); |
| 4134 | |
| 4135 | n = Each(3); |
| 4136 | EXPECT_EQ("whose element #0 doesn't match", Explain(n, b)); |
| 4137 | |
| 4138 | a.insert(1); |
| 4139 | a.insert(2); |
| 4140 | a.insert(3); |
| 4141 | m = Each(GreaterThan(0)); |
| 4142 | EXPECT_EQ("", Explain(m, a)); |
| 4143 | |
| 4144 | m = Each(GreaterThan(10)); |
| 4145 | EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10", |
| 4146 | Explain(m, a)); |
| 4147 | } |
| 4148 | |
| 4149 | TEST(EachTest, DescribesItselfCorrectly) { |
| 4150 | Matcher<vector<int> > m = Each(1); |
| 4151 | EXPECT_EQ("only contains elements that is equal to 1", Describe(m)); |
| 4152 | |
| 4153 | Matcher<vector<int> > m2 = Not(m); |
| 4154 | EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2)); |
| 4155 | } |
| 4156 | |
| 4157 | TEST(EachTest, MatchesVectorWhenAllElementsMatch) { |
| 4158 | vector<int> some_vector; |
| 4159 | EXPECT_THAT(some_vector, Each(1)); |
| 4160 | some_vector.push_back(3); |
| 4161 | EXPECT_THAT(some_vector, Not(Each(1))); |
| 4162 | EXPECT_THAT(some_vector, Each(3)); |
| 4163 | some_vector.push_back(1); |
| 4164 | some_vector.push_back(2); |
| 4165 | EXPECT_THAT(some_vector, Not(Each(3))); |
| 4166 | EXPECT_THAT(some_vector, Each(Lt(3.5))); |
| 4167 | |
| 4168 | vector<string> another_vector; |
| 4169 | another_vector.push_back("fee"); |
| 4170 | EXPECT_THAT(another_vector, Each(string("fee"))); |
| 4171 | another_vector.push_back("fie"); |
| 4172 | another_vector.push_back("foe"); |
| 4173 | another_vector.push_back("fum"); |
| 4174 | EXPECT_THAT(another_vector, Not(Each(string("fee")))); |
| 4175 | } |
| 4176 | |
| 4177 | TEST(EachTest, MatchesMapWhenAllElementsMatch) { |
| 4178 | map<const char*, int> my_map; |
| 4179 | const char* bar = "a string"; |
| 4180 | my_map[bar] = 2; |
| 4181 | EXPECT_THAT(my_map, Each(make_pair(bar, 2))); |
| 4182 | |
| 4183 | map<string, int> another_map; |
| 4184 | EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1))); |
| 4185 | another_map["fee"] = 1; |
| 4186 | EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1))); |
| 4187 | another_map["fie"] = 2; |
| 4188 | another_map["foe"] = 3; |
| 4189 | another_map["fum"] = 4; |
| 4190 | EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1)))); |
| 4191 | EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1)))); |
| 4192 | EXPECT_THAT(another_map, Each(Pair(_, Gt(0)))); |
| 4193 | } |
| 4194 | |
| 4195 | TEST(EachTest, AcceptsMatcher) { |
| 4196 | const int a[] = { 1, 2, 3 }; |
| 4197 | EXPECT_THAT(a, Each(Gt(0))); |
| 4198 | EXPECT_THAT(a, Not(Each(Gt(1)))); |
| 4199 | } |
| 4200 | |
| 4201 | TEST(EachTest, WorksForNativeArrayAsTuple) { |
| 4202 | const int a[] = { 1, 2 }; |
| 4203 | const int* const pointer = a; |
| 4204 | EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0))); |
| 4205 | EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1)))); |
| 4206 | } |
| 4207 | |
zhanyong.wan | ab5b77c | 2010-05-17 19:32:48 +0000 | [diff] [blame] | 4208 | // For testing Pointwise(). |
| 4209 | class IsHalfOfMatcher { |
| 4210 | public: |
| 4211 | template <typename T1, typename T2> |
| 4212 | bool MatchAndExplain(const tuple<T1, T2>& a_pair, |
| 4213 | MatchResultListener* listener) const { |
| 4214 | if (get<0>(a_pair) == get<1>(a_pair)/2) { |
| 4215 | *listener << "where the second is " << get<1>(a_pair); |
| 4216 | return true; |
| 4217 | } else { |
| 4218 | *listener << "where the second/2 is " << get<1>(a_pair)/2; |
| 4219 | return false; |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | void DescribeTo(ostream* os) const { |
| 4224 | *os << "are a pair where the first is half of the second"; |
| 4225 | } |
| 4226 | |
| 4227 | void DescribeNegationTo(ostream* os) const { |
| 4228 | *os << "are a pair where the first isn't half of the second"; |
| 4229 | } |
| 4230 | }; |
| 4231 | |
| 4232 | PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() { |
| 4233 | return MakePolymorphicMatcher(IsHalfOfMatcher()); |
| 4234 | } |
| 4235 | |
| 4236 | TEST(PointwiseTest, DescribesSelf) { |
| 4237 | vector<int> rhs; |
| 4238 | rhs.push_back(1); |
| 4239 | rhs.push_back(2); |
| 4240 | rhs.push_back(3); |
| 4241 | const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs); |
| 4242 | EXPECT_EQ("contains 3 values, where each value and its corresponding value " |
| 4243 | "in { 1, 2, 3 } are a pair where the first is half of the second", |
| 4244 | Describe(m)); |
| 4245 | EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some " |
| 4246 | "index i where x and the i-th value of { 1, 2, 3 } are a pair " |
| 4247 | "where the first isn't half of the second", |
| 4248 | DescribeNegation(m)); |
| 4249 | } |
| 4250 | |
| 4251 | TEST(PointwiseTest, MakesCopyOfRhs) { |
| 4252 | list<signed char> rhs; |
| 4253 | rhs.push_back(2); |
| 4254 | rhs.push_back(4); |
| 4255 | |
| 4256 | int lhs[] = { 1, 2 }; |
| 4257 | const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs); |
| 4258 | EXPECT_THAT(lhs, m); |
| 4259 | |
| 4260 | // Changing rhs now shouldn't affect m, which made a copy of rhs. |
| 4261 | rhs.push_back(6); |
| 4262 | EXPECT_THAT(lhs, m); |
| 4263 | } |
| 4264 | |
| 4265 | TEST(PointwiseTest, WorksForLhsNativeArray) { |
| 4266 | const int lhs[] = { 1, 2, 3 }; |
| 4267 | vector<int> rhs; |
| 4268 | rhs.push_back(2); |
| 4269 | rhs.push_back(4); |
| 4270 | rhs.push_back(6); |
| 4271 | EXPECT_THAT(lhs, Pointwise(Lt(), rhs)); |
| 4272 | EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); |
| 4273 | } |
| 4274 | |
| 4275 | TEST(PointwiseTest, WorksForRhsNativeArray) { |
| 4276 | const int rhs[] = { 1, 2, 3 }; |
| 4277 | vector<int> lhs; |
| 4278 | lhs.push_back(2); |
| 4279 | lhs.push_back(4); |
| 4280 | lhs.push_back(6); |
| 4281 | EXPECT_THAT(lhs, Pointwise(Gt(), rhs)); |
| 4282 | EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs))); |
| 4283 | } |
| 4284 | |
| 4285 | TEST(PointwiseTest, RejectsWrongSize) { |
| 4286 | const double lhs[2] = { 1, 2 }; |
| 4287 | const int rhs[1] = { 0 }; |
| 4288 | EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs))); |
| 4289 | EXPECT_EQ("which contains 2 values", |
| 4290 | Explain(Pointwise(Gt(), rhs), lhs)); |
| 4291 | |
| 4292 | const int rhs2[3] = { 0, 1, 2 }; |
| 4293 | EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2))); |
| 4294 | } |
| 4295 | |
| 4296 | TEST(PointwiseTest, RejectsWrongContent) { |
| 4297 | const double lhs[3] = { 1, 2, 3 }; |
| 4298 | const int rhs[3] = { 2, 6, 4 }; |
| 4299 | EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs))); |
| 4300 | EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, " |
| 4301 | "where the second/2 is 3", |
| 4302 | Explain(Pointwise(IsHalfOf(), rhs), lhs)); |
| 4303 | } |
| 4304 | |
| 4305 | TEST(PointwiseTest, AcceptsCorrectContent) { |
| 4306 | const double lhs[3] = { 1, 2, 3 }; |
| 4307 | const int rhs[3] = { 2, 4, 6 }; |
| 4308 | EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs)); |
| 4309 | EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs)); |
| 4310 | } |
| 4311 | |
| 4312 | TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) { |
| 4313 | const double lhs[3] = { 1, 2, 3 }; |
| 4314 | const int rhs[3] = { 2, 4, 6 }; |
| 4315 | const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf(); |
| 4316 | EXPECT_THAT(lhs, Pointwise(m1, rhs)); |
| 4317 | EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs)); |
| 4318 | |
| 4319 | // This type works as a tuple<const double&, const int&> can be |
| 4320 | // implicitly cast to tuple<double, int>. |
| 4321 | const Matcher<tuple<double, int> > m2 = IsHalfOf(); |
| 4322 | EXPECT_THAT(lhs, Pointwise(m2, rhs)); |
| 4323 | EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs)); |
| 4324 | } |
| 4325 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 4326 | } // namespace gmock_matchers_test |
| 4327 | } // namespace testing |