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