blob: 2f90c5ecf17093b489c2786fe21be0975d7fc65b [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();
63 ~ScopedMockLog();
64
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.
70 MOCK_METHOD3(Log, void(int severity, const char *file,
71 const std::string &user_message));
72
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,
80 const char *file,
81 int line,
82 size_t message_start,
83 const std::string &full_message);
84
85 // A pointer to the current ScopedMockLog object.
86 static ScopedMockLog *instance_;
87
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
93} // namespace shill
94
95#endif // SHILL_MOCK_LOG_H_