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