1 //
2 // Copyright 2022 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
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.
LoggingDefaults(std::ios & str)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
TEST(StreamingFormatTest,LogAsLiteral)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,
55 Send(AllOf(TextMessage(MatchesOstream(stream)),
56 TextMessage(Eq("hello world")),
57 ENCODED_MESSAGE(HasValues(ElementsAre(
58 EqualsProto(R"pb(literal: "hello world")pb")))))));
59
60 sink.StartCapturingLogs();
61 LOG(INFO) << absl::LogAsLiteral(not_a_literal);
62 }
63
64 } // namespace
65