test_matchers.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_matchers.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This file declares Googletest's matchers used in the Abseil Logging library
  20. // unit tests.
  21. #ifndef Y_ABSL_LOG_INTERNAL_TEST_MATCHERS_H_
  22. #define Y_ABSL_LOG_INTERNAL_TEST_MATCHERS_H_
  23. #include <iosfwd>
  24. #include <sstream>
  25. #include <util/generic/string.h>
  26. #include "gmock/gmock.h"
  27. #include "gtest/gtest.h"
  28. #include "y_absl/base/config.h"
  29. #include "y_absl/base/log_severity.h"
  30. #include "y_absl/log/internal/test_helpers.h"
  31. #include "y_absl/log/log_entry.h"
  32. #include "y_absl/strings/string_view.h"
  33. #include "y_absl/time/time.h"
  34. namespace y_absl {
  35. Y_ABSL_NAMESPACE_BEGIN
  36. namespace log_internal {
  37. // In some configurations, Googletest's string matchers (e.g.
  38. // `::testing::EndsWith`) need help to match `y_absl::string_view`.
  39. ::testing::Matcher<y_absl::string_view> AsString(
  40. const ::testing::Matcher<const TString&>& str_matcher);
  41. // These matchers correspond to the components of `y_absl::LogEntry`.
  42. ::testing::Matcher<const y_absl::LogEntry&> SourceFilename(
  43. const ::testing::Matcher<y_absl::string_view>& source_filename);
  44. ::testing::Matcher<const y_absl::LogEntry&> SourceBasename(
  45. const ::testing::Matcher<y_absl::string_view>& source_basename);
  46. // Be careful with this one; multi-line statements using `__LINE__` evaluate
  47. // differently on different platforms. In particular, the MSVC implementation
  48. // of `EXPECT_DEATH` returns the line number of the macro expansion to all lines
  49. // within the code block that's expected to die.
  50. ::testing::Matcher<const y_absl::LogEntry&> SourceLine(
  51. const ::testing::Matcher<int>& source_line);
  52. ::testing::Matcher<const y_absl::LogEntry&> Prefix(
  53. const ::testing::Matcher<bool>& prefix);
  54. ::testing::Matcher<const y_absl::LogEntry&> LogSeverity(
  55. const ::testing::Matcher<y_absl::LogSeverity>& log_severity);
  56. ::testing::Matcher<const y_absl::LogEntry&> Timestamp(
  57. const ::testing::Matcher<y_absl::Time>& timestamp);
  58. // Matches if the `LogEntry`'s timestamp falls after the instantiation of this
  59. // matcher and before its execution, as is normal when used with EXPECT_CALL.
  60. ::testing::Matcher<y_absl::Time> InMatchWindow();
  61. ::testing::Matcher<const y_absl::LogEntry&> ThreadID(
  62. const ::testing::Matcher<y_absl::LogEntry::tid_t>&);
  63. ::testing::Matcher<const y_absl::LogEntry&> TextMessageWithPrefixAndNewline(
  64. const ::testing::Matcher<y_absl::string_view>&
  65. text_message_with_prefix_and_newline);
  66. ::testing::Matcher<const y_absl::LogEntry&> TextMessageWithPrefix(
  67. const ::testing::Matcher<y_absl::string_view>& text_message_with_prefix);
  68. ::testing::Matcher<const y_absl::LogEntry&> TextMessage(
  69. const ::testing::Matcher<y_absl::string_view>& text_message);
  70. ::testing::Matcher<const y_absl::LogEntry&> TextPrefix(
  71. const ::testing::Matcher<y_absl::string_view>& text_prefix);
  72. ::testing::Matcher<const y_absl::LogEntry&> Verbosity(
  73. const ::testing::Matcher<int>& verbosity);
  74. ::testing::Matcher<const y_absl::LogEntry&> Stacktrace(
  75. const ::testing::Matcher<y_absl::string_view>& stacktrace);
  76. // Behaves as `Eq(stream.str())`, but produces better failure messages.
  77. ::testing::Matcher<y_absl::string_view> MatchesOstream(
  78. const std::ostringstream& stream);
  79. ::testing::Matcher<const TString&> DeathTestValidateExpectations();
  80. ::testing::Matcher<const y_absl::LogEntry&> RawEncodedMessage(
  81. const ::testing::Matcher<y_absl::string_view>& raw_encoded_message);
  82. #define ENCODED_MESSAGE(message_matcher) ::testing::_
  83. } // namespace log_internal
  84. Y_ABSL_NAMESPACE_END
  85. } // namespace y_absl
  86. #endif // Y_ABSL_LOG_INTERNAL_TEST_MATCHERS_H_