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