| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdlib.h> | 5 #include <stdlib.h> |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/environment/environment.h" | 10 #include "mojo/public/cpp/environment/environment.h" |
| 11 #include "mojo/public/cpp/environment/logging.h" | 11 #include "mojo/public/cpp/environment/logging.h" |
| 12 #include "mojo/public/cpp/system/macros.h" | 12 #include "mojo/public/cpp/system/macros.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 // A macro, so it can be automatically joined with other string literals. (Not | 15 // A macro, so it can be automatically joined with other string literals. (Not |
| 16 // simply __FILE__, since that may contain a path.) | 16 // simply __FILE__, since that may contain a path.) |
| 17 #define OUR_FILENAME "logging_unittest.cc" | 17 #define OUR_FILENAME "logging_unittest.cc" |
| 18 | 18 |
| 19 namespace mojo { | 19 namespace mojo { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 class PtrToMemberHelper { |
| 23 public: |
| 24 int member; |
| 25 }; |
| 26 |
| 27 bool DcheckTestHelper(bool* was_called) { |
| 28 *was_called = true; |
| 29 return false; |
| 30 } |
| 31 |
| 22 class LoggingTest : public testing::Test { | 32 class LoggingTest : public testing::Test { |
| 23 public: | 33 public: |
| 24 LoggingTest() : environment_(NULL, &kMockLogger) { | 34 LoggingTest() : environment_(NULL, &kMockLogger) { |
| 25 minimum_log_level_ = MOJO_LOG_LEVEL_INFO; | 35 minimum_log_level_ = MOJO_LOG_LEVEL_INFO; |
| 26 ResetMockLogger(); | 36 ResetMockLogger(); |
| 27 } | 37 } |
| 28 virtual ~LoggingTest() {} | 38 virtual ~LoggingTest() {} |
| 29 | 39 |
| 30 protected: | 40 protected: |
| 31 // Note: Does not reset |minimum_log_level_|. | 41 // Note: Does not reset |minimum_log_level_|. |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 << "hello " << "world"; | 172 << "hello " << "world"; |
| 163 EXPECT_TRUE(log_message_was_called()); | 173 EXPECT_TRUE(log_message_was_called()); |
| 164 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); | 174 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 165 EXPECT_EQ(OUR_FILENAME "(123): hello world", last_message()); | 175 EXPECT_EQ(OUR_FILENAME "(123): hello world", last_message()); |
| 166 } | 176 } |
| 167 | 177 |
| 168 TEST_F(LoggingTest, LogStream) { | 178 TEST_F(LoggingTest, LogStream) { |
| 169 MOJO_LOG_STREAM(INFO) << "hello"; | 179 MOJO_LOG_STREAM(INFO) << "hello"; |
| 170 EXPECT_TRUE(log_message_was_called()); | 180 EXPECT_TRUE(log_message_was_called()); |
| 171 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); | 181 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 172 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); | 182 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message()); |
| 173 | 183 |
| 174 ResetMockLogger(); | 184 ResetMockLogger(); |
| 175 | 185 |
| 176 MOJO_LOG_STREAM(ERROR) << "hi " << 123; | 186 MOJO_LOG_STREAM(ERROR) << "hi " << 123; |
| 177 EXPECT_TRUE(log_message_was_called()); | 187 EXPECT_TRUE(log_message_was_called()); |
| 178 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); | 188 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 179 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hi 123"), last_message()); | 189 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hi 123"), last_message()); |
| 180 } | 190 } |
| 181 | 191 |
| 182 TEST_F(LoggingTest, LazyLogStream) { | 192 TEST_F(LoggingTest, LazyLogStream) { |
| 183 MOJO_LAZY_LOG_STREAM(INFO, true) << "hello"; | 193 MOJO_LAZY_LOG_STREAM(INFO, true) << "hello"; |
| 184 EXPECT_TRUE(log_message_was_called()); | 194 EXPECT_TRUE(log_message_was_called()); |
| 185 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); | 195 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 186 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); | 196 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message()); |
| 187 | 197 |
| 188 ResetMockLogger(); | 198 ResetMockLogger(); |
| 189 | 199 |
| 190 MOJO_LAZY_LOG_STREAM(ERROR, true) << "hi " << 123; | 200 MOJO_LAZY_LOG_STREAM(ERROR, true) << "hi " << 123; |
| 191 EXPECT_TRUE(log_message_was_called()); | 201 EXPECT_TRUE(log_message_was_called()); |
| 192 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); | 202 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 193 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hi 123"), last_message()); | 203 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hi 123"), last_message()); |
| 194 | 204 |
| 195 ResetMockLogger(); | 205 ResetMockLogger(); |
| 196 | 206 |
| 197 MOJO_LAZY_LOG_STREAM(INFO, false) << "hello"; | 207 MOJO_LAZY_LOG_STREAM(INFO, false) << "hello"; |
| 198 EXPECT_FALSE(log_message_was_called()); | 208 EXPECT_FALSE(log_message_was_called()); |
| 199 | 209 |
| 200 ResetMockLogger(); | 210 ResetMockLogger(); |
| 201 | 211 |
| 202 MOJO_LAZY_LOG_STREAM(FATAL, false) << "hello"; | 212 MOJO_LAZY_LOG_STREAM(FATAL, false) << "hello"; |
| 203 EXPECT_FALSE(log_message_was_called()); | 213 EXPECT_FALSE(log_message_was_called()); |
| 204 | 214 |
| 205 ResetMockLogger(); | 215 ResetMockLogger(); |
| 206 | 216 |
| 207 bool x = false; | 217 PtrToMemberHelper helper; |
| 218 helper.member = 1; |
| 219 int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member; |
| 220 |
| 208 // This probably fails to compile if we forget to parenthesize the condition | 221 // This probably fails to compile if we forget to parenthesize the condition |
| 209 // in the macro (= has low precedence, and needs an lvalue on the LHS). | 222 // in the macro (.* has lower precedence than !, which can't apply to |
| 210 MOJO_LAZY_LOG_STREAM(ERROR, x = true) << "hello"; | 223 // |helper|). |
| 224 MOJO_LAZY_LOG_STREAM(ERROR, helper.*member_ptr == 1) << "hello"; |
| 211 EXPECT_TRUE(log_message_was_called()); | 225 EXPECT_TRUE(log_message_was_called()); |
| 212 | 226 |
| 213 ResetMockLogger(); | 227 ResetMockLogger(); |
| 214 | 228 |
| 215 MOJO_LAZY_LOG_STREAM(WARNING, x = false) << "hello"; | 229 MOJO_LAZY_LOG_STREAM(WARNING, helper.*member_ptr == 0) << "hello"; |
| 216 EXPECT_FALSE(log_message_was_called()); | 230 EXPECT_FALSE(log_message_was_called()); |
| 217 } | 231 } |
| 218 | 232 |
| 219 TEST_F(LoggingTest, ShouldLog) { | 233 TEST_F(LoggingTest, ShouldLog) { |
| 220 // We start at |MOJO_LOG_LEVEL_INFO|. | 234 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 221 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE)); | 235 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE)); |
| 222 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO)); | 236 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO)); |
| 223 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING)); | 237 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING)); |
| 224 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR)); | 238 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR)); |
| 225 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL)); | 239 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 242 TEST_F(LoggingTest, Log) { | 256 TEST_F(LoggingTest, Log) { |
| 243 // We start at |MOJO_LOG_LEVEL_INFO|. | 257 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 244 MOJO_LOG(VERBOSE) << "hello"; | 258 MOJO_LOG(VERBOSE) << "hello"; |
| 245 EXPECT_FALSE(log_message_was_called()); | 259 EXPECT_FALSE(log_message_was_called()); |
| 246 | 260 |
| 247 ResetMockLogger(); | 261 ResetMockLogger(); |
| 248 | 262 |
| 249 MOJO_LOG(INFO) << "hello"; | 263 MOJO_LOG(INFO) << "hello"; |
| 250 EXPECT_TRUE(log_message_was_called()); | 264 EXPECT_TRUE(log_message_was_called()); |
| 251 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); | 265 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 252 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); | 266 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message()); |
| 253 | 267 |
| 254 ResetMockLogger(); | 268 ResetMockLogger(); |
| 255 | 269 |
| 256 MOJO_LOG(ERROR) << "hello"; | 270 MOJO_LOG(ERROR) << "hello"; |
| 257 EXPECT_TRUE(log_message_was_called()); | 271 EXPECT_TRUE(log_message_was_called()); |
| 258 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); | 272 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 259 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); | 273 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message()); |
| 260 | 274 |
| 261 ResetMockLogger(); | 275 ResetMockLogger(); |
| 262 | 276 |
| 263 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); | 277 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); |
| 264 | 278 |
| 265 MOJO_LOG(VERBOSE) << "hello"; | 279 MOJO_LOG(VERBOSE) << "hello"; |
| 266 EXPECT_FALSE(log_message_was_called()); | 280 EXPECT_FALSE(log_message_was_called()); |
| 267 | 281 |
| 268 ResetMockLogger(); | 282 ResetMockLogger(); |
| 269 | 283 |
| 270 MOJO_LOG(INFO) << "hello"; | 284 MOJO_LOG(INFO) << "hello"; |
| 271 EXPECT_FALSE(log_message_was_called()); | 285 EXPECT_FALSE(log_message_was_called()); |
| 272 | 286 |
| 273 ResetMockLogger(); | 287 ResetMockLogger(); |
| 274 | 288 |
| 275 MOJO_LOG(ERROR) << "hello"; | 289 MOJO_LOG(ERROR) << "hello"; |
| 276 EXPECT_TRUE(log_message_was_called()); | 290 EXPECT_TRUE(log_message_was_called()); |
| 277 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); | 291 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 278 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); | 292 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message()); |
| 279 } | 293 } |
| 280 | 294 |
| 281 TEST_F(LoggingTest, LogIf) { | 295 TEST_F(LoggingTest, LogIf) { |
| 282 // We start at |MOJO_LOG_LEVEL_INFO|. | 296 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 283 MOJO_LOG_IF(VERBOSE, true) << "hello"; | 297 MOJO_LOG_IF(VERBOSE, true) << "hello"; |
| 284 EXPECT_FALSE(log_message_was_called()); | 298 EXPECT_FALSE(log_message_was_called()); |
| 285 | 299 |
| 286 ResetMockLogger(); | 300 ResetMockLogger(); |
| 287 | 301 |
| 288 MOJO_LOG_IF(VERBOSE, false) << "hello"; | 302 MOJO_LOG_IF(VERBOSE, false) << "hello"; |
| 289 EXPECT_FALSE(log_message_was_called()); | 303 EXPECT_FALSE(log_message_was_called()); |
| 290 | 304 |
| 291 ResetMockLogger(); | 305 ResetMockLogger(); |
| 306 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); |
| 292 | 307 |
| 293 bool x = false; | 308 bool x = true; |
| 294 // Also try to make sure that we parenthesize the condition properly. | 309 // Also try to make sure that we parenthesize the condition properly. |
| 295 MOJO_LOG_IF(INFO, x = true) << "hello"; | 310 MOJO_LOG_IF(INFO, false || x) << "hello"; |
| 296 EXPECT_TRUE(log_message_was_called()); | |
| 297 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); | |
| 298 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); | |
| 299 | |
| 300 ResetMockLogger(); | |
| 301 | |
| 302 MOJO_LOG_IF(INFO, x = false) << "hello"; | |
| 303 EXPECT_FALSE(log_message_was_called()); | 311 EXPECT_FALSE(log_message_was_called()); |
| 304 | 312 |
| 305 ResetMockLogger(); | 313 ResetMockLogger(); |
| 306 | 314 |
| 307 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); | |
| 308 | |
| 309 ResetMockLogger(); | |
| 310 | |
| 311 MOJO_LOG_IF(INFO, 0 != 1) << "hello"; | 315 MOJO_LOG_IF(INFO, 0 != 1) << "hello"; |
| 312 EXPECT_FALSE(log_message_was_called()); | 316 EXPECT_FALSE(log_message_was_called()); |
| 313 | 317 |
| 314 ResetMockLogger(); | 318 ResetMockLogger(); |
| 315 | 319 |
| 316 MOJO_LOG_IF(WARNING, 1+1 == 2) << "hello"; | 320 MOJO_LOG_IF(WARNING, 1 + 1 == 2) << "hello"; |
| 317 EXPECT_FALSE(log_message_was_called()); | 321 EXPECT_FALSE(log_message_was_called()); |
| 318 | 322 |
| 319 ResetMockLogger(); | 323 ResetMockLogger(); |
| 320 | 324 |
| 321 MOJO_LOG_IF(ERROR, 1*2 == 2) << "hello"; | 325 MOJO_LOG_IF(ERROR, 1 * 2 == 2) << "hello"; |
| 322 EXPECT_TRUE(log_message_was_called()); | 326 EXPECT_TRUE(log_message_was_called()); |
| 323 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); | 327 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level()); |
| 324 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); | 328 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 3, "hello"), last_message()); |
| 325 | 329 |
| 326 ResetMockLogger(); | 330 ResetMockLogger(); |
| 327 | 331 |
| 328 MOJO_LOG_IF(FATAL, 1*2 == 3) << "hello"; | 332 MOJO_LOG_IF(FATAL, 1 * 2 == 3) << "hello"; |
| 329 EXPECT_FALSE(log_message_was_called()); | 333 EXPECT_FALSE(log_message_was_called()); |
| 330 | 334 |
| 331 ResetMockLogger(); | 335 ResetMockLogger(); |
| 332 | 336 |
| 333 // |MOJO_LOG_IF()| shouldn't evaluate its condition if the level is below the | 337 // |MOJO_LOG_IF()| shouldn't evaluate its condition if the level is below the |
| 334 // minimum. | 338 // minimum. |
| 335 MOJO_LOG_IF(INFO, NotCalled()) << "hello"; | 339 MOJO_LOG_IF(INFO, NotCalled()) << "hello"; |
| 336 EXPECT_FALSE(log_message_was_called()); | 340 EXPECT_FALSE(log_message_was_called()); |
| 337 } | 341 } |
| 338 | 342 |
| 339 TEST_F(LoggingTest, Check) { | 343 TEST_F(LoggingTest, Check) { |
| 340 MOJO_CHECK(true) << "hello"; | 344 MOJO_CHECK(true) << "hello"; |
| 341 EXPECT_FALSE(log_message_was_called()); | 345 EXPECT_FALSE(log_message_was_called()); |
| 342 | 346 |
| 343 ResetMockLogger(); | 347 ResetMockLogger(); |
| 344 | 348 |
| 345 bool x = true; | 349 PtrToMemberHelper helper; |
| 350 helper.member = 0; |
| 351 int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member; |
| 352 |
| 346 // Also try to make sure that we parenthesize the condition properly. | 353 // Also try to make sure that we parenthesize the condition properly. |
| 347 MOJO_CHECK(x = false) << "hello"; | 354 MOJO_CHECK(helper.*member_ptr == 1) << "hello"; |
| 348 EXPECT_TRUE(log_message_was_called()); | 355 EXPECT_TRUE(log_message_was_called()); |
| 349 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); | 356 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); |
| 350 // Different compilers have different ideas about the line number of a split | 357 // Different compilers have different ideas about the line number of a split |
| 351 // line. | 358 // line. |
| 352 int line = __LINE__; | 359 int line = __LINE__; |
| 353 EXPECT_EQ(ExpectedLogMessage(line-5, "Check failed: x = false. hello"), | 360 EXPECT_EQ(ExpectedLogMessage(line - 5, |
| 361 "Check failed: helper.*member_ptr == 1. hello"), |
| 354 last_message()); | 362 last_message()); |
| 355 | 363 |
| 356 ResetMockLogger(); | 364 ResetMockLogger(); |
| 357 | 365 |
| 358 // Also test a "naked" |MOJO_CHECK()|s. | 366 // Also test a "naked" |MOJO_CHECK()|s. |
| 359 MOJO_CHECK(1+2 == 3); | 367 MOJO_CHECK(1 + 2 == 3); |
| 360 EXPECT_FALSE(log_message_was_called()); | 368 EXPECT_FALSE(log_message_was_called()); |
| 361 } | 369 } |
| 362 | 370 |
| 363 TEST_F(LoggingTest, Dlog) { | 371 TEST_F(LoggingTest, Dlog) { |
| 364 // We start at |MOJO_LOG_LEVEL_INFO|. | 372 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 365 MOJO_DLOG(VERBOSE) << "hello"; | 373 MOJO_DLOG(VERBOSE) << "hello"; |
| 366 EXPECT_FALSE(log_message_was_called()); | 374 EXPECT_FALSE(log_message_was_called()); |
| 367 | 375 |
| 368 ResetMockLogger(); | 376 ResetMockLogger(); |
| 369 | 377 |
| 370 MOJO_DLOG(INFO) << "hello"; | 378 MOJO_DLOG(INFO) << "hello"; |
| 371 #ifdef NDEBUG | 379 #ifdef NDEBUG |
| 372 EXPECT_FALSE(log_message_was_called()); | 380 EXPECT_FALSE(log_message_was_called()); |
| 373 #else | 381 #else |
| 374 EXPECT_TRUE(log_message_was_called()); | 382 EXPECT_TRUE(log_message_was_called()); |
| 375 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); | 383 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 376 EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message()); | 384 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message()); |
| 377 #endif | 385 #endif |
| 378 } | 386 } |
| 379 | 387 |
| 380 TEST_F(LoggingTest, DlogIf) { | 388 TEST_F(LoggingTest, DlogIf) { |
| 381 // We start at |MOJO_LOG_LEVEL_INFO|. It shouldn't evaluate the condition in | 389 // We start at |MOJO_LOG_LEVEL_INFO|. It shouldn't evaluate the condition in |
| 382 // this case. | 390 // this case. |
| 383 MOJO_DLOG_IF(VERBOSE, NotCalled()) << "hello"; | 391 MOJO_DLOG_IF(VERBOSE, NotCalled()) << "hello"; |
| 384 EXPECT_FALSE(log_message_was_called()); | 392 EXPECT_FALSE(log_message_was_called()); |
| 385 | 393 |
| 386 ResetMockLogger(); | 394 ResetMockLogger(); |
| 387 | 395 |
| 388 MOJO_DLOG_IF(INFO, 1 == 0) << "hello"; | 396 MOJO_DLOG_IF(INFO, 1 == 0) << "hello"; |
| 389 EXPECT_FALSE(log_message_was_called()); | 397 EXPECT_FALSE(log_message_was_called()); |
| 390 | 398 |
| 391 ResetMockLogger(); | 399 ResetMockLogger(); |
| 392 | 400 |
| 393 MOJO_DLOG_IF(INFO, 1 == 1) << "hello"; | 401 MOJO_DLOG_IF(INFO, 1 == 1) << "hello"; |
| 394 #ifdef NDEBUG | 402 #ifdef NDEBUG |
| 395 EXPECT_FALSE(log_message_was_called()); | 403 EXPECT_FALSE(log_message_was_called()); |
| 396 #else | 404 #else |
| 397 EXPECT_TRUE(log_message_was_called()); | 405 EXPECT_TRUE(log_message_was_called()); |
| 398 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); | 406 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); |
| 399 EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message()); | 407 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message()); |
| 400 #endif | 408 #endif |
| 401 | 409 |
| 402 ResetMockLogger(); | 410 ResetMockLogger(); |
| 403 | 411 |
| 404 // |MOJO_DLOG_IF()| shouldn't compile its condition for non-debug builds. | 412 // |MOJO_DLOG_IF()| shouldn't compile its condition for non-debug builds. |
| 405 #ifndef NDEBUG | 413 #ifndef NDEBUG |
| 406 bool debug_only = true; | 414 bool debug_only = true; |
| 407 #endif | 415 #endif |
| 408 MOJO_DLOG_IF(WARNING, debug_only) << "hello"; | 416 MOJO_DLOG_IF(WARNING, debug_only) << "hello"; |
| 409 #ifdef NDEBUG | 417 #ifdef NDEBUG |
| 410 EXPECT_FALSE(log_message_was_called()); | 418 EXPECT_FALSE(log_message_was_called()); |
| 411 #else | 419 #else |
| 412 EXPECT_TRUE(log_message_was_called()); | 420 EXPECT_TRUE(log_message_was_called()); |
| 413 EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level()); | 421 EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level()); |
| 414 EXPECT_EQ(ExpectedLogMessage(__LINE__-6, "hello"), last_message()); | 422 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message()); |
| 415 #endif | 423 #endif |
| 416 } | 424 } |
| 417 | 425 |
| 418 TEST_F(LoggingTest, Dcheck) { | 426 TEST_F(LoggingTest, Dcheck) { |
| 419 MOJO_DCHECK(true); | 427 MOJO_DCHECK(true); |
| 420 EXPECT_FALSE(log_message_was_called()); | 428 EXPECT_FALSE(log_message_was_called()); |
| 421 | 429 |
| 422 ResetMockLogger(); | 430 ResetMockLogger(); |
| 423 | 431 |
| 424 MOJO_DCHECK(true) << "hello"; | 432 MOJO_DCHECK(true) << "hello"; |
| 425 EXPECT_FALSE(log_message_was_called()); | 433 EXPECT_FALSE(log_message_was_called()); |
| 426 | 434 |
| 427 ResetMockLogger(); | 435 ResetMockLogger(); |
| 428 | 436 |
| 429 // |MOJO_DCHECK()| should compile (but not evaluate) its condition even for | 437 // |MOJO_DCHECK()| should compile (but not evaluate) its condition even for |
| 430 // non-debug builds. (Hopefully, we'll get an unused variable error if it | 438 // non-debug builds. (Hopefully, we'll get an unused variable error if it |
| 431 // fails to compile the condition.) | 439 // fails to compile the condition.) |
| 432 bool x = true; | 440 bool was_called = false; |
| 433 MOJO_DCHECK(x = false) << "hello"; | 441 MOJO_DCHECK(DcheckTestHelper(&was_called)) << "hello"; |
| 434 #ifdef NDEBUG | 442 #ifdef NDEBUG |
| 443 EXPECT_FALSE(was_called); |
| 435 EXPECT_FALSE(log_message_was_called()); | 444 EXPECT_FALSE(log_message_was_called()); |
| 436 #else | 445 #else |
| 446 EXPECT_TRUE(was_called); |
| 437 EXPECT_TRUE(log_message_was_called()); | 447 EXPECT_TRUE(log_message_was_called()); |
| 438 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); | 448 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); |
| 439 // Different compilers have different ideas about the line number of a split | 449 // Different compilers have different ideas about the line number of a split |
| 440 // line. | 450 // line. |
| 441 int line = __LINE__; | 451 int line = __LINE__; |
| 442 EXPECT_EQ(ExpectedLogMessage(line-8, "Check failed: x = false. hello"), | 452 EXPECT_EQ( |
| 443 last_message()); | 453 ExpectedLogMessage(line - 10, |
| 454 "Check failed: DcheckTestHelper(&was_called). hello"), |
| 455 last_message()); |
| 444 #endif | 456 #endif |
| 457 |
| 458 ResetMockLogger(); |
| 459 |
| 460 // Also try to make sure that we parenthesize the condition properly. |
| 461 bool x = true; |
| 462 MOJO_DCHECK(false || x) << "hello"; |
| 463 EXPECT_FALSE(log_message_was_called()); |
| 445 } | 464 } |
| 446 | 465 |
| 447 } // namespace | 466 } // namespace |
| 448 } // namespace mojo | 467 } // namespace mojo |
| OLD | NEW |