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