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