shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1 | // Copyright 2005, 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 | // Tests for Google Test itself. This verifies that the basic constructs of |
| 33 | // Google Test work. |
| 34 | |
| 35 | #include <gtest/gtest.h> |
| 36 | #include <gtest/gtest-spi.h> |
| 37 | |
| 38 | // Indicates that this translation unit is part of Google Test's |
| 39 | // implementation. It must come before gtest-internal-inl.h is |
| 40 | // included, or there will be a compiler error. This trick is to |
| 41 | // prevent a user from accidentally including gtest-internal-inl.h in |
| 42 | // his code. |
| 43 | #define GTEST_IMPLEMENTATION |
| 44 | #include "src/gtest-internal-inl.h" |
| 45 | #undef GTEST_IMPLEMENTATION |
| 46 | |
| 47 | #include <stdlib.h> |
| 48 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 49 | #if GTEST_HAS_PTHREAD |
| 50 | #include <pthread.h> |
| 51 | #endif // GTEST_HAS_PTHREAD |
| 52 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 53 | #ifdef GTEST_OS_LINUX |
| 54 | #include <string.h> |
| 55 | #include <signal.h> |
| 56 | #include <sys/stat.h> |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 57 | #include <unistd.h> |
| 58 | #include <string> |
| 59 | #include <vector> |
| 60 | #endif // GTEST_OS_LINUX |
| 61 | |
| 62 | namespace testing { |
| 63 | namespace internal { |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 64 | const char* FormatTimeInMillisAsSeconds(TimeInMillis ms); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 65 | bool ParseInt32Flag(const char* str, const char* flag, Int32* value); |
| 66 | } // namespace internal |
| 67 | } // namespace testing |
| 68 | |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 69 | using testing::internal::FormatTimeInMillisAsSeconds; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 70 | using testing::internal::ParseInt32Flag; |
| 71 | |
| 72 | namespace testing { |
| 73 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 74 | GTEST_DECLARE_string_(output); |
| 75 | GTEST_DECLARE_string_(color); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 76 | |
| 77 | namespace internal { |
| 78 | bool ShouldUseColor(bool stdout_is_tty); |
| 79 | } // namespace internal |
| 80 | } // namespace testing |
| 81 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 82 | using testing::AssertionFailure; |
| 83 | using testing::AssertionResult; |
| 84 | using testing::AssertionSuccess; |
| 85 | using testing::DoubleLE; |
| 86 | using testing::FloatLE; |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 87 | using testing::GTEST_FLAG(also_run_disabled_tests); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 88 | using testing::GTEST_FLAG(break_on_failure); |
| 89 | using testing::GTEST_FLAG(catch_exceptions); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 90 | using testing::GTEST_FLAG(death_test_use_fork); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 91 | using testing::GTEST_FLAG(color); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 92 | using testing::GTEST_FLAG(filter); |
| 93 | using testing::GTEST_FLAG(list_tests); |
| 94 | using testing::GTEST_FLAG(output); |
| 95 | using testing::GTEST_FLAG(print_time); |
| 96 | using testing::GTEST_FLAG(repeat); |
| 97 | using testing::GTEST_FLAG(show_internal_stack_frames); |
| 98 | using testing::GTEST_FLAG(stack_trace_depth); |
| 99 | using testing::IsNotSubstring; |
| 100 | using testing::IsSubstring; |
| 101 | using testing::Message; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 102 | using testing::ScopedFakeTestPartResultReporter; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 103 | using testing::StaticAssertTypeEq; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 104 | using testing::Test; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 105 | using testing::TestPartResult; |
| 106 | using testing::TestPartResultArray; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 107 | using testing::TPRT_FATAL_FAILURE; |
| 108 | using testing::TPRT_NONFATAL_FAILURE; |
| 109 | using testing::TPRT_SUCCESS; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 110 | using testing::UnitTest; |
shiqian | fe6a9a4 | 2008-11-24 20:13:22 +0000 | [diff] [blame] | 111 | using testing::internal::kTestTypeIdInGoogleTest; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 112 | using testing::internal::AppendUserMessage; |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 113 | using testing::internal::CodePointToUtf8; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 114 | using testing::internal::EqFailure; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 115 | using testing::internal::FloatingPoint; |
vladlosev | f904a61 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 116 | using testing::internal::GetCurrentOsStackTraceExceptTop; |
| 117 | using testing::internal::GetFailedPartCount; |
shiqian | fe6a9a4 | 2008-11-24 20:13:22 +0000 | [diff] [blame] | 118 | using testing::internal::GetTestTypeId; |
| 119 | using testing::internal::GetTypeId; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 120 | using testing::internal::GTestFlagSaver; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 121 | using testing::internal::Int32; |
| 122 | using testing::internal::List; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 123 | using testing::internal::ShouldUseColor; |
| 124 | using testing::internal::StreamableToString; |
| 125 | using testing::internal::String; |
| 126 | using testing::internal::TestProperty; |
| 127 | using testing::internal::TestResult; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 128 | using testing::internal::ThreadLocal; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 129 | using testing::internal::UnitTestImpl; |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 130 | using testing::internal::WideStringToUtf8; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 131 | |
| 132 | // This line tests that we can define tests in an unnamed namespace. |
| 133 | namespace { |
| 134 | |
shiqian | fe6a9a4 | 2008-11-24 20:13:22 +0000 | [diff] [blame] | 135 | // Tests GetTypeId. |
| 136 | |
| 137 | TEST(GetTypeIdTest, ReturnsSameValueForSameType) { |
| 138 | EXPECT_EQ(GetTypeId<int>(), GetTypeId<int>()); |
| 139 | EXPECT_EQ(GetTypeId<Test>(), GetTypeId<Test>()); |
| 140 | } |
| 141 | |
| 142 | class SubClassOfTest : public Test {}; |
| 143 | class AnotherSubClassOfTest : public Test {}; |
| 144 | |
| 145 | TEST(GetTypeIdTest, ReturnsDifferentValuesForDifferentTypes) { |
| 146 | EXPECT_NE(GetTypeId<int>(), GetTypeId<const int>()); |
| 147 | EXPECT_NE(GetTypeId<int>(), GetTypeId<char>()); |
| 148 | EXPECT_NE(GetTypeId<int>(), GetTestTypeId()); |
| 149 | EXPECT_NE(GetTypeId<SubClassOfTest>(), GetTestTypeId()); |
| 150 | EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTestTypeId()); |
| 151 | EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTypeId<SubClassOfTest>()); |
| 152 | } |
| 153 | |
| 154 | // Verifies that GetTestTypeId() returns the same value, no matter it |
| 155 | // is called from inside Google Test or outside of it. |
| 156 | TEST(GetTestTypeIdTest, ReturnsTheSameValueInsideOrOutsideOfGoogleTest) { |
| 157 | EXPECT_EQ(kTestTypeIdInGoogleTest, GetTestTypeId()); |
| 158 | } |
| 159 | |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 160 | // Tests FormatTimeInMillisAsSeconds(). |
| 161 | |
| 162 | TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) { |
| 163 | EXPECT_STREQ("0", FormatTimeInMillisAsSeconds(0)); |
| 164 | } |
| 165 | |
| 166 | TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) { |
| 167 | EXPECT_STREQ("0.003", FormatTimeInMillisAsSeconds(3)); |
| 168 | EXPECT_STREQ("0.01", FormatTimeInMillisAsSeconds(10)); |
| 169 | EXPECT_STREQ("0.2", FormatTimeInMillisAsSeconds(200)); |
| 170 | EXPECT_STREQ("1.2", FormatTimeInMillisAsSeconds(1200)); |
| 171 | EXPECT_STREQ("3", FormatTimeInMillisAsSeconds(3000)); |
| 172 | } |
| 173 | |
| 174 | TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) { |
| 175 | EXPECT_STREQ("-0.003", FormatTimeInMillisAsSeconds(-3)); |
| 176 | EXPECT_STREQ("-0.01", FormatTimeInMillisAsSeconds(-10)); |
| 177 | EXPECT_STREQ("-0.2", FormatTimeInMillisAsSeconds(-200)); |
| 178 | EXPECT_STREQ("-1.2", FormatTimeInMillisAsSeconds(-1200)); |
| 179 | EXPECT_STREQ("-3", FormatTimeInMillisAsSeconds(-3000)); |
| 180 | } |
| 181 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 182 | #ifndef GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 183 | // NULL testing does not work with Symbian compilers. |
| 184 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 185 | // Tests that GTEST_IS_NULL_LITERAL_(x) is true when x is a null |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 186 | // pointer literal. |
| 187 | TEST(NullLiteralTest, IsTrueForNullLiterals) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 188 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(NULL)); |
| 189 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0)); |
| 190 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(1 - 1)); |
| 191 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0U)); |
| 192 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0L)); |
| 193 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(false)); |
| 194 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(true && false)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 195 | } |
| 196 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 197 | // Tests that GTEST_IS_NULL_LITERAL_(x) is false when x is not a null |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 198 | // pointer literal. |
| 199 | TEST(NullLiteralTest, IsFalseForNonNullLiterals) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 200 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(1)); |
| 201 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(0.0)); |
| 202 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_('a')); |
| 203 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(static_cast<void*>(NULL))); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 204 | } |
| 205 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 206 | #endif // GTEST_OS_SYMBIAN |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 207 | // |
| 208 | // Tests CodePointToUtf8(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 209 | |
| 210 | // Tests that the NUL character L'\0' is encoded correctly. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 211 | TEST(CodePointToUtf8Test, CanEncodeNul) { |
| 212 | char buffer[32]; |
| 213 | EXPECT_STREQ("", CodePointToUtf8(L'\0', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Tests that ASCII characters are encoded correctly. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 217 | TEST(CodePointToUtf8Test, CanEncodeAscii) { |
| 218 | char buffer[32]; |
| 219 | EXPECT_STREQ("a", CodePointToUtf8(L'a', buffer)); |
| 220 | EXPECT_STREQ("Z", CodePointToUtf8(L'Z', buffer)); |
| 221 | EXPECT_STREQ("&", CodePointToUtf8(L'&', buffer)); |
| 222 | EXPECT_STREQ("\x7F", CodePointToUtf8(L'\x7F', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Tests that Unicode code-points that have 8 to 11 bits are encoded |
| 226 | // as 110xxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 227 | TEST(CodePointToUtf8Test, CanEncode8To11Bits) { |
| 228 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 229 | // 000 1101 0011 => 110-00011 10-010011 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 230 | EXPECT_STREQ("\xC3\x93", CodePointToUtf8(L'\xD3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 231 | |
| 232 | // 101 0111 0110 => 110-10101 10-110110 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 233 | EXPECT_STREQ("\xD5\xB6", CodePointToUtf8(L'\x576', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Tests that Unicode code-points that have 12 to 16 bits are encoded |
| 237 | // as 1110xxxx 10xxxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 238 | TEST(CodePointToUtf8Test, CanEncode12To16Bits) { |
| 239 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 240 | // 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 241 | EXPECT_STREQ("\xE0\xA3\x93", CodePointToUtf8(L'\x8D3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 242 | |
| 243 | // 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 244 | EXPECT_STREQ("\xEC\x9D\x8D", CodePointToUtf8(L'\xC74D', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 245 | } |
| 246 | |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 247 | #ifndef GTEST_WIDE_STRING_USES_UTF16_ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 248 | // Tests in this group require a wchar_t to hold > 16 bits, and thus |
shiqian | 4f1d72e | 2008-07-09 20:58:26 +0000 | [diff] [blame] | 249 | // are skipped on Windows, Cygwin, and Symbian, where a wchar_t is |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 250 | // 16-bit wide. This code may not compile on those systems. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 251 | |
| 252 | // Tests that Unicode code-points that have 17 to 21 bits are encoded |
| 253 | // as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 254 | TEST(CodePointToUtf8Test, CanEncode17To21Bits) { |
| 255 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 256 | // 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 257 | EXPECT_STREQ("\xF0\x90\xA3\x93", CodePointToUtf8(L'\x108D3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 258 | |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 259 | // 0 0001 0000 0100 0000 0000 => 11110-000 10-010000 10-010000 10-000000 |
| 260 | EXPECT_STREQ("\xF0\x90\x90\x80", CodePointToUtf8(L'\x10400', buffer)); |
| 261 | |
| 262 | // 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100 |
| 263 | EXPECT_STREQ("\xF4\x88\x98\xB4", CodePointToUtf8(L'\x108634', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Tests that encoding an invalid code-point generates the expected result. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 267 | TEST(CodePointToUtf8Test, CanEncodeInvalidCodePoint) { |
| 268 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 269 | EXPECT_STREQ("(Invalid Unicode 0x1234ABCD)", |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 270 | CodePointToUtf8(L'\x1234ABCD', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 271 | } |
| 272 | |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 273 | #endif // GTEST_WIDE_STRING_USES_UTF16_ |
| 274 | |
| 275 | // Tests WideStringToUtf8(). |
| 276 | |
| 277 | // Tests that the NUL character L'\0' is encoded correctly. |
| 278 | TEST(WideStringToUtf8Test, CanEncodeNul) { |
| 279 | EXPECT_STREQ("", WideStringToUtf8(L"", 0).c_str()); |
| 280 | EXPECT_STREQ("", WideStringToUtf8(L"", -1).c_str()); |
| 281 | } |
| 282 | |
| 283 | // Tests that ASCII strings are encoded correctly. |
| 284 | TEST(WideStringToUtf8Test, CanEncodeAscii) { |
| 285 | EXPECT_STREQ("a", WideStringToUtf8(L"a", 1).c_str()); |
| 286 | EXPECT_STREQ("ab", WideStringToUtf8(L"ab", 2).c_str()); |
| 287 | EXPECT_STREQ("a", WideStringToUtf8(L"a", -1).c_str()); |
| 288 | EXPECT_STREQ("ab", WideStringToUtf8(L"ab", -1).c_str()); |
| 289 | } |
| 290 | |
| 291 | // Tests that Unicode code-points that have 8 to 11 bits are encoded |
| 292 | // as 110xxxxx 10xxxxxx. |
| 293 | TEST(WideStringToUtf8Test, CanEncode8To11Bits) { |
| 294 | // 000 1101 0011 => 110-00011 10-010011 |
| 295 | EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", 1).c_str()); |
| 296 | EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", -1).c_str()); |
| 297 | |
| 298 | // 101 0111 0110 => 110-10101 10-110110 |
| 299 | EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(L"\x576", 1).c_str()); |
| 300 | EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(L"\x576", -1).c_str()); |
| 301 | } |
| 302 | |
| 303 | // Tests that Unicode code-points that have 12 to 16 bits are encoded |
| 304 | // as 1110xxxx 10xxxxxx 10xxxxxx. |
| 305 | TEST(WideStringToUtf8Test, CanEncode12To16Bits) { |
| 306 | // 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011 |
| 307 | EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(L"\x8D3", 1).c_str()); |
| 308 | EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(L"\x8D3", -1).c_str()); |
| 309 | |
| 310 | // 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101 |
| 311 | EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(L"\xC74D", 1).c_str()); |
| 312 | EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(L"\xC74D", -1).c_str()); |
| 313 | } |
| 314 | |
| 315 | // Tests that the conversion stops when the function encounters \0 character. |
| 316 | TEST(WideStringToUtf8Test, StopsOnNulCharacter) { |
| 317 | EXPECT_STREQ("ABC", WideStringToUtf8(L"ABC\0XYZ", 100).c_str()); |
| 318 | } |
| 319 | |
| 320 | // Tests that the conversion stops when the function reaches the limit |
| 321 | // specified by the 'length' parameter. |
| 322 | TEST(WideStringToUtf8Test, StopsWhenLengthLimitReached) { |
| 323 | EXPECT_STREQ("ABC", WideStringToUtf8(L"ABCDEF", 3).c_str()); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | #ifndef GTEST_WIDE_STRING_USES_UTF16_ |
| 328 | // Tests that Unicode code-points that have 17 to 21 bits are encoded |
| 329 | // as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. This code may not compile |
| 330 | // on the systems using UTF-16 encoding. |
| 331 | TEST(WideStringToUtf8Test, CanEncode17To21Bits) { |
| 332 | // 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011 |
| 333 | EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", 1).c_str()); |
| 334 | EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", -1).c_str()); |
| 335 | |
| 336 | // 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100 |
| 337 | EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", 1).c_str()); |
| 338 | EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", -1).c_str()); |
| 339 | } |
| 340 | |
| 341 | // Tests that encoding an invalid code-point generates the expected result. |
| 342 | TEST(WideStringToUtf8Test, CanEncodeInvalidCodePoint) { |
| 343 | EXPECT_STREQ("(Invalid Unicode 0xABCDFF)", |
| 344 | WideStringToUtf8(L"\xABCDFF", -1).c_str()); |
| 345 | } |
| 346 | #else |
| 347 | // Tests that surrogate pairs are encoded correctly on the systems using |
| 348 | // UTF-16 encoding in the wide strings. |
| 349 | TEST(WideStringToUtf8Test, CanEncodeValidUtf16SUrrogatePairs) { |
| 350 | EXPECT_STREQ("\xF0\x90\x90\x80", |
| 351 | WideStringToUtf8(L"\xD801\xDC00", -1).c_str()); |
| 352 | } |
| 353 | |
| 354 | // Tests that encoding an invalid UTF-16 surrogate pair |
| 355 | // generates the expected result. |
| 356 | TEST(WideStringToUtf8Test, CanEncodeInvalidUtf16SurrogatePair) { |
| 357 | // Leading surrogate is at the end of the string. |
| 358 | EXPECT_STREQ("\xED\xA0\x80", WideStringToUtf8(L"\xD800", -1).c_str()); |
| 359 | // Leading surrogate is not followed by the trailing surrogate. |
| 360 | EXPECT_STREQ("\xED\xA0\x80$", WideStringToUtf8(L"\xD800$", -1).c_str()); |
| 361 | // Trailing surrogate appearas without a leading surrogate. |
| 362 | EXPECT_STREQ("\xED\xB0\x80PQR", WideStringToUtf8(L"\xDC00PQR", -1).c_str()); |
| 363 | } |
| 364 | #endif // GTEST_WIDE_STRING_USES_UTF16_ |
| 365 | |
| 366 | // Tests that codepoint concatenation works correctly. |
| 367 | #ifndef GTEST_WIDE_STRING_USES_UTF16_ |
| 368 | TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) { |
| 369 | EXPECT_STREQ( |
| 370 | "\xF4\x88\x98\xB4" |
| 371 | "\xEC\x9D\x8D" |
| 372 | "\n" |
| 373 | "\xD5\xB6" |
| 374 | "\xE0\xA3\x93" |
| 375 | "\xF4\x88\x98\xB4", |
| 376 | WideStringToUtf8(L"\x108634\xC74D\n\x576\x8D3\x108634", -1).c_str()); |
| 377 | } |
| 378 | #else |
| 379 | TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) { |
| 380 | EXPECT_STREQ( |
| 381 | "\xEC\x9D\x8D" "\n" "\xD5\xB6" "\xE0\xA3\x93", |
| 382 | WideStringToUtf8(L"\xC74D\n\x576\x8D3", -1).c_str()); |
| 383 | } |
| 384 | #endif // GTEST_WIDE_STRING_USES_UTF16_ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 385 | |
| 386 | // Tests the List template class. |
| 387 | |
| 388 | // Tests List::PushFront(). |
| 389 | TEST(ListTest, PushFront) { |
| 390 | List<int> a; |
| 391 | ASSERT_EQ(0u, a.size()); |
| 392 | |
| 393 | // Calls PushFront() on an empty list. |
| 394 | a.PushFront(1); |
| 395 | ASSERT_EQ(1u, a.size()); |
| 396 | EXPECT_EQ(1, a.Head()->element()); |
| 397 | ASSERT_EQ(a.Head(), a.Last()); |
| 398 | |
| 399 | // Calls PushFront() on a singleton list. |
| 400 | a.PushFront(2); |
| 401 | ASSERT_EQ(2u, a.size()); |
| 402 | EXPECT_EQ(2, a.Head()->element()); |
| 403 | EXPECT_EQ(1, a.Last()->element()); |
| 404 | |
| 405 | // Calls PushFront() on a list with more than one elements. |
| 406 | a.PushFront(3); |
| 407 | ASSERT_EQ(3u, a.size()); |
| 408 | EXPECT_EQ(3, a.Head()->element()); |
| 409 | EXPECT_EQ(2, a.Head()->next()->element()); |
| 410 | EXPECT_EQ(1, a.Last()->element()); |
| 411 | } |
| 412 | |
| 413 | // Tests List::PopFront(). |
| 414 | TEST(ListTest, PopFront) { |
| 415 | List<int> a; |
| 416 | |
| 417 | // Popping on an empty list should fail. |
| 418 | EXPECT_FALSE(a.PopFront(NULL)); |
| 419 | |
| 420 | // Popping again on an empty list should fail, and the result element |
| 421 | // shouldn't be overwritten. |
| 422 | int element = 1; |
| 423 | EXPECT_FALSE(a.PopFront(&element)); |
| 424 | EXPECT_EQ(1, element); |
| 425 | |
| 426 | a.PushFront(2); |
| 427 | a.PushFront(3); |
| 428 | |
| 429 | // PopFront() should pop the element in the front of the list. |
| 430 | EXPECT_TRUE(a.PopFront(&element)); |
| 431 | EXPECT_EQ(3, element); |
| 432 | |
| 433 | // After popping the last element, the list should be empty. |
| 434 | EXPECT_TRUE(a.PopFront(NULL)); |
| 435 | EXPECT_EQ(0u, a.size()); |
| 436 | } |
| 437 | |
| 438 | // Tests inserting at the beginning using List::InsertAfter(). |
| 439 | TEST(ListTest, InsertAfterAtBeginning) { |
| 440 | List<int> a; |
| 441 | ASSERT_EQ(0u, a.size()); |
| 442 | |
| 443 | // Inserts into an empty list. |
| 444 | a.InsertAfter(NULL, 1); |
| 445 | ASSERT_EQ(1u, a.size()); |
| 446 | EXPECT_EQ(1, a.Head()->element()); |
| 447 | ASSERT_EQ(a.Head(), a.Last()); |
| 448 | |
| 449 | // Inserts at the beginning of a singleton list. |
| 450 | a.InsertAfter(NULL, 2); |
| 451 | ASSERT_EQ(2u, a.size()); |
| 452 | EXPECT_EQ(2, a.Head()->element()); |
| 453 | EXPECT_EQ(1, a.Last()->element()); |
| 454 | |
| 455 | // Inserts at the beginning of a list with more than one elements. |
| 456 | a.InsertAfter(NULL, 3); |
| 457 | ASSERT_EQ(3u, a.size()); |
| 458 | EXPECT_EQ(3, a.Head()->element()); |
| 459 | EXPECT_EQ(2, a.Head()->next()->element()); |
| 460 | EXPECT_EQ(1, a.Last()->element()); |
| 461 | } |
| 462 | |
| 463 | // Tests inserting at a location other than the beginning using |
| 464 | // List::InsertAfter(). |
| 465 | TEST(ListTest, InsertAfterNotAtBeginning) { |
| 466 | // Prepares a singleton list. |
| 467 | List<int> a; |
| 468 | a.PushBack(1); |
| 469 | |
| 470 | // Inserts at the end of a singleton list. |
| 471 | a.InsertAfter(a.Last(), 2); |
| 472 | ASSERT_EQ(2u, a.size()); |
| 473 | EXPECT_EQ(1, a.Head()->element()); |
| 474 | EXPECT_EQ(2, a.Last()->element()); |
| 475 | |
| 476 | // Inserts at the end of a list with more than one elements. |
| 477 | a.InsertAfter(a.Last(), 3); |
| 478 | ASSERT_EQ(3u, a.size()); |
| 479 | EXPECT_EQ(1, a.Head()->element()); |
| 480 | EXPECT_EQ(2, a.Head()->next()->element()); |
| 481 | EXPECT_EQ(3, a.Last()->element()); |
| 482 | |
| 483 | // Inserts in the middle of a list. |
| 484 | a.InsertAfter(a.Head(), 4); |
| 485 | ASSERT_EQ(4u, a.size()); |
| 486 | EXPECT_EQ(1, a.Head()->element()); |
| 487 | EXPECT_EQ(4, a.Head()->next()->element()); |
| 488 | EXPECT_EQ(2, a.Head()->next()->next()->element()); |
| 489 | EXPECT_EQ(3, a.Last()->element()); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | // Tests the String class. |
| 494 | |
| 495 | // Tests String's constructors. |
| 496 | TEST(StringTest, Constructors) { |
| 497 | // Default ctor. |
| 498 | String s1; |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 499 | // We aren't using EXPECT_EQ(NULL, s1.c_str()) because comparing |
| 500 | // pointers with NULL isn't supported on all platforms. |
| 501 | EXPECT_TRUE(NULL == s1.c_str()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 502 | |
| 503 | // Implicitly constructs from a C-string. |
| 504 | String s2 = "Hi"; |
| 505 | EXPECT_STREQ("Hi", s2.c_str()); |
| 506 | |
| 507 | // Constructs from a C-string and a length. |
| 508 | String s3("hello", 3); |
| 509 | EXPECT_STREQ("hel", s3.c_str()); |
| 510 | |
| 511 | // Copy ctor. |
| 512 | String s4 = s3; |
| 513 | EXPECT_STREQ("hel", s4.c_str()); |
| 514 | } |
| 515 | |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 516 | #if GTEST_HAS_STD_STRING |
| 517 | |
| 518 | TEST(StringTest, ConvertsFromStdString) { |
| 519 | // An empty std::string. |
| 520 | const std::string src1(""); |
| 521 | const String dest1 = src1; |
| 522 | EXPECT_STREQ("", dest1.c_str()); |
| 523 | |
| 524 | // A normal std::string. |
| 525 | const std::string src2("Hi"); |
| 526 | const String dest2 = src2; |
| 527 | EXPECT_STREQ("Hi", dest2.c_str()); |
| 528 | |
| 529 | // An std::string with an embedded NUL character. |
| 530 | const char src3[] = "Hello\0world."; |
| 531 | const String dest3 = std::string(src3, sizeof(src3)); |
| 532 | EXPECT_STREQ("Hello", dest3.c_str()); |
| 533 | } |
| 534 | |
| 535 | TEST(StringTest, ConvertsToStdString) { |
| 536 | // An empty String. |
| 537 | const String src1(""); |
| 538 | const std::string dest1 = src1; |
| 539 | EXPECT_EQ("", dest1); |
| 540 | |
| 541 | // A normal String. |
| 542 | const String src2("Hi"); |
| 543 | const std::string dest2 = src2; |
| 544 | EXPECT_EQ("Hi", dest2); |
| 545 | } |
| 546 | |
| 547 | #endif // GTEST_HAS_STD_STRING |
| 548 | |
| 549 | #if GTEST_HAS_GLOBAL_STRING |
| 550 | |
| 551 | TEST(StringTest, ConvertsFromGlobalString) { |
| 552 | // An empty ::string. |
| 553 | const ::string src1(""); |
| 554 | const String dest1 = src1; |
| 555 | EXPECT_STREQ("", dest1.c_str()); |
| 556 | |
| 557 | // A normal ::string. |
| 558 | const ::string src2("Hi"); |
| 559 | const String dest2 = src2; |
| 560 | EXPECT_STREQ("Hi", dest2.c_str()); |
| 561 | |
| 562 | // An ::string with an embedded NUL character. |
| 563 | const char src3[] = "Hello\0world."; |
| 564 | const String dest3 = ::string(src3, sizeof(src3)); |
| 565 | EXPECT_STREQ("Hello", dest3.c_str()); |
| 566 | } |
| 567 | |
| 568 | TEST(StringTest, ConvertsToGlobalString) { |
| 569 | // An empty String. |
| 570 | const String src1(""); |
| 571 | const ::string dest1 = src1; |
| 572 | EXPECT_EQ("", dest1); |
| 573 | |
| 574 | // A normal String. |
| 575 | const String src2("Hi"); |
| 576 | const ::string dest2 = src2; |
| 577 | EXPECT_EQ("Hi", dest2); |
| 578 | } |
| 579 | |
| 580 | #endif // GTEST_HAS_GLOBAL_STRING |
| 581 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 582 | // Tests String::ShowCString(). |
| 583 | TEST(StringTest, ShowCString) { |
| 584 | EXPECT_STREQ("(null)", String::ShowCString(NULL)); |
| 585 | EXPECT_STREQ("", String::ShowCString("")); |
| 586 | EXPECT_STREQ("foo", String::ShowCString("foo")); |
| 587 | } |
| 588 | |
| 589 | // Tests String::ShowCStringQuoted(). |
| 590 | TEST(StringTest, ShowCStringQuoted) { |
| 591 | EXPECT_STREQ("(null)", |
| 592 | String::ShowCStringQuoted(NULL).c_str()); |
| 593 | EXPECT_STREQ("\"\"", |
| 594 | String::ShowCStringQuoted("").c_str()); |
| 595 | EXPECT_STREQ("\"foo\"", |
| 596 | String::ShowCStringQuoted("foo").c_str()); |
| 597 | } |
| 598 | |
| 599 | // Tests String::operator==(). |
| 600 | TEST(StringTest, Equals) { |
| 601 | const String null(NULL); |
| 602 | EXPECT_TRUE(null == NULL); // NOLINT |
| 603 | EXPECT_FALSE(null == ""); // NOLINT |
| 604 | EXPECT_FALSE(null == "bar"); // NOLINT |
| 605 | |
| 606 | const String empty(""); |
| 607 | EXPECT_FALSE(empty == NULL); // NOLINT |
| 608 | EXPECT_TRUE(empty == ""); // NOLINT |
| 609 | EXPECT_FALSE(empty == "bar"); // NOLINT |
| 610 | |
| 611 | const String foo("foo"); |
| 612 | EXPECT_FALSE(foo == NULL); // NOLINT |
| 613 | EXPECT_FALSE(foo == ""); // NOLINT |
| 614 | EXPECT_FALSE(foo == "bar"); // NOLINT |
| 615 | EXPECT_TRUE(foo == "foo"); // NOLINT |
| 616 | } |
| 617 | |
| 618 | // Tests String::operator!=(). |
| 619 | TEST(StringTest, NotEquals) { |
| 620 | const String null(NULL); |
| 621 | EXPECT_FALSE(null != NULL); // NOLINT |
| 622 | EXPECT_TRUE(null != ""); // NOLINT |
| 623 | EXPECT_TRUE(null != "bar"); // NOLINT |
| 624 | |
| 625 | const String empty(""); |
| 626 | EXPECT_TRUE(empty != NULL); // NOLINT |
| 627 | EXPECT_FALSE(empty != ""); // NOLINT |
| 628 | EXPECT_TRUE(empty != "bar"); // NOLINT |
| 629 | |
| 630 | const String foo("foo"); |
| 631 | EXPECT_TRUE(foo != NULL); // NOLINT |
| 632 | EXPECT_TRUE(foo != ""); // NOLINT |
| 633 | EXPECT_TRUE(foo != "bar"); // NOLINT |
| 634 | EXPECT_FALSE(foo != "foo"); // NOLINT |
| 635 | } |
| 636 | |
| 637 | // Tests String::EndsWith(). |
| 638 | TEST(StringTest, EndsWith) { |
| 639 | EXPECT_TRUE(String("foobar").EndsWith("bar")); |
| 640 | EXPECT_TRUE(String("foobar").EndsWith("")); |
| 641 | EXPECT_TRUE(String("").EndsWith("")); |
| 642 | |
| 643 | EXPECT_FALSE(String("foobar").EndsWith("foo")); |
| 644 | EXPECT_FALSE(String("").EndsWith("foo")); |
| 645 | } |
| 646 | |
| 647 | // Tests String::EndsWithCaseInsensitive(). |
| 648 | TEST(StringTest, EndsWithCaseInsensitive) { |
| 649 | EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("BAR")); |
| 650 | EXPECT_TRUE(String("foobaR").EndsWithCaseInsensitive("bar")); |
| 651 | EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("")); |
| 652 | EXPECT_TRUE(String("").EndsWithCaseInsensitive("")); |
| 653 | |
| 654 | EXPECT_FALSE(String("Foobar").EndsWithCaseInsensitive("foo")); |
| 655 | EXPECT_FALSE(String("foobar").EndsWithCaseInsensitive("Foo")); |
| 656 | EXPECT_FALSE(String("").EndsWithCaseInsensitive("foo")); |
| 657 | } |
| 658 | |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 659 | // Tests String::CaseInsensitiveWideCStringEquals |
| 660 | TEST(StringTest, CaseInsensitiveWideCStringEquals) { |
| 661 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(NULL, NULL)); |
| 662 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(NULL, L"")); |
| 663 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"", NULL)); |
| 664 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(NULL, L"foobar")); |
| 665 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"foobar", NULL)); |
| 666 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"foobar")); |
| 667 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"FOOBAR")); |
| 668 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"FOOBAR", L"foobar")); |
| 669 | } |
| 670 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 671 | // Tests that NULL can be assigned to a String. |
| 672 | TEST(StringTest, CanBeAssignedNULL) { |
| 673 | const String src(NULL); |
| 674 | String dest; |
| 675 | |
| 676 | dest = src; |
| 677 | EXPECT_STREQ(NULL, dest.c_str()); |
| 678 | } |
| 679 | |
| 680 | // Tests that the empty string "" can be assigned to a String. |
| 681 | TEST(StringTest, CanBeAssignedEmpty) { |
| 682 | const String src(""); |
| 683 | String dest; |
| 684 | |
| 685 | dest = src; |
| 686 | EXPECT_STREQ("", dest.c_str()); |
| 687 | } |
| 688 | |
| 689 | // Tests that a non-empty string can be assigned to a String. |
| 690 | TEST(StringTest, CanBeAssignedNonEmpty) { |
| 691 | const String src("hello"); |
| 692 | String dest; |
| 693 | |
| 694 | dest = src; |
| 695 | EXPECT_STREQ("hello", dest.c_str()); |
| 696 | } |
| 697 | |
| 698 | // Tests that a String can be assigned to itself. |
| 699 | TEST(StringTest, CanBeAssignedSelf) { |
| 700 | String dest("hello"); |
| 701 | |
| 702 | dest = dest; |
| 703 | EXPECT_STREQ("hello", dest.c_str()); |
| 704 | } |
| 705 | |
| 706 | #ifdef GTEST_OS_WINDOWS |
| 707 | |
| 708 | // Tests String::ShowWideCString(). |
| 709 | TEST(StringTest, ShowWideCString) { |
| 710 | EXPECT_STREQ("(null)", |
| 711 | String::ShowWideCString(NULL).c_str()); |
| 712 | EXPECT_STREQ("", String::ShowWideCString(L"").c_str()); |
| 713 | EXPECT_STREQ("foo", String::ShowWideCString(L"foo").c_str()); |
| 714 | } |
| 715 | |
| 716 | // Tests String::ShowWideCStringQuoted(). |
| 717 | TEST(StringTest, ShowWideCStringQuoted) { |
| 718 | EXPECT_STREQ("(null)", |
| 719 | String::ShowWideCStringQuoted(NULL).c_str()); |
| 720 | EXPECT_STREQ("L\"\"", |
| 721 | String::ShowWideCStringQuoted(L"").c_str()); |
| 722 | EXPECT_STREQ("L\"foo\"", |
| 723 | String::ShowWideCStringQuoted(L"foo").c_str()); |
| 724 | } |
| 725 | |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 726 | #ifdef _WIN32_WCE |
| 727 | TEST(StringTest, AnsiAndUtf16Null) { |
| 728 | EXPECT_EQ(NULL, String::AnsiToUtf16(NULL)); |
| 729 | EXPECT_EQ(NULL, String::Utf16ToAnsi(NULL)); |
| 730 | } |
| 731 | |
| 732 | TEST(StringTest, AnsiAndUtf16ConvertBasic) { |
| 733 | const char* ansi = String::Utf16ToAnsi(L"str"); |
| 734 | EXPECT_STREQ("str", ansi); |
| 735 | delete [] ansi; |
| 736 | const WCHAR* utf16 = String::AnsiToUtf16("str"); |
| 737 | EXPECT_TRUE(wcsncmp(L"str", utf16, 3) == 0); |
| 738 | delete [] utf16; |
| 739 | } |
| 740 | |
| 741 | TEST(StringTest, AnsiAndUtf16ConvertPathChars) { |
| 742 | const char* ansi = String::Utf16ToAnsi(L".:\\ \"*?"); |
| 743 | EXPECT_STREQ(".:\\ \"*?", ansi); |
| 744 | delete [] ansi; |
| 745 | const WCHAR* utf16 = String::AnsiToUtf16(".:\\ \"*?"); |
| 746 | EXPECT_TRUE(wcsncmp(L".:\\ \"*?", utf16, 3) == 0); |
| 747 | delete [] utf16; |
| 748 | } |
| 749 | #endif // _WIN32_WCE |
| 750 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 751 | #endif // GTEST_OS_WINDOWS |
| 752 | |
| 753 | // Tests TestProperty construction. |
| 754 | TEST(TestPropertyTest, StringValue) { |
| 755 | TestProperty property("key", "1"); |
| 756 | EXPECT_STREQ("key", property.key()); |
| 757 | EXPECT_STREQ("1", property.value()); |
| 758 | } |
| 759 | |
| 760 | // Tests TestProperty replacing a value. |
| 761 | TEST(TestPropertyTest, ReplaceStringValue) { |
| 762 | TestProperty property("key", "1"); |
| 763 | EXPECT_STREQ("1", property.value()); |
| 764 | property.SetValue("2"); |
| 765 | EXPECT_STREQ("2", property.value()); |
| 766 | } |
| 767 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 768 | class ScopedFakeTestPartResultReporterTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 769 | protected: |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 770 | enum FailureMode { |
| 771 | FATAL_FAILURE, |
| 772 | NONFATAL_FAILURE |
| 773 | }; |
| 774 | static void AddFailure(FailureMode failure) { |
| 775 | if (failure == FATAL_FAILURE) { |
| 776 | FAIL() << "Expected fatal failure."; |
| 777 | } else { |
| 778 | ADD_FAILURE() << "Expected non-fatal failure."; |
| 779 | } |
| 780 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 781 | }; |
| 782 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 783 | // Tests that ScopedFakeTestPartResultReporter intercepts test |
| 784 | // failures. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 785 | TEST_F(ScopedFakeTestPartResultReporterTest, InterceptsTestFailures) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 786 | TestPartResultArray results; |
| 787 | { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 788 | ScopedFakeTestPartResultReporter reporter( |
| 789 | ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, |
| 790 | &results); |
| 791 | AddFailure(NONFATAL_FAILURE); |
| 792 | AddFailure(FATAL_FAILURE); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | EXPECT_EQ(2, results.size()); |
| 796 | EXPECT_TRUE(results.GetTestPartResult(0).nonfatally_failed()); |
| 797 | EXPECT_TRUE(results.GetTestPartResult(1).fatally_failed()); |
| 798 | } |
| 799 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 800 | TEST_F(ScopedFakeTestPartResultReporterTest, DeprecatedConstructor) { |
| 801 | TestPartResultArray results; |
| 802 | { |
| 803 | // Tests, that the deprecated constructor still works. |
| 804 | ScopedFakeTestPartResultReporter reporter(&results); |
| 805 | AddFailure(NONFATAL_FAILURE); |
| 806 | } |
| 807 | EXPECT_EQ(1, results.size()); |
| 808 | } |
| 809 | |
| 810 | #if GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD |
| 811 | |
| 812 | class ScopedFakeTestPartResultReporterWithThreadsTest |
| 813 | : public ScopedFakeTestPartResultReporterTest { |
| 814 | protected: |
| 815 | static void AddFailureInOtherThread(FailureMode failure) { |
| 816 | pthread_t tid; |
| 817 | pthread_create(&tid, |
| 818 | NULL, |
| 819 | ScopedFakeTestPartResultReporterWithThreadsTest:: |
| 820 | FailureThread, |
| 821 | &failure); |
| 822 | pthread_join(tid, NULL); |
| 823 | } |
| 824 | private: |
| 825 | static void* FailureThread(void* attr) { |
| 826 | FailureMode* failure = static_cast<FailureMode*>(attr); |
| 827 | AddFailure(*failure); |
| 828 | return NULL; |
| 829 | } |
| 830 | }; |
| 831 | |
| 832 | TEST_F(ScopedFakeTestPartResultReporterWithThreadsTest, |
| 833 | InterceptsTestFailuresInAllThreads) { |
| 834 | TestPartResultArray results; |
| 835 | { |
| 836 | ScopedFakeTestPartResultReporter reporter( |
| 837 | ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, &results); |
| 838 | AddFailure(NONFATAL_FAILURE); |
| 839 | AddFailure(FATAL_FAILURE); |
| 840 | AddFailureInOtherThread(NONFATAL_FAILURE); |
| 841 | AddFailureInOtherThread(FATAL_FAILURE); |
| 842 | } |
| 843 | |
| 844 | EXPECT_EQ(4, results.size()); |
| 845 | EXPECT_TRUE(results.GetTestPartResult(0).nonfatally_failed()); |
| 846 | EXPECT_TRUE(results.GetTestPartResult(1).fatally_failed()); |
| 847 | EXPECT_TRUE(results.GetTestPartResult(2).nonfatally_failed()); |
| 848 | EXPECT_TRUE(results.GetTestPartResult(3).fatally_failed()); |
| 849 | } |
| 850 | |
| 851 | #endif // GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD |
| 852 | |
| 853 | // Tests EXPECT_{,NON}FATAL_FAILURE{,ON_ALL_THREADS}. |
| 854 | |
| 855 | typedef ScopedFakeTestPartResultReporterTest ExpectFailureTest; |
| 856 | |
| 857 | TEST_F(ExpectFailureTest, ExpectFatalFaliure) { |
| 858 | EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure."); |
| 859 | } |
| 860 | |
| 861 | TEST_F(ExpectFailureTest, ExpectNonFatalFailure) { |
| 862 | EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), |
| 863 | "Expected non-fatal failure."); |
| 864 | } |
| 865 | |
| 866 | TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) { |
| 867 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), |
| 868 | "Expected fatal failure."); |
| 869 | } |
| 870 | |
| 871 | TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) { |
| 872 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), |
| 873 | "Expected non-fatal failure."); |
| 874 | } |
| 875 | |
| 876 | // Tests that the EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS} accepts |
| 877 | // a statement that contains a macro which expands to code containing |
| 878 | // an unprotected comma. |
| 879 | |
| 880 | static int global_var = 0; |
| 881 | #define GTEST_USE_UNPROTECTED_COMMA_ global_var++, global_var++ |
| 882 | |
| 883 | TEST_F(ExpectFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) { |
| 884 | EXPECT_FATAL_FAILURE({ |
| 885 | GTEST_USE_UNPROTECTED_COMMA_; |
| 886 | AddFailure(FATAL_FAILURE); |
| 887 | }, ""); |
| 888 | |
| 889 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS({ |
| 890 | GTEST_USE_UNPROTECTED_COMMA_; |
| 891 | AddFailure(FATAL_FAILURE); |
| 892 | }, ""); |
| 893 | |
| 894 | EXPECT_NONFATAL_FAILURE({ |
| 895 | GTEST_USE_UNPROTECTED_COMMA_; |
| 896 | AddFailure(NONFATAL_FAILURE); |
| 897 | }, ""); |
| 898 | |
| 899 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS({ |
| 900 | GTEST_USE_UNPROTECTED_COMMA_; |
| 901 | AddFailure(NONFATAL_FAILURE); |
| 902 | }, ""); |
| 903 | } |
| 904 | |
| 905 | #if GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD |
| 906 | |
| 907 | typedef ScopedFakeTestPartResultReporterWithThreadsTest |
| 908 | ExpectFailureWithThreadsTest; |
| 909 | |
| 910 | TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailureOnAllThreads) { |
| 911 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailureInOtherThread(FATAL_FAILURE), |
| 912 | "Expected fatal failure."); |
| 913 | } |
| 914 | |
| 915 | TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailureOnAllThreads) { |
| 916 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS( |
| 917 | AddFailureInOtherThread(NONFATAL_FAILURE), "Expected non-fatal failure."); |
| 918 | } |
| 919 | |
| 920 | #endif // GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD |
| 921 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 922 | // Tests the TestResult class |
| 923 | |
| 924 | // The test fixture for testing TestResult. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 925 | class TestResultTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 926 | protected: |
| 927 | typedef List<TestPartResult> TPRList; |
| 928 | |
| 929 | // We make use of 2 TestPartResult objects, |
| 930 | TestPartResult * pr1, * pr2; |
| 931 | |
| 932 | // ... and 3 TestResult objects. |
| 933 | TestResult * r0, * r1, * r2; |
| 934 | |
| 935 | virtual void SetUp() { |
| 936 | // pr1 is for success. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 937 | pr1 = new TestPartResult(TPRT_SUCCESS, "foo/bar.cc", 10, "Success!"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 938 | |
| 939 | // pr2 is for fatal failure. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 940 | pr2 = new TestPartResult(TPRT_FATAL_FAILURE, "foo/bar.cc", |
| 941 | -1, // This line number means "unknown" |
| 942 | "Failure!"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 943 | |
| 944 | // Creates the TestResult objects. |
| 945 | r0 = new TestResult(); |
| 946 | r1 = new TestResult(); |
| 947 | r2 = new TestResult(); |
| 948 | |
| 949 | // In order to test TestResult, we need to modify its internal |
| 950 | // state, in particular the TestPartResult list it holds. |
| 951 | // test_part_results() returns a const reference to this list. |
| 952 | // We cast it to a non-const object s.t. it can be modified (yes, |
| 953 | // this is a hack). |
| 954 | TPRList * list1, * list2; |
| 955 | list1 = const_cast<List<TestPartResult> *>( |
| 956 | & r1->test_part_results()); |
| 957 | list2 = const_cast<List<TestPartResult> *>( |
| 958 | & r2->test_part_results()); |
| 959 | |
| 960 | // r0 is an empty TestResult. |
| 961 | |
| 962 | // r1 contains a single SUCCESS TestPartResult. |
| 963 | list1->PushBack(*pr1); |
| 964 | |
| 965 | // r2 contains a SUCCESS, and a FAILURE. |
| 966 | list2->PushBack(*pr1); |
| 967 | list2->PushBack(*pr2); |
| 968 | } |
| 969 | |
| 970 | virtual void TearDown() { |
| 971 | delete pr1; |
| 972 | delete pr2; |
| 973 | |
| 974 | delete r0; |
| 975 | delete r1; |
| 976 | delete r2; |
| 977 | } |
| 978 | }; |
| 979 | |
| 980 | // Tests TestResult::test_part_results() |
| 981 | TEST_F(TestResultTest, test_part_results) { |
| 982 | ASSERT_EQ(0u, r0->test_part_results().size()); |
| 983 | ASSERT_EQ(1u, r1->test_part_results().size()); |
| 984 | ASSERT_EQ(2u, r2->test_part_results().size()); |
| 985 | } |
| 986 | |
| 987 | // Tests TestResult::successful_part_count() |
| 988 | TEST_F(TestResultTest, successful_part_count) { |
| 989 | ASSERT_EQ(0u, r0->successful_part_count()); |
| 990 | ASSERT_EQ(1u, r1->successful_part_count()); |
| 991 | ASSERT_EQ(1u, r2->successful_part_count()); |
| 992 | } |
| 993 | |
| 994 | // Tests TestResult::failed_part_count() |
| 995 | TEST_F(TestResultTest, failed_part_count) { |
| 996 | ASSERT_EQ(0u, r0->failed_part_count()); |
| 997 | ASSERT_EQ(0u, r1->failed_part_count()); |
| 998 | ASSERT_EQ(1u, r2->failed_part_count()); |
| 999 | } |
| 1000 | |
vladlosev | f904a61 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 1001 | // Tests testing::internal::GetFailedPartCount(). |
| 1002 | TEST_F(TestResultTest, GetFailedPartCount) { |
| 1003 | ASSERT_EQ(0u, GetFailedPartCount(r0)); |
| 1004 | ASSERT_EQ(0u, GetFailedPartCount(r1)); |
| 1005 | ASSERT_EQ(1u, GetFailedPartCount(r2)); |
| 1006 | } |
| 1007 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1008 | // Tests TestResult::total_part_count() |
| 1009 | TEST_F(TestResultTest, total_part_count) { |
| 1010 | ASSERT_EQ(0u, r0->total_part_count()); |
| 1011 | ASSERT_EQ(1u, r1->total_part_count()); |
| 1012 | ASSERT_EQ(2u, r2->total_part_count()); |
| 1013 | } |
| 1014 | |
| 1015 | // Tests TestResult::Passed() |
| 1016 | TEST_F(TestResultTest, Passed) { |
| 1017 | ASSERT_TRUE(r0->Passed()); |
| 1018 | ASSERT_TRUE(r1->Passed()); |
| 1019 | ASSERT_FALSE(r2->Passed()); |
| 1020 | } |
| 1021 | |
| 1022 | // Tests TestResult::Failed() |
| 1023 | TEST_F(TestResultTest, Failed) { |
| 1024 | ASSERT_FALSE(r0->Failed()); |
| 1025 | ASSERT_FALSE(r1->Failed()); |
| 1026 | ASSERT_TRUE(r2->Failed()); |
| 1027 | } |
| 1028 | |
| 1029 | // Tests TestResult::test_properties() has no properties when none are added. |
| 1030 | TEST(TestResultPropertyTest, NoPropertiesFoundWhenNoneAreAdded) { |
| 1031 | TestResult test_result; |
| 1032 | ASSERT_EQ(0u, test_result.test_properties().size()); |
| 1033 | } |
| 1034 | |
| 1035 | // Tests TestResult::test_properties() has the expected property when added. |
| 1036 | TEST(TestResultPropertyTest, OnePropertyFoundWhenAdded) { |
| 1037 | TestResult test_result; |
| 1038 | TestProperty property("key_1", "1"); |
| 1039 | test_result.RecordProperty(property); |
| 1040 | const List<TestProperty>& properties = test_result.test_properties(); |
| 1041 | ASSERT_EQ(1u, properties.size()); |
| 1042 | TestProperty actual_property = properties.Head()->element(); |
| 1043 | EXPECT_STREQ("key_1", actual_property.key()); |
| 1044 | EXPECT_STREQ("1", actual_property.value()); |
| 1045 | } |
| 1046 | |
| 1047 | // Tests TestResult::test_properties() has multiple properties when added. |
| 1048 | TEST(TestResultPropertyTest, MultiplePropertiesFoundWhenAdded) { |
| 1049 | TestResult test_result; |
| 1050 | TestProperty property_1("key_1", "1"); |
| 1051 | TestProperty property_2("key_2", "2"); |
| 1052 | test_result.RecordProperty(property_1); |
| 1053 | test_result.RecordProperty(property_2); |
| 1054 | const List<TestProperty>& properties = test_result.test_properties(); |
| 1055 | ASSERT_EQ(2u, properties.size()); |
| 1056 | TestProperty actual_property_1 = properties.Head()->element(); |
| 1057 | EXPECT_STREQ("key_1", actual_property_1.key()); |
| 1058 | EXPECT_STREQ("1", actual_property_1.value()); |
| 1059 | |
| 1060 | TestProperty actual_property_2 = properties.Last()->element(); |
| 1061 | EXPECT_STREQ("key_2", actual_property_2.key()); |
| 1062 | EXPECT_STREQ("2", actual_property_2.value()); |
| 1063 | } |
| 1064 | |
| 1065 | // Tests TestResult::test_properties() overrides values for duplicate keys. |
| 1066 | TEST(TestResultPropertyTest, OverridesValuesForDuplicateKeys) { |
| 1067 | TestResult test_result; |
| 1068 | TestProperty property_1_1("key_1", "1"); |
| 1069 | TestProperty property_2_1("key_2", "2"); |
| 1070 | TestProperty property_1_2("key_1", "12"); |
| 1071 | TestProperty property_2_2("key_2", "22"); |
| 1072 | test_result.RecordProperty(property_1_1); |
| 1073 | test_result.RecordProperty(property_2_1); |
| 1074 | test_result.RecordProperty(property_1_2); |
| 1075 | test_result.RecordProperty(property_2_2); |
| 1076 | |
| 1077 | const List<TestProperty>& properties = test_result.test_properties(); |
| 1078 | ASSERT_EQ(2u, properties.size()); |
| 1079 | TestProperty actual_property_1 = properties.Head()->element(); |
| 1080 | EXPECT_STREQ("key_1", actual_property_1.key()); |
| 1081 | EXPECT_STREQ("12", actual_property_1.value()); |
| 1082 | |
| 1083 | TestProperty actual_property_2 = properties.Last()->element(); |
| 1084 | EXPECT_STREQ("key_2", actual_property_2.key()); |
| 1085 | EXPECT_STREQ("22", actual_property_2.value()); |
| 1086 | } |
| 1087 | |
| 1088 | // When a property using a reserved key is supplied to this function, it tests |
| 1089 | // that a non-fatal failure is added, a fatal failure is not added, and that the |
| 1090 | // property is not recorded. |
| 1091 | void ExpectNonFatalFailureRecordingPropertyWithReservedKey(const char* key) { |
| 1092 | TestResult test_result; |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame^] | 1093 | TestProperty property(key, "1"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1094 | EXPECT_NONFATAL_FAILURE(test_result.RecordProperty(property), "Reserved key"); |
| 1095 | ASSERT_TRUE(test_result.test_properties().IsEmpty()) << "Not recorded"; |
| 1096 | } |
| 1097 | |
| 1098 | // Attempting to recording a property with the Reserved literal "name" |
| 1099 | // should add a non-fatal failure and the property should not be recorded. |
| 1100 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledName) { |
| 1101 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("name"); |
| 1102 | } |
| 1103 | |
| 1104 | // Attempting to recording a property with the Reserved literal "status" |
| 1105 | // should add a non-fatal failure and the property should not be recorded. |
| 1106 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledStatus) { |
| 1107 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("status"); |
| 1108 | } |
| 1109 | |
| 1110 | // Attempting to recording a property with the Reserved literal "time" |
| 1111 | // should add a non-fatal failure and the property should not be recorded. |
| 1112 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledTime) { |
| 1113 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("time"); |
| 1114 | } |
| 1115 | |
| 1116 | // Attempting to recording a property with the Reserved literal "classname" |
| 1117 | // should add a non-fatal failure and the property should not be recorded. |
| 1118 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledClassname) { |
| 1119 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("classname"); |
| 1120 | } |
| 1121 | |
| 1122 | // Tests that GTestFlagSaver works on Windows and Mac. |
| 1123 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1124 | class GTestFlagSaverTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1125 | protected: |
| 1126 | // Saves the Google Test flags such that we can restore them later, and |
| 1127 | // then sets them to their default values. This will be called |
| 1128 | // before the first test in this test case is run. |
| 1129 | static void SetUpTestCase() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1130 | saver_ = new GTestFlagSaver; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1131 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 1132 | GTEST_FLAG(also_run_disabled_tests) = false; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1133 | GTEST_FLAG(break_on_failure) = false; |
| 1134 | GTEST_FLAG(catch_exceptions) = false; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 1135 | GTEST_FLAG(death_test_use_fork) = false; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1136 | GTEST_FLAG(color) = "auto"; |
| 1137 | GTEST_FLAG(filter) = ""; |
| 1138 | GTEST_FLAG(list_tests) = false; |
| 1139 | GTEST_FLAG(output) = ""; |
| 1140 | GTEST_FLAG(print_time) = false; |
| 1141 | GTEST_FLAG(repeat) = 1; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | // Restores the Google Test flags that the tests have modified. This will |
| 1145 | // be called after the last test in this test case is run. |
| 1146 | static void TearDownTestCase() { |
| 1147 | delete saver_; |
| 1148 | saver_ = NULL; |
| 1149 | } |
| 1150 | |
| 1151 | // Verifies that the Google Test flags have their default values, and then |
| 1152 | // modifies each of them. |
| 1153 | void VerifyAndModifyFlags() { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 1154 | EXPECT_FALSE(GTEST_FLAG(also_run_disabled_tests)); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1155 | EXPECT_FALSE(GTEST_FLAG(break_on_failure)); |
| 1156 | EXPECT_FALSE(GTEST_FLAG(catch_exceptions)); |
| 1157 | EXPECT_STREQ("auto", GTEST_FLAG(color).c_str()); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 1158 | EXPECT_FALSE(GTEST_FLAG(death_test_use_fork)); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1159 | EXPECT_STREQ("", GTEST_FLAG(filter).c_str()); |
| 1160 | EXPECT_FALSE(GTEST_FLAG(list_tests)); |
| 1161 | EXPECT_STREQ("", GTEST_FLAG(output).c_str()); |
| 1162 | EXPECT_FALSE(GTEST_FLAG(print_time)); |
| 1163 | EXPECT_EQ(1, GTEST_FLAG(repeat)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1164 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 1165 | GTEST_FLAG(also_run_disabled_tests) = true; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1166 | GTEST_FLAG(break_on_failure) = true; |
| 1167 | GTEST_FLAG(catch_exceptions) = true; |
| 1168 | GTEST_FLAG(color) = "no"; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 1169 | GTEST_FLAG(death_test_use_fork) = true; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1170 | GTEST_FLAG(filter) = "abc"; |
| 1171 | GTEST_FLAG(list_tests) = true; |
| 1172 | GTEST_FLAG(output) = "xml:foo.xml"; |
| 1173 | GTEST_FLAG(print_time) = true; |
| 1174 | GTEST_FLAG(repeat) = 100; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1175 | } |
| 1176 | private: |
| 1177 | // For saving Google Test flags during this test case. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1178 | static GTestFlagSaver* saver_; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1179 | }; |
| 1180 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1181 | GTestFlagSaver* GTestFlagSaverTest::saver_ = NULL; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1182 | |
| 1183 | // Google Test doesn't guarantee the order of tests. The following two |
| 1184 | // tests are designed to work regardless of their order. |
| 1185 | |
| 1186 | // Modifies the Google Test flags in the test body. |
| 1187 | TEST_F(GTestFlagSaverTest, ModifyGTestFlags) { |
| 1188 | VerifyAndModifyFlags(); |
| 1189 | } |
| 1190 | |
| 1191 | // Verifies that the Google Test flags in the body of the previous test were |
| 1192 | // restored to their original values. |
| 1193 | TEST_F(GTestFlagSaverTest, VerifyGTestFlags) { |
| 1194 | VerifyAndModifyFlags(); |
| 1195 | } |
| 1196 | |
| 1197 | // Sets an environment variable with the given name to the given |
| 1198 | // value. If the value argument is "", unsets the environment |
| 1199 | // variable. The caller must ensure that both arguments are not NULL. |
| 1200 | static void SetEnv(const char* name, const char* value) { |
| 1201 | #ifdef _WIN32_WCE |
| 1202 | // Environment variables are not supported on Windows CE. |
| 1203 | return; |
| 1204 | #elif defined(GTEST_OS_WINDOWS) // If we are on Windows proper. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1205 | _putenv((Message() << name << "=" << value).GetString().c_str()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1206 | #else |
| 1207 | if (*value == '\0') { |
| 1208 | unsetenv(name); |
| 1209 | } else { |
| 1210 | setenv(name, value, 1); |
| 1211 | } |
| 1212 | #endif |
| 1213 | } |
| 1214 | |
| 1215 | #ifndef _WIN32_WCE |
| 1216 | // Environment variables are not supported on Windows CE. |
| 1217 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1218 | using testing::internal::Int32FromGTestEnv; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1219 | |
| 1220 | // Tests Int32FromGTestEnv(). |
| 1221 | |
| 1222 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 1223 | // environment variable is not set. |
| 1224 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenVariableIsNotSet) { |
| 1225 | SetEnv(GTEST_FLAG_PREFIX_UPPER "TEMP", ""); |
| 1226 | EXPECT_EQ(10, Int32FromGTestEnv("temp", 10)); |
| 1227 | } |
| 1228 | |
| 1229 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 1230 | // environment variable overflows as an Int32. |
| 1231 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueOverflows) { |
| 1232 | printf("(expecting 2 warnings)\n"); |
| 1233 | |
| 1234 | SetEnv(GTEST_FLAG_PREFIX_UPPER "TEMP", "12345678987654321"); |
| 1235 | EXPECT_EQ(20, Int32FromGTestEnv("temp", 20)); |
| 1236 | |
| 1237 | SetEnv(GTEST_FLAG_PREFIX_UPPER "TEMP", "-12345678987654321"); |
| 1238 | EXPECT_EQ(30, Int32FromGTestEnv("temp", 30)); |
| 1239 | } |
| 1240 | |
| 1241 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 1242 | // environment variable does not represent a valid decimal integer. |
| 1243 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueIsInvalid) { |
| 1244 | printf("(expecting 2 warnings)\n"); |
| 1245 | |
| 1246 | SetEnv(GTEST_FLAG_PREFIX_UPPER "TEMP", "A1"); |
| 1247 | EXPECT_EQ(40, Int32FromGTestEnv("temp", 40)); |
| 1248 | |
| 1249 | SetEnv(GTEST_FLAG_PREFIX_UPPER "TEMP", "12X"); |
| 1250 | EXPECT_EQ(50, Int32FromGTestEnv("temp", 50)); |
| 1251 | } |
| 1252 | |
| 1253 | // Tests that Int32FromGTestEnv() parses and returns the value of the |
| 1254 | // environment variable when it represents a valid decimal integer in |
| 1255 | // the range of an Int32. |
| 1256 | TEST(Int32FromGTestEnvTest, ParsesAndReturnsValidValue) { |
| 1257 | SetEnv(GTEST_FLAG_PREFIX_UPPER "TEMP", "123"); |
| 1258 | EXPECT_EQ(123, Int32FromGTestEnv("temp", 0)); |
| 1259 | |
| 1260 | SetEnv(GTEST_FLAG_PREFIX_UPPER "TEMP", "-321"); |
| 1261 | EXPECT_EQ(-321, Int32FromGTestEnv("temp", 0)); |
| 1262 | } |
| 1263 | #endif // !defined(_WIN32_WCE) |
| 1264 | |
| 1265 | // Tests ParseInt32Flag(). |
| 1266 | |
| 1267 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 1268 | // output value when the flag has wrong format |
| 1269 | TEST(ParseInt32FlagTest, ReturnsFalseForInvalidFlag) { |
| 1270 | Int32 value = 123; |
| 1271 | EXPECT_FALSE(ParseInt32Flag("--a=100", "b", &value)); |
| 1272 | EXPECT_EQ(123, value); |
| 1273 | |
| 1274 | EXPECT_FALSE(ParseInt32Flag("a=100", "a", &value)); |
| 1275 | EXPECT_EQ(123, value); |
| 1276 | } |
| 1277 | |
| 1278 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 1279 | // output value when the flag overflows as an Int32. |
| 1280 | TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) { |
| 1281 | printf("(expecting 2 warnings)\n"); |
| 1282 | |
| 1283 | Int32 value = 123; |
| 1284 | EXPECT_FALSE(ParseInt32Flag("--abc=12345678987654321", "abc", &value)); |
| 1285 | EXPECT_EQ(123, value); |
| 1286 | |
| 1287 | EXPECT_FALSE(ParseInt32Flag("--abc=-12345678987654321", "abc", &value)); |
| 1288 | EXPECT_EQ(123, value); |
| 1289 | } |
| 1290 | |
| 1291 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 1292 | // output value when the flag does not represent a valid decimal |
| 1293 | // integer. |
| 1294 | TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) { |
| 1295 | printf("(expecting 2 warnings)\n"); |
| 1296 | |
| 1297 | Int32 value = 123; |
| 1298 | EXPECT_FALSE(ParseInt32Flag("--abc=A1", "abc", &value)); |
| 1299 | EXPECT_EQ(123, value); |
| 1300 | |
| 1301 | EXPECT_FALSE(ParseInt32Flag("--abc=12X", "abc", &value)); |
| 1302 | EXPECT_EQ(123, value); |
| 1303 | } |
| 1304 | |
| 1305 | // Tests that ParseInt32Flag() parses the value of the flag and |
| 1306 | // returns true when the flag represents a valid decimal integer in |
| 1307 | // the range of an Int32. |
| 1308 | TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) { |
| 1309 | Int32 value = 123; |
| 1310 | EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX "abc=456", "abc", &value)); |
| 1311 | EXPECT_EQ(456, value); |
| 1312 | |
| 1313 | EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX "abc=-789", "abc", &value)); |
| 1314 | EXPECT_EQ(-789, value); |
| 1315 | } |
| 1316 | |
| 1317 | // For the same reason we are not explicitly testing everything in the |
shiqian | c3b4de3 | 2008-09-12 04:01:37 +0000 | [diff] [blame] | 1318 | // Test class, there are no separate tests for the following classes |
| 1319 | // (except for some trivial cases): |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1320 | // |
| 1321 | // TestCase, UnitTest, UnitTestResultPrinter. |
| 1322 | // |
| 1323 | // Similarly, there are no separate tests for the following macros: |
| 1324 | // |
| 1325 | // TEST, TEST_F, RUN_ALL_TESTS |
| 1326 | |
shiqian | c3b4de3 | 2008-09-12 04:01:37 +0000 | [diff] [blame] | 1327 | TEST(UnitTestTest, CanGetOriginalWorkingDir) { |
| 1328 | ASSERT_TRUE(UnitTest::GetInstance()->original_working_dir() != NULL); |
| 1329 | EXPECT_STRNE(UnitTest::GetInstance()->original_working_dir(), ""); |
| 1330 | } |
| 1331 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1332 | // This group of tests is for predicate assertions (ASSERT_PRED*, etc) |
| 1333 | // of various arities. They do not attempt to be exhaustive. Rather, |
| 1334 | // view them as smoke tests that can be easily reviewed and verified. |
| 1335 | // A more complete set of tests for predicate assertions can be found |
| 1336 | // in gtest_pred_impl_unittest.cc. |
| 1337 | |
| 1338 | // First, some predicates and predicate-formatters needed by the tests. |
| 1339 | |
| 1340 | // Returns true iff the argument is an even number. |
| 1341 | bool IsEven(int n) { |
| 1342 | return (n % 2) == 0; |
| 1343 | } |
| 1344 | |
| 1345 | // A functor that returns true iff the argument is an even number. |
| 1346 | struct IsEvenFunctor { |
| 1347 | bool operator()(int n) { return IsEven(n); } |
| 1348 | }; |
| 1349 | |
| 1350 | // A predicate-formatter function that asserts the argument is an even |
| 1351 | // number. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1352 | AssertionResult AssertIsEven(const char* expr, int n) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1353 | if (IsEven(n)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1354 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1357 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1358 | msg << expr << " evaluates to " << n << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1359 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | // A predicate-formatter functor that asserts the argument is an even |
| 1363 | // number. |
| 1364 | struct AssertIsEvenFunctor { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1365 | AssertionResult operator()(const char* expr, int n) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1366 | return AssertIsEven(expr, n); |
| 1367 | } |
| 1368 | }; |
| 1369 | |
| 1370 | // Returns true iff the sum of the arguments is an even number. |
| 1371 | bool SumIsEven2(int n1, int n2) { |
| 1372 | return IsEven(n1 + n2); |
| 1373 | } |
| 1374 | |
| 1375 | // A functor that returns true iff the sum of the arguments is an even |
| 1376 | // number. |
| 1377 | struct SumIsEven3Functor { |
| 1378 | bool operator()(int n1, int n2, int n3) { |
| 1379 | return IsEven(n1 + n2 + n3); |
| 1380 | } |
| 1381 | }; |
| 1382 | |
| 1383 | // A predicate-formatter function that asserts the sum of the |
| 1384 | // arguments is an even number. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1385 | AssertionResult AssertSumIsEven4( |
| 1386 | const char* e1, const char* e2, const char* e3, const char* e4, |
| 1387 | int n1, int n2, int n3, int n4) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1388 | const int sum = n1 + n2 + n3 + n4; |
| 1389 | if (IsEven(sum)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1390 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1393 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1394 | msg << e1 << " + " << e2 << " + " << e3 << " + " << e4 |
| 1395 | << " (" << n1 << " + " << n2 << " + " << n3 << " + " << n4 |
| 1396 | << ") evaluates to " << sum << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1397 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | // A predicate-formatter functor that asserts the sum of the arguments |
| 1401 | // is an even number. |
| 1402 | struct AssertSumIsEven5Functor { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1403 | AssertionResult operator()( |
| 1404 | const char* e1, const char* e2, const char* e3, const char* e4, |
| 1405 | const char* e5, int n1, int n2, int n3, int n4, int n5) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1406 | const int sum = n1 + n2 + n3 + n4 + n5; |
| 1407 | if (IsEven(sum)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1408 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1411 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1412 | msg << e1 << " + " << e2 << " + " << e3 << " + " << e4 << " + " << e5 |
| 1413 | << " (" |
| 1414 | << n1 << " + " << n2 << " + " << n3 << " + " << n4 << " + " << n5 |
| 1415 | << ") evaluates to " << sum << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1416 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1417 | } |
| 1418 | }; |
| 1419 | |
| 1420 | |
| 1421 | // Tests unary predicate assertions. |
| 1422 | |
| 1423 | // Tests unary predicate assertions that don't use a custom formatter. |
| 1424 | TEST(Pred1Test, WithoutFormat) { |
| 1425 | // Success cases. |
| 1426 | EXPECT_PRED1(IsEvenFunctor(), 2) << "This failure is UNEXPECTED!"; |
| 1427 | ASSERT_PRED1(IsEven, 4); |
| 1428 | |
| 1429 | // Failure cases. |
| 1430 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1431 | EXPECT_PRED1(IsEven, 5) << "This failure is expected."; |
| 1432 | }, "This failure is expected."); |
| 1433 | EXPECT_FATAL_FAILURE(ASSERT_PRED1(IsEvenFunctor(), 5), |
| 1434 | "evaluates to false"); |
| 1435 | } |
| 1436 | |
| 1437 | // Tests unary predicate assertions that use a custom formatter. |
| 1438 | TEST(Pred1Test, WithFormat) { |
| 1439 | // Success cases. |
| 1440 | EXPECT_PRED_FORMAT1(AssertIsEven, 2); |
| 1441 | ASSERT_PRED_FORMAT1(AssertIsEvenFunctor(), 4) |
| 1442 | << "This failure is UNEXPECTED!"; |
| 1443 | |
| 1444 | // Failure cases. |
| 1445 | const int n = 5; |
| 1446 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT1(AssertIsEvenFunctor(), n), |
| 1447 | "n evaluates to 5, which is not even."); |
| 1448 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 1449 | ASSERT_PRED_FORMAT1(AssertIsEven, 5) << "This failure is expected."; |
| 1450 | }, "This failure is expected."); |
| 1451 | } |
| 1452 | |
| 1453 | // Tests that unary predicate assertions evaluates their arguments |
| 1454 | // exactly once. |
| 1455 | TEST(Pred1Test, SingleEvaluationOnFailure) { |
| 1456 | // A success case. |
| 1457 | static int n = 0; |
| 1458 | EXPECT_PRED1(IsEven, n++); |
| 1459 | EXPECT_EQ(1, n) << "The argument is not evaluated exactly once."; |
| 1460 | |
| 1461 | // A failure case. |
| 1462 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 1463 | ASSERT_PRED_FORMAT1(AssertIsEvenFunctor(), n++) |
| 1464 | << "This failure is expected."; |
| 1465 | }, "This failure is expected."); |
| 1466 | EXPECT_EQ(2, n) << "The argument is not evaluated exactly once."; |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | // Tests predicate assertions whose arity is >= 2. |
| 1471 | |
| 1472 | // Tests predicate assertions that don't use a custom formatter. |
| 1473 | TEST(PredTest, WithoutFormat) { |
| 1474 | // Success cases. |
| 1475 | ASSERT_PRED2(SumIsEven2, 2, 4) << "This failure is UNEXPECTED!"; |
| 1476 | EXPECT_PRED3(SumIsEven3Functor(), 4, 6, 8); |
| 1477 | |
| 1478 | // Failure cases. |
| 1479 | const int n1 = 1; |
| 1480 | const int n2 = 2; |
| 1481 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1482 | EXPECT_PRED2(SumIsEven2, n1, n2) << "This failure is expected."; |
| 1483 | }, "This failure is expected."); |
| 1484 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 1485 | ASSERT_PRED3(SumIsEven3Functor(), 1, 2, 4); |
| 1486 | }, "evaluates to false"); |
| 1487 | } |
| 1488 | |
| 1489 | // Tests predicate assertions that use a custom formatter. |
| 1490 | TEST(PredTest, WithFormat) { |
| 1491 | // Success cases. |
| 1492 | ASSERT_PRED_FORMAT4(AssertSumIsEven4, 4, 6, 8, 10) << |
| 1493 | "This failure is UNEXPECTED!"; |
| 1494 | EXPECT_PRED_FORMAT5(AssertSumIsEven5Functor(), 2, 4, 6, 8, 10); |
| 1495 | |
| 1496 | // Failure cases. |
| 1497 | const int n1 = 1; |
| 1498 | const int n2 = 2; |
| 1499 | const int n3 = 4; |
| 1500 | const int n4 = 6; |
| 1501 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1502 | EXPECT_PRED_FORMAT4(AssertSumIsEven4, n1, n2, n3, n4); |
| 1503 | }, "evaluates to 13, which is not even."); |
| 1504 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 1505 | ASSERT_PRED_FORMAT5(AssertSumIsEven5Functor(), 1, 2, 4, 6, 8) |
| 1506 | << "This failure is expected."; |
| 1507 | }, "This failure is expected."); |
| 1508 | } |
| 1509 | |
| 1510 | // Tests that predicate assertions evaluates their arguments |
| 1511 | // exactly once. |
| 1512 | TEST(PredTest, SingleEvaluationOnFailure) { |
| 1513 | // A success case. |
| 1514 | int n1 = 0; |
| 1515 | int n2 = 0; |
| 1516 | EXPECT_PRED2(SumIsEven2, n1++, n2++); |
| 1517 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 1518 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 1519 | |
| 1520 | // Another success case. |
| 1521 | n1 = n2 = 0; |
| 1522 | int n3 = 0; |
| 1523 | int n4 = 0; |
| 1524 | int n5 = 0; |
| 1525 | ASSERT_PRED_FORMAT5(AssertSumIsEven5Functor(), |
| 1526 | n1++, n2++, n3++, n4++, n5++) |
| 1527 | << "This failure is UNEXPECTED!"; |
| 1528 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 1529 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 1530 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 1531 | EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; |
| 1532 | EXPECT_EQ(1, n5) << "Argument 5 is not evaluated exactly once."; |
| 1533 | |
| 1534 | // A failure case. |
| 1535 | n1 = n2 = n3 = 0; |
| 1536 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1537 | EXPECT_PRED3(SumIsEven3Functor(), ++n1, n2++, n3++) |
| 1538 | << "This failure is expected."; |
| 1539 | }, "This failure is expected."); |
| 1540 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 1541 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 1542 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 1543 | |
| 1544 | // Another failure case. |
| 1545 | n1 = n2 = n3 = n4 = 0; |
| 1546 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1547 | EXPECT_PRED_FORMAT4(AssertSumIsEven4, ++n1, n2++, n3++, n4++); |
| 1548 | }, "evaluates to 1, which is not even."); |
| 1549 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 1550 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 1551 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 1552 | EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | // Some helper functions for testing using overloaded/template |
| 1557 | // functions with ASSERT_PREDn and EXPECT_PREDn. |
| 1558 | |
| 1559 | bool IsPositive(int n) { |
| 1560 | return n > 0; |
| 1561 | } |
| 1562 | |
| 1563 | bool IsPositive(double x) { |
| 1564 | return x > 0; |
| 1565 | } |
| 1566 | |
| 1567 | template <typename T> |
| 1568 | bool IsNegative(T x) { |
| 1569 | return x < 0; |
| 1570 | } |
| 1571 | |
| 1572 | template <typename T1, typename T2> |
| 1573 | bool GreaterThan(T1 x1, T2 x2) { |
| 1574 | return x1 > x2; |
| 1575 | } |
| 1576 | |
| 1577 | // Tests that overloaded functions can be used in *_PRED* as long as |
| 1578 | // their types are explicitly specified. |
| 1579 | TEST(PredicateAssertionTest, AcceptsOverloadedFunction) { |
| 1580 | EXPECT_PRED1(static_cast<bool (*)(int)>(IsPositive), 5); // NOLINT |
| 1581 | ASSERT_PRED1(static_cast<bool (*)(double)>(IsPositive), 6.0); // NOLINT |
| 1582 | } |
| 1583 | |
| 1584 | // Tests that template functions can be used in *_PRED* as long as |
| 1585 | // their types are explicitly specified. |
| 1586 | TEST(PredicateAssertionTest, AcceptsTemplateFunction) { |
| 1587 | EXPECT_PRED1(IsNegative<int>, -5); |
| 1588 | // Makes sure that we can handle templates with more than one |
| 1589 | // parameter. |
| 1590 | ASSERT_PRED2((GreaterThan<int, int>), 5, 0); |
| 1591 | } |
| 1592 | |
| 1593 | |
| 1594 | // Some helper functions for testing using overloaded/template |
| 1595 | // functions with ASSERT_PRED_FORMATn and EXPECT_PRED_FORMATn. |
| 1596 | |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame^] | 1597 | AssertionResult IsPositiveFormat(const char* /* expr */, int n) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1598 | return n > 0 ? AssertionSuccess() : |
| 1599 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame^] | 1602 | AssertionResult IsPositiveFormat(const char* /* expr */, double x) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1603 | return x > 0 ? AssertionSuccess() : |
| 1604 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | template <typename T> |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame^] | 1608 | AssertionResult IsNegativeFormat(const char* /* expr */, T x) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1609 | return x < 0 ? AssertionSuccess() : |
| 1610 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | template <typename T1, typename T2> |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame^] | 1614 | AssertionResult EqualsFormat(const char* /* expr1 */, const char* /* expr2 */, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1615 | const T1& x1, const T2& x2) { |
| 1616 | return x1 == x2 ? AssertionSuccess() : |
| 1617 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | // Tests that overloaded functions can be used in *_PRED_FORMAT* |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame^] | 1621 | // without explicitly specifying their types. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1622 | TEST(PredicateFormatAssertionTest, AcceptsOverloadedFunction) { |
| 1623 | EXPECT_PRED_FORMAT1(IsPositiveFormat, 5); |
| 1624 | ASSERT_PRED_FORMAT1(IsPositiveFormat, 6.0); |
| 1625 | } |
| 1626 | |
| 1627 | // Tests that template functions can be used in *_PRED_FORMAT* without |
| 1628 | // explicitly specifying their types. |
| 1629 | TEST(PredicateFormatAssertionTest, AcceptsTemplateFunction) { |
| 1630 | EXPECT_PRED_FORMAT1(IsNegativeFormat, -5); |
| 1631 | ASSERT_PRED_FORMAT2(EqualsFormat, 3, 3); |
| 1632 | } |
| 1633 | |
| 1634 | |
| 1635 | // Tests string assertions. |
| 1636 | |
| 1637 | // Tests ASSERT_STREQ with non-NULL arguments. |
| 1638 | TEST(StringAssertionTest, ASSERT_STREQ) { |
| 1639 | const char * const p1 = "good"; |
| 1640 | ASSERT_STREQ(p1, p1); |
| 1641 | |
| 1642 | // Let p2 have the same content as p1, but be at a different address. |
| 1643 | const char p2[] = "good"; |
| 1644 | ASSERT_STREQ(p1, p2); |
| 1645 | |
| 1646 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("bad", "good"), |
| 1647 | "Expected: \"bad\""); |
| 1648 | } |
| 1649 | |
| 1650 | // Tests ASSERT_STREQ with NULL arguments. |
| 1651 | TEST(StringAssertionTest, ASSERT_STREQ_Null) { |
| 1652 | ASSERT_STREQ(static_cast<const char *>(NULL), NULL); |
| 1653 | EXPECT_FATAL_FAILURE(ASSERT_STREQ(NULL, "non-null"), |
| 1654 | "non-null"); |
| 1655 | } |
| 1656 | |
| 1657 | // Tests ASSERT_STREQ with NULL arguments. |
| 1658 | TEST(StringAssertionTest, ASSERT_STREQ_Null2) { |
| 1659 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("non-null", NULL), |
| 1660 | "non-null"); |
| 1661 | } |
| 1662 | |
| 1663 | // Tests ASSERT_STRNE. |
| 1664 | TEST(StringAssertionTest, ASSERT_STRNE) { |
| 1665 | ASSERT_STRNE("hi", "Hi"); |
| 1666 | ASSERT_STRNE("Hi", NULL); |
| 1667 | ASSERT_STRNE(NULL, "Hi"); |
| 1668 | ASSERT_STRNE("", NULL); |
| 1669 | ASSERT_STRNE(NULL, ""); |
| 1670 | ASSERT_STRNE("", "Hi"); |
| 1671 | ASSERT_STRNE("Hi", ""); |
| 1672 | EXPECT_FATAL_FAILURE(ASSERT_STRNE("Hi", "Hi"), |
| 1673 | "\"Hi\" vs \"Hi\""); |
| 1674 | } |
| 1675 | |
| 1676 | // Tests ASSERT_STRCASEEQ. |
| 1677 | TEST(StringAssertionTest, ASSERT_STRCASEEQ) { |
| 1678 | ASSERT_STRCASEEQ("hi", "Hi"); |
| 1679 | ASSERT_STRCASEEQ(static_cast<const char *>(NULL), NULL); |
| 1680 | |
| 1681 | ASSERT_STRCASEEQ("", ""); |
| 1682 | EXPECT_FATAL_FAILURE(ASSERT_STRCASEEQ("Hi", "hi2"), |
| 1683 | "(ignoring case)"); |
| 1684 | } |
| 1685 | |
| 1686 | // Tests ASSERT_STRCASENE. |
| 1687 | TEST(StringAssertionTest, ASSERT_STRCASENE) { |
| 1688 | ASSERT_STRCASENE("hi1", "Hi2"); |
| 1689 | ASSERT_STRCASENE("Hi", NULL); |
| 1690 | ASSERT_STRCASENE(NULL, "Hi"); |
| 1691 | ASSERT_STRCASENE("", NULL); |
| 1692 | ASSERT_STRCASENE(NULL, ""); |
| 1693 | ASSERT_STRCASENE("", "Hi"); |
| 1694 | ASSERT_STRCASENE("Hi", ""); |
| 1695 | EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("Hi", "hi"), |
| 1696 | "(ignoring case)"); |
| 1697 | } |
| 1698 | |
| 1699 | // Tests *_STREQ on wide strings. |
| 1700 | TEST(StringAssertionTest, STREQ_Wide) { |
| 1701 | // NULL strings. |
| 1702 | ASSERT_STREQ(static_cast<const wchar_t *>(NULL), NULL); |
| 1703 | |
| 1704 | // Empty strings. |
| 1705 | ASSERT_STREQ(L"", L""); |
| 1706 | |
| 1707 | // Non-null vs NULL. |
| 1708 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"non-null", NULL), |
| 1709 | "non-null"); |
| 1710 | |
| 1711 | // Equal strings. |
| 1712 | EXPECT_STREQ(L"Hi", L"Hi"); |
| 1713 | |
| 1714 | // Unequal strings. |
| 1715 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc", L"Abc"), |
| 1716 | "Abc"); |
| 1717 | |
| 1718 | // Strings containing wide characters. |
| 1719 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc\x8119", L"abc\x8120"), |
| 1720 | "abc"); |
| 1721 | } |
| 1722 | |
| 1723 | // Tests *_STRNE on wide strings. |
| 1724 | TEST(StringAssertionTest, STRNE_Wide) { |
| 1725 | // NULL strings. |
| 1726 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1727 | EXPECT_STRNE(static_cast<const wchar_t *>(NULL), NULL); |
| 1728 | }, ""); |
| 1729 | |
| 1730 | // Empty strings. |
| 1731 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"", L""), |
| 1732 | "L\"\""); |
| 1733 | |
| 1734 | // Non-null vs NULL. |
| 1735 | ASSERT_STRNE(L"non-null", NULL); |
| 1736 | |
| 1737 | // Equal strings. |
| 1738 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"Hi", L"Hi"), |
| 1739 | "L\"Hi\""); |
| 1740 | |
| 1741 | // Unequal strings. |
| 1742 | EXPECT_STRNE(L"abc", L"Abc"); |
| 1743 | |
| 1744 | // Strings containing wide characters. |
| 1745 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"abc\x8119", L"abc\x8119"), |
| 1746 | "abc"); |
| 1747 | } |
| 1748 | |
| 1749 | // Tests for ::testing::IsSubstring(). |
| 1750 | |
| 1751 | // Tests that IsSubstring() returns the correct result when the input |
| 1752 | // argument type is const char*. |
| 1753 | TEST(IsSubstringTest, ReturnsCorrectResultForCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1754 | EXPECT_FALSE(IsSubstring("", "", NULL, "a")); |
| 1755 | EXPECT_FALSE(IsSubstring("", "", "b", NULL)); |
| 1756 | EXPECT_FALSE(IsSubstring("", "", "needle", "haystack")); |
| 1757 | |
| 1758 | EXPECT_TRUE(IsSubstring("", "", static_cast<const char*>(NULL), NULL)); |
| 1759 | EXPECT_TRUE(IsSubstring("", "", "needle", "two needles")); |
| 1760 | } |
| 1761 | |
| 1762 | // Tests that IsSubstring() returns the correct result when the input |
| 1763 | // argument type is const wchar_t*. |
| 1764 | TEST(IsSubstringTest, ReturnsCorrectResultForWideCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1765 | EXPECT_FALSE(IsSubstring("", "", NULL, L"a")); |
| 1766 | EXPECT_FALSE(IsSubstring("", "", L"b", NULL)); |
| 1767 | EXPECT_FALSE(IsSubstring("", "", L"needle", L"haystack")); |
| 1768 | |
| 1769 | EXPECT_TRUE(IsSubstring("", "", static_cast<const wchar_t*>(NULL), NULL)); |
| 1770 | EXPECT_TRUE(IsSubstring("", "", L"needle", L"two needles")); |
| 1771 | } |
| 1772 | |
| 1773 | // Tests that IsSubstring() generates the correct message when the input |
| 1774 | // argument type is const char*. |
| 1775 | TEST(IsSubstringTest, GeneratesCorrectMessageForCString) { |
| 1776 | EXPECT_STREQ("Value of: needle_expr\n" |
| 1777 | " Actual: \"needle\"\n" |
| 1778 | "Expected: a substring of haystack_expr\n" |
| 1779 | "Which is: \"haystack\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1780 | IsSubstring("needle_expr", "haystack_expr", |
| 1781 | "needle", "haystack").failure_message()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | #if GTEST_HAS_STD_STRING |
| 1785 | |
| 1786 | // Tests that IsSubstring returns the correct result when the input |
| 1787 | // argument type is ::std::string. |
| 1788 | TEST(IsSubstringTest, ReturnsCorrectResultsForStdString) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1789 | EXPECT_TRUE(IsSubstring("", "", std::string("hello"), "ahellob")); |
| 1790 | EXPECT_FALSE(IsSubstring("", "", "hello", std::string("world"))); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | #endif // GTEST_HAS_STD_STRING |
| 1794 | |
| 1795 | #if GTEST_HAS_STD_WSTRING |
| 1796 | // Tests that IsSubstring returns the correct result when the input |
| 1797 | // argument type is ::std::wstring. |
| 1798 | TEST(IsSubstringTest, ReturnsCorrectResultForStdWstring) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1799 | EXPECT_TRUE(IsSubstring("", "", ::std::wstring(L"needle"), L"two needles")); |
| 1800 | EXPECT_FALSE(IsSubstring("", "", L"needle", ::std::wstring(L"haystack"))); |
| 1801 | } |
| 1802 | |
| 1803 | // Tests that IsSubstring() generates the correct message when the input |
| 1804 | // argument type is ::std::wstring. |
| 1805 | TEST(IsSubstringTest, GeneratesCorrectMessageForWstring) { |
| 1806 | EXPECT_STREQ("Value of: needle_expr\n" |
| 1807 | " Actual: L\"needle\"\n" |
| 1808 | "Expected: a substring of haystack_expr\n" |
| 1809 | "Which is: L\"haystack\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1810 | IsSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1811 | "needle_expr", "haystack_expr", |
| 1812 | ::std::wstring(L"needle"), L"haystack").failure_message()); |
| 1813 | } |
| 1814 | |
| 1815 | #endif // GTEST_HAS_STD_WSTRING |
| 1816 | |
| 1817 | // Tests for ::testing::IsNotSubstring(). |
| 1818 | |
| 1819 | // Tests that IsNotSubstring() returns the correct result when the input |
| 1820 | // argument type is const char*. |
| 1821 | TEST(IsNotSubstringTest, ReturnsCorrectResultForCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1822 | EXPECT_TRUE(IsNotSubstring("", "", "needle", "haystack")); |
| 1823 | EXPECT_FALSE(IsNotSubstring("", "", "needle", "two needles")); |
| 1824 | } |
| 1825 | |
| 1826 | // Tests that IsNotSubstring() returns the correct result when the input |
| 1827 | // argument type is const wchar_t*. |
| 1828 | TEST(IsNotSubstringTest, ReturnsCorrectResultForWideCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1829 | EXPECT_TRUE(IsNotSubstring("", "", L"needle", L"haystack")); |
| 1830 | EXPECT_FALSE(IsNotSubstring("", "", L"needle", L"two needles")); |
| 1831 | } |
| 1832 | |
| 1833 | // Tests that IsNotSubstring() generates the correct message when the input |
| 1834 | // argument type is const wchar_t*. |
| 1835 | TEST(IsNotSubstringTest, GeneratesCorrectMessageForWideCString) { |
| 1836 | EXPECT_STREQ("Value of: needle_expr\n" |
| 1837 | " Actual: L\"needle\"\n" |
| 1838 | "Expected: not a substring of haystack_expr\n" |
| 1839 | "Which is: L\"two needles\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1840 | IsNotSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1841 | "needle_expr", "haystack_expr", |
| 1842 | L"needle", L"two needles").failure_message()); |
| 1843 | } |
| 1844 | |
| 1845 | #if GTEST_HAS_STD_STRING |
| 1846 | |
| 1847 | // Tests that IsNotSubstring returns the correct result when the input |
| 1848 | // argument type is ::std::string. |
| 1849 | TEST(IsNotSubstringTest, ReturnsCorrectResultsForStdString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1850 | EXPECT_FALSE(IsNotSubstring("", "", std::string("hello"), "ahellob")); |
| 1851 | EXPECT_TRUE(IsNotSubstring("", "", "hello", std::string("world"))); |
| 1852 | } |
| 1853 | |
| 1854 | // Tests that IsNotSubstring() generates the correct message when the input |
| 1855 | // argument type is ::std::string. |
| 1856 | TEST(IsNotSubstringTest, GeneratesCorrectMessageForStdString) { |
| 1857 | EXPECT_STREQ("Value of: needle_expr\n" |
| 1858 | " Actual: \"needle\"\n" |
| 1859 | "Expected: not a substring of haystack_expr\n" |
| 1860 | "Which is: \"two needles\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1861 | IsNotSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1862 | "needle_expr", "haystack_expr", |
| 1863 | ::std::string("needle"), "two needles").failure_message()); |
| 1864 | } |
| 1865 | |
| 1866 | #endif // GTEST_HAS_STD_STRING |
| 1867 | |
| 1868 | #if GTEST_HAS_STD_WSTRING |
| 1869 | |
| 1870 | // Tests that IsNotSubstring returns the correct result when the input |
| 1871 | // argument type is ::std::wstring. |
| 1872 | TEST(IsNotSubstringTest, ReturnsCorrectResultForStdWstring) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1873 | EXPECT_FALSE( |
| 1874 | IsNotSubstring("", "", ::std::wstring(L"needle"), L"two needles")); |
| 1875 | EXPECT_TRUE(IsNotSubstring("", "", L"needle", ::std::wstring(L"haystack"))); |
| 1876 | } |
| 1877 | |
| 1878 | #endif // GTEST_HAS_STD_WSTRING |
| 1879 | |
| 1880 | // Tests floating-point assertions. |
| 1881 | |
| 1882 | template <typename RawType> |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1883 | class FloatingPointTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1884 | protected: |
| 1885 | typedef typename testing::internal::FloatingPoint<RawType> Floating; |
| 1886 | typedef typename Floating::Bits Bits; |
| 1887 | |
| 1888 | virtual void SetUp() { |
| 1889 | const size_t max_ulps = Floating::kMaxUlps; |
| 1890 | |
| 1891 | // The bits that represent 0.0. |
| 1892 | const Bits zero_bits = Floating(0).bits(); |
| 1893 | |
| 1894 | // Makes some numbers close to 0.0. |
| 1895 | close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2); |
| 1896 | close_to_negative_zero_ = -Floating::ReinterpretBits( |
| 1897 | zero_bits + max_ulps - max_ulps/2); |
| 1898 | further_from_negative_zero_ = -Floating::ReinterpretBits( |
| 1899 | zero_bits + max_ulps + 1 - max_ulps/2); |
| 1900 | |
| 1901 | // The bits that represent 1.0. |
| 1902 | const Bits one_bits = Floating(1).bits(); |
| 1903 | |
| 1904 | // Makes some numbers close to 1.0. |
| 1905 | close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps); |
| 1906 | further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1); |
| 1907 | |
| 1908 | // +infinity. |
| 1909 | infinity_ = Floating::Infinity(); |
| 1910 | |
| 1911 | // The bits that represent +infinity. |
| 1912 | const Bits infinity_bits = Floating(infinity_).bits(); |
| 1913 | |
| 1914 | // Makes some numbers close to infinity. |
| 1915 | close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps); |
| 1916 | further_from_infinity_ = Floating::ReinterpretBits( |
| 1917 | infinity_bits - max_ulps - 1); |
| 1918 | |
| 1919 | // Makes some NAN's. |
| 1920 | nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1); |
| 1921 | nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200); |
| 1922 | } |
| 1923 | |
| 1924 | void TestSize() { |
| 1925 | EXPECT_EQ(sizeof(RawType), sizeof(Bits)); |
| 1926 | } |
| 1927 | |
| 1928 | // Pre-calculated numbers to be used by the tests. |
| 1929 | |
| 1930 | static RawType close_to_positive_zero_; |
| 1931 | static RawType close_to_negative_zero_; |
| 1932 | static RawType further_from_negative_zero_; |
| 1933 | |
| 1934 | static RawType close_to_one_; |
| 1935 | static RawType further_from_one_; |
| 1936 | |
| 1937 | static RawType infinity_; |
| 1938 | static RawType close_to_infinity_; |
| 1939 | static RawType further_from_infinity_; |
| 1940 | |
| 1941 | static RawType nan1_; |
| 1942 | static RawType nan2_; |
| 1943 | }; |
| 1944 | |
| 1945 | template <typename RawType> |
| 1946 | RawType FloatingPointTest<RawType>::close_to_positive_zero_; |
| 1947 | |
| 1948 | template <typename RawType> |
| 1949 | RawType FloatingPointTest<RawType>::close_to_negative_zero_; |
| 1950 | |
| 1951 | template <typename RawType> |
| 1952 | RawType FloatingPointTest<RawType>::further_from_negative_zero_; |
| 1953 | |
| 1954 | template <typename RawType> |
| 1955 | RawType FloatingPointTest<RawType>::close_to_one_; |
| 1956 | |
| 1957 | template <typename RawType> |
| 1958 | RawType FloatingPointTest<RawType>::further_from_one_; |
| 1959 | |
| 1960 | template <typename RawType> |
| 1961 | RawType FloatingPointTest<RawType>::infinity_; |
| 1962 | |
| 1963 | template <typename RawType> |
| 1964 | RawType FloatingPointTest<RawType>::close_to_infinity_; |
| 1965 | |
| 1966 | template <typename RawType> |
| 1967 | RawType FloatingPointTest<RawType>::further_from_infinity_; |
| 1968 | |
| 1969 | template <typename RawType> |
| 1970 | RawType FloatingPointTest<RawType>::nan1_; |
| 1971 | |
| 1972 | template <typename RawType> |
| 1973 | RawType FloatingPointTest<RawType>::nan2_; |
| 1974 | |
| 1975 | // Instantiates FloatingPointTest for testing *_FLOAT_EQ. |
| 1976 | typedef FloatingPointTest<float> FloatTest; |
| 1977 | |
| 1978 | // Tests that the size of Float::Bits matches the size of float. |
| 1979 | TEST_F(FloatTest, Size) { |
| 1980 | TestSize(); |
| 1981 | } |
| 1982 | |
| 1983 | // Tests comparing with +0 and -0. |
| 1984 | TEST_F(FloatTest, Zeros) { |
| 1985 | EXPECT_FLOAT_EQ(0.0, -0.0); |
| 1986 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(-0.0, 1.0), |
| 1987 | "1.0"); |
| 1988 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.5), |
| 1989 | "1.5"); |
| 1990 | } |
| 1991 | |
| 1992 | // Tests comparing numbers close to 0. |
| 1993 | // |
| 1994 | // This ensures that *_FLOAT_EQ handles the sign correctly and no |
| 1995 | // overflow occurs when comparing numbers whose absolute value is very |
| 1996 | // small. |
| 1997 | TEST_F(FloatTest, AlmostZeros) { |
| 1998 | EXPECT_FLOAT_EQ(0.0, close_to_positive_zero_); |
| 1999 | EXPECT_FLOAT_EQ(-0.0, close_to_negative_zero_); |
| 2000 | EXPECT_FLOAT_EQ(close_to_positive_zero_, close_to_negative_zero_); |
| 2001 | |
| 2002 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2003 | ASSERT_FLOAT_EQ(close_to_positive_zero_, further_from_negative_zero_); |
| 2004 | }, "further_from_negative_zero_"); |
| 2005 | } |
| 2006 | |
| 2007 | // Tests comparing numbers close to each other. |
| 2008 | TEST_F(FloatTest, SmallDiff) { |
| 2009 | EXPECT_FLOAT_EQ(1.0, close_to_one_); |
| 2010 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, further_from_one_), |
| 2011 | "further_from_one_"); |
| 2012 | } |
| 2013 | |
| 2014 | // Tests comparing numbers far apart. |
| 2015 | TEST_F(FloatTest, LargeDiff) { |
| 2016 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(2.5, 3.0), |
| 2017 | "3.0"); |
| 2018 | } |
| 2019 | |
| 2020 | // Tests comparing with infinity. |
| 2021 | // |
| 2022 | // This ensures that no overflow occurs when comparing numbers whose |
| 2023 | // absolute value is very large. |
| 2024 | TEST_F(FloatTest, Infinity) { |
| 2025 | EXPECT_FLOAT_EQ(infinity_, close_to_infinity_); |
| 2026 | EXPECT_FLOAT_EQ(-infinity_, -close_to_infinity_); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2027 | #ifndef GTEST_OS_SYMBIAN |
| 2028 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2029 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(infinity_, -infinity_), |
| 2030 | "-infinity_"); |
| 2031 | |
| 2032 | // This is interesting as the representations of infinity_ and nan1_ |
| 2033 | // are only 1 DLP apart. |
| 2034 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(infinity_, nan1_), |
| 2035 | "nan1_"); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2036 | #endif // ! GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | // Tests that comparing with NAN always returns false. |
| 2040 | TEST_F(FloatTest, NaN) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2041 | #ifndef GTEST_OS_SYMBIAN |
| 2042 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2043 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(nan1_, nan1_), |
| 2044 | "nan1_"); |
| 2045 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(nan1_, nan2_), |
| 2046 | "nan2_"); |
| 2047 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, nan1_), |
| 2048 | "nan1_"); |
| 2049 | |
| 2050 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(nan1_, infinity_), |
| 2051 | "infinity_"); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2052 | #endif // ! GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | // Tests that *_FLOAT_EQ are reflexive. |
| 2056 | TEST_F(FloatTest, Reflexive) { |
| 2057 | EXPECT_FLOAT_EQ(0.0, 0.0); |
| 2058 | EXPECT_FLOAT_EQ(1.0, 1.0); |
| 2059 | ASSERT_FLOAT_EQ(infinity_, infinity_); |
| 2060 | } |
| 2061 | |
| 2062 | // Tests that *_FLOAT_EQ are commutative. |
| 2063 | TEST_F(FloatTest, Commutative) { |
| 2064 | // We already tested EXPECT_FLOAT_EQ(1.0, close_to_one_). |
| 2065 | EXPECT_FLOAT_EQ(close_to_one_, 1.0); |
| 2066 | |
| 2067 | // We already tested EXPECT_FLOAT_EQ(1.0, further_from_one_). |
| 2068 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(further_from_one_, 1.0), |
| 2069 | "1.0"); |
| 2070 | } |
| 2071 | |
| 2072 | // Tests EXPECT_NEAR. |
| 2073 | TEST_F(FloatTest, EXPECT_NEAR) { |
| 2074 | EXPECT_NEAR(-1.0f, -1.1f, 0.2f); |
| 2075 | EXPECT_NEAR(2.0f, 3.0f, 1.0f); |
| 2076 | EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0f,1.2f, 0.1f), // NOLINT |
| 2077 | "The difference between 1.0f and 1.2f is 0.2, " |
| 2078 | "which exceeds 0.1f"); |
| 2079 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 2080 | // space after the first comma in the previous line. |
| 2081 | } |
| 2082 | |
| 2083 | // Tests ASSERT_NEAR. |
| 2084 | TEST_F(FloatTest, ASSERT_NEAR) { |
| 2085 | ASSERT_NEAR(-1.0f, -1.1f, 0.2f); |
| 2086 | ASSERT_NEAR(2.0f, 3.0f, 1.0f); |
| 2087 | EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0f,1.2f, 0.1f), // NOLINT |
| 2088 | "The difference between 1.0f and 1.2f is 0.2, " |
| 2089 | "which exceeds 0.1f"); |
| 2090 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 2091 | // space after the first comma in the previous line. |
| 2092 | } |
| 2093 | |
| 2094 | // Tests the cases where FloatLE() should succeed. |
| 2095 | TEST_F(FloatTest, FloatLESucceeds) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2096 | EXPECT_PRED_FORMAT2(FloatLE, 1.0f, 2.0f); // When val1 < val2, |
| 2097 | ASSERT_PRED_FORMAT2(FloatLE, 1.0f, 1.0f); // val1 == val2, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2098 | |
| 2099 | // or when val1 is greater than, but almost equals to, val2. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2100 | EXPECT_PRED_FORMAT2(FloatLE, close_to_positive_zero_, 0.0f); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | // Tests the cases where FloatLE() should fail. |
| 2104 | TEST_F(FloatTest, FloatLEFails) { |
| 2105 | // When val1 is greater than val2 by a large margin, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2106 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT2(FloatLE, 2.0f, 1.0f), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2107 | "(2.0f) <= (1.0f)"); |
| 2108 | |
| 2109 | // or by a small yet non-negligible margin, |
| 2110 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2111 | EXPECT_PRED_FORMAT2(FloatLE, further_from_one_, 1.0f); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2112 | }, "(further_from_one_) <= (1.0f)"); |
| 2113 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2114 | #ifndef GTEST_OS_SYMBIAN |
| 2115 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2116 | // or when either val1 or val2 is NaN. |
| 2117 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2118 | EXPECT_PRED_FORMAT2(FloatLE, nan1_, infinity_); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2119 | }, "(nan1_) <= (infinity_)"); |
| 2120 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2121 | EXPECT_PRED_FORMAT2(FloatLE, -infinity_, nan1_); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2122 | }, "(-infinity_) <= (nan1_)"); |
| 2123 | |
| 2124 | EXPECT_FATAL_FAILURE({ // NOLINT |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2125 | ASSERT_PRED_FORMAT2(FloatLE, nan1_, nan1_); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2126 | }, "(nan1_) <= (nan1_)"); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2127 | #endif // ! GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | // Instantiates FloatingPointTest for testing *_DOUBLE_EQ. |
| 2131 | typedef FloatingPointTest<double> DoubleTest; |
| 2132 | |
| 2133 | // Tests that the size of Double::Bits matches the size of double. |
| 2134 | TEST_F(DoubleTest, Size) { |
| 2135 | TestSize(); |
| 2136 | } |
| 2137 | |
| 2138 | // Tests comparing with +0 and -0. |
| 2139 | TEST_F(DoubleTest, Zeros) { |
| 2140 | EXPECT_DOUBLE_EQ(0.0, -0.0); |
| 2141 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(-0.0, 1.0), |
| 2142 | "1.0"); |
| 2143 | EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(0.0, 1.0), |
| 2144 | "1.0"); |
| 2145 | } |
| 2146 | |
| 2147 | // Tests comparing numbers close to 0. |
| 2148 | // |
| 2149 | // This ensures that *_DOUBLE_EQ handles the sign correctly and no |
| 2150 | // overflow occurs when comparing numbers whose absolute value is very |
| 2151 | // small. |
| 2152 | TEST_F(DoubleTest, AlmostZeros) { |
| 2153 | EXPECT_DOUBLE_EQ(0.0, close_to_positive_zero_); |
| 2154 | EXPECT_DOUBLE_EQ(-0.0, close_to_negative_zero_); |
| 2155 | EXPECT_DOUBLE_EQ(close_to_positive_zero_, close_to_negative_zero_); |
| 2156 | |
| 2157 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2158 | ASSERT_DOUBLE_EQ(close_to_positive_zero_, further_from_negative_zero_); |
| 2159 | }, "further_from_negative_zero_"); |
| 2160 | } |
| 2161 | |
| 2162 | // Tests comparing numbers close to each other. |
| 2163 | TEST_F(DoubleTest, SmallDiff) { |
| 2164 | EXPECT_DOUBLE_EQ(1.0, close_to_one_); |
| 2165 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, further_from_one_), |
| 2166 | "further_from_one_"); |
| 2167 | } |
| 2168 | |
| 2169 | // Tests comparing numbers far apart. |
| 2170 | TEST_F(DoubleTest, LargeDiff) { |
| 2171 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(2.0, 3.0), |
| 2172 | "3.0"); |
| 2173 | } |
| 2174 | |
| 2175 | // Tests comparing with infinity. |
| 2176 | // |
| 2177 | // This ensures that no overflow occurs when comparing numbers whose |
| 2178 | // absolute value is very large. |
| 2179 | TEST_F(DoubleTest, Infinity) { |
| 2180 | EXPECT_DOUBLE_EQ(infinity_, close_to_infinity_); |
| 2181 | EXPECT_DOUBLE_EQ(-infinity_, -close_to_infinity_); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2182 | #ifndef GTEST_OS_SYMBIAN |
| 2183 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2184 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(infinity_, -infinity_), |
| 2185 | "-infinity_"); |
| 2186 | |
| 2187 | // This is interesting as the representations of infinity_ and nan1_ |
| 2188 | // are only 1 DLP apart. |
| 2189 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(infinity_, nan1_), |
| 2190 | "nan1_"); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2191 | #endif // ! GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | // Tests that comparing with NAN always returns false. |
| 2195 | TEST_F(DoubleTest, NaN) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2196 | #ifndef GTEST_OS_SYMBIAN |
| 2197 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2198 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(nan1_, nan1_), |
| 2199 | "nan1_"); |
| 2200 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(nan1_, nan2_), "nan2_"); |
| 2201 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, nan1_), "nan1_"); |
| 2202 | EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(nan1_, infinity_), "infinity_"); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2203 | #endif // ! GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | // Tests that *_DOUBLE_EQ are reflexive. |
| 2207 | TEST_F(DoubleTest, Reflexive) { |
| 2208 | EXPECT_DOUBLE_EQ(0.0, 0.0); |
| 2209 | EXPECT_DOUBLE_EQ(1.0, 1.0); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2210 | #ifndef GTEST_OS_SYMBIAN |
| 2211 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2212 | ASSERT_DOUBLE_EQ(infinity_, infinity_); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2213 | #endif // ! GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | // Tests that *_DOUBLE_EQ are commutative. |
| 2217 | TEST_F(DoubleTest, Commutative) { |
| 2218 | // We already tested EXPECT_DOUBLE_EQ(1.0, close_to_one_). |
| 2219 | EXPECT_DOUBLE_EQ(close_to_one_, 1.0); |
| 2220 | |
| 2221 | // We already tested EXPECT_DOUBLE_EQ(1.0, further_from_one_). |
| 2222 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(further_from_one_, 1.0), "1.0"); |
| 2223 | } |
| 2224 | |
| 2225 | // Tests EXPECT_NEAR. |
| 2226 | TEST_F(DoubleTest, EXPECT_NEAR) { |
| 2227 | EXPECT_NEAR(-1.0, -1.1, 0.2); |
| 2228 | EXPECT_NEAR(2.0, 3.0, 1.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2229 | EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0, 1.2, 0.1), // NOLINT |
| 2230 | "The difference between 1.0 and 1.2 is 0.2, " |
| 2231 | "which exceeds 0.1"); |
| 2232 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 2233 | // space after the first comma in the previous statement. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | // Tests ASSERT_NEAR. |
| 2237 | TEST_F(DoubleTest, ASSERT_NEAR) { |
| 2238 | ASSERT_NEAR(-1.0, -1.1, 0.2); |
| 2239 | ASSERT_NEAR(2.0, 3.0, 1.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2240 | EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0, 1.2, 0.1), // NOLINT |
| 2241 | "The difference between 1.0 and 1.2 is 0.2, " |
| 2242 | "which exceeds 0.1"); |
| 2243 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 2244 | // space after the first comma in the previous statement. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | // Tests the cases where DoubleLE() should succeed. |
| 2248 | TEST_F(DoubleTest, DoubleLESucceeds) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2249 | EXPECT_PRED_FORMAT2(DoubleLE, 1.0, 2.0); // When val1 < val2, |
| 2250 | ASSERT_PRED_FORMAT2(DoubleLE, 1.0, 1.0); // val1 == val2, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2251 | |
| 2252 | // or when val1 is greater than, but almost equals to, val2. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2253 | EXPECT_PRED_FORMAT2(DoubleLE, close_to_positive_zero_, 0.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | // Tests the cases where DoubleLE() should fail. |
| 2257 | TEST_F(DoubleTest, DoubleLEFails) { |
| 2258 | // When val1 is greater than val2 by a large margin, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2259 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT2(DoubleLE, 2.0, 1.0), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2260 | "(2.0) <= (1.0)"); |
| 2261 | |
| 2262 | // or by a small yet non-negligible margin, |
| 2263 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2264 | EXPECT_PRED_FORMAT2(DoubleLE, further_from_one_, 1.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2265 | }, "(further_from_one_) <= (1.0)"); |
| 2266 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2267 | #ifndef GTEST_OS_SYMBIAN |
| 2268 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2269 | // or when either val1 or val2 is NaN. |
| 2270 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2271 | EXPECT_PRED_FORMAT2(DoubleLE, nan1_, infinity_); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2272 | }, "(nan1_) <= (infinity_)"); |
| 2273 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2274 | EXPECT_PRED_FORMAT2(DoubleLE, -infinity_, nan1_); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2275 | }, " (-infinity_) <= (nan1_)"); |
| 2276 | EXPECT_FATAL_FAILURE({ // NOLINT |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2277 | ASSERT_PRED_FORMAT2(DoubleLE, nan1_, nan1_); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2278 | }, "(nan1_) <= (nan1_)"); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2279 | #endif // ! GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | |
| 2283 | // Verifies that a test or test case whose name starts with DISABLED_ is |
| 2284 | // not run. |
| 2285 | |
| 2286 | // A test whose name starts with DISABLED_. |
| 2287 | // Should not run. |
| 2288 | TEST(DisabledTest, DISABLED_TestShouldNotRun) { |
| 2289 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 2290 | } |
| 2291 | |
| 2292 | // A test whose name does not start with DISABLED_. |
| 2293 | // Should run. |
| 2294 | TEST(DisabledTest, NotDISABLED_TestShouldRun) { |
| 2295 | EXPECT_EQ(1, 1); |
| 2296 | } |
| 2297 | |
| 2298 | // A test case whose name starts with DISABLED_. |
| 2299 | // Should not run. |
| 2300 | TEST(DISABLED_TestCase, TestShouldNotRun) { |
| 2301 | FAIL() << "Unexpected failure: Test in disabled test case should not be run."; |
| 2302 | } |
| 2303 | |
| 2304 | // A test case and test whose names start with DISABLED_. |
| 2305 | // Should not run. |
| 2306 | TEST(DISABLED_TestCase, DISABLED_TestShouldNotRun) { |
| 2307 | FAIL() << "Unexpected failure: Test in disabled test case should not be run."; |
| 2308 | } |
| 2309 | |
| 2310 | // Check that when all tests in a test case are disabled, SetupTestCase() and |
| 2311 | // TearDownTestCase() are not called. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2312 | class DisabledTestsTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2313 | protected: |
| 2314 | static void SetUpTestCase() { |
| 2315 | FAIL() << "Unexpected failure: All tests disabled in test case. " |
| 2316 | "SetupTestCase() should not be called."; |
| 2317 | } |
| 2318 | |
| 2319 | static void TearDownTestCase() { |
| 2320 | FAIL() << "Unexpected failure: All tests disabled in test case. " |
| 2321 | "TearDownTestCase() should not be called."; |
| 2322 | } |
| 2323 | }; |
| 2324 | |
| 2325 | TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_1) { |
| 2326 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 2327 | } |
| 2328 | |
| 2329 | TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) { |
| 2330 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 2331 | } |
| 2332 | |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 2333 | // Tests that disabled typed tests aren't run. |
| 2334 | |
| 2335 | #ifdef GTEST_HAS_TYPED_TEST |
| 2336 | |
| 2337 | template <typename T> |
| 2338 | class TypedTest : public Test { |
| 2339 | }; |
| 2340 | |
| 2341 | typedef testing::Types<int, double> NumericTypes; |
| 2342 | TYPED_TEST_CASE(TypedTest, NumericTypes); |
| 2343 | |
| 2344 | TYPED_TEST(TypedTest, DISABLED_ShouldNotRun) { |
| 2345 | FAIL() << "Unexpected failure: Disabled typed test should not run."; |
| 2346 | } |
| 2347 | |
| 2348 | template <typename T> |
| 2349 | class DISABLED_TypedTest : public Test { |
| 2350 | }; |
| 2351 | |
| 2352 | TYPED_TEST_CASE(DISABLED_TypedTest, NumericTypes); |
| 2353 | |
| 2354 | TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) { |
| 2355 | FAIL() << "Unexpected failure: Disabled typed test should not run."; |
| 2356 | } |
| 2357 | |
| 2358 | #endif // GTEST_HAS_TYPED_TEST |
| 2359 | |
| 2360 | // Tests that disabled type-parameterized tests aren't run. |
| 2361 | |
| 2362 | #ifdef GTEST_HAS_TYPED_TEST_P |
| 2363 | |
| 2364 | template <typename T> |
| 2365 | class TypedTestP : public Test { |
| 2366 | }; |
| 2367 | |
| 2368 | TYPED_TEST_CASE_P(TypedTestP); |
| 2369 | |
| 2370 | TYPED_TEST_P(TypedTestP, DISABLED_ShouldNotRun) { |
| 2371 | FAIL() << "Unexpected failure: " |
| 2372 | << "Disabled type-parameterized test should not run."; |
| 2373 | } |
| 2374 | |
| 2375 | REGISTER_TYPED_TEST_CASE_P(TypedTestP, DISABLED_ShouldNotRun); |
| 2376 | |
| 2377 | INSTANTIATE_TYPED_TEST_CASE_P(My, TypedTestP, NumericTypes); |
| 2378 | |
| 2379 | template <typename T> |
| 2380 | class DISABLED_TypedTestP : public Test { |
| 2381 | }; |
| 2382 | |
| 2383 | TYPED_TEST_CASE_P(DISABLED_TypedTestP); |
| 2384 | |
| 2385 | TYPED_TEST_P(DISABLED_TypedTestP, ShouldNotRun) { |
| 2386 | FAIL() << "Unexpected failure: " |
| 2387 | << "Disabled type-parameterized test should not run."; |
| 2388 | } |
| 2389 | |
| 2390 | REGISTER_TYPED_TEST_CASE_P(DISABLED_TypedTestP, ShouldNotRun); |
| 2391 | |
| 2392 | INSTANTIATE_TYPED_TEST_CASE_P(My, DISABLED_TypedTestP, NumericTypes); |
| 2393 | |
| 2394 | #endif // GTEST_HAS_TYPED_TEST_P |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2395 | |
| 2396 | // Tests that assertion macros evaluate their arguments exactly once. |
| 2397 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2398 | class SingleEvaluationTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2399 | protected: |
| 2400 | SingleEvaluationTest() { |
| 2401 | p1_ = s1_; |
| 2402 | p2_ = s2_; |
| 2403 | a_ = 0; |
| 2404 | b_ = 0; |
| 2405 | } |
| 2406 | |
| 2407 | // This helper function is needed by the FailedASSERT_STREQ test |
| 2408 | // below. |
| 2409 | static void CompareAndIncrementCharPtrs() { |
| 2410 | ASSERT_STREQ(p1_++, p2_++); |
| 2411 | } |
| 2412 | |
| 2413 | // This helper function is needed by the FailedASSERT_NE test below. |
| 2414 | static void CompareAndIncrementInts() { |
| 2415 | ASSERT_NE(a_++, b_++); |
| 2416 | } |
| 2417 | |
| 2418 | static const char* const s1_; |
| 2419 | static const char* const s2_; |
| 2420 | static const char* p1_; |
| 2421 | static const char* p2_; |
| 2422 | |
| 2423 | static int a_; |
| 2424 | static int b_; |
| 2425 | }; |
| 2426 | |
| 2427 | const char* const SingleEvaluationTest::s1_ = "01234"; |
| 2428 | const char* const SingleEvaluationTest::s2_ = "abcde"; |
| 2429 | const char* SingleEvaluationTest::p1_; |
| 2430 | const char* SingleEvaluationTest::p2_; |
| 2431 | int SingleEvaluationTest::a_; |
| 2432 | int SingleEvaluationTest::b_; |
| 2433 | |
| 2434 | // Tests that when ASSERT_STREQ fails, it evaluates its arguments |
| 2435 | // exactly once. |
| 2436 | TEST_F(SingleEvaluationTest, FailedASSERT_STREQ) { |
| 2437 | EXPECT_FATAL_FAILURE(CompareAndIncrementCharPtrs(), |
| 2438 | "p2_++"); |
| 2439 | EXPECT_EQ(s1_ + 1, p1_); |
| 2440 | EXPECT_EQ(s2_ + 1, p2_); |
| 2441 | } |
| 2442 | |
| 2443 | // Tests that string assertion arguments are evaluated exactly once. |
| 2444 | TEST_F(SingleEvaluationTest, ASSERT_STR) { |
| 2445 | // successful EXPECT_STRNE |
| 2446 | EXPECT_STRNE(p1_++, p2_++); |
| 2447 | EXPECT_EQ(s1_ + 1, p1_); |
| 2448 | EXPECT_EQ(s2_ + 1, p2_); |
| 2449 | |
| 2450 | // failed EXPECT_STRCASEEQ |
| 2451 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASEEQ(p1_++, p2_++), |
| 2452 | "ignoring case"); |
| 2453 | EXPECT_EQ(s1_ + 2, p1_); |
| 2454 | EXPECT_EQ(s2_ + 2, p2_); |
| 2455 | } |
| 2456 | |
| 2457 | // Tests that when ASSERT_NE fails, it evaluates its arguments exactly |
| 2458 | // once. |
| 2459 | TEST_F(SingleEvaluationTest, FailedASSERT_NE) { |
| 2460 | EXPECT_FATAL_FAILURE(CompareAndIncrementInts(), "(a_++) != (b_++)"); |
| 2461 | EXPECT_EQ(1, a_); |
| 2462 | EXPECT_EQ(1, b_); |
| 2463 | } |
| 2464 | |
| 2465 | // Tests that assertion arguments are evaluated exactly once. |
| 2466 | TEST_F(SingleEvaluationTest, OtherCases) { |
| 2467 | // successful EXPECT_TRUE |
| 2468 | EXPECT_TRUE(0 == a_++); // NOLINT |
| 2469 | EXPECT_EQ(1, a_); |
| 2470 | |
| 2471 | // failed EXPECT_TRUE |
| 2472 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(-1 == a_++), "-1 == a_++"); |
| 2473 | EXPECT_EQ(2, a_); |
| 2474 | |
| 2475 | // successful EXPECT_GT |
| 2476 | EXPECT_GT(a_++, b_++); |
| 2477 | EXPECT_EQ(3, a_); |
| 2478 | EXPECT_EQ(1, b_); |
| 2479 | |
| 2480 | // failed EXPECT_LT |
| 2481 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(a_++, b_++), "(a_++) < (b_++)"); |
| 2482 | EXPECT_EQ(4, a_); |
| 2483 | EXPECT_EQ(2, b_); |
| 2484 | |
| 2485 | // successful ASSERT_TRUE |
| 2486 | ASSERT_TRUE(0 < a_++); // NOLINT |
| 2487 | EXPECT_EQ(5, a_); |
| 2488 | |
| 2489 | // successful ASSERT_GT |
| 2490 | ASSERT_GT(a_++, b_++); |
| 2491 | EXPECT_EQ(6, a_); |
| 2492 | EXPECT_EQ(3, b_); |
| 2493 | } |
| 2494 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 2495 | #if GTEST_HAS_EXCEPTIONS |
| 2496 | |
| 2497 | void ThrowAnInteger() { |
| 2498 | throw 1; |
| 2499 | } |
| 2500 | |
| 2501 | // Tests that assertion arguments are evaluated exactly once. |
| 2502 | TEST_F(SingleEvaluationTest, ExceptionTests) { |
| 2503 | // successful EXPECT_THROW |
| 2504 | EXPECT_THROW({ // NOLINT |
| 2505 | a_++; |
| 2506 | ThrowAnInteger(); |
| 2507 | }, int); |
| 2508 | EXPECT_EQ(1, a_); |
| 2509 | |
| 2510 | // failed EXPECT_THROW, throws different |
| 2511 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT |
| 2512 | a_++; |
| 2513 | ThrowAnInteger(); |
| 2514 | }, bool), "throws a different type"); |
| 2515 | EXPECT_EQ(2, a_); |
| 2516 | |
| 2517 | // failed EXPECT_THROW, throws nothing |
| 2518 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(a_++, bool), "throws nothing"); |
| 2519 | EXPECT_EQ(3, a_); |
| 2520 | |
| 2521 | // successful EXPECT_NO_THROW |
| 2522 | EXPECT_NO_THROW(a_++); |
| 2523 | EXPECT_EQ(4, a_); |
| 2524 | |
| 2525 | // failed EXPECT_NO_THROW |
| 2526 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW({ // NOLINT |
| 2527 | a_++; |
| 2528 | ThrowAnInteger(); |
| 2529 | }), "it throws"); |
| 2530 | EXPECT_EQ(5, a_); |
| 2531 | |
| 2532 | // successful EXPECT_ANY_THROW |
| 2533 | EXPECT_ANY_THROW({ // NOLINT |
| 2534 | a_++; |
| 2535 | ThrowAnInteger(); |
| 2536 | }); |
| 2537 | EXPECT_EQ(6, a_); |
| 2538 | |
| 2539 | // failed EXPECT_ANY_THROW |
| 2540 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't"); |
| 2541 | EXPECT_EQ(7, a_); |
| 2542 | } |
| 2543 | |
| 2544 | #endif // GTEST_HAS_EXCEPTIONS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2545 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2546 | // Tests {ASSERT|EXPECT}_NO_FATAL_FAILURE. |
| 2547 | class NoFatalFailureTest : public Test { |
| 2548 | protected: |
| 2549 | void Succeeds() {} |
| 2550 | void FailsNonFatal() { |
| 2551 | ADD_FAILURE() << "some non-fatal failure"; |
| 2552 | } |
| 2553 | void Fails() { |
| 2554 | FAIL() << "some fatal failure"; |
| 2555 | } |
| 2556 | |
| 2557 | void DoAssertNoFatalFailureOnFails() { |
| 2558 | ASSERT_NO_FATAL_FAILURE(Fails()); |
| 2559 | ADD_FAILURE() << "shold not reach here."; |
| 2560 | } |
| 2561 | |
| 2562 | void DoExpectNoFatalFailureOnFails() { |
| 2563 | EXPECT_NO_FATAL_FAILURE(Fails()); |
| 2564 | ADD_FAILURE() << "other failure"; |
| 2565 | } |
| 2566 | }; |
| 2567 | |
| 2568 | TEST_F(NoFatalFailureTest, NoFailure) { |
| 2569 | EXPECT_NO_FATAL_FAILURE(Succeeds()); |
| 2570 | ASSERT_NO_FATAL_FAILURE(Succeeds()); |
| 2571 | } |
| 2572 | |
| 2573 | TEST_F(NoFatalFailureTest, NonFatalIsNoFailure) { |
| 2574 | EXPECT_NONFATAL_FAILURE( |
| 2575 | EXPECT_NO_FATAL_FAILURE(FailsNonFatal()), |
| 2576 | "some non-fatal failure"); |
| 2577 | EXPECT_NONFATAL_FAILURE( |
| 2578 | ASSERT_NO_FATAL_FAILURE(FailsNonFatal()), |
| 2579 | "some non-fatal failure"); |
| 2580 | } |
| 2581 | |
| 2582 | TEST_F(NoFatalFailureTest, AssertNoFatalFailureOnFatalFailure) { |
| 2583 | TestPartResultArray gtest_failures; |
| 2584 | { |
| 2585 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 2586 | DoAssertNoFatalFailureOnFails(); |
| 2587 | } |
| 2588 | ASSERT_EQ(2, gtest_failures.size()); |
| 2589 | EXPECT_EQ(testing::TPRT_FATAL_FAILURE, |
| 2590 | gtest_failures.GetTestPartResult(0).type()); |
| 2591 | EXPECT_EQ(testing::TPRT_FATAL_FAILURE, |
| 2592 | gtest_failures.GetTestPartResult(1).type()); |
| 2593 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "some fatal failure", |
| 2594 | gtest_failures.GetTestPartResult(0).message()); |
| 2595 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "it does", |
| 2596 | gtest_failures.GetTestPartResult(1).message()); |
| 2597 | } |
| 2598 | |
| 2599 | TEST_F(NoFatalFailureTest, ExpectNoFatalFailureOnFatalFailure) { |
| 2600 | TestPartResultArray gtest_failures; |
| 2601 | { |
| 2602 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 2603 | DoExpectNoFatalFailureOnFails(); |
| 2604 | } |
| 2605 | ASSERT_EQ(3, gtest_failures.size()); |
| 2606 | EXPECT_EQ(testing::TPRT_FATAL_FAILURE, |
| 2607 | gtest_failures.GetTestPartResult(0).type()); |
| 2608 | EXPECT_EQ(testing::TPRT_NONFATAL_FAILURE, |
| 2609 | gtest_failures.GetTestPartResult(1).type()); |
| 2610 | EXPECT_EQ(testing::TPRT_NONFATAL_FAILURE, |
| 2611 | gtest_failures.GetTestPartResult(2).type()); |
| 2612 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "some fatal failure", |
| 2613 | gtest_failures.GetTestPartResult(0).message()); |
| 2614 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "it does", |
| 2615 | gtest_failures.GetTestPartResult(1).message()); |
| 2616 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "other failure", |
| 2617 | gtest_failures.GetTestPartResult(2).message()); |
| 2618 | } |
| 2619 | |
| 2620 | TEST_F(NoFatalFailureTest, MessageIsStreamable) { |
| 2621 | TestPartResultArray gtest_failures; |
| 2622 | { |
| 2623 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 2624 | EXPECT_NO_FATAL_FAILURE(FAIL() << "foo") << "my message"; |
| 2625 | } |
| 2626 | ASSERT_EQ(2, gtest_failures.size()); |
| 2627 | EXPECT_EQ(testing::TPRT_NONFATAL_FAILURE, |
| 2628 | gtest_failures.GetTestPartResult(0).type()); |
| 2629 | EXPECT_EQ(testing::TPRT_NONFATAL_FAILURE, |
| 2630 | gtest_failures.GetTestPartResult(1).type()); |
| 2631 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "foo", |
| 2632 | gtest_failures.GetTestPartResult(0).message()); |
| 2633 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "my message", |
| 2634 | gtest_failures.GetTestPartResult(1).message()); |
| 2635 | } |
| 2636 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2637 | // Tests non-string assertions. |
| 2638 | |
| 2639 | // Tests EqFailure(), used for implementing *EQ* assertions. |
| 2640 | TEST(AssertionTest, EqFailure) { |
| 2641 | const String foo_val("5"), bar_val("6"); |
| 2642 | const String msg1( |
| 2643 | EqFailure("foo", "bar", foo_val, bar_val, false) |
| 2644 | .failure_message()); |
| 2645 | EXPECT_STREQ( |
| 2646 | "Value of: bar\n" |
| 2647 | " Actual: 6\n" |
| 2648 | "Expected: foo\n" |
| 2649 | "Which is: 5", |
| 2650 | msg1.c_str()); |
| 2651 | |
| 2652 | const String msg2( |
| 2653 | EqFailure("foo", "6", foo_val, bar_val, false) |
| 2654 | .failure_message()); |
| 2655 | EXPECT_STREQ( |
| 2656 | "Value of: 6\n" |
| 2657 | "Expected: foo\n" |
| 2658 | "Which is: 5", |
| 2659 | msg2.c_str()); |
| 2660 | |
| 2661 | const String msg3( |
| 2662 | EqFailure("5", "bar", foo_val, bar_val, false) |
| 2663 | .failure_message()); |
| 2664 | EXPECT_STREQ( |
| 2665 | "Value of: bar\n" |
| 2666 | " Actual: 6\n" |
| 2667 | "Expected: 5", |
| 2668 | msg3.c_str()); |
| 2669 | |
| 2670 | const String msg4( |
| 2671 | EqFailure("5", "6", foo_val, bar_val, false).failure_message()); |
| 2672 | EXPECT_STREQ( |
| 2673 | "Value of: 6\n" |
| 2674 | "Expected: 5", |
| 2675 | msg4.c_str()); |
| 2676 | |
| 2677 | const String msg5( |
| 2678 | EqFailure("foo", "bar", |
| 2679 | String("\"x\""), String("\"y\""), |
| 2680 | true).failure_message()); |
| 2681 | EXPECT_STREQ( |
| 2682 | "Value of: bar\n" |
| 2683 | " Actual: \"y\"\n" |
| 2684 | "Expected: foo (ignoring case)\n" |
| 2685 | "Which is: \"x\"", |
| 2686 | msg5.c_str()); |
| 2687 | } |
| 2688 | |
| 2689 | // Tests AppendUserMessage(), used for implementing the *EQ* macros. |
| 2690 | TEST(AssertionTest, AppendUserMessage) { |
| 2691 | const String foo("foo"); |
| 2692 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2693 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2694 | EXPECT_STREQ("foo", |
| 2695 | AppendUserMessage(foo, msg).c_str()); |
| 2696 | |
| 2697 | msg << "bar"; |
| 2698 | EXPECT_STREQ("foo\nbar", |
| 2699 | AppendUserMessage(foo, msg).c_str()); |
| 2700 | } |
| 2701 | |
| 2702 | // Tests ASSERT_TRUE. |
| 2703 | TEST(AssertionTest, ASSERT_TRUE) { |
| 2704 | ASSERT_TRUE(2 > 1); // NOLINT |
| 2705 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(2 < 1), |
| 2706 | "2 < 1"); |
| 2707 | } |
| 2708 | |
| 2709 | // Tests ASSERT_FALSE. |
| 2710 | TEST(AssertionTest, ASSERT_FALSE) { |
| 2711 | ASSERT_FALSE(2 < 1); // NOLINT |
| 2712 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(2 > 1), |
| 2713 | "Value of: 2 > 1\n" |
| 2714 | " Actual: true\n" |
| 2715 | "Expected: false"); |
| 2716 | } |
| 2717 | |
| 2718 | // Tests using ASSERT_EQ on double values. The purpose is to make |
| 2719 | // sure that the specialization we did for integer and anonymous enums |
| 2720 | // isn't used for double arguments. |
| 2721 | TEST(ExpectTest, ASSERT_EQ_Double) { |
| 2722 | // A success. |
| 2723 | ASSERT_EQ(5.6, 5.6); |
| 2724 | |
| 2725 | // A failure. |
| 2726 | EXPECT_FATAL_FAILURE(ASSERT_EQ(5.1, 5.2), |
| 2727 | "5.1"); |
| 2728 | } |
| 2729 | |
| 2730 | // Tests ASSERT_EQ. |
| 2731 | TEST(AssertionTest, ASSERT_EQ) { |
| 2732 | ASSERT_EQ(5, 2 + 3); |
| 2733 | EXPECT_FATAL_FAILURE(ASSERT_EQ(5, 2*3), |
| 2734 | "Value of: 2*3\n" |
| 2735 | " Actual: 6\n" |
| 2736 | "Expected: 5"); |
| 2737 | } |
| 2738 | |
| 2739 | // Tests ASSERT_EQ(NULL, pointer). |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2740 | #ifndef GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2741 | // The NULL-detection template magic fails to compile with |
| 2742 | // the Nokia compiler and crashes the ARM compiler, hence |
| 2743 | // not testing on Symbian. |
| 2744 | TEST(AssertionTest, ASSERT_EQ_NULL) { |
| 2745 | // A success. |
| 2746 | const char* p = NULL; |
| 2747 | ASSERT_EQ(NULL, p); |
| 2748 | |
| 2749 | // A failure. |
| 2750 | static int n = 0; |
| 2751 | EXPECT_FATAL_FAILURE(ASSERT_EQ(NULL, &n), |
| 2752 | "Value of: &n\n"); |
| 2753 | } |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2754 | #endif // GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2755 | |
| 2756 | // Tests ASSERT_EQ(0, non_pointer). Since the literal 0 can be |
| 2757 | // treated as a null pointer by the compiler, we need to make sure |
| 2758 | // that ASSERT_EQ(0, non_pointer) isn't interpreted by Google Test as |
| 2759 | // ASSERT_EQ(static_cast<void*>(NULL), non_pointer). |
| 2760 | TEST(ExpectTest, ASSERT_EQ_0) { |
| 2761 | int n = 0; |
| 2762 | |
| 2763 | // A success. |
| 2764 | ASSERT_EQ(0, n); |
| 2765 | |
| 2766 | // A failure. |
| 2767 | EXPECT_FATAL_FAILURE(ASSERT_EQ(0, 5.6), |
| 2768 | "Expected: 0"); |
| 2769 | } |
| 2770 | |
| 2771 | // Tests ASSERT_NE. |
| 2772 | TEST(AssertionTest, ASSERT_NE) { |
| 2773 | ASSERT_NE(6, 7); |
| 2774 | EXPECT_FATAL_FAILURE(ASSERT_NE('a', 'a'), |
| 2775 | "Expected: ('a') != ('a'), " |
| 2776 | "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)"); |
| 2777 | } |
| 2778 | |
| 2779 | // Tests ASSERT_LE. |
| 2780 | TEST(AssertionTest, ASSERT_LE) { |
| 2781 | ASSERT_LE(2, 3); |
| 2782 | ASSERT_LE(2, 2); |
| 2783 | EXPECT_FATAL_FAILURE(ASSERT_LE(2, 0), |
| 2784 | "Expected: (2) <= (0), actual: 2 vs 0"); |
| 2785 | } |
| 2786 | |
| 2787 | // Tests ASSERT_LT. |
| 2788 | TEST(AssertionTest, ASSERT_LT) { |
| 2789 | ASSERT_LT(2, 3); |
| 2790 | EXPECT_FATAL_FAILURE(ASSERT_LT(2, 2), |
| 2791 | "Expected: (2) < (2), actual: 2 vs 2"); |
| 2792 | } |
| 2793 | |
| 2794 | // Tests ASSERT_GE. |
| 2795 | TEST(AssertionTest, ASSERT_GE) { |
| 2796 | ASSERT_GE(2, 1); |
| 2797 | ASSERT_GE(2, 2); |
| 2798 | EXPECT_FATAL_FAILURE(ASSERT_GE(2, 3), |
| 2799 | "Expected: (2) >= (3), actual: 2 vs 3"); |
| 2800 | } |
| 2801 | |
| 2802 | // Tests ASSERT_GT. |
| 2803 | TEST(AssertionTest, ASSERT_GT) { |
| 2804 | ASSERT_GT(2, 1); |
| 2805 | EXPECT_FATAL_FAILURE(ASSERT_GT(2, 2), |
| 2806 | "Expected: (2) > (2), actual: 2 vs 2"); |
| 2807 | } |
| 2808 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 2809 | #if GTEST_HAS_EXCEPTIONS |
| 2810 | |
| 2811 | // Tests ASSERT_THROW. |
| 2812 | TEST(AssertionTest, ASSERT_THROW) { |
| 2813 | ASSERT_THROW(ThrowAnInteger(), int); |
| 2814 | EXPECT_FATAL_FAILURE(ASSERT_THROW(ThrowAnInteger(), bool), |
| 2815 | "Expected: ThrowAnInteger() throws an exception of type"\ |
| 2816 | " bool.\n Actual: it throws a different type."); |
| 2817 | EXPECT_FATAL_FAILURE(ASSERT_THROW(1, bool), |
| 2818 | "Expected: 1 throws an exception of type bool.\n"\ |
| 2819 | " Actual: it throws nothing."); |
| 2820 | } |
| 2821 | |
| 2822 | // Tests ASSERT_NO_THROW. |
| 2823 | TEST(AssertionTest, ASSERT_NO_THROW) { |
| 2824 | ASSERT_NO_THROW(1); |
| 2825 | EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()), |
| 2826 | "Expected: ThrowAnInteger() doesn't throw an exception."\ |
| 2827 | "\n Actual: it throws."); |
| 2828 | } |
| 2829 | |
| 2830 | // Tests ASSERT_ANY_THROW. |
| 2831 | TEST(AssertionTest, ASSERT_ANY_THROW) { |
| 2832 | ASSERT_ANY_THROW(ThrowAnInteger()); |
| 2833 | EXPECT_FATAL_FAILURE(ASSERT_ANY_THROW(1), |
| 2834 | "Expected: 1 throws an exception.\n Actual: it "\ |
| 2835 | "doesn't."); |
| 2836 | } |
| 2837 | |
| 2838 | #endif // GTEST_HAS_EXCEPTIONS |
| 2839 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2840 | // Makes sure we deal with the precedence of <<. This test should |
| 2841 | // compile. |
| 2842 | TEST(AssertionTest, AssertPrecedence) { |
| 2843 | ASSERT_EQ(1 < 2, true); |
| 2844 | ASSERT_EQ(true && false, false); |
| 2845 | } |
| 2846 | |
| 2847 | // A subroutine used by the following test. |
| 2848 | void TestEq1(int x) { |
| 2849 | ASSERT_EQ(1, x); |
| 2850 | } |
| 2851 | |
| 2852 | // Tests calling a test subroutine that's not part of a fixture. |
| 2853 | TEST(AssertionTest, NonFixtureSubroutine) { |
| 2854 | EXPECT_FATAL_FAILURE(TestEq1(2), |
| 2855 | "Value of: x"); |
| 2856 | } |
| 2857 | |
| 2858 | // An uncopyable class. |
| 2859 | class Uncopyable { |
| 2860 | public: |
| 2861 | explicit Uncopyable(int value) : value_(value) {} |
| 2862 | |
| 2863 | int value() const { return value_; } |
| 2864 | bool operator==(const Uncopyable& rhs) const { |
| 2865 | return value() == rhs.value(); |
| 2866 | } |
| 2867 | private: |
| 2868 | // This constructor deliberately has no implementation, as we don't |
| 2869 | // want this class to be copyable. |
| 2870 | Uncopyable(const Uncopyable&); // NOLINT |
| 2871 | |
| 2872 | int value_; |
| 2873 | }; |
| 2874 | |
| 2875 | ::std::ostream& operator<<(::std::ostream& os, const Uncopyable& value) { |
| 2876 | return os << value.value(); |
| 2877 | } |
| 2878 | |
| 2879 | |
| 2880 | bool IsPositiveUncopyable(const Uncopyable& x) { |
| 2881 | return x.value() > 0; |
| 2882 | } |
| 2883 | |
| 2884 | // A subroutine used by the following test. |
| 2885 | void TestAssertNonPositive() { |
| 2886 | Uncopyable y(-1); |
| 2887 | ASSERT_PRED1(IsPositiveUncopyable, y); |
| 2888 | } |
| 2889 | // A subroutine used by the following test. |
| 2890 | void TestAssertEqualsUncopyable() { |
| 2891 | Uncopyable x(5); |
| 2892 | Uncopyable y(-1); |
| 2893 | ASSERT_EQ(x, y); |
| 2894 | } |
| 2895 | |
| 2896 | // Tests that uncopyable objects can be used in assertions. |
| 2897 | TEST(AssertionTest, AssertWorksWithUncopyableObject) { |
| 2898 | Uncopyable x(5); |
| 2899 | ASSERT_PRED1(IsPositiveUncopyable, x); |
| 2900 | ASSERT_EQ(x, x); |
| 2901 | EXPECT_FATAL_FAILURE(TestAssertNonPositive(), |
| 2902 | "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1"); |
| 2903 | EXPECT_FATAL_FAILURE(TestAssertEqualsUncopyable(), |
| 2904 | "Value of: y\n Actual: -1\nExpected: x\nWhich is: 5"); |
| 2905 | } |
| 2906 | |
| 2907 | // Tests that uncopyable objects can be used in expects. |
| 2908 | TEST(AssertionTest, ExpectWorksWithUncopyableObject) { |
| 2909 | Uncopyable x(5); |
| 2910 | EXPECT_PRED1(IsPositiveUncopyable, x); |
| 2911 | Uncopyable y(-1); |
| 2912 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED1(IsPositiveUncopyable, y), |
| 2913 | "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1"); |
| 2914 | EXPECT_EQ(x, x); |
| 2915 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(x, y), |
| 2916 | "Value of: y\n Actual: -1\nExpected: x\nWhich is: 5"); |
| 2917 | } |
| 2918 | |
| 2919 | |
| 2920 | // The version of gcc used in XCode 2.2 has a bug and doesn't allow |
| 2921 | // anonymous enums in assertions. Therefore the following test is |
| 2922 | // done only on Linux and Windows. |
| 2923 | #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_WINDOWS) |
| 2924 | |
| 2925 | // Tests using assertions with anonymous enums. |
| 2926 | enum { |
| 2927 | CASE_A = -1, |
| 2928 | #ifdef GTEST_OS_LINUX |
| 2929 | // We want to test the case where the size of the anonymous enum is |
| 2930 | // larger than sizeof(int), to make sure our implementation of the |
| 2931 | // assertions doesn't truncate the enums. However, MSVC |
| 2932 | // (incorrectly) doesn't allow an enum value to exceed the range of |
| 2933 | // an int, so this has to be conditionally compiled. |
| 2934 | // |
| 2935 | // On Linux, CASE_B and CASE_A have the same value when truncated to |
| 2936 | // int size. We want to test whether this will confuse the |
| 2937 | // assertions. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2938 | CASE_B = testing::internal::kMaxBiggestInt, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2939 | #else |
| 2940 | CASE_B = INT_MAX, |
| 2941 | #endif // GTEST_OS_LINUX |
| 2942 | }; |
| 2943 | |
| 2944 | TEST(AssertionTest, AnonymousEnum) { |
| 2945 | #ifdef GTEST_OS_LINUX |
| 2946 | EXPECT_EQ(static_cast<int>(CASE_A), static_cast<int>(CASE_B)); |
| 2947 | #endif // GTEST_OS_LINUX |
| 2948 | |
| 2949 | EXPECT_EQ(CASE_A, CASE_A); |
| 2950 | EXPECT_NE(CASE_A, CASE_B); |
| 2951 | EXPECT_LT(CASE_A, CASE_B); |
| 2952 | EXPECT_LE(CASE_A, CASE_B); |
| 2953 | EXPECT_GT(CASE_B, CASE_A); |
| 2954 | EXPECT_GE(CASE_A, CASE_A); |
| 2955 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(CASE_A, CASE_B), |
| 2956 | "(CASE_A) >= (CASE_B)"); |
| 2957 | |
| 2958 | ASSERT_EQ(CASE_A, CASE_A); |
| 2959 | ASSERT_NE(CASE_A, CASE_B); |
| 2960 | ASSERT_LT(CASE_A, CASE_B); |
| 2961 | ASSERT_LE(CASE_A, CASE_B); |
| 2962 | ASSERT_GT(CASE_B, CASE_A); |
| 2963 | ASSERT_GE(CASE_A, CASE_A); |
| 2964 | EXPECT_FATAL_FAILURE(ASSERT_EQ(CASE_A, CASE_B), |
| 2965 | "Value of: CASE_B"); |
| 2966 | } |
| 2967 | |
| 2968 | #endif // defined(GTEST_OS_LINUX) || defined(GTEST_OS_WINDOWS) |
| 2969 | |
| 2970 | #if defined(GTEST_OS_WINDOWS) |
| 2971 | |
| 2972 | static HRESULT UnexpectedHRESULTFailure() { |
| 2973 | return E_UNEXPECTED; |
| 2974 | } |
| 2975 | |
| 2976 | static HRESULT OkHRESULTSuccess() { |
| 2977 | return S_OK; |
| 2978 | } |
| 2979 | |
| 2980 | static HRESULT FalseHRESULTSuccess() { |
| 2981 | return S_FALSE; |
| 2982 | } |
| 2983 | |
| 2984 | // HRESULT assertion tests test both zero and non-zero |
| 2985 | // success codes as well as failure message for each. |
| 2986 | // |
| 2987 | // Windows CE doesn't support message texts. |
| 2988 | TEST(HRESULTAssertionTest, EXPECT_HRESULT_SUCCEEDED) { |
| 2989 | EXPECT_HRESULT_SUCCEEDED(S_OK); |
| 2990 | EXPECT_HRESULT_SUCCEEDED(S_FALSE); |
| 2991 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2992 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_SUCCEEDED(UnexpectedHRESULTFailure()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 2993 | "Expected: (UnexpectedHRESULTFailure()) succeeds.\n" |
| 2994 | " Actual: 0x8000FFFF"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | TEST(HRESULTAssertionTest, ASSERT_HRESULT_SUCCEEDED) { |
| 2998 | ASSERT_HRESULT_SUCCEEDED(S_OK); |
| 2999 | ASSERT_HRESULT_SUCCEEDED(S_FALSE); |
| 3000 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3001 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_SUCCEEDED(UnexpectedHRESULTFailure()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3002 | "Expected: (UnexpectedHRESULTFailure()) succeeds.\n" |
| 3003 | " Actual: 0x8000FFFF"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | TEST(HRESULTAssertionTest, EXPECT_HRESULT_FAILED) { |
| 3007 | EXPECT_HRESULT_FAILED(E_UNEXPECTED); |
| 3008 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3009 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_FAILED(OkHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3010 | "Expected: (OkHRESULTSuccess()) fails.\n" |
| 3011 | " Actual: 0x00000000"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3012 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_FAILED(FalseHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3013 | "Expected: (FalseHRESULTSuccess()) fails.\n" |
| 3014 | " Actual: 0x00000001"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | TEST(HRESULTAssertionTest, ASSERT_HRESULT_FAILED) { |
| 3018 | ASSERT_HRESULT_FAILED(E_UNEXPECTED); |
| 3019 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3020 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_FAILED(OkHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3021 | "Expected: (OkHRESULTSuccess()) fails.\n" |
| 3022 | " Actual: 0x00000000"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3023 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_FAILED(FalseHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3024 | "Expected: (FalseHRESULTSuccess()) fails.\n" |
| 3025 | " Actual: 0x00000001"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | // Tests that streaming to the HRESULT macros works. |
| 3029 | TEST(HRESULTAssertionTest, Streaming) { |
| 3030 | EXPECT_HRESULT_SUCCEEDED(S_OK) << "unexpected failure"; |
| 3031 | ASSERT_HRESULT_SUCCEEDED(S_OK) << "unexpected failure"; |
| 3032 | EXPECT_HRESULT_FAILED(E_UNEXPECTED) << "unexpected failure"; |
| 3033 | ASSERT_HRESULT_FAILED(E_UNEXPECTED) << "unexpected failure"; |
| 3034 | |
| 3035 | EXPECT_NONFATAL_FAILURE( |
| 3036 | EXPECT_HRESULT_SUCCEEDED(E_UNEXPECTED) << "expected failure", |
| 3037 | "expected failure"); |
| 3038 | |
| 3039 | EXPECT_FATAL_FAILURE( |
| 3040 | ASSERT_HRESULT_SUCCEEDED(E_UNEXPECTED) << "expected failure", |
| 3041 | "expected failure"); |
| 3042 | |
| 3043 | EXPECT_NONFATAL_FAILURE( |
| 3044 | EXPECT_HRESULT_FAILED(S_OK) << "expected failure", |
| 3045 | "expected failure"); |
| 3046 | |
| 3047 | EXPECT_FATAL_FAILURE( |
| 3048 | ASSERT_HRESULT_FAILED(S_OK) << "expected failure", |
| 3049 | "expected failure"); |
| 3050 | } |
| 3051 | |
| 3052 | #endif // defined(GTEST_OS_WINDOWS) |
| 3053 | |
| 3054 | // Tests that the assertion macros behave like single statements. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3055 | TEST(AssertionSyntaxTest, BasicAssertionsBehavesLikeSingleStatement) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3056 | if (false) |
| 3057 | ASSERT_TRUE(false) << "This should never be executed; " |
| 3058 | "It's a compilation test only."; |
| 3059 | |
| 3060 | if (true) |
| 3061 | EXPECT_FALSE(false); |
| 3062 | else |
| 3063 | ; |
| 3064 | |
| 3065 | if (false) |
| 3066 | ASSERT_LT(1, 3); |
| 3067 | |
| 3068 | if (false) |
| 3069 | ; |
| 3070 | else |
| 3071 | EXPECT_GT(3, 2) << ""; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3072 | } |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3073 | |
| 3074 | #if GTEST_HAS_EXCEPTIONS |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3075 | TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) { |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3076 | if (false) |
| 3077 | EXPECT_THROW(1, bool); |
| 3078 | |
| 3079 | if (true) |
| 3080 | EXPECT_THROW(ThrowAnInteger(), int); |
| 3081 | else |
| 3082 | ; |
| 3083 | |
| 3084 | if (false) |
| 3085 | EXPECT_NO_THROW(ThrowAnInteger()); |
| 3086 | |
| 3087 | if (true) |
| 3088 | EXPECT_NO_THROW(1); |
| 3089 | else |
| 3090 | ; |
| 3091 | |
| 3092 | if (false) |
| 3093 | EXPECT_ANY_THROW(1); |
| 3094 | |
| 3095 | if (true) |
| 3096 | EXPECT_ANY_THROW(ThrowAnInteger()); |
| 3097 | else |
| 3098 | ; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3099 | } |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3100 | #endif // GTEST_HAS_EXCEPTIONS |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3101 | |
| 3102 | TEST(AssertionSyntaxTest, NoFatalFailureAssertionsBehavesLikeSingleStatement) { |
| 3103 | if (false) |
| 3104 | EXPECT_NO_FATAL_FAILURE(FAIL()) << "This should never be executed. " |
| 3105 | << "It's a compilation test only."; |
| 3106 | else |
| 3107 | ; |
| 3108 | |
| 3109 | if (false) |
| 3110 | ASSERT_NO_FATAL_FAILURE(FAIL()) << ""; |
| 3111 | else |
| 3112 | ; |
| 3113 | |
| 3114 | if (true) |
| 3115 | EXPECT_NO_FATAL_FAILURE(SUCCEED()); |
| 3116 | else |
| 3117 | ; |
| 3118 | |
| 3119 | if (false) |
| 3120 | ; |
| 3121 | else |
| 3122 | ASSERT_NO_FATAL_FAILURE(SUCCEED()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | // Tests that the assertion macros work well with switch statements. |
| 3126 | TEST(AssertionSyntaxTest, WorksWithSwitch) { |
| 3127 | switch (0) { |
| 3128 | case 1: |
| 3129 | break; |
| 3130 | default: |
| 3131 | ASSERT_TRUE(true); |
| 3132 | } |
| 3133 | |
| 3134 | switch (0) |
| 3135 | case 0: |
| 3136 | EXPECT_FALSE(false) << "EXPECT_FALSE failed in switch case"; |
| 3137 | |
| 3138 | // Binary assertions are implemented using a different code path |
| 3139 | // than the Boolean assertions. Hence we test them separately. |
| 3140 | switch (0) { |
| 3141 | case 1: |
| 3142 | default: |
| 3143 | ASSERT_EQ(1, 1) << "ASSERT_EQ failed in default switch handler"; |
| 3144 | } |
| 3145 | |
| 3146 | switch (0) |
| 3147 | case 0: |
| 3148 | EXPECT_NE(1, 2); |
| 3149 | } |
| 3150 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3151 | #if GTEST_HAS_EXCEPTIONS |
| 3152 | |
| 3153 | void ThrowAString() { |
| 3154 | throw "String"; |
| 3155 | } |
| 3156 | |
| 3157 | // Test that the exception assertion macros compile and work with const |
| 3158 | // type qualifier. |
| 3159 | TEST(AssertionSyntaxTest, WorksWithConst) { |
| 3160 | ASSERT_THROW(ThrowAString(), const char*); |
| 3161 | |
| 3162 | EXPECT_THROW(ThrowAString(), const char*); |
| 3163 | } |
| 3164 | |
| 3165 | #endif // GTEST_HAS_EXCEPTIONS |
| 3166 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3167 | } // namespace |
| 3168 | |
| 3169 | // Returns the number of successful parts in the current test. |
| 3170 | static size_t GetSuccessfulPartCount() { |
| 3171 | return UnitTest::GetInstance()->impl()->current_test_result()-> |
| 3172 | successful_part_count(); |
| 3173 | } |
| 3174 | |
| 3175 | namespace testing { |
| 3176 | |
| 3177 | // Tests that Google Test tracks SUCCEED*. |
| 3178 | TEST(SuccessfulAssertionTest, SUCCEED) { |
| 3179 | SUCCEED(); |
| 3180 | SUCCEED() << "OK"; |
| 3181 | EXPECT_EQ(2u, GetSuccessfulPartCount()); |
| 3182 | } |
| 3183 | |
| 3184 | // Tests that Google Test doesn't track successful EXPECT_*. |
| 3185 | TEST(SuccessfulAssertionTest, EXPECT) { |
| 3186 | EXPECT_TRUE(true); |
| 3187 | EXPECT_EQ(0u, GetSuccessfulPartCount()); |
| 3188 | } |
| 3189 | |
| 3190 | // Tests that Google Test doesn't track successful EXPECT_STR*. |
| 3191 | TEST(SuccessfulAssertionTest, EXPECT_STR) { |
| 3192 | EXPECT_STREQ("", ""); |
| 3193 | EXPECT_EQ(0u, GetSuccessfulPartCount()); |
| 3194 | } |
| 3195 | |
| 3196 | // Tests that Google Test doesn't track successful ASSERT_*. |
| 3197 | TEST(SuccessfulAssertionTest, ASSERT) { |
| 3198 | ASSERT_TRUE(true); |
| 3199 | EXPECT_EQ(0u, GetSuccessfulPartCount()); |
| 3200 | } |
| 3201 | |
| 3202 | // Tests that Google Test doesn't track successful ASSERT_STR*. |
| 3203 | TEST(SuccessfulAssertionTest, ASSERT_STR) { |
| 3204 | ASSERT_STREQ("", ""); |
| 3205 | EXPECT_EQ(0u, GetSuccessfulPartCount()); |
| 3206 | } |
| 3207 | |
| 3208 | } // namespace testing |
| 3209 | |
| 3210 | namespace { |
| 3211 | |
| 3212 | // Tests EXPECT_TRUE. |
| 3213 | TEST(ExpectTest, EXPECT_TRUE) { |
| 3214 | EXPECT_TRUE(2 > 1); // NOLINT |
| 3215 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 < 1), |
| 3216 | "Value of: 2 < 1\n" |
| 3217 | " Actual: false\n" |
| 3218 | "Expected: true"); |
| 3219 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 > 3), |
| 3220 | "2 > 3"); |
| 3221 | } |
| 3222 | |
| 3223 | // Tests EXPECT_FALSE. |
| 3224 | TEST(ExpectTest, EXPECT_FALSE) { |
| 3225 | EXPECT_FALSE(2 < 1); // NOLINT |
| 3226 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 > 1), |
| 3227 | "Value of: 2 > 1\n" |
| 3228 | " Actual: true\n" |
| 3229 | "Expected: false"); |
| 3230 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 < 3), |
| 3231 | "2 < 3"); |
| 3232 | } |
| 3233 | |
| 3234 | // Tests EXPECT_EQ. |
| 3235 | TEST(ExpectTest, EXPECT_EQ) { |
| 3236 | EXPECT_EQ(5, 2 + 3); |
| 3237 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5, 2*3), |
| 3238 | "Value of: 2*3\n" |
| 3239 | " Actual: 6\n" |
| 3240 | "Expected: 5"); |
| 3241 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5, 2 - 3), |
| 3242 | "2 - 3"); |
| 3243 | } |
| 3244 | |
| 3245 | // Tests using EXPECT_EQ on double values. The purpose is to make |
| 3246 | // sure that the specialization we did for integer and anonymous enums |
| 3247 | // isn't used for double arguments. |
| 3248 | TEST(ExpectTest, EXPECT_EQ_Double) { |
| 3249 | // A success. |
| 3250 | EXPECT_EQ(5.6, 5.6); |
| 3251 | |
| 3252 | // A failure. |
| 3253 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5.1, 5.2), |
| 3254 | "5.1"); |
| 3255 | } |
| 3256 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3257 | #ifndef GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3258 | // Tests EXPECT_EQ(NULL, pointer). |
| 3259 | TEST(ExpectTest, EXPECT_EQ_NULL) { |
| 3260 | // A success. |
| 3261 | const char* p = NULL; |
| 3262 | EXPECT_EQ(NULL, p); |
| 3263 | |
| 3264 | // A failure. |
| 3265 | int n = 0; |
| 3266 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(NULL, &n), |
| 3267 | "Value of: &n\n"); |
| 3268 | } |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3269 | #endif // GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3270 | |
| 3271 | // Tests EXPECT_EQ(0, non_pointer). Since the literal 0 can be |
| 3272 | // treated as a null pointer by the compiler, we need to make sure |
| 3273 | // that EXPECT_EQ(0, non_pointer) isn't interpreted by Google Test as |
| 3274 | // EXPECT_EQ(static_cast<void*>(NULL), non_pointer). |
| 3275 | TEST(ExpectTest, EXPECT_EQ_0) { |
| 3276 | int n = 0; |
| 3277 | |
| 3278 | // A success. |
| 3279 | EXPECT_EQ(0, n); |
| 3280 | |
| 3281 | // A failure. |
| 3282 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(0, 5.6), |
| 3283 | "Expected: 0"); |
| 3284 | } |
| 3285 | |
| 3286 | // Tests EXPECT_NE. |
| 3287 | TEST(ExpectTest, EXPECT_NE) { |
| 3288 | EXPECT_NE(6, 7); |
| 3289 | |
| 3290 | EXPECT_NONFATAL_FAILURE(EXPECT_NE('a', 'a'), |
| 3291 | "Expected: ('a') != ('a'), " |
| 3292 | "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)"); |
| 3293 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(2, 2), |
| 3294 | "2"); |
| 3295 | char* const p0 = NULL; |
| 3296 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(p0, p0), |
| 3297 | "p0"); |
| 3298 | // Only way to get the Nokia compiler to compile the cast |
| 3299 | // is to have a separate void* variable first. Putting |
| 3300 | // the two casts on the same line doesn't work, neither does |
| 3301 | // a direct C-style to char*. |
| 3302 | void* pv1 = (void*)0x1234; // NOLINT |
| 3303 | char* const p1 = reinterpret_cast<char*>(pv1); |
| 3304 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(p1, p1), |
| 3305 | "p1"); |
| 3306 | } |
| 3307 | |
| 3308 | // Tests EXPECT_LE. |
| 3309 | TEST(ExpectTest, EXPECT_LE) { |
| 3310 | EXPECT_LE(2, 3); |
| 3311 | EXPECT_LE(2, 2); |
| 3312 | EXPECT_NONFATAL_FAILURE(EXPECT_LE(2, 0), |
| 3313 | "Expected: (2) <= (0), actual: 2 vs 0"); |
| 3314 | EXPECT_NONFATAL_FAILURE(EXPECT_LE(1.1, 0.9), |
| 3315 | "(1.1) <= (0.9)"); |
| 3316 | } |
| 3317 | |
| 3318 | // Tests EXPECT_LT. |
| 3319 | TEST(ExpectTest, EXPECT_LT) { |
| 3320 | EXPECT_LT(2, 3); |
| 3321 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 2), |
| 3322 | "Expected: (2) < (2), actual: 2 vs 2"); |
| 3323 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 1), |
| 3324 | "(2) < (1)"); |
| 3325 | } |
| 3326 | |
| 3327 | // Tests EXPECT_GE. |
| 3328 | TEST(ExpectTest, EXPECT_GE) { |
| 3329 | EXPECT_GE(2, 1); |
| 3330 | EXPECT_GE(2, 2); |
| 3331 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(2, 3), |
| 3332 | "Expected: (2) >= (3), actual: 2 vs 3"); |
| 3333 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(0.9, 1.1), |
| 3334 | "(0.9) >= (1.1)"); |
| 3335 | } |
| 3336 | |
| 3337 | // Tests EXPECT_GT. |
| 3338 | TEST(ExpectTest, EXPECT_GT) { |
| 3339 | EXPECT_GT(2, 1); |
| 3340 | EXPECT_NONFATAL_FAILURE(EXPECT_GT(2, 2), |
| 3341 | "Expected: (2) > (2), actual: 2 vs 2"); |
| 3342 | EXPECT_NONFATAL_FAILURE(EXPECT_GT(2, 3), |
| 3343 | "(2) > (3)"); |
| 3344 | } |
| 3345 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3346 | #if GTEST_HAS_EXCEPTIONS |
| 3347 | |
| 3348 | // Tests EXPECT_THROW. |
| 3349 | TEST(ExpectTest, EXPECT_THROW) { |
| 3350 | EXPECT_THROW(ThrowAnInteger(), int); |
| 3351 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool), |
| 3352 | "Expected: ThrowAnInteger() throws an exception of "\ |
| 3353 | "type bool.\n Actual: it throws a different type."); |
| 3354 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(1, bool), |
| 3355 | "Expected: 1 throws an exception of type bool.\n"\ |
| 3356 | " Actual: it throws nothing."); |
| 3357 | } |
| 3358 | |
| 3359 | // Tests EXPECT_NO_THROW. |
| 3360 | TEST(ExpectTest, EXPECT_NO_THROW) { |
| 3361 | EXPECT_NO_THROW(1); |
| 3362 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()), |
| 3363 | "Expected: ThrowAnInteger() doesn't throw an "\ |
| 3364 | "exception.\n Actual: it throws."); |
| 3365 | } |
| 3366 | |
| 3367 | // Tests EXPECT_ANY_THROW. |
| 3368 | TEST(ExpectTest, EXPECT_ANY_THROW) { |
| 3369 | EXPECT_ANY_THROW(ThrowAnInteger()); |
| 3370 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(1), |
| 3371 | "Expected: 1 throws an exception.\n Actual: it "\ |
| 3372 | "doesn't."); |
| 3373 | } |
| 3374 | |
| 3375 | #endif // GTEST_HAS_EXCEPTIONS |
| 3376 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3377 | // Make sure we deal with the precedence of <<. |
| 3378 | TEST(ExpectTest, ExpectPrecedence) { |
| 3379 | EXPECT_EQ(1 < 2, true); |
| 3380 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(true, true && false), |
| 3381 | "Value of: true && false"); |
| 3382 | } |
| 3383 | |
| 3384 | |
| 3385 | // Tests the StreamableToString() function. |
| 3386 | |
| 3387 | // Tests using StreamableToString() on a scalar. |
| 3388 | TEST(StreamableToStringTest, Scalar) { |
| 3389 | EXPECT_STREQ("5", StreamableToString(5).c_str()); |
| 3390 | } |
| 3391 | |
| 3392 | // Tests using StreamableToString() on a non-char pointer. |
| 3393 | TEST(StreamableToStringTest, Pointer) { |
| 3394 | int n = 0; |
| 3395 | int* p = &n; |
| 3396 | EXPECT_STRNE("(null)", StreamableToString(p).c_str()); |
| 3397 | } |
| 3398 | |
| 3399 | // Tests using StreamableToString() on a NULL non-char pointer. |
| 3400 | TEST(StreamableToStringTest, NullPointer) { |
| 3401 | int* p = NULL; |
| 3402 | EXPECT_STREQ("(null)", StreamableToString(p).c_str()); |
| 3403 | } |
| 3404 | |
| 3405 | // Tests using StreamableToString() on a C string. |
| 3406 | TEST(StreamableToStringTest, CString) { |
| 3407 | EXPECT_STREQ("Foo", StreamableToString("Foo").c_str()); |
| 3408 | } |
| 3409 | |
| 3410 | // Tests using StreamableToString() on a NULL C string. |
| 3411 | TEST(StreamableToStringTest, NullCString) { |
| 3412 | char* p = NULL; |
| 3413 | EXPECT_STREQ("(null)", StreamableToString(p).c_str()); |
| 3414 | } |
| 3415 | |
| 3416 | // Tests using streamable values as assertion messages. |
| 3417 | |
| 3418 | #if GTEST_HAS_STD_STRING |
| 3419 | // Tests using std::string as an assertion message. |
| 3420 | TEST(StreamableTest, string) { |
| 3421 | static const std::string str( |
| 3422 | "This failure message is a std::string, and is expected."); |
| 3423 | EXPECT_FATAL_FAILURE(FAIL() << str, |
| 3424 | str.c_str()); |
| 3425 | } |
| 3426 | |
| 3427 | // Tests that we can output strings containing embedded NULs. |
| 3428 | // Limited to Linux because we can only do this with std::string's. |
| 3429 | TEST(StreamableTest, stringWithEmbeddedNUL) { |
| 3430 | static const char char_array_with_nul[] = |
| 3431 | "Here's a NUL\0 and some more string"; |
| 3432 | static const std::string string_with_nul(char_array_with_nul, |
| 3433 | sizeof(char_array_with_nul) |
| 3434 | - 1); // drops the trailing NUL |
| 3435 | EXPECT_FATAL_FAILURE(FAIL() << string_with_nul, |
| 3436 | "Here's a NUL\\0 and some more string"); |
| 3437 | } |
| 3438 | |
| 3439 | #endif // GTEST_HAS_STD_STRING |
| 3440 | |
| 3441 | // Tests that we can output a NUL char. |
| 3442 | TEST(StreamableTest, NULChar) { |
| 3443 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 3444 | FAIL() << "A NUL" << '\0' << " and some more string"; |
| 3445 | }, "A NUL\\0 and some more string"); |
| 3446 | } |
| 3447 | |
| 3448 | // Tests using int as an assertion message. |
| 3449 | TEST(StreamableTest, int) { |
| 3450 | EXPECT_FATAL_FAILURE(FAIL() << 900913, |
| 3451 | "900913"); |
| 3452 | } |
| 3453 | |
| 3454 | // Tests using NULL char pointer as an assertion message. |
| 3455 | // |
| 3456 | // In MSVC, streaming a NULL char * causes access violation. Google Test |
| 3457 | // implemented a workaround (substituting "(null)" for NULL). This |
| 3458 | // tests whether the workaround works. |
| 3459 | TEST(StreamableTest, NullCharPtr) { |
| 3460 | EXPECT_FATAL_FAILURE(FAIL() << static_cast<const char*>(NULL), |
| 3461 | "(null)"); |
| 3462 | } |
| 3463 | |
| 3464 | // Tests that basic IO manipulators (endl, ends, and flush) can be |
| 3465 | // streamed to testing::Message. |
| 3466 | TEST(StreamableTest, BasicIoManip) { |
| 3467 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 3468 | FAIL() << "Line 1." << std::endl |
| 3469 | << "A NUL char " << std::ends << std::flush << " in line 2."; |
| 3470 | }, "Line 1.\nA NUL char \\0 in line 2."); |
| 3471 | } |
| 3472 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3473 | // Tests the macros that haven't been covered so far. |
| 3474 | |
| 3475 | void AddFailureHelper(bool* aborted) { |
| 3476 | *aborted = true; |
| 3477 | ADD_FAILURE() << "Failure"; |
| 3478 | *aborted = false; |
| 3479 | } |
| 3480 | |
| 3481 | // Tests ADD_FAILURE. |
| 3482 | TEST(MacroTest, ADD_FAILURE) { |
| 3483 | bool aborted = true; |
| 3484 | EXPECT_NONFATAL_FAILURE(AddFailureHelper(&aborted), |
| 3485 | "Failure"); |
| 3486 | EXPECT_FALSE(aborted); |
| 3487 | } |
| 3488 | |
| 3489 | // Tests FAIL. |
| 3490 | TEST(MacroTest, FAIL) { |
| 3491 | EXPECT_FATAL_FAILURE(FAIL(), |
| 3492 | "Failed"); |
| 3493 | EXPECT_FATAL_FAILURE(FAIL() << "Intentional failure.", |
| 3494 | "Intentional failure."); |
| 3495 | } |
| 3496 | |
| 3497 | // Tests SUCCEED |
| 3498 | TEST(MacroTest, SUCCEED) { |
| 3499 | SUCCEED(); |
| 3500 | SUCCEED() << "Explicit success."; |
| 3501 | } |
| 3502 | |
| 3503 | |
| 3504 | // Tests for EXPECT_EQ() and ASSERT_EQ(). |
| 3505 | // |
| 3506 | // These tests fail *intentionally*, s.t. the failure messages can be |
| 3507 | // generated and tested. |
| 3508 | // |
| 3509 | // We have different tests for different argument types. |
| 3510 | |
| 3511 | // Tests using bool values in {EXPECT|ASSERT}_EQ. |
| 3512 | TEST(EqAssertionTest, Bool) { |
| 3513 | EXPECT_EQ(true, true); |
| 3514 | EXPECT_FATAL_FAILURE(ASSERT_EQ(false, true), |
| 3515 | "Value of: true"); |
| 3516 | } |
| 3517 | |
| 3518 | // Tests using int values in {EXPECT|ASSERT}_EQ. |
| 3519 | TEST(EqAssertionTest, Int) { |
| 3520 | ASSERT_EQ(32, 32); |
| 3521 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(32, 33), |
| 3522 | "33"); |
| 3523 | } |
| 3524 | |
| 3525 | // Tests using time_t values in {EXPECT|ASSERT}_EQ. |
| 3526 | TEST(EqAssertionTest, Time_T) { |
| 3527 | EXPECT_EQ(static_cast<time_t>(0), |
| 3528 | static_cast<time_t>(0)); |
| 3529 | EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<time_t>(0), |
| 3530 | static_cast<time_t>(1234)), |
| 3531 | "1234"); |
| 3532 | } |
| 3533 | |
| 3534 | // Tests using char values in {EXPECT|ASSERT}_EQ. |
| 3535 | TEST(EqAssertionTest, Char) { |
| 3536 | ASSERT_EQ('z', 'z'); |
| 3537 | const char ch = 'b'; |
| 3538 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ('\0', ch), |
| 3539 | "ch"); |
| 3540 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ('a', ch), |
| 3541 | "ch"); |
| 3542 | } |
| 3543 | |
| 3544 | // Tests using wchar_t values in {EXPECT|ASSERT}_EQ. |
| 3545 | TEST(EqAssertionTest, WideChar) { |
| 3546 | EXPECT_EQ(L'b', L'b'); |
| 3547 | |
| 3548 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(L'\0', L'x'), |
| 3549 | "Value of: L'x'\n" |
| 3550 | " Actual: L'x' (120, 0x78)\n" |
| 3551 | "Expected: L'\0'\n" |
| 3552 | "Which is: L'\0' (0, 0x0)"); |
| 3553 | |
| 3554 | static wchar_t wchar; |
| 3555 | wchar = L'b'; |
| 3556 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(L'a', wchar), |
| 3557 | "wchar"); |
| 3558 | wchar = L'\x8119'; |
| 3559 | EXPECT_FATAL_FAILURE(ASSERT_EQ(L'\x8120', wchar), |
| 3560 | "Value of: wchar"); |
| 3561 | } |
| 3562 | |
| 3563 | #if GTEST_HAS_STD_STRING |
| 3564 | // Tests using ::std::string values in {EXPECT|ASSERT}_EQ. |
| 3565 | TEST(EqAssertionTest, StdString) { |
| 3566 | // Compares a const char* to an std::string that has identical |
| 3567 | // content. |
| 3568 | ASSERT_EQ("Test", ::std::string("Test")); |
| 3569 | |
| 3570 | // Compares two identical std::strings. |
| 3571 | static const ::std::string str1("A * in the middle"); |
| 3572 | static const ::std::string str2(str1); |
| 3573 | EXPECT_EQ(str1, str2); |
| 3574 | |
| 3575 | // Compares a const char* to an std::string that has different |
| 3576 | // content |
| 3577 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ("Test", ::std::string("test")), |
| 3578 | "::std::string(\"test\")"); |
| 3579 | |
| 3580 | // Compares an std::string to a char* that has different content. |
| 3581 | char* const p1 = const_cast<char*>("foo"); |
| 3582 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(::std::string("bar"), p1), |
| 3583 | "p1"); |
| 3584 | |
| 3585 | // Compares two std::strings that have different contents, one of |
| 3586 | // which having a NUL character in the middle. This should fail. |
| 3587 | static ::std::string str3(str1); |
| 3588 | str3.at(2) = '\0'; |
| 3589 | EXPECT_FATAL_FAILURE(ASSERT_EQ(str1, str3), |
| 3590 | "Value of: str3\n" |
| 3591 | " Actual: \"A \\0 in the middle\""); |
| 3592 | } |
| 3593 | |
| 3594 | #endif // GTEST_HAS_STD_STRING |
| 3595 | |
| 3596 | #if GTEST_HAS_STD_WSTRING |
| 3597 | |
| 3598 | // Tests using ::std::wstring values in {EXPECT|ASSERT}_EQ. |
| 3599 | TEST(EqAssertionTest, StdWideString) { |
| 3600 | // Compares an std::wstring to a const wchar_t* that has identical |
| 3601 | // content. |
| 3602 | EXPECT_EQ(::std::wstring(L"Test\x8119"), L"Test\x8119"); |
| 3603 | |
| 3604 | // Compares two identical std::wstrings. |
| 3605 | const ::std::wstring wstr1(L"A * in the middle"); |
| 3606 | const ::std::wstring wstr2(wstr1); |
| 3607 | ASSERT_EQ(wstr1, wstr2); |
| 3608 | |
| 3609 | // Compares an std::wstring to a const wchar_t* that has different |
| 3610 | // content. |
| 3611 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 3612 | EXPECT_EQ(::std::wstring(L"Test\x8119"), L"Test\x8120"); |
| 3613 | }, "L\"Test\\x8120\""); |
| 3614 | |
| 3615 | // Compares two std::wstrings that have different contents, one of |
| 3616 | // which having a NUL character in the middle. |
| 3617 | ::std::wstring wstr3(wstr1); |
| 3618 | wstr3.at(2) = L'\0'; |
| 3619 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(wstr1, wstr3), |
| 3620 | "wstr3"); |
| 3621 | |
| 3622 | // Compares a wchar_t* to an std::wstring that has different |
| 3623 | // content. |
| 3624 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 3625 | ASSERT_EQ(const_cast<wchar_t*>(L"foo"), ::std::wstring(L"bar")); |
| 3626 | }, ""); |
| 3627 | } |
| 3628 | |
| 3629 | #endif // GTEST_HAS_STD_WSTRING |
| 3630 | |
| 3631 | #if GTEST_HAS_GLOBAL_STRING |
| 3632 | // Tests using ::string values in {EXPECT|ASSERT}_EQ. |
| 3633 | TEST(EqAssertionTest, GlobalString) { |
| 3634 | // Compares a const char* to a ::string that has identical content. |
| 3635 | EXPECT_EQ("Test", ::string("Test")); |
| 3636 | |
| 3637 | // Compares two identical ::strings. |
| 3638 | const ::string str1("A * in the middle"); |
| 3639 | const ::string str2(str1); |
| 3640 | ASSERT_EQ(str1, str2); |
| 3641 | |
| 3642 | // Compares a ::string to a const char* that has different content. |
| 3643 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(::string("Test"), "test"), |
| 3644 | "test"); |
| 3645 | |
| 3646 | // Compares two ::strings that have different contents, one of which |
| 3647 | // having a NUL character in the middle. |
| 3648 | ::string str3(str1); |
| 3649 | str3.at(2) = '\0'; |
| 3650 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(str1, str3), |
| 3651 | "str3"); |
| 3652 | |
| 3653 | // Compares a ::string to a char* that has different content. |
| 3654 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 3655 | ASSERT_EQ(::string("bar"), const_cast<char*>("foo")); |
| 3656 | }, ""); |
| 3657 | } |
| 3658 | |
| 3659 | #endif // GTEST_HAS_GLOBAL_STRING |
| 3660 | |
| 3661 | #if GTEST_HAS_GLOBAL_WSTRING |
| 3662 | |
| 3663 | // Tests using ::wstring values in {EXPECT|ASSERT}_EQ. |
| 3664 | TEST(EqAssertionTest, GlobalWideString) { |
| 3665 | // Compares a const wchar_t* to a ::wstring that has identical content. |
| 3666 | ASSERT_EQ(L"Test\x8119", ::wstring(L"Test\x8119")); |
| 3667 | |
| 3668 | // Compares two identical ::wstrings. |
| 3669 | static const ::wstring wstr1(L"A * in the middle"); |
| 3670 | static const ::wstring wstr2(wstr1); |
| 3671 | EXPECT_EQ(wstr1, wstr2); |
| 3672 | |
| 3673 | // Compares a const wchar_t* to a ::wstring that has different |
| 3674 | // content. |
| 3675 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 3676 | EXPECT_EQ(L"Test\x8120", ::wstring(L"Test\x8119")); |
| 3677 | }, "Test\\x8119"); |
| 3678 | |
| 3679 | // Compares a wchar_t* to a ::wstring that has different content. |
| 3680 | wchar_t* const p1 = const_cast<wchar_t*>(L"foo"); |
| 3681 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, ::wstring(L"bar")), |
| 3682 | "bar"); |
| 3683 | |
| 3684 | // Compares two ::wstrings that have different contents, one of which |
| 3685 | // having a NUL character in the middle. |
| 3686 | static ::wstring wstr3; |
| 3687 | wstr3 = wstr1; |
| 3688 | wstr3.at(2) = L'\0'; |
| 3689 | EXPECT_FATAL_FAILURE(ASSERT_EQ(wstr1, wstr3), |
| 3690 | "wstr3"); |
| 3691 | } |
| 3692 | |
| 3693 | #endif // GTEST_HAS_GLOBAL_WSTRING |
| 3694 | |
| 3695 | // Tests using char pointers in {EXPECT|ASSERT}_EQ. |
| 3696 | TEST(EqAssertionTest, CharPointer) { |
| 3697 | char* const p0 = NULL; |
| 3698 | // Only way to get the Nokia compiler to compile the cast |
| 3699 | // is to have a separate void* variable first. Putting |
| 3700 | // the two casts on the same line doesn't work, neither does |
| 3701 | // a direct C-style to char*. |
| 3702 | void* pv1 = (void*)0x1234; // NOLINT |
| 3703 | void* pv2 = (void*)0xABC0; // NOLINT |
| 3704 | char* const p1 = reinterpret_cast<char*>(pv1); |
| 3705 | char* const p2 = reinterpret_cast<char*>(pv2); |
| 3706 | ASSERT_EQ(p1, p1); |
| 3707 | |
| 3708 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p0, p2), |
| 3709 | "Value of: p2"); |
| 3710 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, p2), |
| 3711 | "p2"); |
| 3712 | EXPECT_FATAL_FAILURE(ASSERT_EQ(reinterpret_cast<char*>(0x1234), |
| 3713 | reinterpret_cast<char*>(0xABC0)), |
| 3714 | "ABC0"); |
| 3715 | } |
| 3716 | |
| 3717 | // Tests using wchar_t pointers in {EXPECT|ASSERT}_EQ. |
| 3718 | TEST(EqAssertionTest, WideCharPointer) { |
| 3719 | wchar_t* const p0 = NULL; |
| 3720 | // Only way to get the Nokia compiler to compile the cast |
| 3721 | // is to have a separate void* variable first. Putting |
| 3722 | // the two casts on the same line doesn't work, neither does |
| 3723 | // a direct C-style to char*. |
| 3724 | void* pv1 = (void*)0x1234; // NOLINT |
| 3725 | void* pv2 = (void*)0xABC0; // NOLINT |
| 3726 | wchar_t* const p1 = reinterpret_cast<wchar_t*>(pv1); |
| 3727 | wchar_t* const p2 = reinterpret_cast<wchar_t*>(pv2); |
| 3728 | EXPECT_EQ(p0, p0); |
| 3729 | |
| 3730 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p0, p2), |
| 3731 | "Value of: p2"); |
| 3732 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, p2), |
| 3733 | "p2"); |
| 3734 | void* pv3 = (void*)0x1234; // NOLINT |
| 3735 | void* pv4 = (void*)0xABC0; // NOLINT |
| 3736 | const wchar_t* p3 = reinterpret_cast<const wchar_t*>(pv3); |
| 3737 | const wchar_t* p4 = reinterpret_cast<const wchar_t*>(pv4); |
| 3738 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p3, p4), |
| 3739 | "p4"); |
| 3740 | } |
| 3741 | |
| 3742 | // Tests using other types of pointers in {EXPECT|ASSERT}_EQ. |
| 3743 | TEST(EqAssertionTest, OtherPointer) { |
| 3744 | ASSERT_EQ(static_cast<const int*>(NULL), |
| 3745 | static_cast<const int*>(NULL)); |
| 3746 | EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<const int*>(NULL), |
| 3747 | reinterpret_cast<const int*>(0x1234)), |
| 3748 | "0x1234"); |
| 3749 | } |
| 3750 | |
| 3751 | // Tests the FRIEND_TEST macro. |
| 3752 | |
| 3753 | // This class has a private member we want to test. We will test it |
| 3754 | // both in a TEST and in a TEST_F. |
| 3755 | class Foo { |
| 3756 | public: |
| 3757 | Foo() {} |
| 3758 | |
| 3759 | private: |
| 3760 | int Bar() const { return 1; } |
| 3761 | |
| 3762 | // Declares the friend tests that can access the private member |
| 3763 | // Bar(). |
| 3764 | FRIEND_TEST(FRIEND_TEST_Test, TEST); |
| 3765 | FRIEND_TEST(FRIEND_TEST_Test2, TEST_F); |
| 3766 | }; |
| 3767 | |
| 3768 | // Tests that the FRIEND_TEST declaration allows a TEST to access a |
| 3769 | // class's private members. This should compile. |
| 3770 | TEST(FRIEND_TEST_Test, TEST) { |
| 3771 | ASSERT_EQ(1, Foo().Bar()); |
| 3772 | } |
| 3773 | |
| 3774 | // The fixture needed to test using FRIEND_TEST with TEST_F. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3775 | class FRIEND_TEST_Test2 : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3776 | protected: |
| 3777 | Foo foo; |
| 3778 | }; |
| 3779 | |
| 3780 | // Tests that the FRIEND_TEST declaration allows a TEST_F to access a |
| 3781 | // class's private members. This should compile. |
| 3782 | TEST_F(FRIEND_TEST_Test2, TEST_F) { |
| 3783 | ASSERT_EQ(1, foo.Bar()); |
| 3784 | } |
| 3785 | |
| 3786 | // Tests the life cycle of Test objects. |
| 3787 | |
| 3788 | // The test fixture for testing the life cycle of Test objects. |
| 3789 | // |
| 3790 | // This class counts the number of live test objects that uses this |
| 3791 | // fixture. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3792 | class TestLifeCycleTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3793 | protected: |
| 3794 | // Constructor. Increments the number of test objects that uses |
| 3795 | // this fixture. |
| 3796 | TestLifeCycleTest() { count_++; } |
| 3797 | |
| 3798 | // Destructor. Decrements the number of test objects that uses this |
| 3799 | // fixture. |
| 3800 | ~TestLifeCycleTest() { count_--; } |
| 3801 | |
| 3802 | // Returns the number of live test objects that uses this fixture. |
| 3803 | int count() const { return count_; } |
| 3804 | |
| 3805 | private: |
| 3806 | static int count_; |
| 3807 | }; |
| 3808 | |
| 3809 | int TestLifeCycleTest::count_ = 0; |
| 3810 | |
| 3811 | // Tests the life cycle of test objects. |
| 3812 | TEST_F(TestLifeCycleTest, Test1) { |
| 3813 | // There should be only one test object in this test case that's |
| 3814 | // currently alive. |
| 3815 | ASSERT_EQ(1, count()); |
| 3816 | } |
| 3817 | |
| 3818 | // Tests the life cycle of test objects. |
| 3819 | TEST_F(TestLifeCycleTest, Test2) { |
| 3820 | // After Test1 is done and Test2 is started, there should still be |
| 3821 | // only one live test object, as the object for Test1 should've been |
| 3822 | // deleted. |
| 3823 | ASSERT_EQ(1, count()); |
| 3824 | } |
| 3825 | |
| 3826 | } // namespace |
| 3827 | |
| 3828 | // Tests streaming a user type whose definition and operator << are |
| 3829 | // both in the global namespace. |
| 3830 | class Base { |
| 3831 | public: |
| 3832 | explicit Base(int x) : x_(x) {} |
| 3833 | int x() const { return x_; } |
| 3834 | private: |
| 3835 | int x_; |
| 3836 | }; |
| 3837 | std::ostream& operator<<(std::ostream& os, |
| 3838 | const Base& val) { |
| 3839 | return os << val.x(); |
| 3840 | } |
| 3841 | std::ostream& operator<<(std::ostream& os, |
| 3842 | const Base* pointer) { |
| 3843 | return os << "(" << pointer->x() << ")"; |
| 3844 | } |
| 3845 | |
| 3846 | TEST(MessageTest, CanStreamUserTypeInGlobalNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3847 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3848 | Base a(1); |
| 3849 | |
| 3850 | msg << a << &a; // Uses ::operator<<. |
| 3851 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 3852 | } |
| 3853 | |
| 3854 | // Tests streaming a user type whose definition and operator<< are |
| 3855 | // both in an unnamed namespace. |
| 3856 | namespace { |
| 3857 | class MyTypeInUnnamedNameSpace : public Base { |
| 3858 | public: |
| 3859 | explicit MyTypeInUnnamedNameSpace(int x): Base(x) {} |
| 3860 | }; |
| 3861 | std::ostream& operator<<(std::ostream& os, |
| 3862 | const MyTypeInUnnamedNameSpace& val) { |
| 3863 | return os << val.x(); |
| 3864 | } |
| 3865 | std::ostream& operator<<(std::ostream& os, |
| 3866 | const MyTypeInUnnamedNameSpace* pointer) { |
| 3867 | return os << "(" << pointer->x() << ")"; |
| 3868 | } |
| 3869 | } // namespace |
| 3870 | |
| 3871 | TEST(MessageTest, CanStreamUserTypeInUnnamedNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3872 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3873 | MyTypeInUnnamedNameSpace a(1); |
| 3874 | |
| 3875 | msg << a << &a; // Uses <unnamed_namespace>::operator<<. |
| 3876 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 3877 | } |
| 3878 | |
| 3879 | // Tests streaming a user type whose definition and operator<< are |
| 3880 | // both in a user namespace. |
| 3881 | namespace namespace1 { |
| 3882 | class MyTypeInNameSpace1 : public Base { |
| 3883 | public: |
| 3884 | explicit MyTypeInNameSpace1(int x): Base(x) {} |
| 3885 | }; |
| 3886 | std::ostream& operator<<(std::ostream& os, |
| 3887 | const MyTypeInNameSpace1& val) { |
| 3888 | return os << val.x(); |
| 3889 | } |
| 3890 | std::ostream& operator<<(std::ostream& os, |
| 3891 | const MyTypeInNameSpace1* pointer) { |
| 3892 | return os << "(" << pointer->x() << ")"; |
| 3893 | } |
| 3894 | } // namespace namespace1 |
| 3895 | |
| 3896 | TEST(MessageTest, CanStreamUserTypeInUserNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3897 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3898 | namespace1::MyTypeInNameSpace1 a(1); |
| 3899 | |
| 3900 | msg << a << &a; // Uses namespace1::operator<<. |
| 3901 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 3902 | } |
| 3903 | |
| 3904 | // Tests streaming a user type whose definition is in a user namespace |
| 3905 | // but whose operator<< is in the global namespace. |
| 3906 | namespace namespace2 { |
| 3907 | class MyTypeInNameSpace2 : public ::Base { |
| 3908 | public: |
| 3909 | explicit MyTypeInNameSpace2(int x): Base(x) {} |
| 3910 | }; |
| 3911 | } // namespace namespace2 |
| 3912 | std::ostream& operator<<(std::ostream& os, |
| 3913 | const namespace2::MyTypeInNameSpace2& val) { |
| 3914 | return os << val.x(); |
| 3915 | } |
| 3916 | std::ostream& operator<<(std::ostream& os, |
| 3917 | const namespace2::MyTypeInNameSpace2* pointer) { |
| 3918 | return os << "(" << pointer->x() << ")"; |
| 3919 | } |
| 3920 | |
| 3921 | TEST(MessageTest, CanStreamUserTypeInUserNameSpaceWithStreamOperatorInGlobal) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3922 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3923 | namespace2::MyTypeInNameSpace2 a(1); |
| 3924 | |
| 3925 | msg << a << &a; // Uses ::operator<<. |
| 3926 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 3927 | } |
| 3928 | |
| 3929 | // Tests streaming NULL pointers to testing::Message. |
| 3930 | TEST(MessageTest, NullPointers) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3931 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3932 | char* const p1 = NULL; |
| 3933 | unsigned char* const p2 = NULL; |
| 3934 | int* p3 = NULL; |
| 3935 | double* p4 = NULL; |
| 3936 | bool* p5 = NULL; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3937 | Message* p6 = NULL; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3938 | |
| 3939 | msg << p1 << p2 << p3 << p4 << p5 << p6; |
| 3940 | ASSERT_STREQ("(null)(null)(null)(null)(null)(null)", |
| 3941 | msg.GetString().c_str()); |
| 3942 | } |
| 3943 | |
| 3944 | // Tests streaming wide strings to testing::Message. |
| 3945 | TEST(MessageTest, WideStrings) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3946 | // Streams a NULL of type const wchar_t*. |
| 3947 | const wchar_t* const_wstr = NULL; |
| 3948 | EXPECT_STREQ("(null)", |
| 3949 | (Message() << const_wstr).GetString().c_str()); |
| 3950 | |
| 3951 | // Streams a NULL of type wchar_t*. |
| 3952 | wchar_t* wstr = NULL; |
| 3953 | EXPECT_STREQ("(null)", |
| 3954 | (Message() << wstr).GetString().c_str()); |
| 3955 | |
| 3956 | // Streams a non-NULL of type const wchar_t*. |
| 3957 | const_wstr = L"abc\x8119"; |
| 3958 | EXPECT_STREQ("abc\xe8\x84\x99", |
| 3959 | (Message() << const_wstr).GetString().c_str()); |
| 3960 | |
| 3961 | // Streams a non-NULL of type wchar_t*. |
| 3962 | wstr = const_cast<wchar_t*>(const_wstr); |
| 3963 | EXPECT_STREQ("abc\xe8\x84\x99", |
| 3964 | (Message() << wstr).GetString().c_str()); |
| 3965 | } |
| 3966 | |
| 3967 | |
| 3968 | // This line tests that we can define tests in the testing namespace. |
| 3969 | namespace testing { |
| 3970 | |
| 3971 | // Tests the TestInfo class. |
| 3972 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3973 | class TestInfoTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3974 | protected: |
| 3975 | static TestInfo * GetTestInfo(const char* test_name) { |
| 3976 | return UnitTest::GetInstance()->impl()-> |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 3977 | GetTestCase("TestInfoTest", "", NULL, NULL)-> |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3978 | GetTestInfo(test_name); |
| 3979 | } |
| 3980 | |
| 3981 | static const TestResult* GetTestResult( |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3982 | const TestInfo* test_info) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3983 | return test_info->result(); |
| 3984 | } |
| 3985 | }; |
| 3986 | |
| 3987 | // Tests TestInfo::test_case_name() and TestInfo::name(). |
| 3988 | TEST_F(TestInfoTest, Names) { |
| 3989 | TestInfo * const test_info = GetTestInfo("Names"); |
| 3990 | |
| 3991 | ASSERT_STREQ("TestInfoTest", test_info->test_case_name()); |
| 3992 | ASSERT_STREQ("Names", test_info->name()); |
| 3993 | } |
| 3994 | |
| 3995 | // Tests TestInfo::result(). |
| 3996 | TEST_F(TestInfoTest, result) { |
| 3997 | TestInfo * const test_info = GetTestInfo("result"); |
| 3998 | |
| 3999 | // Initially, there is no TestPartResult for this test. |
| 4000 | ASSERT_EQ(0u, GetTestResult(test_info)->total_part_count()); |
| 4001 | |
| 4002 | // After the previous assertion, there is still none. |
| 4003 | ASSERT_EQ(0u, GetTestResult(test_info)->total_part_count()); |
| 4004 | } |
| 4005 | |
| 4006 | // Tests setting up and tearing down a test case. |
| 4007 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4008 | class SetUpTestCaseTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4009 | protected: |
| 4010 | // This will be called once before the first test in this test case |
| 4011 | // is run. |
| 4012 | static void SetUpTestCase() { |
| 4013 | printf("Setting up the test case . . .\n"); |
| 4014 | |
| 4015 | // Initializes some shared resource. In this simple example, we |
| 4016 | // just create a C string. More complex stuff can be done if |
| 4017 | // desired. |
| 4018 | shared_resource_ = "123"; |
| 4019 | |
| 4020 | // Increments the number of test cases that have been set up. |
| 4021 | counter_++; |
| 4022 | |
| 4023 | // SetUpTestCase() should be called only once. |
| 4024 | EXPECT_EQ(1, counter_); |
| 4025 | } |
| 4026 | |
| 4027 | // This will be called once after the last test in this test case is |
| 4028 | // run. |
| 4029 | static void TearDownTestCase() { |
| 4030 | printf("Tearing down the test case . . .\n"); |
| 4031 | |
| 4032 | // Decrements the number of test cases that have been set up. |
| 4033 | counter_--; |
| 4034 | |
| 4035 | // TearDownTestCase() should be called only once. |
| 4036 | EXPECT_EQ(0, counter_); |
| 4037 | |
| 4038 | // Cleans up the shared resource. |
| 4039 | shared_resource_ = NULL; |
| 4040 | } |
| 4041 | |
| 4042 | // This will be called before each test in this test case. |
| 4043 | virtual void SetUp() { |
| 4044 | // SetUpTestCase() should be called only once, so counter_ should |
| 4045 | // always be 1. |
| 4046 | EXPECT_EQ(1, counter_); |
| 4047 | } |
| 4048 | |
| 4049 | // Number of test cases that have been set up. |
| 4050 | static int counter_; |
| 4051 | |
| 4052 | // Some resource to be shared by all tests in this test case. |
| 4053 | static const char* shared_resource_; |
| 4054 | }; |
| 4055 | |
| 4056 | int SetUpTestCaseTest::counter_ = 0; |
| 4057 | const char* SetUpTestCaseTest::shared_resource_ = NULL; |
| 4058 | |
| 4059 | // A test that uses the shared resource. |
| 4060 | TEST_F(SetUpTestCaseTest, Test1) { |
| 4061 | EXPECT_STRNE(NULL, shared_resource_); |
| 4062 | } |
| 4063 | |
| 4064 | // Another test that uses the shared resource. |
| 4065 | TEST_F(SetUpTestCaseTest, Test2) { |
| 4066 | EXPECT_STREQ("123", shared_resource_); |
| 4067 | } |
| 4068 | |
| 4069 | // The InitGoogleTestTest test case tests testing::InitGoogleTest(). |
| 4070 | |
| 4071 | // The Flags struct stores a copy of all Google Test flags. |
| 4072 | struct Flags { |
| 4073 | // Constructs a Flags struct where each flag has its default value. |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 4074 | Flags() : also_run_disabled_tests(false), |
| 4075 | break_on_failure(false), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4076 | catch_exceptions(false), |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 4077 | death_test_use_fork(false), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4078 | filter(""), |
| 4079 | list_tests(false), |
| 4080 | output(""), |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 4081 | print_time(false), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4082 | repeat(1) {} |
| 4083 | |
| 4084 | // Factory methods. |
| 4085 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 4086 | // Creates a Flags struct where the gtest_also_run_disabled_tests flag has |
| 4087 | // the given value. |
| 4088 | static Flags AlsoRunDisabledTests(bool also_run_disabled_tests) { |
| 4089 | Flags flags; |
| 4090 | flags.also_run_disabled_tests = also_run_disabled_tests; |
| 4091 | return flags; |
| 4092 | } |
| 4093 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4094 | // Creates a Flags struct where the gtest_break_on_failure flag has |
| 4095 | // the given value. |
| 4096 | static Flags BreakOnFailure(bool break_on_failure) { |
| 4097 | Flags flags; |
| 4098 | flags.break_on_failure = break_on_failure; |
| 4099 | return flags; |
| 4100 | } |
| 4101 | |
| 4102 | // Creates a Flags struct where the gtest_catch_exceptions flag has |
| 4103 | // the given value. |
| 4104 | static Flags CatchExceptions(bool catch_exceptions) { |
| 4105 | Flags flags; |
| 4106 | flags.catch_exceptions = catch_exceptions; |
| 4107 | return flags; |
| 4108 | } |
| 4109 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 4110 | // Creates a Flags struct where the gtest_death_test_use_fork flag has |
| 4111 | // the given value. |
| 4112 | static Flags DeathTestUseFork(bool death_test_use_fork) { |
| 4113 | Flags flags; |
| 4114 | flags.death_test_use_fork = death_test_use_fork; |
| 4115 | return flags; |
| 4116 | } |
| 4117 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4118 | // Creates a Flags struct where the gtest_filter flag has the given |
| 4119 | // value. |
| 4120 | static Flags Filter(const char* filter) { |
| 4121 | Flags flags; |
| 4122 | flags.filter = filter; |
| 4123 | return flags; |
| 4124 | } |
| 4125 | |
| 4126 | // Creates a Flags struct where the gtest_list_tests flag has the |
| 4127 | // given value. |
| 4128 | static Flags ListTests(bool list_tests) { |
| 4129 | Flags flags; |
| 4130 | flags.list_tests = list_tests; |
| 4131 | return flags; |
| 4132 | } |
| 4133 | |
| 4134 | // Creates a Flags struct where the gtest_output flag has the given |
| 4135 | // value. |
| 4136 | static Flags Output(const char* output) { |
| 4137 | Flags flags; |
| 4138 | flags.output = output; |
| 4139 | return flags; |
| 4140 | } |
| 4141 | |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 4142 | // Creates a Flags struct where the gtest_print_time flag has the given |
| 4143 | // value. |
| 4144 | static Flags PrintTime(bool print_time) { |
| 4145 | Flags flags; |
| 4146 | flags.print_time = print_time; |
| 4147 | return flags; |
| 4148 | } |
| 4149 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4150 | // Creates a Flags struct where the gtest_repeat flag has the given |
| 4151 | // value. |
| 4152 | static Flags Repeat(Int32 repeat) { |
| 4153 | Flags flags; |
| 4154 | flags.repeat = repeat; |
| 4155 | return flags; |
| 4156 | } |
| 4157 | |
| 4158 | // These fields store the flag values. |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 4159 | bool also_run_disabled_tests; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4160 | bool break_on_failure; |
| 4161 | bool catch_exceptions; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 4162 | bool death_test_use_fork; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4163 | const char* filter; |
| 4164 | bool list_tests; |
| 4165 | const char* output; |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 4166 | bool print_time; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4167 | Int32 repeat; |
| 4168 | }; |
| 4169 | |
| 4170 | // Fixture for testing InitGoogleTest(). |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4171 | class InitGoogleTestTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4172 | protected: |
| 4173 | // Clears the flags before each test. |
| 4174 | virtual void SetUp() { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 4175 | GTEST_FLAG(also_run_disabled_tests) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4176 | GTEST_FLAG(break_on_failure) = false; |
| 4177 | GTEST_FLAG(catch_exceptions) = false; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 4178 | GTEST_FLAG(death_test_use_fork) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4179 | GTEST_FLAG(filter) = ""; |
| 4180 | GTEST_FLAG(list_tests) = false; |
| 4181 | GTEST_FLAG(output) = ""; |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 4182 | GTEST_FLAG(print_time) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4183 | GTEST_FLAG(repeat) = 1; |
| 4184 | } |
| 4185 | |
| 4186 | // Asserts that two narrow or wide string arrays are equal. |
| 4187 | template <typename CharType> |
| 4188 | static void AssertStringArrayEq(size_t size1, CharType** array1, |
| 4189 | size_t size2, CharType** array2) { |
| 4190 | ASSERT_EQ(size1, size2) << " Array sizes different."; |
| 4191 | |
| 4192 | for (size_t i = 0; i != size1; i++) { |
| 4193 | ASSERT_STREQ(array1[i], array2[i]) << " where i == " << i; |
| 4194 | } |
| 4195 | } |
| 4196 | |
| 4197 | // Verifies that the flag values match the expected values. |
| 4198 | static void CheckFlags(const Flags& expected) { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 4199 | EXPECT_EQ(expected.also_run_disabled_tests, |
| 4200 | GTEST_FLAG(also_run_disabled_tests)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4201 | EXPECT_EQ(expected.break_on_failure, GTEST_FLAG(break_on_failure)); |
| 4202 | EXPECT_EQ(expected.catch_exceptions, GTEST_FLAG(catch_exceptions)); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 4203 | EXPECT_EQ(expected.death_test_use_fork, GTEST_FLAG(death_test_use_fork)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4204 | EXPECT_STREQ(expected.filter, GTEST_FLAG(filter).c_str()); |
| 4205 | EXPECT_EQ(expected.list_tests, GTEST_FLAG(list_tests)); |
| 4206 | EXPECT_STREQ(expected.output, GTEST_FLAG(output).c_str()); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 4207 | EXPECT_EQ(expected.print_time, GTEST_FLAG(print_time)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4208 | EXPECT_EQ(expected.repeat, GTEST_FLAG(repeat)); |
| 4209 | } |
| 4210 | |
| 4211 | // Parses a command line (specified by argc1 and argv1), then |
| 4212 | // verifies that the flag values are expected and that the |
| 4213 | // recognized flags are removed from the command line. |
| 4214 | template <typename CharType> |
| 4215 | static void TestParsingFlags(int argc1, const CharType** argv1, |
| 4216 | int argc2, const CharType** argv2, |
| 4217 | const Flags& expected) { |
| 4218 | // Parses the command line. |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 4219 | internal::ParseGoogleTestFlagsOnly(&argc1, const_cast<CharType**>(argv1)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4220 | |
| 4221 | // Verifies the flag values. |
| 4222 | CheckFlags(expected); |
| 4223 | |
| 4224 | // Verifies that the recognized flags are removed from the command |
| 4225 | // line. |
| 4226 | AssertStringArrayEq(argc1 + 1, argv1, argc2 + 1, argv2); |
| 4227 | } |
| 4228 | |
| 4229 | // This macro wraps TestParsingFlags s.t. the user doesn't need |
| 4230 | // to specify the array sizes. |
| 4231 | #define TEST_PARSING_FLAGS(argv1, argv2, expected) \ |
| 4232 | TestParsingFlags(sizeof(argv1)/sizeof(*argv1) - 1, argv1, \ |
| 4233 | sizeof(argv2)/sizeof(*argv2) - 1, argv2, expected) |
| 4234 | }; |
| 4235 | |
| 4236 | // Tests parsing an empty command line. |
| 4237 | TEST_F(InitGoogleTestTest, Empty) { |
| 4238 | const char* argv[] = { |
| 4239 | NULL |
| 4240 | }; |
| 4241 | |
| 4242 | const char* argv2[] = { |
| 4243 | NULL |
| 4244 | }; |
| 4245 | |
| 4246 | TEST_PARSING_FLAGS(argv, argv2, Flags()); |
| 4247 | } |
| 4248 | |
| 4249 | // Tests parsing a command line that has no flag. |
| 4250 | TEST_F(InitGoogleTestTest, NoFlag) { |
| 4251 | const char* argv[] = { |
| 4252 | "foo.exe", |
| 4253 | NULL |
| 4254 | }; |
| 4255 | |
| 4256 | const char* argv2[] = { |
| 4257 | "foo.exe", |
| 4258 | NULL |
| 4259 | }; |
| 4260 | |
| 4261 | TEST_PARSING_FLAGS(argv, argv2, Flags()); |
| 4262 | } |
| 4263 | |
| 4264 | // Tests parsing a bad --gtest_filter flag. |
| 4265 | TEST_F(InitGoogleTestTest, FilterBad) { |
| 4266 | const char* argv[] = { |
| 4267 | "foo.exe", |
| 4268 | "--gtest_filter", |
| 4269 | NULL |
| 4270 | }; |
| 4271 | |
| 4272 | const char* argv2[] = { |
| 4273 | "foo.exe", |
| 4274 | "--gtest_filter", |
| 4275 | NULL |
| 4276 | }; |
| 4277 | |
| 4278 | TEST_PARSING_FLAGS(argv, argv2, Flags::Filter("")); |
| 4279 | } |
| 4280 | |
| 4281 | // Tests parsing an empty --gtest_filter flag. |
| 4282 | TEST_F(InitGoogleTestTest, FilterEmpty) { |
| 4283 | const char* argv[] = { |
| 4284 | "foo.exe", |
| 4285 | "--gtest_filter=", |
| 4286 | NULL |
| 4287 | }; |
| 4288 | |
| 4289 | const char* argv2[] = { |
| 4290 | "foo.exe", |
| 4291 | NULL |
| 4292 | }; |
| 4293 | |
| 4294 | TEST_PARSING_FLAGS(argv, argv2, Flags::Filter("")); |
| 4295 | } |
| 4296 | |
| 4297 | // Tests parsing a non-empty --gtest_filter flag. |
| 4298 | TEST_F(InitGoogleTestTest, FilterNonEmpty) { |
| 4299 | const char* argv[] = { |
| 4300 | "foo.exe", |
| 4301 | "--gtest_filter=abc", |
| 4302 | NULL |
| 4303 | }; |
| 4304 | |
| 4305 | const char* argv2[] = { |
| 4306 | "foo.exe", |
| 4307 | NULL |
| 4308 | }; |
| 4309 | |
| 4310 | TEST_PARSING_FLAGS(argv, argv2, Flags::Filter("abc")); |
| 4311 | } |
| 4312 | |
| 4313 | // Tests parsing --gtest_break_on_failure. |
| 4314 | TEST_F(InitGoogleTestTest, BreakOnFailureNoDef) { |
| 4315 | const char* argv[] = { |
| 4316 | "foo.exe", |
| 4317 | "--gtest_break_on_failure", |
| 4318 | NULL |
| 4319 | }; |
| 4320 | |
| 4321 | const char* argv2[] = { |
| 4322 | "foo.exe", |
| 4323 | NULL |
| 4324 | }; |
| 4325 | |
| 4326 | TEST_PARSING_FLAGS(argv, argv2, Flags::BreakOnFailure(true)); |
| 4327 | } |
| 4328 | |
| 4329 | // Tests parsing --gtest_break_on_failure=0. |
| 4330 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_0) { |
| 4331 | const char* argv[] = { |
| 4332 | "foo.exe", |
| 4333 | "--gtest_break_on_failure=0", |
| 4334 | NULL |
| 4335 | }; |
| 4336 | |
| 4337 | const char* argv2[] = { |
| 4338 | "foo.exe", |
| 4339 | NULL |
| 4340 | }; |
| 4341 | |
| 4342 | TEST_PARSING_FLAGS(argv, argv2, Flags::BreakOnFailure(false)); |
| 4343 | } |
| 4344 | |
| 4345 | // Tests parsing --gtest_break_on_failure=f. |
| 4346 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_f) { |
| 4347 | const char* argv[] = { |
| 4348 | "foo.exe", |
| 4349 | "--gtest_break_on_failure=f", |
| 4350 | NULL |
| 4351 | }; |
| 4352 | |
| 4353 | const char* argv2[] = { |
| 4354 | "foo.exe", |
| 4355 | NULL |
| 4356 | }; |
| 4357 | |
| 4358 | TEST_PARSING_FLAGS(argv, argv2, Flags::BreakOnFailure(false)); |
| 4359 | } |
| 4360 | |
| 4361 | // Tests parsing --gtest_break_on_failure=F. |
| 4362 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_F) { |
| 4363 | const char* argv[] = { |
| 4364 | "foo.exe", |
| 4365 | "--gtest_break_on_failure=F", |
| 4366 | NULL |
| 4367 | }; |
| 4368 | |
| 4369 | const char* argv2[] = { |
| 4370 | "foo.exe", |
| 4371 | NULL |
| 4372 | }; |
| 4373 | |
| 4374 | TEST_PARSING_FLAGS(argv, argv2, Flags::BreakOnFailure(false)); |
| 4375 | } |
| 4376 | |
| 4377 | // Tests parsing a --gtest_break_on_failure flag that has a "true" |
| 4378 | // definition. |
| 4379 | TEST_F(InitGoogleTestTest, BreakOnFailureTrue) { |
| 4380 | const char* argv[] = { |
| 4381 | "foo.exe", |
| 4382 | "--gtest_break_on_failure=1", |
| 4383 | NULL |
| 4384 | }; |
| 4385 | |
| 4386 | const char* argv2[] = { |
| 4387 | "foo.exe", |
| 4388 | NULL |
| 4389 | }; |
| 4390 | |
| 4391 | TEST_PARSING_FLAGS(argv, argv2, Flags::BreakOnFailure(true)); |
| 4392 | } |
| 4393 | |
| 4394 | // Tests parsing --gtest_catch_exceptions. |
| 4395 | TEST_F(InitGoogleTestTest, CatchExceptions) { |
| 4396 | const char* argv[] = { |
| 4397 | "foo.exe", |
| 4398 | "--gtest_catch_exceptions", |
| 4399 | NULL |
| 4400 | }; |
| 4401 | |
| 4402 | const char* argv2[] = { |
| 4403 | "foo.exe", |
| 4404 | NULL |
| 4405 | }; |
| 4406 | |
| 4407 | TEST_PARSING_FLAGS(argv, argv2, Flags::CatchExceptions(true)); |
| 4408 | } |
| 4409 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 4410 | // Tests parsing --gtest_death_test_use_fork. |
| 4411 | TEST_F(InitGoogleTestTest, DeathTestUseFork) { |
| 4412 | const char* argv[] = { |
| 4413 | "foo.exe", |
| 4414 | "--gtest_death_test_use_fork", |
| 4415 | NULL |
| 4416 | }; |
| 4417 | |
| 4418 | const char* argv2[] = { |
| 4419 | "foo.exe", |
| 4420 | NULL |
| 4421 | }; |
| 4422 | |
| 4423 | TEST_PARSING_FLAGS(argv, argv2, Flags::DeathTestUseFork(true)); |
| 4424 | } |
| 4425 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4426 | // Tests having the same flag twice with different values. The |
| 4427 | // expected behavior is that the one coming last takes precedence. |
| 4428 | TEST_F(InitGoogleTestTest, DuplicatedFlags) { |
| 4429 | const char* argv[] = { |
| 4430 | "foo.exe", |
| 4431 | "--gtest_filter=a", |
| 4432 | "--gtest_filter=b", |
| 4433 | NULL |
| 4434 | }; |
| 4435 | |
| 4436 | const char* argv2[] = { |
| 4437 | "foo.exe", |
| 4438 | NULL |
| 4439 | }; |
| 4440 | |
| 4441 | TEST_PARSING_FLAGS(argv, argv2, Flags::Filter("b")); |
| 4442 | } |
| 4443 | |
| 4444 | // Tests having an unrecognized flag on the command line. |
| 4445 | TEST_F(InitGoogleTestTest, UnrecognizedFlag) { |
| 4446 | const char* argv[] = { |
| 4447 | "foo.exe", |
| 4448 | "--gtest_break_on_failure", |
| 4449 | "bar", // Unrecognized by Google Test. |
| 4450 | "--gtest_filter=b", |
| 4451 | NULL |
| 4452 | }; |
| 4453 | |
| 4454 | const char* argv2[] = { |
| 4455 | "foo.exe", |
| 4456 | "bar", |
| 4457 | NULL |
| 4458 | }; |
| 4459 | |
| 4460 | Flags flags; |
| 4461 | flags.break_on_failure = true; |
| 4462 | flags.filter = "b"; |
| 4463 | TEST_PARSING_FLAGS(argv, argv2, flags); |
| 4464 | } |
| 4465 | |
| 4466 | // Tests having a --gtest_list_tests flag |
| 4467 | TEST_F(InitGoogleTestTest, ListTestsFlag) { |
| 4468 | const char* argv[] = { |
| 4469 | "foo.exe", |
| 4470 | "--gtest_list_tests", |
| 4471 | NULL |
| 4472 | }; |
| 4473 | |
| 4474 | const char* argv2[] = { |
| 4475 | "foo.exe", |
| 4476 | NULL |
| 4477 | }; |
| 4478 | |
| 4479 | TEST_PARSING_FLAGS(argv, argv2, Flags::ListTests(true)); |
| 4480 | } |
| 4481 | |
| 4482 | // Tests having a --gtest_list_tests flag with a "true" value |
| 4483 | TEST_F(InitGoogleTestTest, ListTestsTrue) { |
| 4484 | const char* argv[] = { |
| 4485 | "foo.exe", |
| 4486 | "--gtest_list_tests=1", |
| 4487 | NULL |
| 4488 | }; |
| 4489 | |
| 4490 | const char* argv2[] = { |
| 4491 | "foo.exe", |
| 4492 | NULL |
| 4493 | }; |
| 4494 | |
| 4495 | TEST_PARSING_FLAGS(argv, argv2, Flags::ListTests(true)); |
| 4496 | } |
| 4497 | |
| 4498 | // Tests having a --gtest_list_tests flag with a "false" value |
| 4499 | TEST_F(InitGoogleTestTest, ListTestsFalse) { |
| 4500 | const char* argv[] = { |
| 4501 | "foo.exe", |
| 4502 | "--gtest_list_tests=0", |
| 4503 | NULL |
| 4504 | }; |
| 4505 | |
| 4506 | const char* argv2[] = { |
| 4507 | "foo.exe", |
| 4508 | NULL |
| 4509 | }; |
| 4510 | |
| 4511 | TEST_PARSING_FLAGS(argv, argv2, Flags::ListTests(false)); |
| 4512 | } |
| 4513 | |
| 4514 | // Tests parsing --gtest_list_tests=f. |
| 4515 | TEST_F(InitGoogleTestTest, ListTestsFalse_f) { |
| 4516 | const char* argv[] = { |
| 4517 | "foo.exe", |
| 4518 | "--gtest_list_tests=f", |
| 4519 | NULL |
| 4520 | }; |
| 4521 | |
| 4522 | const char* argv2[] = { |
| 4523 | "foo.exe", |
| 4524 | NULL |
| 4525 | }; |
| 4526 | |
| 4527 | TEST_PARSING_FLAGS(argv, argv2, Flags::ListTests(false)); |
| 4528 | } |
| 4529 | |
| 4530 | // Tests parsing --gtest_break_on_failure=F. |
| 4531 | TEST_F(InitGoogleTestTest, ListTestsFalse_F) { |
| 4532 | const char* argv[] = { |
| 4533 | "foo.exe", |
| 4534 | "--gtest_list_tests=F", |
| 4535 | NULL |
| 4536 | }; |
| 4537 | |
| 4538 | const char* argv2[] = { |
| 4539 | "foo.exe", |
| 4540 | NULL |
| 4541 | }; |
| 4542 | |
| 4543 | TEST_PARSING_FLAGS(argv, argv2, Flags::ListTests(false)); |
| 4544 | } |
| 4545 | |
| 4546 | // Tests parsing --gtest_output (invalid). |
| 4547 | TEST_F(InitGoogleTestTest, OutputEmpty) { |
| 4548 | const char* argv[] = { |
| 4549 | "foo.exe", |
| 4550 | "--gtest_output", |
| 4551 | NULL |
| 4552 | }; |
| 4553 | |
| 4554 | const char* argv2[] = { |
| 4555 | "foo.exe", |
| 4556 | "--gtest_output", |
| 4557 | NULL |
| 4558 | }; |
| 4559 | |
| 4560 | TEST_PARSING_FLAGS(argv, argv2, Flags()); |
| 4561 | } |
| 4562 | |
| 4563 | // Tests parsing --gtest_output=xml |
| 4564 | TEST_F(InitGoogleTestTest, OutputXml) { |
| 4565 | const char* argv[] = { |
| 4566 | "foo.exe", |
| 4567 | "--gtest_output=xml", |
| 4568 | NULL |
| 4569 | }; |
| 4570 | |
| 4571 | const char* argv2[] = { |
| 4572 | "foo.exe", |
| 4573 | NULL |
| 4574 | }; |
| 4575 | |
| 4576 | TEST_PARSING_FLAGS(argv, argv2, Flags::Output("xml")); |
| 4577 | } |
| 4578 | |
| 4579 | // Tests parsing --gtest_output=xml:file |
| 4580 | TEST_F(InitGoogleTestTest, OutputXmlFile) { |
| 4581 | const char* argv[] = { |
| 4582 | "foo.exe", |
| 4583 | "--gtest_output=xml:file", |
| 4584 | NULL |
| 4585 | }; |
| 4586 | |
| 4587 | const char* argv2[] = { |
| 4588 | "foo.exe", |
| 4589 | NULL |
| 4590 | }; |
| 4591 | |
| 4592 | TEST_PARSING_FLAGS(argv, argv2, Flags::Output("xml:file")); |
| 4593 | } |
| 4594 | |
| 4595 | // Tests parsing --gtest_output=xml:directory/path/ |
| 4596 | TEST_F(InitGoogleTestTest, OutputXmlDirectory) { |
| 4597 | const char* argv[] = { |
| 4598 | "foo.exe", |
| 4599 | "--gtest_output=xml:directory/path/", |
| 4600 | NULL |
| 4601 | }; |
| 4602 | |
| 4603 | const char* argv2[] = { |
| 4604 | "foo.exe", |
| 4605 | NULL |
| 4606 | }; |
| 4607 | |
| 4608 | TEST_PARSING_FLAGS(argv, argv2, Flags::Output("xml:directory/path/")); |
| 4609 | } |
| 4610 | |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 4611 | // Tests having a --gtest_print_time flag |
| 4612 | TEST_F(InitGoogleTestTest, PrintTimeFlag) { |
| 4613 | const char* argv[] = { |
| 4614 | "foo.exe", |
| 4615 | "--gtest_print_time", |
| 4616 | NULL |
| 4617 | }; |
| 4618 | |
| 4619 | const char* argv2[] = { |
| 4620 | "foo.exe", |
| 4621 | NULL |
| 4622 | }; |
| 4623 | |
| 4624 | TEST_PARSING_FLAGS(argv, argv2, Flags::PrintTime(true)); |
| 4625 | } |
| 4626 | |
| 4627 | // Tests having a --gtest_print_time flag with a "true" value |
| 4628 | TEST_F(InitGoogleTestTest, PrintTimeTrue) { |
| 4629 | const char* argv[] = { |
| 4630 | "foo.exe", |
| 4631 | "--gtest_print_time=1", |
| 4632 | NULL |
| 4633 | }; |
| 4634 | |
| 4635 | const char* argv2[] = { |
| 4636 | "foo.exe", |
| 4637 | NULL |
| 4638 | }; |
| 4639 | |
| 4640 | TEST_PARSING_FLAGS(argv, argv2, Flags::PrintTime(true)); |
| 4641 | } |
| 4642 | |
| 4643 | // Tests having a --gtest_print_time flag with a "false" value |
| 4644 | TEST_F(InitGoogleTestTest, PrintTimeFalse) { |
| 4645 | const char* argv[] = { |
| 4646 | "foo.exe", |
| 4647 | "--gtest_print_time=0", |
| 4648 | NULL |
| 4649 | }; |
| 4650 | |
| 4651 | const char* argv2[] = { |
| 4652 | "foo.exe", |
| 4653 | NULL |
| 4654 | }; |
| 4655 | |
| 4656 | TEST_PARSING_FLAGS(argv, argv2, Flags::PrintTime(false)); |
| 4657 | } |
| 4658 | |
| 4659 | // Tests parsing --gtest_print_time=f. |
| 4660 | TEST_F(InitGoogleTestTest, PrintTimeFalse_f) { |
| 4661 | const char* argv[] = { |
| 4662 | "foo.exe", |
| 4663 | "--gtest_print_time=f", |
| 4664 | NULL |
| 4665 | }; |
| 4666 | |
| 4667 | const char* argv2[] = { |
| 4668 | "foo.exe", |
| 4669 | NULL |
| 4670 | }; |
| 4671 | |
| 4672 | TEST_PARSING_FLAGS(argv, argv2, Flags::PrintTime(false)); |
| 4673 | } |
| 4674 | |
| 4675 | // Tests parsing --gtest_print_time=F. |
| 4676 | TEST_F(InitGoogleTestTest, PrintTimeFalse_F) { |
| 4677 | const char* argv[] = { |
| 4678 | "foo.exe", |
| 4679 | "--gtest_print_time=F", |
| 4680 | NULL |
| 4681 | }; |
| 4682 | |
| 4683 | const char* argv2[] = { |
| 4684 | "foo.exe", |
| 4685 | NULL |
| 4686 | }; |
| 4687 | |
| 4688 | TEST_PARSING_FLAGS(argv, argv2, Flags::PrintTime(false)); |
| 4689 | } |
| 4690 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4691 | // Tests parsing --gtest_repeat=number |
| 4692 | TEST_F(InitGoogleTestTest, Repeat) { |
| 4693 | const char* argv[] = { |
| 4694 | "foo.exe", |
| 4695 | "--gtest_repeat=1000", |
| 4696 | NULL |
| 4697 | }; |
| 4698 | |
| 4699 | const char* argv2[] = { |
| 4700 | "foo.exe", |
| 4701 | NULL |
| 4702 | }; |
| 4703 | |
| 4704 | TEST_PARSING_FLAGS(argv, argv2, Flags::Repeat(1000)); |
| 4705 | } |
| 4706 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 4707 | // Tests having a --gtest_also_run_disabled_tests flag |
| 4708 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsFlag) { |
| 4709 | const char* argv[] = { |
| 4710 | "foo.exe", |
| 4711 | "--gtest_also_run_disabled_tests", |
| 4712 | NULL |
| 4713 | }; |
| 4714 | |
| 4715 | const char* argv2[] = { |
| 4716 | "foo.exe", |
| 4717 | NULL |
| 4718 | }; |
| 4719 | |
| 4720 | TEST_PARSING_FLAGS(argv, argv2, Flags::AlsoRunDisabledTests(true)); |
| 4721 | } |
| 4722 | |
| 4723 | // Tests having a --gtest_also_run_disabled_tests flag with a "true" value |
| 4724 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsTrue) { |
| 4725 | const char* argv[] = { |
| 4726 | "foo.exe", |
| 4727 | "--gtest_also_run_disabled_tests=1", |
| 4728 | NULL |
| 4729 | }; |
| 4730 | |
| 4731 | const char* argv2[] = { |
| 4732 | "foo.exe", |
| 4733 | NULL |
| 4734 | }; |
| 4735 | |
| 4736 | TEST_PARSING_FLAGS(argv, argv2, Flags::AlsoRunDisabledTests(true)); |
| 4737 | } |
| 4738 | |
| 4739 | // Tests having a --gtest_also_run_disabled_tests flag with a "false" value |
| 4740 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsFalse) { |
| 4741 | const char* argv[] = { |
| 4742 | "foo.exe", |
| 4743 | "--gtest_also_run_disabled_tests=0", |
| 4744 | NULL |
| 4745 | }; |
| 4746 | |
| 4747 | const char* argv2[] = { |
| 4748 | "foo.exe", |
| 4749 | NULL |
| 4750 | }; |
| 4751 | |
| 4752 | TEST_PARSING_FLAGS(argv, argv2, Flags::AlsoRunDisabledTests(false)); |
| 4753 | } |
| 4754 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4755 | #ifdef GTEST_OS_WINDOWS |
| 4756 | // Tests parsing wide strings. |
| 4757 | TEST_F(InitGoogleTestTest, WideStrings) { |
| 4758 | const wchar_t* argv[] = { |
| 4759 | L"foo.exe", |
| 4760 | L"--gtest_filter=Foo*", |
| 4761 | L"--gtest_list_tests=1", |
| 4762 | L"--gtest_break_on_failure", |
| 4763 | L"--non_gtest_flag", |
| 4764 | NULL |
| 4765 | }; |
| 4766 | |
| 4767 | const wchar_t* argv2[] = { |
| 4768 | L"foo.exe", |
| 4769 | L"--non_gtest_flag", |
| 4770 | NULL |
| 4771 | }; |
| 4772 | |
| 4773 | Flags expected_flags; |
| 4774 | expected_flags.break_on_failure = true; |
| 4775 | expected_flags.filter = "Foo*"; |
| 4776 | expected_flags.list_tests = true; |
| 4777 | |
| 4778 | TEST_PARSING_FLAGS(argv, argv2, expected_flags); |
| 4779 | } |
| 4780 | #endif // GTEST_OS_WINDOWS |
| 4781 | |
| 4782 | // Tests current_test_info() in UnitTest. |
| 4783 | class CurrentTestInfoTest : public Test { |
| 4784 | protected: |
| 4785 | // Tests that current_test_info() returns NULL before the first test in |
| 4786 | // the test case is run. |
| 4787 | static void SetUpTestCase() { |
| 4788 | // There should be no tests running at this point. |
| 4789 | const TestInfo* test_info = |
| 4790 | UnitTest::GetInstance()->current_test_info(); |
| 4791 | EXPECT_EQ(NULL, test_info) |
| 4792 | << "There should be no tests running at this point."; |
| 4793 | } |
| 4794 | |
| 4795 | // Tests that current_test_info() returns NULL after the last test in |
| 4796 | // the test case has run. |
| 4797 | static void TearDownTestCase() { |
| 4798 | const TestInfo* test_info = |
| 4799 | UnitTest::GetInstance()->current_test_info(); |
| 4800 | EXPECT_EQ(NULL, test_info) |
| 4801 | << "There should be no tests running at this point."; |
| 4802 | } |
| 4803 | }; |
| 4804 | |
| 4805 | // Tests that current_test_info() returns TestInfo for currently running |
| 4806 | // test by checking the expected test name against the actual one. |
| 4807 | TEST_F(CurrentTestInfoTest, WorksForFirstTestInATestCase) { |
| 4808 | const TestInfo* test_info = |
| 4809 | UnitTest::GetInstance()->current_test_info(); |
| 4810 | ASSERT_TRUE(NULL != test_info) |
| 4811 | << "There is a test running so we should have a valid TestInfo."; |
| 4812 | EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) |
| 4813 | << "Expected the name of the currently running test case."; |
| 4814 | EXPECT_STREQ("WorksForFirstTestInATestCase", test_info->name()) |
| 4815 | << "Expected the name of the currently running test."; |
| 4816 | } |
| 4817 | |
| 4818 | // Tests that current_test_info() returns TestInfo for currently running |
| 4819 | // test by checking the expected test name against the actual one. We |
| 4820 | // use this test to see that the TestInfo object actually changed from |
| 4821 | // the previous invocation. |
| 4822 | TEST_F(CurrentTestInfoTest, WorksForSecondTestInATestCase) { |
| 4823 | const TestInfo* test_info = |
| 4824 | UnitTest::GetInstance()->current_test_info(); |
| 4825 | ASSERT_TRUE(NULL != test_info) |
| 4826 | << "There is a test running so we should have a valid TestInfo."; |
| 4827 | EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) |
| 4828 | << "Expected the name of the currently running test case."; |
| 4829 | EXPECT_STREQ("WorksForSecondTestInATestCase", test_info->name()) |
| 4830 | << "Expected the name of the currently running test."; |
| 4831 | } |
| 4832 | |
| 4833 | } // namespace testing |
| 4834 | |
| 4835 | // These two lines test that we can define tests in a namespace that |
| 4836 | // has the name "testing" and is nested in another namespace. |
| 4837 | namespace my_namespace { |
| 4838 | namespace testing { |
| 4839 | |
| 4840 | // Makes sure that TEST knows to use ::testing::Test instead of |
| 4841 | // ::my_namespace::testing::Test. |
| 4842 | class Test {}; |
| 4843 | |
| 4844 | // Makes sure that an assertion knows to use ::testing::Message instead of |
| 4845 | // ::my_namespace::testing::Message. |
| 4846 | class Message {}; |
| 4847 | |
| 4848 | // Makes sure that an assertion knows to use |
| 4849 | // ::testing::AssertionResult instead of |
| 4850 | // ::my_namespace::testing::AssertionResult. |
| 4851 | class AssertionResult {}; |
| 4852 | |
| 4853 | // Tests that an assertion that should succeed works as expected. |
| 4854 | TEST(NestedTestingNamespaceTest, Success) { |
| 4855 | EXPECT_EQ(1, 1) << "This shouldn't fail."; |
| 4856 | } |
| 4857 | |
| 4858 | // Tests that an assertion that should fail works as expected. |
| 4859 | TEST(NestedTestingNamespaceTest, Failure) { |
| 4860 | EXPECT_FATAL_FAILURE(FAIL() << "This failure is expected.", |
| 4861 | "This failure is expected."); |
| 4862 | } |
| 4863 | |
| 4864 | } // namespace testing |
| 4865 | } // namespace my_namespace |
| 4866 | |
| 4867 | // Tests that one can call superclass SetUp and TearDown methods-- |
| 4868 | // that is, that they are not private. |
| 4869 | // No tests are based on this fixture; the test "passes" if it compiles |
| 4870 | // successfully. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4871 | class ProtectedFixtureMethodsTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4872 | protected: |
| 4873 | virtual void SetUp() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4874 | Test::SetUp(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4875 | } |
| 4876 | virtual void TearDown() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4877 | Test::TearDown(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4878 | } |
| 4879 | }; |
| 4880 | |
| 4881 | // StreamingAssertionsTest tests the streaming versions of a representative |
| 4882 | // sample of assertions. |
| 4883 | TEST(StreamingAssertionsTest, Unconditional) { |
| 4884 | SUCCEED() << "expected success"; |
| 4885 | EXPECT_NONFATAL_FAILURE(ADD_FAILURE() << "expected failure", |
| 4886 | "expected failure"); |
| 4887 | EXPECT_FATAL_FAILURE(FAIL() << "expected failure", |
| 4888 | "expected failure"); |
| 4889 | } |
| 4890 | |
| 4891 | TEST(StreamingAssertionsTest, Truth) { |
| 4892 | EXPECT_TRUE(true) << "unexpected failure"; |
| 4893 | ASSERT_TRUE(true) << "unexpected failure"; |
| 4894 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "expected failure", |
| 4895 | "expected failure"); |
| 4896 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(false) << "expected failure", |
| 4897 | "expected failure"); |
| 4898 | } |
| 4899 | |
| 4900 | TEST(StreamingAssertionsTest, Truth2) { |
| 4901 | EXPECT_FALSE(false) << "unexpected failure"; |
| 4902 | ASSERT_FALSE(false) << "unexpected failure"; |
| 4903 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "expected failure", |
| 4904 | "expected failure"); |
| 4905 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(true) << "expected failure", |
| 4906 | "expected failure"); |
| 4907 | } |
| 4908 | |
| 4909 | TEST(StreamingAssertionsTest, IntegerEquals) { |
| 4910 | EXPECT_EQ(1, 1) << "unexpected failure"; |
| 4911 | ASSERT_EQ(1, 1) << "unexpected failure"; |
| 4912 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(1, 2) << "expected failure", |
| 4913 | "expected failure"); |
| 4914 | EXPECT_FATAL_FAILURE(ASSERT_EQ(1, 2) << "expected failure", |
| 4915 | "expected failure"); |
| 4916 | } |
| 4917 | |
| 4918 | TEST(StreamingAssertionsTest, IntegerLessThan) { |
| 4919 | EXPECT_LT(1, 2) << "unexpected failure"; |
| 4920 | ASSERT_LT(1, 2) << "unexpected failure"; |
| 4921 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 1) << "expected failure", |
| 4922 | "expected failure"); |
| 4923 | EXPECT_FATAL_FAILURE(ASSERT_LT(2, 1) << "expected failure", |
| 4924 | "expected failure"); |
| 4925 | } |
| 4926 | |
| 4927 | TEST(StreamingAssertionsTest, StringsEqual) { |
| 4928 | EXPECT_STREQ("foo", "foo") << "unexpected failure"; |
| 4929 | ASSERT_STREQ("foo", "foo") << "unexpected failure"; |
| 4930 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ("foo", "bar") << "expected failure", |
| 4931 | "expected failure"); |
| 4932 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("foo", "bar") << "expected failure", |
| 4933 | "expected failure"); |
| 4934 | } |
| 4935 | |
| 4936 | TEST(StreamingAssertionsTest, StringsNotEqual) { |
| 4937 | EXPECT_STRNE("foo", "bar") << "unexpected failure"; |
| 4938 | ASSERT_STRNE("foo", "bar") << "unexpected failure"; |
| 4939 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE("foo", "foo") << "expected failure", |
| 4940 | "expected failure"); |
| 4941 | EXPECT_FATAL_FAILURE(ASSERT_STRNE("foo", "foo") << "expected failure", |
| 4942 | "expected failure"); |
| 4943 | } |
| 4944 | |
| 4945 | TEST(StreamingAssertionsTest, StringsEqualIgnoringCase) { |
| 4946 | EXPECT_STRCASEEQ("foo", "FOO") << "unexpected failure"; |
| 4947 | ASSERT_STRCASEEQ("foo", "FOO") << "unexpected failure"; |
| 4948 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASEEQ("foo", "bar") << "expected failure", |
| 4949 | "expected failure"); |
| 4950 | EXPECT_FATAL_FAILURE(ASSERT_STRCASEEQ("foo", "bar") << "expected failure", |
| 4951 | "expected failure"); |
| 4952 | } |
| 4953 | |
| 4954 | TEST(StreamingAssertionsTest, StringNotEqualIgnoringCase) { |
| 4955 | EXPECT_STRCASENE("foo", "bar") << "unexpected failure"; |
| 4956 | ASSERT_STRCASENE("foo", "bar") << "unexpected failure"; |
| 4957 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASENE("foo", "FOO") << "expected failure", |
| 4958 | "expected failure"); |
| 4959 | EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("bar", "BAR") << "expected failure", |
| 4960 | "expected failure"); |
| 4961 | } |
| 4962 | |
| 4963 | TEST(StreamingAssertionsTest, FloatingPointEquals) { |
| 4964 | EXPECT_FLOAT_EQ(1.0, 1.0) << "unexpected failure"; |
| 4965 | ASSERT_FLOAT_EQ(1.0, 1.0) << "unexpected failure"; |
| 4966 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(0.0, 1.0) << "expected failure", |
| 4967 | "expected failure"); |
| 4968 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.0) << "expected failure", |
| 4969 | "expected failure"); |
| 4970 | } |
| 4971 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4972 | #if GTEST_HAS_EXCEPTIONS |
| 4973 | |
| 4974 | TEST(StreamingAssertionsTest, Throw) { |
| 4975 | EXPECT_THROW(ThrowAnInteger(), int) << "unexpected failure"; |
| 4976 | ASSERT_THROW(ThrowAnInteger(), int) << "unexpected failure"; |
| 4977 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool) << |
| 4978 | "expected failure", "expected failure"); |
| 4979 | EXPECT_FATAL_FAILURE(ASSERT_THROW(ThrowAnInteger(), bool) << |
| 4980 | "expected failure", "expected failure"); |
| 4981 | } |
| 4982 | |
| 4983 | TEST(StreamingAssertionsTest, NoThrow) { |
| 4984 | EXPECT_NO_THROW(1) << "unexpected failure"; |
| 4985 | ASSERT_NO_THROW(1) << "unexpected failure"; |
| 4986 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()) << |
| 4987 | "expected failure", "expected failure"); |
| 4988 | EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()) << |
| 4989 | "expected failure", "expected failure"); |
| 4990 | } |
| 4991 | |
| 4992 | TEST(StreamingAssertionsTest, AnyThrow) { |
| 4993 | EXPECT_ANY_THROW(ThrowAnInteger()) << "unexpected failure"; |
| 4994 | ASSERT_ANY_THROW(ThrowAnInteger()) << "unexpected failure"; |
| 4995 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(1) << |
| 4996 | "expected failure", "expected failure"); |
| 4997 | EXPECT_FATAL_FAILURE(ASSERT_ANY_THROW(1) << |
| 4998 | "expected failure", "expected failure"); |
| 4999 | } |
| 5000 | |
| 5001 | #endif // GTEST_HAS_EXCEPTIONS |
| 5002 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5003 | // Tests that Google Test correctly decides whether to use colors in the output. |
| 5004 | |
| 5005 | TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsYes) { |
| 5006 | GTEST_FLAG(color) = "yes"; |
| 5007 | |
| 5008 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 5009 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5010 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 5011 | |
| 5012 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 5013 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5014 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 5015 | } |
| 5016 | |
| 5017 | TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsAliasOfYes) { |
| 5018 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 5019 | |
| 5020 | GTEST_FLAG(color) = "True"; |
| 5021 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 5022 | |
| 5023 | GTEST_FLAG(color) = "t"; |
| 5024 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 5025 | |
| 5026 | GTEST_FLAG(color) = "1"; |
| 5027 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 5028 | } |
| 5029 | |
| 5030 | TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsNo) { |
| 5031 | GTEST_FLAG(color) = "no"; |
| 5032 | |
| 5033 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 5034 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5035 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 5036 | |
| 5037 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 5038 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5039 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 5040 | } |
| 5041 | |
| 5042 | TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsInvalid) { |
| 5043 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 5044 | |
| 5045 | GTEST_FLAG(color) = "F"; |
| 5046 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5047 | |
| 5048 | GTEST_FLAG(color) = "0"; |
| 5049 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5050 | |
| 5051 | GTEST_FLAG(color) = "unknown"; |
| 5052 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5053 | } |
| 5054 | |
| 5055 | TEST(ColoredOutputTest, UsesColorsWhenStdoutIsTty) { |
| 5056 | GTEST_FLAG(color) = "auto"; |
| 5057 | |
| 5058 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 5059 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 5060 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5061 | } |
| 5062 | |
| 5063 | TEST(ColoredOutputTest, UsesColorsWhenTermSupportsColors) { |
| 5064 | GTEST_FLAG(color) = "auto"; |
| 5065 | |
| 5066 | #ifdef GTEST_OS_WINDOWS |
| 5067 | // On Windows, we ignore the TERM variable as it's usually not set. |
| 5068 | |
| 5069 | SetEnv("TERM", "dumb"); |
| 5070 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5071 | |
| 5072 | SetEnv("TERM", ""); |
| 5073 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5074 | |
| 5075 | SetEnv("TERM", "xterm"); |
| 5076 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5077 | #else |
| 5078 | // On non-Windows platforms, we rely on TERM to determine if the |
| 5079 | // terminal supports colors. |
| 5080 | |
| 5081 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 5082 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5083 | |
| 5084 | SetEnv("TERM", "emacs"); // TERM doesn't support colors. |
| 5085 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5086 | |
| 5087 | SetEnv("TERM", "vt100"); // TERM doesn't support colors. |
| 5088 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5089 | |
| 5090 | SetEnv("TERM", "xterm-mono"); // TERM doesn't support colors. |
| 5091 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5092 | |
| 5093 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 5094 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5095 | |
| 5096 | SetEnv("TERM", "xterm-color"); // TERM supports colors. |
| 5097 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 5098 | #endif // GTEST_OS_WINDOWS |
| 5099 | } |
| 5100 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5101 | // Verifies that StaticAssertTypeEq works in a namespace scope. |
| 5102 | |
| 5103 | static bool dummy1 = StaticAssertTypeEq<bool, bool>(); |
| 5104 | static bool dummy2 = StaticAssertTypeEq<const int, const int>(); |
| 5105 | |
| 5106 | // Verifies that StaticAssertTypeEq works in a class. |
| 5107 | |
| 5108 | template <typename T> |
| 5109 | class StaticAssertTypeEqTestHelper { |
| 5110 | public: |
| 5111 | StaticAssertTypeEqTestHelper() { StaticAssertTypeEq<bool, T>(); } |
| 5112 | }; |
| 5113 | |
| 5114 | TEST(StaticAssertTypeEqTest, WorksInClass) { |
| 5115 | StaticAssertTypeEqTestHelper<bool>(); |
| 5116 | } |
| 5117 | |
| 5118 | // Verifies that StaticAssertTypeEq works inside a function. |
| 5119 | |
| 5120 | typedef int IntAlias; |
| 5121 | |
| 5122 | TEST(StaticAssertTypeEqTest, CompilesForEqualTypes) { |
| 5123 | StaticAssertTypeEq<int, IntAlias>(); |
| 5124 | StaticAssertTypeEq<int*, IntAlias*>(); |
| 5125 | } |
| 5126 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 5127 | TEST(ThreadLocalTest, DefaultConstructor) { |
| 5128 | ThreadLocal<int> t1; |
| 5129 | EXPECT_EQ(0, t1.get()); |
| 5130 | |
| 5131 | ThreadLocal<void*> t2; |
| 5132 | EXPECT_TRUE(t2.get() == NULL); |
| 5133 | } |
| 5134 | |
| 5135 | TEST(ThreadLocalTest, Init) { |
| 5136 | ThreadLocal<int> t1(123); |
| 5137 | EXPECT_EQ(123, t1.get()); |
| 5138 | |
| 5139 | int i = 0; |
| 5140 | ThreadLocal<int*> t2(&i); |
| 5141 | EXPECT_EQ(&i, t2.get()); |
| 5142 | } |
| 5143 | |
vladlosev | f904a61 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 5144 | TEST(GetCurrentOsStackTraceExceptTopTest, ReturnsTheStackTrace) { |
| 5145 | testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
| 5146 | |
| 5147 | // We don't have a stack walker in Google Test yet. |
| 5148 | EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 0).c_str()); |
| 5149 | EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 1).c_str()); |
| 5150 | } |
| 5151 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 5152 | #ifndef GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5153 | // We will want to integrate running the unittests to a different |
| 5154 | // main application on Symbian. |
| 5155 | int main(int argc, char** argv) { |
| 5156 | testing::InitGoogleTest(&argc, argv); |
| 5157 | |
| 5158 | #ifdef GTEST_HAS_DEATH_TEST |
| 5159 | if (!testing::internal::GTEST_FLAG(internal_run_death_test).empty()) { |
| 5160 | // Skip the usual output capturing if we're running as the child |
| 5161 | // process of an threadsafe-style death test. |
| 5162 | freopen("/dev/null", "w", stdout); |
| 5163 | } |
| 5164 | #endif // GTEST_HAS_DEATH_TEST |
| 5165 | |
| 5166 | // Runs all tests using Google Test. |
| 5167 | return RUN_ALL_TESTS(); |
| 5168 | } |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 5169 | #endif // GTEST_OS_SYMBIAN |