mock-log_unittest.cc
Go to the documentation of this file.
1 // Copyright (c) 2022, Google Inc.
2 // 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 
41 namespace {
42 
46 using GOOGLE_NAMESPACE::glog_testing::ScopedMockLog;
47 using std::string;
48 using testing::_;
49 using testing::EndsWith;
50 using testing::InSequence;
52 
53 // Tests that ScopedMockLog intercepts LOG()s when it's alive.
54 TEST(ScopedMockLogTest, InterceptsLog) {
55  ScopedMockLog log;
56 
57  InSequence s;
58  EXPECT_CALL(log,
59  Log(GLOG_WARNING, EndsWith("mock-log_unittest.cc"), "Fishy."));
60  EXPECT_CALL(log, Log(GLOG_INFO, _, "Working..."))
61  .Times(2);
62  EXPECT_CALL(log, Log(GLOG_ERROR, _, "Bad!!"));
63 
64  LOG(WARNING) << "Fishy.";
65  LOG(INFO) << "Working...";
66  LOG(INFO) << "Working...";
67  LOG(ERROR) << "Bad!!";
68 }
69 
70 void LogBranch() {
71  LOG(INFO) << "Logging a branch...";
72 }
73 
74 void LogTree() {
75  LOG(INFO) << "Logging the whole tree...";
76 }
77 
78 void 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.
87 TEST(ScopedMockLogTest, LogDuringIntercept) {
88  ScopedMockLog log;
89  InSequence s;
90  EXPECT_CALL(log, Log(GLOG_INFO, __FILE__, "Logging a branch..."))
91  .WillOnce(InvokeWithoutArgs(LogTree));
92  EXPECT_CALL(log, Log(GLOG_INFO, __FILE__, "Logging the whole tree..."))
93  .WillOnce(InvokeWithoutArgs(LogForest));
94  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..."));
97  LogBranch();
98 }
99 
100 } // namespace
101 
102 int main(int argc, char **argv) {
104  testing::InitGoogleTest(&argc, argv);
105  testing::InitGoogleMock(&argc, argv);
106 
107  return RUN_ALL_TESTS();
108 }
testing::InitGoogleMock
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
Definition: gmock.cc:191
INFO
const int INFO
Definition: log_severity.h:59
ERROR
const int ERROR
Definition: log_severity.h:60
testing::internal::Log
GTEST_API_ void Log(LogSeverity severity, const std::string &message, int stack_frames_to_skip)
Definition: gmock-internal-utils.cc:149
gtest.h
s
XmlRpcServer s
GLOG_INFO
const int GLOG_INFO
Definition: log_severity.h:53
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
testing::InvokeWithoutArgs
internal::InvokeWithoutArgsAction< typename std::decay< FunctionImpl >::type > InvokeWithoutArgs(FunctionImpl function_impl)
Definition: gmock-actions.h:1160
gmock.h
WARNING
const int WARNING
Definition: log_severity.h:59
gmock_output_test._
_
Definition: gmock_output_test.py:173
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2502
GLOG_WARNING
const int GLOG_WARNING
Definition: log_severity.h:53
TEST
#define TEST(a, b)
Definition: glog/src/googletest.h:194
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6005
LOG
#define LOG(x)
Definition: sdk/include/aditof/log.h:72
google::protobuf::strings::EndsWith
bool EndsWith(StringPiece text, StringPiece suffix)
Definition: strutil.h:927
EXPECT_CALL
#define EXPECT_CALL(obj, call)
GLOG_ERROR
const int GLOG_ERROR
Definition: log_severity.h:53
mock-log.h
InitGoogleLogging
void InitGoogleLogging(const char *argv0)
Definition: logging.cc:2618
main
int main(int argc, char **argv)
Definition: mock-log_unittest.cc:102


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57