test_actions.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef ABSL_LOG_INTERNAL_TEST_ACTIONS_H_
  22. #define ABSL_LOG_INTERNAL_TEST_ACTIONS_H_
  23. #include <iostream>
  24. #include <ostream>
  25. #include <string>
  26. #include "absl/base/config.h"
  27. #include "absl/base/log_severity.h"
  28. #include "absl/log/log_entry.h"
  29. #include "absl/strings/string_view.h"
  30. namespace absl {
  31. ABSL_NAMESPACE_BEGIN
  32. namespace log_internal {
  33. // These actions are used by the child process in a death test.
  34. //
  35. // Expectations set in the child cannot cause test failure in the parent
  36. // directly. Instead, the child can use these actions with
  37. // `EXPECT_CALL`/`WillOnce` and `ON_CALL`/`WillByDefault` (for unexpected calls)
  38. // to write messages to stderr that the parent can match against.
  39. struct WriteToStderr final {
  40. explicit WriteToStderr(absl::string_view m) : message(m) {}
  41. std::string message;
  42. template <typename... Args>
  43. void operator()(const Args&...) const {
  44. std::cerr << message << std::endl;
  45. }
  46. };
  47. struct WriteToStderrWithFilename final {
  48. explicit WriteToStderrWithFilename(absl::string_view m) : message(m) {}
  49. std::string message;
  50. void operator()(const absl::LogEntry& entry) const;
  51. };
  52. struct WriteEntryToStderr final {
  53. explicit WriteEntryToStderr(absl::string_view m) : message(m) {}
  54. std::string message = "";
  55. void operator()(const absl::LogEntry& entry) const;
  56. void operator()(absl::LogSeverity, absl::string_view,
  57. absl::string_view) const;
  58. };
  59. // See the documentation for `DeathTestValidateExpectations` above.
  60. // `DeathTestExpectedLogging` should be used once in a given death test, and the
  61. // applicable severity level is the one that should be passed to
  62. // `DeathTestValidateExpectations`.
  63. inline WriteEntryToStderr DeathTestExpectedLogging() {
  64. return WriteEntryToStderr{"Mock received expected entry:"};
  65. }
  66. // `DeathTestUnexpectedLogging` should be used zero or more times to mark
  67. // messages that should not hit the logs as the process dies.
  68. inline WriteEntryToStderr DeathTestUnexpectedLogging() {
  69. return WriteEntryToStderr{"Mock received unexpected entry:"};
  70. }
  71. } // namespace log_internal
  72. ABSL_NAMESPACE_END
  73. } // namespace absl
  74. #endif // ABSL_LOG_INTERNAL_TEST_ACTIONS_H_