summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2016-09-08 00:58:00 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2016-09-08 00:58:00 +0000
commitaf6a185fb44a30a0988688ff48e8e0fb104bf6c5 (patch)
tree6d4e10294e920551615901a0ac07b6cc3d01b376 /base
parent44d7a87621cc75f4544ce77a1a6b1338d7378292 (diff)
parent13d78e440ae4cc5527bf7dde6e8ba4e4a7a8738c (diff)
downloadcore-af6a185fb44a30a0988688ff48e8e0fb104bf6c5.tar.gz
core-af6a185fb44a30a0988688ff48e8e0fb104bf6c5.tar.bz2
core-af6a185fb44a30a0988688ff48e8e0fb104bf6c5.zip
Merge "Clean up the libbase logging test."
Diffstat (limited to 'base')
-rw-r--r--base/logging_test.cpp272
1 files changed, 148 insertions, 124 deletions
diff --git a/base/logging_test.cpp b/base/logging_test.cpp
index 8ecd8fd68..6a321fce5 100644
--- a/base/logging_test.cpp
+++ b/base/logging_test.cpp
@@ -120,167 +120,191 @@ TEST(logging, CHECK) {
EXPECT_FALSE(flag) << "CHECK_STREQ probably has a dangling if with no else";
}
-std::string make_log_pattern(android::base::LogSeverity severity,
- const char* message) {
+static std::string make_log_pattern(android::base::LogSeverity severity,
+ const char* message) {
static const char* log_characters = "VDIWEF";
char log_char = log_characters[severity];
std::string holder(__FILE__);
return android::base::StringPrintf(
- "%c[[:space:]]+"
- "[[:digit:]]+-[[:digit:]]+[[:space:]]+"
- "[[:digit:]]+:[[:digit:]]+:[[:digit:]]+[[:space:]]+"
- "[[:digit:]]+[[:space:]]+"
- "[[:digit:]]+[[:space:]]+"
- "%s:[[:digit:]]+] %s",
+ "%c \\d+-\\d+ \\d+:\\d+:\\d+ \\s*\\d+ \\s*\\d+ %s:\\d+] %s",
log_char, basename(&holder[0]), message);
}
-TEST(logging, LOG) {
- ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
+#define CHECK_LOG_DISABLED(severity) \
+ android::base::ScopedLogSeverity sls1(android::base::FATAL); \
+ CapturedStderr cap1; \
+ LOG(severity) << "foo bar"; \
+ ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
+
+#define CHECK_LOG_ENABLED(severity) \
+ android::base::ScopedLogSeverity sls2(android::base::severity); \
+ CapturedStderr cap2; \
+ LOG(severity) << "foobar"; \
+ CheckMessage(cap2, android::base::severity, "foobar"); \
+
+static void CheckMessage(const CapturedStderr& cap,
+ android::base::LogSeverity severity, const char* expected) {
+ std::string output;
+ ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
+ android::base::ReadFdToString(cap.fd(), &output);
// We can't usefully check the output of any of these on Windows because we
// don't have std::regex, but we can at least make sure we printed at least as
// many characters are in the log message.
- {
- CapturedStderr cap;
- LOG(WARNING) << "foobar";
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
-
- std::string output;
- android::base::ReadFdToString(cap.fd(), &output);
- ASSERT_GT(output.length(), strlen("foobar"));
+ ASSERT_GT(output.length(), strlen(expected));
+ ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output;
#if !defined(_WIN32)
- std::regex message_regex(
- make_log_pattern(android::base::WARNING, "foobar"));
- ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
+ std::regex message_regex(make_log_pattern(severity, expected));
+ ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
#endif
- }
+}
- {
- CapturedStderr cap;
- LOG(INFO) << "foobar";
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
+TEST(logging, LOG_FATAL) {
+ ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
+}
- std::string output;
- android::base::ReadFdToString(cap.fd(), &output);
- ASSERT_GT(output.length(), strlen("foobar"));
+TEST(logging, LOG_ERROR_disabled) {
+ CHECK_LOG_DISABLED(ERROR);
+}
-#if !defined(_WIN32)
- std::regex message_regex(
- make_log_pattern(android::base::INFO, "foobar"));
- ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
-#endif
- }
+TEST(logging, LOG_ERROR_enabled) {
+ CHECK_LOG_ENABLED(ERROR);
+}
- {
- CapturedStderr cap;
- LOG(DEBUG) << "foobar";
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
+TEST(logging, LOG_WARNING_disabled) {
+ CHECK_LOG_DISABLED(WARNING);
+}
- std::string output;
- android::base::ReadFdToString(cap.fd(), &output);
- ASSERT_TRUE(output.empty());
- }
+TEST(logging, LOG_WARNING_enabled) {
+ CHECK_LOG_ENABLED(WARNING);
+}
- {
- android::base::ScopedLogSeverity severity(android::base::DEBUG);
- CapturedStderr cap;
- LOG(DEBUG) << "foobar";
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
+TEST(logging, LOG_INFO_disabled) {
+ CHECK_LOG_DISABLED(INFO);
+}
- std::string output;
- android::base::ReadFdToString(cap.fd(), &output);
- ASSERT_GT(output.length(), strlen("foobar"));
+TEST(logging, LOG_INFO_enabled) {
+ CHECK_LOG_ENABLED(INFO);
+}
-#if !defined(_WIN32)
- std::regex message_regex(
- make_log_pattern(android::base::DEBUG, "foobar"));
- ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
-#endif
- }
+TEST(logging, LOG_DEBUG_disabled) {
+ CHECK_LOG_DISABLED(DEBUG);
+}
- // Test whether LOG() saves and restores errno.
- {
- CapturedStderr cap;
- errno = 12345;
- LOG(INFO) << (errno = 67890);
- EXPECT_EQ(12345, errno) << "errno was not restored";
+TEST(logging, LOG_DEBUG_enabled) {
+ CHECK_LOG_ENABLED(DEBUG);
+}
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
+TEST(logging, LOG_VERBOSE_disabled) {
+ CHECK_LOG_DISABLED(VERBOSE);
+}
- std::string output;
- android::base::ReadFdToString(cap.fd(), &output);
- EXPECT_NE(nullptr, strstr(output.c_str(), "67890")) << output;
+TEST(logging, LOG_VERBOSE_enabled) {
+ CHECK_LOG_ENABLED(VERBOSE);
+}
-#if !defined(_WIN32)
- std::regex message_regex(
- make_log_pattern(android::base::INFO, "67890"));
- ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
-#endif
- }
+TEST(logging, LOG_does_not_clobber_errno) {
+ CapturedStderr cap;
+ errno = 12345;
+ LOG(INFO) << (errno = 67890);
+ EXPECT_EQ(12345, errno) << "errno was not restored";
- // Test whether LOG() has a dangling if with no else.
- {
- CapturedStderr cap;
+ CheckMessage(cap, android::base::INFO, "67890");
+}
- // Do the test two ways: once where we hypothesize that LOG()'s if
- // will evaluate to true (when severity is high enough) and once when we
- // expect it to evaluate to false (when severity is not high enough).
- bool flag = false;
- if (true)
- LOG(INFO) << "foobar";
- else
- flag = true;
+TEST(logging, PLOG_does_not_clobber_errno) {
+ CapturedStderr cap;
+ errno = 12345;
+ PLOG(INFO) << (errno = 67890);
+ EXPECT_EQ(12345, errno) << "errno was not restored";
- EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
+ CheckMessage(cap, android::base::INFO, "67890");
+}
- flag = false;
- if (true)
- LOG(VERBOSE) << "foobar";
- else
- flag = true;
+TEST(logging, LOG_does_not_have_dangling_if) {
+ CapturedStderr cap; // So the logging below has no side-effects.
- EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
- }
+ // Do the test two ways: once where we hypothesize that LOG()'s if
+ // will evaluate to true (when severity is high enough) and once when we
+ // expect it to evaluate to false (when severity is not high enough).
+ bool flag = false;
+ if (true)
+ LOG(INFO) << "foobar";
+ else
+ flag = true;
+
+ EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
+
+ flag = false;
+ if (true)
+ LOG(VERBOSE) << "foobar";
+ else
+ flag = true;
+
+ EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
}
-TEST(logging, PLOG) {
- {
- CapturedStderr cap;
- errno = ENOENT;
- PLOG(INFO) << "foobar";
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
+#define CHECK_PLOG(severity) \
- std::string output;
- android::base::ReadFdToString(cap.fd(), &output);
- ASSERT_GT(output.length(), strlen("foobar"));
+#define CHECK_PLOG_DISABLED(severity) \
+ android::base::ScopedLogSeverity sls1(android::base::FATAL); \
+ CapturedStderr cap1; \
+ PLOG(severity) << "foo bar"; \
+ ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
-#if !defined(_WIN32)
- std::regex message_regex(make_log_pattern(
- android::base::INFO, "foobar: No such file or directory"));
- ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
-#endif
- }
+#define CHECK_PLOG_ENABLED(severity) \
+ android::base::ScopedLogSeverity sls2(android::base::severity); \
+ CapturedStderr cap2; \
+ errno = ENOENT; \
+ PLOG(severity) << "foobar"; \
+ CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
+
+TEST(logging, PLOG_ERROR_disabled) {
+ CHECK_PLOG_DISABLED(ERROR);
}
-TEST(logging, UNIMPLEMENTED) {
- {
- CapturedStderr cap;
- errno = ENOENT;
- UNIMPLEMENTED(ERROR);
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
+TEST(logging, PLOG_ERROR_enabled) {
+ CHECK_PLOG_ENABLED(ERROR);
+}
- std::string output;
- android::base::ReadFdToString(cap.fd(), &output);
- ASSERT_GT(output.length(), strlen("unimplemented"));
+TEST(logging, PLOG_WARNING_disabled) {
+ CHECK_PLOG_DISABLED(WARNING);
+}
-#if !defined(_WIN32)
- std::string expected_message =
- android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
- std::regex message_regex(
- make_log_pattern(android::base::ERROR, expected_message.c_str()));
- ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
-#endif
- }
+TEST(logging, PLOG_WARNING_enabled) {
+ CHECK_PLOG_ENABLED(WARNING);
+}
+
+TEST(logging, PLOG_INFO_disabled) {
+ CHECK_PLOG_DISABLED(INFO);
+}
+
+TEST(logging, PLOG_INFO_enabled) {
+ CHECK_PLOG_ENABLED(INFO);
+}
+
+TEST(logging, PLOG_DEBUG_disabled) {
+ CHECK_PLOG_DISABLED(DEBUG);
+}
+
+TEST(logging, PLOG_DEBUG_enabled) {
+ CHECK_PLOG_ENABLED(DEBUG);
+}
+
+TEST(logging, PLOG_VERBOSE_disabled) {
+ CHECK_PLOG_DISABLED(VERBOSE);
+}
+
+TEST(logging, PLOG_VERBOSE_enabled) {
+ CHECK_PLOG_ENABLED(VERBOSE);
+}
+
+TEST(logging, UNIMPLEMENTED) {
+ std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
+
+ CapturedStderr cap;
+ errno = ENOENT;
+ UNIMPLEMENTED(ERROR);
+ CheckMessage(cap, android::base::ERROR, expected.c_str());
}