| 6 |
| // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 |
| 16 | #include "absl/log/structured.h" |
| 17 |
| 18 | #include <ios> |
| 19 | #include <sstream> |
| 20 | #include <string> |
| 21 |
| 22 | #include "gmock/gmock.h" |
| 23 | #include "gtest/gtest.h" |
| 24 | #include "absl/base/attributes.h" |
| 25 | #include "absl/log/internal/test_helpers.h" |
| 26 | #include "absl/log/internal/test_matchers.h" |
| 27 | #include "absl/log/log.h" |
| 28 | #include "absl/log/scoped_mock_log.h" |
| 29 |
| 30 | namespace { |
| 31 | using ::absl::log_internal::MatchesOstream; |
| 32 | using ::absl::log_internal::TextMessage; |
| 33 | using ::testing::ElementsAre; |
| 34 | using ::testing::Eq; |
| 35 |
| 36 | auto *test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment( |
| 37 | new absl::log_internal::LogTestEnvironment); |
| 38 |
| 39 | // Abseil Logging library uses these by default, so we set them on the |
| 40 | // `std::ostream` we compare against too. |
| 41 | std::ios &LoggingDefaults(std::ios &str) { |
| 42 | str.setf(std::ios_base::showbase | std::ios_base::boolalpha | |
| 43 | std::ios_base::internal); |
| 44 | return str; |
| 45 | } |
| 46 |
| 47 | TEST(StreamingFormatTest, LogAsLiteral) { |
| 48 | std::ostringstream stream; |
| 49 | const std::string not_a_literal("hello world"); |
| 50 | stream << LoggingDefaults << absl::LogAsLiteral(not_a_literal); |
| 51 |
| 52 | absl::ScopedMockLog sink; |
| 53 |
| 54 | EXPECT_CALL(sink, Send(AllOf(TextMessage(MatchesOstream(stream)), |
| 55 | TextMessage(Eq("hello world")), |
| 56 | ENCODED_MESSAGE(HasValues(ElementsAre( |
| 57 | ValueWithLiteral(Eq("hello world")))))))); |
| 58 |
| 59 | sink.StartCapturingLogs(); |
| 60 | LOG(INFO) << absl::LogAsLiteral(not_a_literal); |
| 61 | } |
| 62 |
| 63 | } // namespace |
| 64 |