Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: mojo/public/cpp/environment/tests/logging_unittest.cc

Issue 419933002: Make Mojo's LoggingTest.LogIf resilient to MSVC's unreachable code warning (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 27 matching lines...) Expand all
38 virtual ~LoggingTest() {} 38 virtual ~LoggingTest() {}
39 39
40 protected: 40 protected:
41 // Note: Does not reset |minimum_log_level_|. 41 // Note: Does not reset |minimum_log_level_|.
42 static void ResetMockLogger() { 42 static void ResetMockLogger() {
43 log_message_was_called_ = false; 43 log_message_was_called_ = false;
44 last_log_level_ = MOJO_LOG_LEVEL_INFO; 44 last_log_level_ = MOJO_LOG_LEVEL_INFO;
45 last_message_.clear(); 45 last_message_.clear();
46 } 46 }
47 47
48 // A function returning |bool| that shouldn't be called.
49 static bool NotCalledCondition() {
50 not_called_condition_was_called_ = true;
51 return false;
52 }
53
48 static bool log_message_was_called() { return log_message_was_called_; } 54 static bool log_message_was_called() { return log_message_was_called_; }
49 static MojoLogLevel last_log_level() { return last_log_level_; } 55 static MojoLogLevel last_log_level() { return last_log_level_; }
50 static const std::string& last_message() { return last_message_; } 56 static const std::string& last_message() { return last_message_; }
57 static bool not_called_condition_was_called() {
58 return not_called_condition_was_called_;
59 }
51 60
52 private: 61 private:
53 // Note: We record calls even if |log_level| is below |minimum_log_level_| 62 // Note: We record calls even if |log_level| is below |minimum_log_level_|
54 // (since the macros should mostly avoid this, and we want to be able to check 63 // (since the macros should mostly avoid this, and we want to be able to check
55 // that they do). 64 // that they do).
56 static void MockLogMessage(MojoLogLevel log_level, const char* message) { 65 static void MockLogMessage(MojoLogLevel log_level, const char* message) {
57 log_message_was_called_ = true; 66 log_message_was_called_ = true;
58 last_log_level_ = log_level; 67 last_log_level_ = log_level;
59 last_message_ = message; 68 last_message_ = message;
60 } 69 }
61 70
62 static MojoLogLevel MockGetMinimumLogLevel() { 71 static MojoLogLevel MockGetMinimumLogLevel() {
63 return minimum_log_level_; 72 return minimum_log_level_;
64 } 73 }
65 74
66 static void MockSetMinimumLogLevel(MojoLogLevel minimum_log_level) { 75 static void MockSetMinimumLogLevel(MojoLogLevel minimum_log_level) {
67 minimum_log_level_ = minimum_log_level; 76 minimum_log_level_ = minimum_log_level;
68 } 77 }
69 78
70 Environment environment_; 79 Environment environment_;
71 80
72 static const MojoLogger kMockLogger; 81 static const MojoLogger kMockLogger;
73 static MojoLogLevel minimum_log_level_; 82 static MojoLogLevel minimum_log_level_;
74 static bool log_message_was_called_; 83 static bool log_message_was_called_;
75 static MojoLogLevel last_log_level_; 84 static MojoLogLevel last_log_level_;
76 static std::string last_message_; 85 static std::string last_message_;
86 static bool not_called_condition_was_called_;
77 87
78 MOJO_DISALLOW_COPY_AND_ASSIGN(LoggingTest); 88 MOJO_DISALLOW_COPY_AND_ASSIGN(LoggingTest);
79 }; 89 };
80 90
81 // static 91 // static
82 const MojoLogger LoggingTest::kMockLogger = { 92 const MojoLogger LoggingTest::kMockLogger = {
83 &LoggingTest::MockLogMessage, 93 &LoggingTest::MockLogMessage,
84 &LoggingTest::MockGetMinimumLogLevel, 94 &LoggingTest::MockGetMinimumLogLevel,
85 &LoggingTest::MockSetMinimumLogLevel 95 &LoggingTest::MockSetMinimumLogLevel
86 }; 96 };
87 97
88 // static 98 // static
89 MojoLogLevel LoggingTest::minimum_log_level_ = MOJO_LOG_LEVEL_INFO; 99 MojoLogLevel LoggingTest::minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
90 100
91 // static 101 // static
92 bool LoggingTest::log_message_was_called_ = MOJO_LOG_LEVEL_INFO; 102 bool LoggingTest::log_message_was_called_ = MOJO_LOG_LEVEL_INFO;
93 103
94 // static 104 // static
95 MojoLogLevel LoggingTest::last_log_level_ = MOJO_LOG_LEVEL_INFO; 105 MojoLogLevel LoggingTest::last_log_level_ = MOJO_LOG_LEVEL_INFO;
96 106
97 // static 107 // static
98 std::string LoggingTest::last_message_; 108 std::string LoggingTest::last_message_;
99 109
110 // static
111 bool LoggingTest::not_called_condition_was_called_ = false;
112
100 std::string ExpectedLogMessage(int line, const char* message) { 113 std::string ExpectedLogMessage(int line, const char* message) {
101 std::ostringstream s; 114 std::ostringstream s;
102 s << OUR_FILENAME "(" << line << "): " << message; 115 s << OUR_FILENAME "(" << line << "): " << message;
103 return s.str(); 116 return s.str();
104 } 117 }
105 118
106 // A function returning |bool| that shouldn't be called.
107 bool NotCalled() {
108 abort();
109 // I think all compilers are smart enough to recognize that nothing is run
110 // after |abort()|. (Some definitely complain that things after it are
111 // unreachable.)
112 }
113
114 TEST_F(LoggingTest, InternalLogMessage) { 119 TEST_F(LoggingTest, InternalLogMessage) {
115 internal::LogMessage("foo.cc", 123, MOJO_LOG_LEVEL_INFO).stream() 120 internal::LogMessage("foo.cc", 123, MOJO_LOG_LEVEL_INFO).stream()
116 << "hello " << "world"; 121 << "hello " << "world";
117 EXPECT_TRUE(log_message_was_called()); 122 EXPECT_TRUE(log_message_was_called());
118 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); 123 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
119 EXPECT_EQ("foo.cc(123): hello world", last_message()); 124 EXPECT_EQ("foo.cc(123): hello world", last_message());
120 125
121 ResetMockLogger(); 126 ResetMockLogger();
122 127
123 internal::LogMessage("./path/to/foo.cc", 123, MOJO_LOG_LEVEL_WARNING).stream() 128 internal::LogMessage("./path/to/foo.cc", 123, MOJO_LOG_LEVEL_WARNING).stream()
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 334
330 ResetMockLogger(); 335 ResetMockLogger();
331 336
332 MOJO_LOG_IF(FATAL, 1 * 2 == 3) << "hello"; 337 MOJO_LOG_IF(FATAL, 1 * 2 == 3) << "hello";
333 EXPECT_FALSE(log_message_was_called()); 338 EXPECT_FALSE(log_message_was_called());
334 339
335 ResetMockLogger(); 340 ResetMockLogger();
336 341
337 // |MOJO_LOG_IF()| shouldn't evaluate its condition if the level is below the 342 // |MOJO_LOG_IF()| shouldn't evaluate its condition if the level is below the
338 // minimum. 343 // minimum.
339 MOJO_LOG_IF(INFO, NotCalled()) << "hello"; 344 MOJO_LOG_IF(INFO, NotCalledCondition()) << "hello";
345 EXPECT_FALSE(not_called_condition_was_called());
340 EXPECT_FALSE(log_message_was_called()); 346 EXPECT_FALSE(log_message_was_called());
341 } 347 }
342 348
343 TEST_F(LoggingTest, Check) { 349 TEST_F(LoggingTest, Check) {
344 MOJO_CHECK(true) << "hello"; 350 MOJO_CHECK(true) << "hello";
345 EXPECT_FALSE(log_message_was_called()); 351 EXPECT_FALSE(log_message_was_called());
346 352
347 ResetMockLogger(); 353 ResetMockLogger();
348 354
349 PtrToMemberHelper helper; 355 PtrToMemberHelper helper;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 #else 387 #else
382 EXPECT_TRUE(log_message_was_called()); 388 EXPECT_TRUE(log_message_was_called());
383 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); 389 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
384 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message()); 390 EXPECT_EQ(ExpectedLogMessage(__LINE__ - 6, "hello"), last_message());
385 #endif 391 #endif
386 } 392 }
387 393
388 TEST_F(LoggingTest, DlogIf) { 394 TEST_F(LoggingTest, DlogIf) {
389 // We start at |MOJO_LOG_LEVEL_INFO|. It shouldn't evaluate the condition in 395 // We start at |MOJO_LOG_LEVEL_INFO|. It shouldn't evaluate the condition in
390 // this case. 396 // this case.
391 MOJO_DLOG_IF(VERBOSE, NotCalled()) << "hello"; 397 MOJO_DLOG_IF(VERBOSE, NotCalledCondition()) << "hello";
398 EXPECT_FALSE(not_called_condition_was_called());
392 EXPECT_FALSE(log_message_was_called()); 399 EXPECT_FALSE(log_message_was_called());
393 400
394 ResetMockLogger(); 401 ResetMockLogger();
395 402
396 MOJO_DLOG_IF(INFO, 1 == 0) << "hello"; 403 MOJO_DLOG_IF(INFO, 1 == 0) << "hello";
397 EXPECT_FALSE(log_message_was_called()); 404 EXPECT_FALSE(log_message_was_called());
398 405
399 ResetMockLogger(); 406 ResetMockLogger();
400 407
401 MOJO_DLOG_IF(INFO, 1 == 1) << "hello"; 408 MOJO_DLOG_IF(INFO, 1 == 1) << "hello";
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 ResetMockLogger(); 465 ResetMockLogger();
459 466
460 // Also try to make sure that we parenthesize the condition properly. 467 // Also try to make sure that we parenthesize the condition properly.
461 bool x = true; 468 bool x = true;
462 MOJO_DCHECK(false || x) << "hello"; 469 MOJO_DCHECK(false || x) << "hello";
463 EXPECT_FALSE(log_message_was_called()); 470 EXPECT_FALSE(log_message_was_called());
464 } 471 }
465 472
466 } // namespace 473 } // namespace
467 } // namespace mojo 474 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698