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