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::Eq;
34
35 auto *test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
36 new absl::log_internal::LogTestEnvironment);
37
38 // Abseil Logging library uses these by default, so we set them on the
39 // `std::ostream` we compare against too.
LoggingDefaults(std::ios & str)40 std::ios &LoggingDefaults(std::ios &str) {
41 str.setf(std::ios_base::showbase | std::ios_base::boolalpha |
42 std::ios_base::internal);
43 return str;
44 }
45
TEST(StreamingFormatTest,LogAsLiteral)46 TEST(StreamingFormatTest, LogAsLiteral) {
47 std::ostringstream stream;
48 const std::string not_a_literal("hello world");
49 stream << LoggingDefaults << absl::LogAsLiteral(not_a_literal);
50
51 absl::ScopedMockLog sink;
52
53 EXPECT_CALL(sink,
54 Send(AllOf(TextMessage(MatchesOstream(stream)),
55 TextMessage(Eq("hello world")),
56 ENCODED_MESSAGE(EqualsProto(
57 R"pb(value { literal: "hello world" })pb")))));
58
59 sink.StartCapturingLogs();
60 LOG(INFO) << absl::LogAsLiteral(not_a_literal);
61 }
62
63 } // namespace
64