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