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

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

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <stdlib.h>
6
7 #include <sstream>
8 #include <string>
9
10 #include "gtest/gtest.h"
11 #include "mojo/public/cpp/environment/environment.h"
12 #include "mojo/public/cpp/environment/logging.h"
13 #include "mojo/public/cpp/system/macros.h"
14
15 namespace mojo {
16 namespace {
17
18 // The current logging system strips the path, so we need our filename.
19 const char kOurFilename[] = "logging_unittest.cc";
20
21 class PtrToMemberHelper {
22 public:
23 int member;
24 };
25
26 bool DcheckTestHelper(bool* was_called) {
27 *was_called = true;
28 return false;
29 }
30
31 class LoggingTest : public testing::Test {
32 public:
33 LoggingTest() {
34 Environment::SetDefaultLogger(&kMockLogger);
35 minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
36 ResetMockLogger();
37 }
38 ~LoggingTest() override {}
39
40 protected:
41 // Note: Does not reset |minimum_log_level_|.
42 static void ResetMockLogger() {
43 log_message_was_called_ = false;
44 last_log_level_ = MOJO_LOG_LEVEL_INFO;
45 last_source_file_.clear();
46 last_source_line_ = 0;
47 last_message_.clear();
48 }
49
50 // A function returning |bool| that shouldn't be called.
51 static bool NotCalledCondition() {
52 not_called_condition_was_called_ = true;
53 return false;
54 }
55
56 static bool log_message_was_called() { return log_message_was_called_; }
57 static MojoLogLevel last_log_level() { return last_log_level_; }
58 static const std::string& last_source_file() { return last_source_file_; }
59 static uint32_t last_source_line() { return last_source_line_; }
60 static const std::string& last_message() { return last_message_; }
61 static bool not_called_condition_was_called() {
62 return not_called_condition_was_called_;
63 }
64
65 private:
66 // Note: We record calls even if |log_level| is below |minimum_log_level_|
67 // (since the macros should mostly avoid this, and we want to be able to check
68 // that they do).
69 static void MockLogMessage(MojoLogLevel log_level,
70 const char* source_file,
71 uint32_t source_line,
72 const char* message) {
73 log_message_was_called_ = true;
74 last_log_level_ = log_level;
75 last_source_file_ = source_file;
76 last_source_line_ = source_line;
77 last_message_ = message;
78 }
79
80 static MojoLogLevel MockGetMinimumLogLevel() { return minimum_log_level_; }
81
82 static void MockSetMinimumLogLevel(MojoLogLevel minimum_log_level) {
83 minimum_log_level_ = minimum_log_level;
84 }
85
86 static const MojoLogger kMockLogger;
87 static MojoLogLevel minimum_log_level_;
88 static bool log_message_was_called_;
89 static MojoLogLevel last_log_level_;
90 static std::string last_source_file_;
91 static uint32_t last_source_line_;
92 static std::string last_message_;
93 static bool not_called_condition_was_called_;
94
95 MOJO_DISALLOW_COPY_AND_ASSIGN(LoggingTest);
96 };
97
98 // static
99 const MojoLogger LoggingTest::kMockLogger = {
100 &LoggingTest::MockLogMessage,
101 &LoggingTest::MockGetMinimumLogLevel,
102 &LoggingTest::MockSetMinimumLogLevel};
103
104 // static
105 MojoLogLevel LoggingTest::minimum_log_level_ = MOJO_LOG_LEVEL_INFO;
106
107 // static
108 bool LoggingTest::log_message_was_called_ = MOJO_LOG_LEVEL_INFO;
109
110 // static
111 MojoLogLevel LoggingTest::last_log_level_ = MOJO_LOG_LEVEL_INFO;
112
113 // static
114 std::string LoggingTest::last_source_file_;
115
116 // static
117 uint32_t LoggingTest::last_source_line_ = 0;
118
119 // static
120 std::string LoggingTest::last_message_;
121
122 // static
123 bool LoggingTest::not_called_condition_was_called_ = false;
124
125 TEST_F(LoggingTest, InternalLogMessage) {
126 internal::LogMessage(MOJO_LOG_LEVEL_INFO, "foo.cc", 123).stream() << "hello "
127 << "world";
128 EXPECT_TRUE(log_message_was_called());
129 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
130 EXPECT_EQ("foo.cc", last_source_file());
131 EXPECT_EQ(123u, last_source_line());
132 EXPECT_EQ("hello world", last_message());
133
134 ResetMockLogger();
135
136 internal::LogMessage(MOJO_LOG_LEVEL_WARNING, "./path/to/foo.cc", 123).stream()
137 << "hello "
138 << "world";
139 EXPECT_TRUE(log_message_was_called());
140 EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level());
141 EXPECT_EQ("foo.cc", last_source_file());
142 EXPECT_EQ(123u, last_source_line());
143 EXPECT_EQ("hello world", last_message());
144
145 ResetMockLogger();
146
147 internal::LogMessage(MOJO_LOG_LEVEL_ERROR, "/path/to/foo.cc", 123).stream()
148 << "hello "
149 << "world";
150 EXPECT_TRUE(log_message_was_called());
151 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
152 EXPECT_EQ("foo.cc", last_source_file());
153 EXPECT_EQ(123u, last_source_line());
154 EXPECT_EQ("hello world", last_message());
155
156 ResetMockLogger();
157
158 internal::LogMessage(MOJO_LOG_LEVEL_FATAL, "path/to/foo.cc", 123).stream()
159 << "hello "
160 << "world";
161 EXPECT_TRUE(log_message_was_called());
162 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
163 EXPECT_EQ("foo.cc", last_source_file());
164 EXPECT_EQ(123u, last_source_line());
165 EXPECT_EQ("hello world", last_message());
166
167 ResetMockLogger();
168
169 internal::LogMessage(MOJO_LOG_LEVEL_VERBOSE, ".\\xy\\foo.cc", 123).stream()
170 << "hello "
171 << "world";
172 EXPECT_TRUE(log_message_was_called());
173 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE, last_log_level());
174 EXPECT_EQ("foo.cc", last_source_file());
175 EXPECT_EQ(123u, last_source_line());
176 EXPECT_EQ("hello world", last_message());
177
178 ResetMockLogger();
179
180 internal::LogMessage(MOJO_LOG_LEVEL_VERBOSE - 1, "xy\\foo.cc", 123).stream()
181 << "hello "
182 << "world";
183 EXPECT_TRUE(log_message_was_called());
184 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE - 1, last_log_level());
185 EXPECT_EQ("foo.cc", last_source_file());
186 EXPECT_EQ(123u, last_source_line());
187 EXPECT_EQ("hello world", last_message());
188
189 ResetMockLogger();
190
191 internal::LogMessage(MOJO_LOG_LEVEL_VERBOSE - 9, "C:\\xy\\foo.cc", 123)
192 .stream()
193 << "hello "
194 << "world";
195 EXPECT_TRUE(log_message_was_called());
196 EXPECT_EQ(MOJO_LOG_LEVEL_VERBOSE - 9, last_log_level());
197 EXPECT_EQ("foo.cc", last_source_file());
198 EXPECT_EQ(123u, last_source_line());
199 EXPECT_EQ("hello world", last_message());
200
201 ResetMockLogger();
202
203 internal::LogMessage(MOJO_LOG_LEVEL_INFO, __FILE__, 123).stream() << "hello "
204 << "world";
205 EXPECT_TRUE(log_message_was_called());
206 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
207 EXPECT_EQ(kOurFilename, last_source_file());
208 EXPECT_EQ(123u, last_source_line());
209 EXPECT_EQ("hello world", last_message());
210 }
211
212 TEST_F(LoggingTest, LogStream) {
213 MOJO_LOG_STREAM(INFO) << "hello";
214 EXPECT_TRUE(log_message_was_called());
215 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
216 EXPECT_EQ(kOurFilename, last_source_file());
217 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
218 EXPECT_EQ("hello", last_message());
219
220 ResetMockLogger();
221
222 MOJO_LOG_STREAM(ERROR) << "hi " << 123;
223 EXPECT_TRUE(log_message_was_called());
224 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
225 EXPECT_EQ(kOurFilename, last_source_file());
226 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
227 EXPECT_EQ("hi 123", last_message());
228 }
229
230 TEST_F(LoggingTest, LazyLogStream) {
231 MOJO_LAZY_LOG_STREAM(INFO, true) << "hello";
232 EXPECT_TRUE(log_message_was_called());
233 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
234 EXPECT_EQ(kOurFilename, last_source_file());
235 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
236 EXPECT_EQ("hello", last_message());
237
238 ResetMockLogger();
239
240 MOJO_LAZY_LOG_STREAM(ERROR, true) << "hi " << 123;
241 EXPECT_TRUE(log_message_was_called());
242 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
243 EXPECT_EQ(kOurFilename, last_source_file());
244 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
245 EXPECT_EQ("hi 123", last_message());
246
247 ResetMockLogger();
248
249 MOJO_LAZY_LOG_STREAM(INFO, false) << "hello";
250 EXPECT_FALSE(log_message_was_called());
251
252 ResetMockLogger();
253
254 MOJO_LAZY_LOG_STREAM(FATAL, false) << "hello";
255 EXPECT_FALSE(log_message_was_called());
256
257 ResetMockLogger();
258
259 PtrToMemberHelper helper;
260 helper.member = 1;
261 int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member;
262
263 // This probably fails to compile if we forget to parenthesize the condition
264 // in the macro (.* has lower precedence than !, which can't apply to
265 // |helper|).
266 MOJO_LAZY_LOG_STREAM(ERROR, helper.*member_ptr == 1) << "hello";
267 EXPECT_TRUE(log_message_was_called());
268
269 ResetMockLogger();
270
271 MOJO_LAZY_LOG_STREAM(WARNING, helper.*member_ptr == 0) << "hello";
272 EXPECT_FALSE(log_message_was_called());
273 }
274
275 TEST_F(LoggingTest, ShouldLog) {
276 // We start at |MOJO_LOG_LEVEL_INFO|.
277 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE));
278 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO));
279 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING));
280 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
281 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
282
283 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
284 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE));
285 EXPECT_FALSE(MOJO_SHOULD_LOG(INFO));
286 EXPECT_FALSE(MOJO_SHOULD_LOG(WARNING));
287 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
288 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
289
290 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_VERBOSE -
291 1);
292 EXPECT_TRUE(MOJO_SHOULD_LOG(VERBOSE));
293 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO));
294 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING));
295 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR));
296 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL));
297 }
298
299 TEST_F(LoggingTest, Log) {
300 // We start at |MOJO_LOG_LEVEL_INFO|.
301 MOJO_LOG(VERBOSE) << "hello";
302 EXPECT_FALSE(log_message_was_called());
303
304 ResetMockLogger();
305
306 MOJO_LOG(INFO) << "hello";
307 EXPECT_TRUE(log_message_was_called());
308 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
309 EXPECT_EQ(kOurFilename, last_source_file());
310 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
311 EXPECT_EQ("hello", last_message());
312
313 ResetMockLogger();
314
315 MOJO_LOG(ERROR) << "hello";
316 EXPECT_TRUE(log_message_was_called());
317 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
318 EXPECT_EQ(kOurFilename, last_source_file());
319 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
320 EXPECT_EQ("hello", last_message());
321
322 ResetMockLogger();
323
324 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
325
326 MOJO_LOG(VERBOSE) << "hello";
327 EXPECT_FALSE(log_message_was_called());
328
329 ResetMockLogger();
330
331 MOJO_LOG(INFO) << "hello";
332 EXPECT_FALSE(log_message_was_called());
333
334 ResetMockLogger();
335
336 MOJO_LOG(ERROR) << "hello";
337 EXPECT_TRUE(log_message_was_called());
338 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
339 EXPECT_EQ(kOurFilename, last_source_file());
340 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
341 EXPECT_EQ("hello", last_message());
342 }
343
344 TEST_F(LoggingTest, LogIf) {
345 // We start at |MOJO_LOG_LEVEL_INFO|.
346 MOJO_LOG_IF(VERBOSE, true) << "hello";
347 EXPECT_FALSE(log_message_was_called());
348
349 ResetMockLogger();
350
351 MOJO_LOG_IF(VERBOSE, false) << "hello";
352 EXPECT_FALSE(log_message_was_called());
353
354 ResetMockLogger();
355 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR);
356
357 bool x = true;
358 // Also try to make sure that we parenthesize the condition properly.
359 MOJO_LOG_IF(INFO, false || x) << "hello";
360 EXPECT_FALSE(log_message_was_called());
361
362 ResetMockLogger();
363
364 MOJO_LOG_IF(INFO, 0 != 1) << "hello";
365 EXPECT_FALSE(log_message_was_called());
366
367 ResetMockLogger();
368
369 MOJO_LOG_IF(WARNING, 1 + 1 == 2) << "hello";
370 EXPECT_FALSE(log_message_was_called());
371
372 ResetMockLogger();
373
374 MOJO_LOG_IF(ERROR, 1 * 2 == 2) << "hello";
375 EXPECT_TRUE(log_message_was_called());
376 EXPECT_EQ(MOJO_LOG_LEVEL_ERROR, last_log_level());
377 EXPECT_EQ(kOurFilename, last_source_file());
378 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
379 EXPECT_EQ("hello", last_message());
380
381 ResetMockLogger();
382
383 MOJO_LOG_IF(FATAL, 1 * 2 == 3) << "hello";
384 EXPECT_FALSE(log_message_was_called());
385
386 ResetMockLogger();
387
388 // |MOJO_LOG_IF()| shouldn't evaluate its condition if the level is below the
389 // minimum.
390 MOJO_LOG_IF(INFO, NotCalledCondition()) << "hello";
391 EXPECT_FALSE(not_called_condition_was_called());
392 EXPECT_FALSE(log_message_was_called());
393 }
394
395 TEST_F(LoggingTest, Check) {
396 MOJO_CHECK(true) << "hello";
397 EXPECT_FALSE(log_message_was_called());
398
399 ResetMockLogger();
400
401 PtrToMemberHelper helper;
402 helper.member = 0;
403 int PtrToMemberHelper::*member_ptr = &PtrToMemberHelper::member;
404
405 // Also try to make sure that we parenthesize the condition properly.
406 MOJO_CHECK(helper.*member_ptr == 1) << "hello";
407 EXPECT_TRUE(log_message_was_called());
408 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
409 EXPECT_EQ(kOurFilename, last_source_file());
410 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 4), last_source_line());
411 EXPECT_EQ("Check failed: helper.*member_ptr == 1. hello", last_message());
412
413 ResetMockLogger();
414
415 // Also test a "naked" |MOJO_CHECK()|s.
416 MOJO_CHECK(1 + 2 == 3);
417 EXPECT_FALSE(log_message_was_called());
418 }
419
420 TEST_F(LoggingTest, Dlog) {
421 // We start at |MOJO_LOG_LEVEL_INFO|.
422 MOJO_DLOG(VERBOSE) << "hello";
423 EXPECT_FALSE(log_message_was_called());
424
425 ResetMockLogger();
426
427 MOJO_DLOG(INFO) << "hello";
428 #ifdef NDEBUG
429 EXPECT_FALSE(log_message_was_called());
430 #else
431 EXPECT_TRUE(log_message_was_called());
432 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
433 EXPECT_EQ(kOurFilename, last_source_file());
434 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 7), last_source_line());
435 EXPECT_EQ("hello", last_message());
436 #endif
437 }
438
439 TEST_F(LoggingTest, DlogIf) {
440 // We start at |MOJO_LOG_LEVEL_INFO|. It shouldn't evaluate the condition in
441 // this case.
442 MOJO_DLOG_IF(VERBOSE, NotCalledCondition()) << "hello";
443 EXPECT_FALSE(not_called_condition_was_called());
444 EXPECT_FALSE(log_message_was_called());
445
446 ResetMockLogger();
447
448 MOJO_DLOG_IF(INFO, 1 == 0) << "hello";
449 EXPECT_FALSE(log_message_was_called());
450
451 ResetMockLogger();
452
453 MOJO_DLOG_IF(INFO, 1 == 1) << "hello";
454 #ifdef NDEBUG
455 EXPECT_FALSE(log_message_was_called());
456 #else
457 EXPECT_TRUE(log_message_was_called());
458 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level());
459 EXPECT_EQ(kOurFilename, last_source_file());
460 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 7), last_source_line());
461 EXPECT_EQ("hello", last_message());
462 #endif
463
464 ResetMockLogger();
465
466 // |MOJO_DLOG_IF()| shouldn't compile its condition for non-debug builds.
467 #ifndef NDEBUG
468 bool debug_only = true;
469 #endif
470 MOJO_DLOG_IF(WARNING, debug_only) << "hello";
471 #ifdef NDEBUG
472 EXPECT_FALSE(log_message_was_called());
473 #else
474 EXPECT_TRUE(log_message_was_called());
475 EXPECT_EQ(MOJO_LOG_LEVEL_WARNING, last_log_level());
476 EXPECT_EQ(kOurFilename, last_source_file());
477 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 7), last_source_line());
478 EXPECT_EQ("hello", last_message());
479 #endif
480 }
481
482 TEST_F(LoggingTest, Dcheck) {
483 MOJO_DCHECK(true);
484 EXPECT_FALSE(log_message_was_called());
485
486 ResetMockLogger();
487
488 MOJO_DCHECK(true) << "hello";
489 EXPECT_FALSE(log_message_was_called());
490
491 ResetMockLogger();
492
493 // |MOJO_DCHECK()| should compile (but not evaluate) its condition even for
494 // non-debug builds. (Hopefully, we'll get an unused variable error if it
495 // fails to compile the condition.)
496 bool was_called = false;
497 MOJO_DCHECK(DcheckTestHelper(&was_called)) << "hello";
498 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
499 EXPECT_FALSE(was_called);
500 EXPECT_FALSE(log_message_was_called());
501 #else
502 EXPECT_TRUE(was_called);
503 EXPECT_TRUE(log_message_was_called());
504 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level());
505 EXPECT_EQ(kOurFilename, last_source_file());
506 EXPECT_EQ(static_cast<uint32_t>(__LINE__ - 9), last_source_line());
507 EXPECT_EQ("Check failed: DcheckTestHelper(&was_called). hello",
508 last_message());
509 #endif
510
511 ResetMockLogger();
512
513 // Also try to make sure that we parenthesize the condition properly.
514 bool x = true;
515 MOJO_DCHECK(false || x) << "hello";
516 EXPECT_FALSE(log_message_was_called());
517 }
518
519 } // namespace
520 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698