blob: b9bef4ea6f5edfded5461f98c372773fd7e59611 [file] [log] [blame]
James Kuszmaulba0ac1a2022-08-12 16:29:30 -07001// Copyright (c) 2022, Google Inc.
Austin Schuh906616c2019-01-21 20:25:11 -08002// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: Zhanyong Wan
31
32// Tests the ScopedMockLog class.
33
34#include "mock-log.h"
35
36#include <string>
37
38#include <gmock/gmock.h>
39#include <gtest/gtest.h>
40
41namespace {
42
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070043using GOOGLE_NAMESPACE::GLOG_ERROR;
44using GOOGLE_NAMESPACE::GLOG_INFO;
45using GOOGLE_NAMESPACE::GLOG_WARNING;
Austin Schuh906616c2019-01-21 20:25:11 -080046using GOOGLE_NAMESPACE::glog_testing::ScopedMockLog;
47using std::string;
48using testing::_;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070049using testing::EndsWith;
Austin Schuh906616c2019-01-21 20:25:11 -080050using testing::InSequence;
51using testing::InvokeWithoutArgs;
52
53// Tests that ScopedMockLog intercepts LOG()s when it's alive.
54TEST(ScopedMockLogTest, InterceptsLog) {
55 ScopedMockLog log;
56
57 InSequence s;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070058 EXPECT_CALL(log,
59 Log(GLOG_WARNING, EndsWith("mock-log_unittest.cc"), "Fishy."));
60 EXPECT_CALL(log, Log(GLOG_INFO, _, "Working..."))
Austin Schuh906616c2019-01-21 20:25:11 -080061 .Times(2);
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070062 EXPECT_CALL(log, Log(GLOG_ERROR, _, "Bad!!"));
Austin Schuh906616c2019-01-21 20:25:11 -080063
64 LOG(WARNING) << "Fishy.";
65 LOG(INFO) << "Working...";
66 LOG(INFO) << "Working...";
67 LOG(ERROR) << "Bad!!";
68}
69
70void LogBranch() {
71 LOG(INFO) << "Logging a branch...";
72}
73
74void LogTree() {
75 LOG(INFO) << "Logging the whole tree...";
76}
77
78void LogForest() {
79 LOG(INFO) << "Logging the entire forest.";
80 LOG(INFO) << "Logging the entire forest..";
81 LOG(INFO) << "Logging the entire forest...";
82}
83
84// The purpose of the following test is to verify that intercepting logging
85// continues to work properly if a LOG statement is executed within the scope
86// of a mocked call.
87TEST(ScopedMockLogTest, LogDuringIntercept) {
88 ScopedMockLog log;
89 InSequence s;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070090 EXPECT_CALL(log, Log(GLOG_INFO, __FILE__, "Logging a branch..."))
Austin Schuh906616c2019-01-21 20:25:11 -080091 .WillOnce(InvokeWithoutArgs(LogTree));
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070092 EXPECT_CALL(log, Log(GLOG_INFO, __FILE__, "Logging the whole tree..."))
Austin Schuh906616c2019-01-21 20:25:11 -080093 .WillOnce(InvokeWithoutArgs(LogForest));
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070094 EXPECT_CALL(log, Log(GLOG_INFO, __FILE__, "Logging the entire forest."));
95 EXPECT_CALL(log, Log(GLOG_INFO, __FILE__, "Logging the entire forest.."));
96 EXPECT_CALL(log, Log(GLOG_INFO, __FILE__, "Logging the entire forest..."));
Austin Schuh906616c2019-01-21 20:25:11 -080097 LogBranch();
98}
99
100} // namespace
101
102int main(int argc, char **argv) {
103 GOOGLE_NAMESPACE::InitGoogleLogging(argv[0]);
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700104 testing::InitGoogleTest(&argc, argv);
Austin Schuh906616c2019-01-21 20:25:11 -0800105 testing::InitGoogleMock(&argc, argv);
106
107 return RUN_ALL_TESTS();
108}