Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2007, 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 | |
| 43 | using GOOGLE_NAMESPACE::INFO; |
| 44 | using GOOGLE_NAMESPACE::WARNING; |
| 45 | using GOOGLE_NAMESPACE::ERROR; |
| 46 | using GOOGLE_NAMESPACE::glog_testing::ScopedMockLog; |
| 47 | using std::string; |
| 48 | using testing::_; |
| 49 | using testing::HasSubstr; |
| 50 | using testing::InSequence; |
| 51 | using testing::InvokeWithoutArgs; |
| 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, Log(WARNING, HasSubstr("/mock-log_test.cc"), "Fishy.")); |
| 59 | EXPECT_CALL(log, Log(INFO, _, "Working...")) |
| 60 | .Times(2); |
| 61 | EXPECT_CALL(log, Log(ERROR, _, "Bad!!")); |
| 62 | |
| 63 | LOG(WARNING) << "Fishy."; |
| 64 | LOG(INFO) << "Working..."; |
| 65 | LOG(INFO) << "Working..."; |
| 66 | LOG(ERROR) << "Bad!!"; |
| 67 | } |
| 68 | |
| 69 | void LogBranch() { |
| 70 | LOG(INFO) << "Logging a branch..."; |
| 71 | } |
| 72 | |
| 73 | void LogTree() { |
| 74 | LOG(INFO) << "Logging the whole tree..."; |
| 75 | } |
| 76 | |
| 77 | void LogForest() { |
| 78 | LOG(INFO) << "Logging the entire forest."; |
| 79 | LOG(INFO) << "Logging the entire forest.."; |
| 80 | LOG(INFO) << "Logging the entire forest..."; |
| 81 | } |
| 82 | |
| 83 | // The purpose of the following test is to verify that intercepting logging |
| 84 | // continues to work properly if a LOG statement is executed within the scope |
| 85 | // of a mocked call. |
| 86 | TEST(ScopedMockLogTest, LogDuringIntercept) { |
| 87 | ScopedMockLog log; |
| 88 | InSequence s; |
| 89 | EXPECT_CALL(log, Log(INFO, __FILE__, "Logging a branch...")) |
| 90 | .WillOnce(InvokeWithoutArgs(LogTree)); |
| 91 | EXPECT_CALL(log, Log(INFO, __FILE__, "Logging the whole tree...")) |
| 92 | .WillOnce(InvokeWithoutArgs(LogForest)); |
| 93 | EXPECT_CALL(log, Log(INFO, __FILE__, "Logging the entire forest.")); |
| 94 | EXPECT_CALL(log, Log(INFO, __FILE__, "Logging the entire forest..")); |
| 95 | EXPECT_CALL(log, Log(INFO, __FILE__, "Logging the entire forest...")); |
| 96 | LogBranch(); |
| 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | |
| 101 | int main(int argc, char **argv) { |
| 102 | GOOGLE_NAMESPACE::InitGoogleLogging(argv[0]); |
| 103 | testing::InitGoogleMock(&argc, argv); |
| 104 | |
| 105 | return RUN_ALL_TESTS(); |
| 106 | } |