blob: da25e7606d77fe79df204c6a3796cc9004672511 [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>
54#include <base/logging.h>
55#include <gmock/gmock.h>
56
57namespace shill {
58
59class ScopedMockLog {
60 public:
61 ScopedMockLog();
62 ~ScopedMockLog();
63
64 // Users set expecations on this method. |severity| is defined in
65 // base/logging.h, like logging:::LOG_INFO. |file| is the filename which
66 // issues the log message, like "foo.cc". |user_messages| is the message you
67 // expect to see. Arguments can be ignored by specifying ::testing::_. You
68 // can also specify gMock matchers for arguments.
69 MOCK_METHOD3(Log, void(int severity, const char *file,
70 const std::string &user_message));
71
72 private:
73 // This function gets invoked by the logging subsystem for each message that
74 // is logged. It calls ScopedMockLog::Log() declared above. It must be a
75 // static method because the logging subsystem does not allow for an object to
76 // be passed. See the typedef LogMessageHandlerFunction in base/logging.h for
77 // this function signature.
78 static bool HandleLogMessages(int severity,
79 const char *file,
80 int line,
81 size_t message_start,
82 const std::string &full_message);
83
84 // A pointer to the current ScopedMockLog object.
85 static ScopedMockLog *instance_;
86
87 // A pointer to any pre-existing message hander function in the logging
88 // system. It is invoked after calling ScopedMockLog::Log().
89 ::logging::LogMessageHandlerFunction previous_handler_;
90};
91
92} // namespace shill
93
94#endif // SHILL_MOCK_LOG_H_