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