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 the internal utilities. |
| 35 | |
| 36 | #include <gmock/internal/gmock-internal-utils.h> |
| 37 | #include <map> |
| 38 | #include <string> |
| 39 | #include <sstream> |
| 40 | #include <vector> |
| 41 | #include <gmock/gmock.h> |
| 42 | #include <gmock/internal/gmock-port.h> |
| 43 | #include <gtest/gtest.h> |
| 44 | #include <gtest/gtest-spi.h> |
| 45 | |
| 46 | namespace testing { |
| 47 | namespace internal { |
| 48 | |
| 49 | namespace { |
| 50 | |
| 51 | using ::std::tr1::tuple; |
| 52 | |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 53 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) { |
| 54 | EXPECT_EQ("", ConvertIdentifierNameToWords("")); |
| 55 | EXPECT_EQ("", ConvertIdentifierNameToWords("_")); |
| 56 | EXPECT_EQ("", ConvertIdentifierNameToWords("__")); |
| 57 | } |
| 58 | |
| 59 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) { |
| 60 | EXPECT_EQ("1", ConvertIdentifierNameToWords("_1")); |
| 61 | EXPECT_EQ("2", ConvertIdentifierNameToWords("2_")); |
| 62 | EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_")); |
| 63 | EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56")); |
| 64 | } |
| 65 | |
| 66 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) { |
| 67 | EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord")); |
| 68 | EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar")); |
| 69 | EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_")); |
| 70 | EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_")); |
| 71 | EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar")); |
| 72 | } |
| 73 | |
| 74 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) { |
| 75 | EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar")); |
| 76 | EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_")); |
| 77 | EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_")); |
| 78 | EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar")); |
| 79 | } |
| 80 | |
| 81 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) { |
| 82 | EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123")); |
| 83 | EXPECT_EQ("chapter 11 section 1", |
| 84 | ConvertIdentifierNameToWords("_Chapter11Section_1_")); |
| 85 | } |
| 86 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 87 | // Tests that CompileAssertTypesEqual compiles when the type arguments are |
| 88 | // equal. |
| 89 | TEST(CompileAssertTypesEqual, CompilesWhenTypesAreEqual) { |
| 90 | CompileAssertTypesEqual<void, void>(); |
| 91 | CompileAssertTypesEqual<int*, int*>(); |
| 92 | } |
| 93 | |
| 94 | // Tests that RemoveReference does not affect non-reference types. |
| 95 | TEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) { |
| 96 | CompileAssertTypesEqual<int, RemoveReference<int>::type>(); |
| 97 | CompileAssertTypesEqual<const char, RemoveReference<const char>::type>(); |
| 98 | } |
| 99 | |
| 100 | // Tests that RemoveReference removes reference from reference types. |
| 101 | TEST(RemoveReferenceTest, RemovesReference) { |
| 102 | CompileAssertTypesEqual<int, RemoveReference<int&>::type>(); |
| 103 | CompileAssertTypesEqual<const char, RemoveReference<const char&>::type>(); |
| 104 | } |
| 105 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 106 | // Tests GMOCK_REMOVE_REFERENCE_. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 107 | |
| 108 | template <typename T1, typename T2> |
| 109 | void TestGMockRemoveReference() { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 110 | CompileAssertTypesEqual<T1, GMOCK_REMOVE_REFERENCE_(T2)>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST(RemoveReferenceTest, MacroVersion) { |
| 114 | TestGMockRemoveReference<int, int>(); |
| 115 | TestGMockRemoveReference<const char, const char&>(); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | // Tests that RemoveConst does not affect non-const types. |
| 120 | TEST(RemoveConstTest, DoesNotAffectNonConstType) { |
| 121 | CompileAssertTypesEqual<int, RemoveConst<int>::type>(); |
| 122 | CompileAssertTypesEqual<char&, RemoveConst<char&>::type>(); |
| 123 | } |
| 124 | |
| 125 | // Tests that RemoveConst removes const from const types. |
| 126 | TEST(RemoveConstTest, RemovesConst) { |
| 127 | CompileAssertTypesEqual<int, RemoveConst<const int>::type>(); |
| 128 | } |
| 129 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 130 | // Tests GMOCK_REMOVE_CONST_. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 131 | |
| 132 | template <typename T1, typename T2> |
| 133 | void TestGMockRemoveConst() { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 134 | CompileAssertTypesEqual<T1, GMOCK_REMOVE_CONST_(T2)>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | TEST(RemoveConstTest, MacroVersion) { |
| 138 | TestGMockRemoveConst<int, int>(); |
| 139 | TestGMockRemoveConst<double&, double&>(); |
| 140 | TestGMockRemoveConst<char, const char>(); |
| 141 | } |
| 142 | |
| 143 | // Tests that AddReference does not affect reference types. |
| 144 | TEST(AddReferenceTest, DoesNotAffectReferenceType) { |
| 145 | CompileAssertTypesEqual<int&, AddReference<int&>::type>(); |
| 146 | CompileAssertTypesEqual<const char&, AddReference<const char&>::type>(); |
| 147 | } |
| 148 | |
| 149 | // Tests that AddReference adds reference to non-reference types. |
| 150 | TEST(AddReferenceTest, AddsReference) { |
| 151 | CompileAssertTypesEqual<int&, AddReference<int>::type>(); |
| 152 | CompileAssertTypesEqual<const char&, AddReference<const char>::type>(); |
| 153 | } |
| 154 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 155 | // Tests GMOCK_ADD_REFERENCE_. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 156 | |
| 157 | template <typename T1, typename T2> |
| 158 | void TestGMockAddReference() { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 159 | CompileAssertTypesEqual<T1, GMOCK_ADD_REFERENCE_(T2)>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TEST(AddReferenceTest, MacroVersion) { |
| 163 | TestGMockAddReference<int&, int>(); |
| 164 | TestGMockAddReference<const char&, const char&>(); |
| 165 | } |
| 166 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 167 | // Tests GMOCK_REFERENCE_TO_CONST_. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 168 | |
| 169 | template <typename T1, typename T2> |
| 170 | void TestGMockReferenceToConst() { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 171 | CompileAssertTypesEqual<T1, GMOCK_REFERENCE_TO_CONST_(T2)>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TEST(GMockReferenceToConstTest, Works) { |
| 175 | TestGMockReferenceToConst<const char&, char>(); |
| 176 | TestGMockReferenceToConst<const int&, const int>(); |
| 177 | TestGMockReferenceToConst<const double&, double>(); |
| 178 | TestGMockReferenceToConst<const string&, const string&>(); |
| 179 | } |
| 180 | |
| 181 | TEST(PointeeOfTest, WorksForSmartPointers) { |
| 182 | CompileAssertTypesEqual<const char, |
| 183 | PointeeOf<internal::linked_ptr<const char> >::type>(); |
| 184 | } |
| 185 | |
| 186 | TEST(PointeeOfTest, WorksForRawPointers) { |
| 187 | CompileAssertTypesEqual<int, PointeeOf<int*>::type>(); |
| 188 | CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>(); |
| 189 | CompileAssertTypesEqual<void, PointeeOf<void*>::type>(); |
| 190 | } |
| 191 | |
| 192 | TEST(GetRawPointerTest, WorksForSmartPointers) { |
| 193 | const char* const raw_p4 = new const char('a'); // NOLINT |
| 194 | const internal::linked_ptr<const char> p4(raw_p4); |
| 195 | EXPECT_EQ(raw_p4, GetRawPointer(p4)); |
| 196 | } |
| 197 | |
| 198 | TEST(GetRawPointerTest, WorksForRawPointers) { |
| 199 | int* p = NULL; |
| 200 | EXPECT_EQ(NULL, GetRawPointer(p)); |
| 201 | int n = 1; |
| 202 | EXPECT_EQ(&n, GetRawPointer(&n)); |
| 203 | } |
| 204 | |
| 205 | class Base {}; |
| 206 | class Derived : public Base {}; |
| 207 | |
| 208 | // Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant. |
| 209 | TEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 210 | GMOCK_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true); |
| 211 | GMOCK_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value), |
| 212 | const_false); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can |
| 216 | // be implicitly converted to T2. |
| 217 | TEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) { |
| 218 | EXPECT_TRUE((ImplicitlyConvertible<int, double>::value)); |
| 219 | EXPECT_TRUE((ImplicitlyConvertible<double, int>::value)); |
| 220 | EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value)); |
| 221 | EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value)); |
| 222 | EXPECT_TRUE((ImplicitlyConvertible<Derived&, const Base&>::value)); |
| 223 | EXPECT_TRUE((ImplicitlyConvertible<const Base, Base>::value)); |
| 224 | } |
| 225 | |
| 226 | // Tests that ImplicitlyConvertible<T1, T2>::value is false when T1 |
| 227 | // cannot be implicitly converted to T2. |
| 228 | TEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) { |
| 229 | EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value)); |
| 230 | EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value)); |
| 231 | EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value)); |
| 232 | EXPECT_FALSE((ImplicitlyConvertible<Base&, Derived&>::value)); |
| 233 | } |
| 234 | |
| 235 | // Tests that IsAProtocolMessage<T>::value is a compile-time constant. |
| 236 | TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 237 | GMOCK_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value, const_true); |
| 238 | GMOCK_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Tests that IsAProtocolMessage<T>::value is true when T is |
| 242 | // ProtocolMessage or a sub-class of it. |
| 243 | TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { |
| 244 | EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); |
| 245 | #if GMOCK_HAS_PROTOBUF_ |
| 246 | EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); |
| 247 | #endif // GMOCK_HAS_PROTOBUF_ |
| 248 | } |
| 249 | |
| 250 | // Tests that IsAProtocolMessage<T>::value is false when T is neither |
| 251 | // ProtocolMessage nor a sub-class of it. |
| 252 | TEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) { |
| 253 | EXPECT_FALSE(IsAProtocolMessage<int>::value); |
| 254 | EXPECT_FALSE(IsAProtocolMessage<const Base>::value); |
| 255 | } |
| 256 | |
| 257 | // Tests IsContainerTest. |
| 258 | |
| 259 | class NonContainer {}; |
| 260 | |
| 261 | TEST(IsContainerTestTest, WorksForNonContainer) { |
| 262 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0))); |
| 263 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0))); |
| 264 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0))); |
| 265 | } |
| 266 | |
| 267 | TEST(IsContainerTestTest, WorksForContainer) { |
| 268 | EXPECT_EQ(sizeof(IsContainer), sizeof(IsContainerTest<std::vector<bool> >(0))); |
| 269 | EXPECT_EQ(sizeof(IsContainer), sizeof(IsContainerTest<std::map<int, double> >(0))); |
| 270 | } |
| 271 | |
| 272 | // Tests the TupleMatches() template function. |
| 273 | |
| 274 | TEST(TupleMatchesTest, WorksForSize0) { |
| 275 | tuple<> matchers; |
| 276 | tuple<> values; |
| 277 | |
| 278 | EXPECT_TRUE(TupleMatches(matchers, values)); |
| 279 | } |
| 280 | |
| 281 | TEST(TupleMatchesTest, WorksForSize1) { |
| 282 | tuple<Matcher<int> > matchers(Eq(1)); |
| 283 | tuple<int> values1(1), |
| 284 | values2(2); |
| 285 | |
| 286 | EXPECT_TRUE(TupleMatches(matchers, values1)); |
| 287 | EXPECT_FALSE(TupleMatches(matchers, values2)); |
| 288 | } |
| 289 | |
| 290 | TEST(TupleMatchesTest, WorksForSize2) { |
| 291 | tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a')); |
| 292 | tuple<int, char> values1(1, 'a'), |
| 293 | values2(1, 'b'), |
| 294 | values3(2, 'a'), |
| 295 | values4(2, 'b'); |
| 296 | |
| 297 | EXPECT_TRUE(TupleMatches(matchers, values1)); |
| 298 | EXPECT_FALSE(TupleMatches(matchers, values2)); |
| 299 | EXPECT_FALSE(TupleMatches(matchers, values3)); |
| 300 | EXPECT_FALSE(TupleMatches(matchers, values4)); |
| 301 | } |
| 302 | |
| 303 | TEST(TupleMatchesTest, WorksForSize5) { |
| 304 | tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>, // NOLINT |
| 305 | Matcher<string> > |
| 306 | matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi")); |
| 307 | tuple<int, char, bool, long, string> // NOLINT |
| 308 | values1(1, 'a', true, 2L, "hi"), |
| 309 | values2(1, 'a', true, 2L, "hello"), |
| 310 | values3(2, 'a', true, 2L, "hi"); |
| 311 | |
| 312 | EXPECT_TRUE(TupleMatches(matchers, values1)); |
| 313 | EXPECT_FALSE(TupleMatches(matchers, values2)); |
| 314 | EXPECT_FALSE(TupleMatches(matchers, values3)); |
| 315 | } |
| 316 | |
| 317 | // Tests that Assert(true, ...) succeeds. |
| 318 | TEST(AssertTest, SucceedsOnTrue) { |
| 319 | Assert(true, __FILE__, __LINE__, "This should succeed."); |
| 320 | Assert(true, __FILE__, __LINE__); // This should succeed too. |
| 321 | } |
| 322 | |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 323 | #if GTEST_HAS_DEATH_TEST |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 324 | |
| 325 | // Tests that Assert(false, ...) generates a fatal failure. |
| 326 | TEST(AssertTest, FailsFatallyOnFalse) { |
| 327 | EXPECT_DEATH({ // NOLINT |
| 328 | Assert(false, __FILE__, __LINE__, "This should fail."); |
| 329 | }, ""); |
| 330 | |
| 331 | EXPECT_DEATH({ // NOLINT |
| 332 | Assert(false, __FILE__, __LINE__); |
| 333 | }, ""); |
| 334 | } |
| 335 | |
| 336 | #endif // GTEST_HAS_DEATH_TEST |
| 337 | |
| 338 | // Tests that Expect(true, ...) succeeds. |
| 339 | TEST(ExpectTest, SucceedsOnTrue) { |
| 340 | Expect(true, __FILE__, __LINE__, "This should succeed."); |
| 341 | Expect(true, __FILE__, __LINE__); // This should succeed too. |
| 342 | } |
| 343 | |
| 344 | // Tests that Expect(false, ...) generates a non-fatal failure. |
| 345 | TEST(ExpectTest, FailsNonfatallyOnFalse) { |
| 346 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 347 | Expect(false, __FILE__, __LINE__, "This should fail."); |
| 348 | }, "This should fail"); |
| 349 | |
| 350 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 351 | Expect(false, __FILE__, __LINE__); |
| 352 | }, "Expectation failed"); |
| 353 | } |
| 354 | |
| 355 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 356 | #if 0 |
| 357 | |
| 358 | // Tests the Log() function. |
| 359 | |
| 360 | // Verifies that Log() behaves correctly for the given verbosity level |
| 361 | // and log severity. |
| 362 | void TestLogWithSeverity(const string& verbosity, LogSeverity severity, |
| 363 | bool should_print) { |
| 364 | const string old_flag = GMOCK_FLAG(verbose); |
| 365 | GMOCK_FLAG(verbose) = verbosity; |
| 366 | CaptureTestStdout(); |
| 367 | Log(severity, "Test log.\n", 0); |
| 368 | if (should_print) { |
| 369 | EXPECT_PRED2(RE::FullMatch, |
| 370 | GetCapturedTestStdout(), |
| 371 | severity == WARNING ? |
| 372 | "\nGMOCK WARNING:\nTest log\\.\nStack trace:\n[\\s\\S]*" : |
| 373 | "\nTest log\\.\nStack trace:\n[\\s\\S]*"); |
| 374 | } else { |
| 375 | EXPECT_EQ("", GetCapturedTestStdout()); |
| 376 | } |
| 377 | GMOCK_FLAG(verbose) = old_flag; |
| 378 | } |
| 379 | |
| 380 | // Tests that when the stack_frames_to_skip parameter is negative, |
| 381 | // Log() doesn't include the stack trace in the output. |
| 382 | TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) { |
| 383 | GMOCK_FLAG(verbose) = kInfoVerbosity; |
| 384 | CaptureTestStdout(); |
| 385 | Log(INFO, "Test log.\n", -1); |
| 386 | EXPECT_EQ("\nTest log.\n", GetCapturedTestStdout()); |
| 387 | } |
| 388 | |
| 389 | // Tests that in opt mode, a positive stack_frames_to_skip argument is |
| 390 | // treated as 0. |
| 391 | TEST(LogTest, NoSkippingStackFrameInOptMode) { |
| 392 | CaptureTestStdout(); |
| 393 | Log(WARNING, "Test log.\n", 100); |
| 394 | const string log = GetCapturedTestStdout(); |
| 395 | #ifdef NDEBUG |
| 396 | // In opt mode, no stack frame should be skipped. |
| 397 | EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n" |
| 398 | "Test log\\.\n" |
| 399 | "Stack trace:\n" |
| 400 | ".+")); |
| 401 | #else |
| 402 | // In dbg mode, the stack frames should be skipped. |
| 403 | EXPECT_EQ("\nGMOCK WARNING:\n" |
| 404 | "Test log.\n" |
| 405 | "Stack trace:\n", log); |
| 406 | #endif // NDEBUG |
| 407 | } |
| 408 | |
| 409 | // Tests that all logs are printed when the value of the |
| 410 | // --gmock_verbose flag is "info". |
| 411 | TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) { |
| 412 | TestLogWithSeverity(kInfoVerbosity, INFO, true); |
| 413 | TestLogWithSeverity(kInfoVerbosity, WARNING, true); |
| 414 | } |
| 415 | |
| 416 | // Tests that only warnings are printed when the value of the |
| 417 | // --gmock_verbose flag is "warning". |
| 418 | TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) { |
| 419 | TestLogWithSeverity(kWarningVerbosity, INFO, false); |
| 420 | TestLogWithSeverity(kWarningVerbosity, WARNING, true); |
| 421 | } |
| 422 | |
| 423 | // Tests that no logs are printed when the value of the |
| 424 | // --gmock_verbose flag is "error". |
| 425 | TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) { |
| 426 | TestLogWithSeverity(kErrorVerbosity, INFO, false); |
| 427 | TestLogWithSeverity(kErrorVerbosity, WARNING, false); |
| 428 | } |
| 429 | |
| 430 | // Tests that only warnings are printed when the value of the |
| 431 | // --gmock_verbose flag is invalid. |
| 432 | TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) { |
| 433 | TestLogWithSeverity("invalid", INFO, false); |
| 434 | TestLogWithSeverity("invalid", WARNING, true); |
| 435 | } |
| 436 | |
| 437 | #endif // 0 |
| 438 | |
| 439 | TEST(TypeTraitsTest, true_type) { |
| 440 | EXPECT_TRUE(true_type::value); |
| 441 | } |
| 442 | |
| 443 | TEST(TypeTraitsTest, false_type) { |
| 444 | EXPECT_FALSE(false_type::value); |
| 445 | } |
| 446 | |
| 447 | TEST(TypeTraitsTest, is_reference) { |
| 448 | EXPECT_FALSE(is_reference<int>::value); |
| 449 | EXPECT_FALSE(is_reference<char*>::value); |
| 450 | EXPECT_TRUE(is_reference<const int&>::value); |
| 451 | } |
| 452 | |
| 453 | TEST(TypeTraitsTest, is_pointer) { |
| 454 | EXPECT_FALSE(is_pointer<int>::value); |
| 455 | EXPECT_FALSE(is_pointer<char&>::value); |
| 456 | EXPECT_TRUE(is_pointer<const int*>::value); |
| 457 | } |
| 458 | |
| 459 | TEST(TypeTraitsTest, type_equals) { |
| 460 | EXPECT_FALSE((type_equals<int, const int>::value)); |
| 461 | EXPECT_FALSE((type_equals<int, int&>::value)); |
| 462 | EXPECT_FALSE((type_equals<int, double>::value)); |
| 463 | EXPECT_TRUE((type_equals<char, char>::value)); |
| 464 | } |
| 465 | |
| 466 | TEST(TypeTraitsTest, remove_reference) { |
| 467 | EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value)); |
| 468 | EXPECT_TRUE((type_equals<const int, |
| 469 | remove_reference<const int&>::type>::value)); |
| 470 | EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value)); |
| 471 | EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value)); |
| 472 | } |
| 473 | |
| 474 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 475 | #if 0 |
| 476 | |
| 477 | // Verifies that Log() behaves correctly for the given verbosity level |
| 478 | // and log severity. |
| 479 | string GrabOutput(void(*logger)(), const char* verbosity) { |
| 480 | const string saved_flag = GMOCK_FLAG(verbose); |
| 481 | GMOCK_FLAG(verbose) = verbosity; |
| 482 | CaptureTestStdout(); |
| 483 | logger(); |
| 484 | GMOCK_FLAG(verbose) = saved_flag; |
| 485 | return GetCapturedTestStdout(); |
| 486 | } |
| 487 | |
| 488 | class DummyMock { |
| 489 | public: |
| 490 | MOCK_METHOD0(TestMethod, void()); |
| 491 | MOCK_METHOD1(TestMethodArg, void(int dummy)); |
| 492 | }; |
| 493 | |
| 494 | void ExpectCallLogger() { |
| 495 | DummyMock mock; |
| 496 | EXPECT_CALL(mock, TestMethod()); |
| 497 | mock.TestMethod(); |
| 498 | }; |
| 499 | |
| 500 | // Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info". |
| 501 | TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) { |
| 502 | EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity), |
| 503 | HasSubstr("EXPECT_CALL(mock, TestMethod())")); |
| 504 | } |
| 505 | |
| 506 | // Verifies that EXPECT_CALL doesn't log |
| 507 | // if the --gmock_verbose flag is set to "warning". |
| 508 | TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) { |
| 509 | EXPECT_EQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity)); |
| 510 | } |
| 511 | |
| 512 | // Verifies that EXPECT_CALL doesn't log |
| 513 | // if the --gmock_verbose flag is set to "error". |
| 514 | TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) { |
| 515 | EXPECT_EQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity)); |
| 516 | } |
| 517 | |
| 518 | void OnCallLogger() { |
| 519 | DummyMock mock; |
| 520 | ON_CALL(mock, TestMethod()); |
| 521 | }; |
| 522 | |
| 523 | // Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info". |
| 524 | TEST(OnCallTest, LogsWhenVerbosityIsInfo) { |
| 525 | EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity), |
| 526 | HasSubstr("ON_CALL(mock, TestMethod())")); |
| 527 | } |
| 528 | |
| 529 | // Verifies that ON_CALL doesn't log |
| 530 | // if the --gmock_verbose flag is set to "warning". |
| 531 | TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) { |
| 532 | EXPECT_EQ("", GrabOutput(OnCallLogger, kWarningVerbosity)); |
| 533 | } |
| 534 | |
| 535 | // Verifies that ON_CALL doesn't log if |
| 536 | // the --gmock_verbose flag is set to "error". |
| 537 | TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) { |
| 538 | EXPECT_EQ("", GrabOutput(OnCallLogger, kErrorVerbosity)); |
| 539 | } |
| 540 | |
| 541 | void OnCallAnyArgumentLogger() { |
| 542 | DummyMock mock; |
| 543 | ON_CALL(mock, TestMethodArg(_)); |
| 544 | } |
| 545 | |
| 546 | // Verifies that ON_CALL prints provided _ argument. |
| 547 | TEST(OnCallTest, LogsAnythingArgument) { |
| 548 | EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity), |
| 549 | HasSubstr("ON_CALL(mock, TestMethodArg(_)")); |
| 550 | } |
| 551 | |
| 552 | #endif // 0 |
| 553 | |
| 554 | } // namespace |
| 555 | } // namespace internal |
| 556 | } // namespace testing |