1 // Copyright 2017 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 // This test serves primarily as a compilation test for base/raw_logging.h.
16 // Raw logging testing is covered by logging_unittest.cc, which is not as
17 // portable as this test.
18
19 #include "absl/base/internal/raw_logging.h"
20
21 #include <tuple>
22
23 #include "gtest/gtest.h"
24 #include "absl/strings/str_cat.h"
25
26 namespace {
27
TEST(RawLoggingCompilationTest,Log)28 TEST(RawLoggingCompilationTest, Log) {
29 ABSL_RAW_LOG(INFO, "RAW INFO: %d", 1);
30 ABSL_RAW_LOG(INFO, "RAW INFO: %d %d", 1, 2);
31 ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d", 1, 2, 3);
32 ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d %d", 1, 2, 3, 4);
33 ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d %d %d", 1, 2, 3, 4, 5);
34 ABSL_RAW_LOG(WARNING, "RAW WARNING: %d", 1);
35 ABSL_RAW_LOG(ERROR, "RAW ERROR: %d", 1);
36 }
37
TEST(RawLoggingCompilationTest,PassingCheck)38 TEST(RawLoggingCompilationTest, PassingCheck) {
39 ABSL_RAW_CHECK(true, "RAW CHECK");
40 }
41
42 // Not all platforms support output from raw log, so we don't verify any
43 // particular output for RAW check failures (expecting the empty string
44 // accomplishes this). This test is primarily a compilation test, but we
45 // are verifying process death when EXPECT_DEATH works for a platform.
46 const char kExpectedDeathOutput[] = "";
47
TEST(RawLoggingDeathTest,FailingCheck)48 TEST(RawLoggingDeathTest, FailingCheck) {
49 EXPECT_DEATH_IF_SUPPORTED(ABSL_RAW_CHECK(1 == 0, "explanation"),
50 kExpectedDeathOutput);
51 }
52
TEST(RawLoggingDeathTest,LogFatal)53 TEST(RawLoggingDeathTest, LogFatal) {
54 EXPECT_DEATH_IF_SUPPORTED(ABSL_RAW_LOG(FATAL, "my dog has fleas"),
55 kExpectedDeathOutput);
56 }
57
TEST(InternalLog,CompilationTest)58 TEST(InternalLog, CompilationTest) {
59 ABSL_INTERNAL_LOG(INFO, "Internal Log");
60 std::string log_msg = "Internal Log";
61 ABSL_INTERNAL_LOG(INFO, log_msg);
62
63 ABSL_INTERNAL_LOG(INFO, log_msg + " 2");
64
65 float d = 1.1f;
66 ABSL_INTERNAL_LOG(INFO, absl::StrCat("Internal log ", 3, " + ", d));
67 }
68
TEST(InternalLogDeathTest,FailingCheck)69 TEST(InternalLogDeathTest, FailingCheck) {
70 EXPECT_DEATH_IF_SUPPORTED(ABSL_INTERNAL_CHECK(1 == 0, "explanation"),
71 kExpectedDeathOutput);
72 }
73
TEST(InternalLogDeathTest,LogFatal)74 TEST(InternalLogDeathTest, LogFatal) {
75 EXPECT_DEATH_IF_SUPPORTED(ABSL_INTERNAL_LOG(FATAL, "my dog has fleas"),
76 kExpectedDeathOutput);
77 }
78
79 } // namespace
80