blob: d64b5453b655c3f7966ba4cc4e8d5279b57b0dcb [file] [log] [blame]
Gary Morain8a5726a2012-05-15 10:56:49 -07001// Copyright (c) 2012 The Chromium OS 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#ifndef SHILL_MOCK_LOG_H_
6#define SHILL_MOCK_LOG_H_
7
8// ScopedMockLog provides a way for unittests to validate log messages. You can
9// set expectations that certain log messages will be emited by your functions.
10// To use ScopedMockLog, simply create a ScopedMockLog in your test and set
11// expectations on its Log() method. When the ScopedMockLog object goes out of
12// scope, the log messages sent to it will be verified against expectations.
13//
14// Note: Use only one ScopedMockLog in a test because more than one won't work!
15//
16// Sample usage:
17//
18// You can verify that a function "DoSomething" emits a specific log text:
19//
20// TEST_F(YourTest, DoesSomething) {
21// ScopedMockLog log;
22// EXPECT_CALL(log, Log(_, _, "Some log message text"));
23// DoSomething(); // Causes "Some log message text" to be logged.
24// }
25//
26// If the function DoSomething() executes something like:
27//
28// LOG(INFO) << "Some log message text";
29//
30// then this will match the expectation.
31//
32// The first two parameters to ScopedMockLog::Log are the log severity and
33// filename. You can use them like this:
34//
35// TEST_F(MockLogTest, MockLogSeverityAndFileAndMessage) {
36// ScopedMockLog log;
37// EXPECT_CALL(log, Log(logging::LOG_INFO, "your_file.cc", "your message"));
38// DoSomething();
39// }
40//
41// You can also use gMock matchers for matching arguments to Log():
42//
43// TEST_F(MockLogTest, MatchWithGmockMatchers) {
44// ScopedMockLog log;
45// EXPECT_CALL(log, Log(::testing::Lt(::logging::LOG_ERROR),
46// ::testing::EndsWith(".cc"),
47// ::testing::StartsWith("Some")));
48// DoSomething();
49// }
50//
51// For some examples, see mock_log_unittest.cc.
52
53#include <string>
Gary Morain8a5726a2012-05-15 10:56:49 -070054#include <gmock/gmock.h>
55
Christopher Wileyd7783522012-08-10 14:21:47 -070056#include "shill/logging.h"
57
Gary Morain8a5726a2012-05-15 10:56:49 -070058namespace shill {
59
60class ScopedMockLog {
61 public:
62 ScopedMockLog();
Paul Stewartdd374ca2013-04-15 13:59:57 -070063 virtual ~ScopedMockLog();
Gary Morain8a5726a2012-05-15 10:56:49 -070064
65 // Users set expecations on this method. |severity| is defined in
66 // base/logging.h, like logging:::LOG_INFO. |file| is the filename which
67 // issues the log message, like "foo.cc". |user_messages| is the message you
68 // expect to see. Arguments can be ignored by specifying ::testing::_. You
69 // can also specify gMock matchers for arguments.
Paul Stewart1e006c62015-06-16 12:29:06 -070070 MOCK_METHOD3(Log, void(int severity, const char* file,
71 const std::string& user_message));
Gary Morain8a5726a2012-05-15 10:56:49 -070072
73 private:
74 // This function gets invoked by the logging subsystem for each message that
75 // is logged. It calls ScopedMockLog::Log() declared above. It must be a
76 // static method because the logging subsystem does not allow for an object to
77 // be passed. See the typedef LogMessageHandlerFunction in base/logging.h for
78 // this function signature.
79 static bool HandleLogMessages(int severity,
Paul Stewart1e006c62015-06-16 12:29:06 -070080 const char* file,
Gary Morain8a5726a2012-05-15 10:56:49 -070081 int line,
82 size_t message_start,
Paul Stewart1e006c62015-06-16 12:29:06 -070083 const std::string& full_message);
Gary Morain8a5726a2012-05-15 10:56:49 -070084
85 // A pointer to the current ScopedMockLog object.
Paul Stewart1e006c62015-06-16 12:29:06 -070086 static ScopedMockLog* instance_;
Gary Morain8a5726a2012-05-15 10:56:49 -070087
88 // A pointer to any pre-existing message hander function in the logging
89 // system. It is invoked after calling ScopedMockLog::Log().
90 ::logging::LogMessageHandlerFunction previous_handler_;
91};
92
Paul Stewartdd374ca2013-04-15 13:59:57 -070093// A NiceScopedMockLog is the same as ScopedMockLog, except it creates an
94// implicit expectation on any Log() call. This allows tests to avoid having
95// to explictly expect log messages they don't care about.
96class NiceScopedMockLog : public ScopedMockLog {
97 public:
98 NiceScopedMockLog();
Ben Chan5ea763b2014-08-13 11:07:54 -070099 ~NiceScopedMockLog() override;
Paul Stewartdd374ca2013-04-15 13:59:57 -0700100};
101
Gary Morain8a5726a2012-05-15 10:56:49 -0700102} // namespace shill
103
104#endif // SHILL_MOCK_LOG_H_