1 // Copyright 2022 The Abseil Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // ----------------------------------------------------------------------------- 16 // File: log/internal/test_actions.h 17 // ----------------------------------------------------------------------------- 18 // 19 // This file declares Googletest's actions used in the Abseil Logging library 20 // unit tests. 21 22 #ifndef ABSL_LOG_INTERNAL_TEST_ACTIONS_H_ 23 #define ABSL_LOG_INTERNAL_TEST_ACTIONS_H_ 24 25 #include <iostream> 26 #include <ostream> 27 #include <string> 28 29 #include "absl/base/config.h" 30 #include "absl/base/log_severity.h" 31 #include "absl/log/log_entry.h" 32 #include "absl/strings/string_view.h" 33 34 namespace absl { 35 ABSL_NAMESPACE_BEGIN 36 namespace log_internal { 37 38 // These actions are used by the child process in a death test. 39 // 40 // Expectations set in the child cannot cause test failure in the parent 41 // directly. Instead, the child can use these actions with 42 // `EXPECT_CALL`/`WillOnce` and `ON_CALL`/`WillByDefault` (for unexpected calls) 43 // to write messages to stderr that the parent can match against. 44 struct WriteToStderr final { WriteToStderrfinal45 explicit WriteToStderr(absl::string_view m) : message(m) {} 46 std::string message; 47 48 template <typename... Args> operatorfinal49 void operator()(const Args&...) const { 50 std::cerr << message << std::endl; 51 } 52 }; 53 54 struct WriteToStderrWithFilename final { WriteToStderrWithFilenamefinal55 explicit WriteToStderrWithFilename(absl::string_view m) : message(m) {} 56 57 std::string message; 58 59 void operator()(const absl::LogEntry& entry) const; 60 }; 61 62 struct WriteEntryToStderr final { WriteEntryToStderrfinal63 explicit WriteEntryToStderr(absl::string_view m) : message(m) {} 64 65 std::string message = ""; 66 67 void operator()(const absl::LogEntry& entry) const; 68 void operator()(absl::LogSeverity, absl::string_view, 69 absl::string_view) const; 70 }; 71 72 // See the documentation for `DeathTestValidateExpectations` above. 73 // `DeathTestExpectedLogging` should be used once in a given death test, and the 74 // applicable severity level is the one that should be passed to 75 // `DeathTestValidateExpectations`. DeathTestExpectedLogging()76inline WriteEntryToStderr DeathTestExpectedLogging() { 77 return WriteEntryToStderr{"Mock received expected entry:"}; 78 } 79 80 // `DeathTestUnexpectedLogging` should be used zero or more times to mark 81 // messages that should not hit the logs as the process dies. DeathTestUnexpectedLogging()82inline WriteEntryToStderr DeathTestUnexpectedLogging() { 83 return WriteEntryToStderr{"Mock received unexpected entry:"}; 84 } 85 86 } // namespace log_internal 87 ABSL_NAMESPACE_END 88 } // namespace absl 89 90 #endif // ABSL_LOG_INTERNAL_TEST_ACTIONS_H_ 91