Commit 317a3e0d authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

logging: update the tests to allow alternative type name demangling

Summary:
Several of the tests in LoggerTest.cpp check error messages that include
demangled type names.  The type names are returned slightly differently on
other platforms (e.g., `i` instead of `int`, or `A13_c` instead of
`char [13]`)

This changes the tests to perform regular expression matches rather than exact
string matches.  This now allows any strings to appear where a type name is
expected.

Reviewed By: yfeldblum

Differential Revision: D6805680

fbshipit-source-id: 56d501944dacb505c3c09d84c555a1f686065077
parent df8c294a
...@@ -13,16 +13,18 @@ ...@@ -13,16 +13,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <folly/experimental/logging/Logger.h>
#include <folly/experimental/logging/LogCategory.h> #include <folly/experimental/logging/LogCategory.h>
#include <folly/experimental/logging/LogHandler.h> #include <folly/experimental/logging/LogHandler.h>
#include <folly/experimental/logging/LogMessage.h> #include <folly/experimental/logging/LogMessage.h>
#include <folly/experimental/logging/Logger.h>
#include <folly/experimental/logging/LoggerDB.h> #include <folly/experimental/logging/LoggerDB.h>
#include <folly/experimental/logging/test/TestLogHandler.h> #include <folly/experimental/logging/test/TestLogHandler.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
using namespace folly; using namespace folly;
using std::make_shared; using std::make_shared;
using testing::MatchesRegex;
class LoggerTest : public ::testing::Test { class LoggerTest : public ::testing::Test {
protected: protected:
...@@ -104,12 +106,15 @@ TEST_F(LoggerTest, follyFormatError) { ...@@ -104,12 +106,15 @@ TEST_F(LoggerTest, follyFormatError) {
auto& messages = handler_->getMessages(); auto& messages = handler_->getMessages();
ASSERT_EQ(1, messages.size()); ASSERT_EQ(1, messages.size());
EXPECT_EQ( // Use a regex match here, since the type IDs are reported slightly
"error formatting log message: " // differently on different platforms.
"invalid format argument {:6.3f}: invalid specifier 'f'; " EXPECT_THAT(
"format string: \"param1: {:06d}, param2: {:6.3f}\", " messages[0].first.getMessage(),
"arguments: (int: 1234), (char [13]: hello world!)", MatchesRegex(
messages[0].first.getMessage()); R"(error formatting log message: )"
R"(invalid format argument \{:6.3f\}: invalid specifier 'f'; )"
R"(format string: "param1: \{:06d\}, param2: \{:6.3f\}", )"
R"(arguments: \((.*: )?1234\), \((.*: )?hello world\!\))"));
EXPECT_EQ("LoggerTest.cpp", pathBasename(messages[0].first.getFileName())); EXPECT_EQ("LoggerTest.cpp", pathBasename(messages[0].first.getFileName()));
EXPECT_EQ(LogLevel::WARN, messages[0].first.getLevel()); EXPECT_EQ(LogLevel::WARN, messages[0].first.getLevel());
EXPECT_FALSE(messages[0].first.containsNewlines()); EXPECT_FALSE(messages[0].first.containsNewlines());
...@@ -198,12 +203,14 @@ TEST_F(LoggerTest, formatFallbackError) { ...@@ -198,12 +203,14 @@ TEST_F(LoggerTest, formatFallbackError) {
auto& messages = handler_->getMessages(); auto& messages = handler_->getMessages();
ASSERT_EQ(1, messages.size()); ASSERT_EQ(1, messages.size());
EXPECT_EQ( EXPECT_THAT(
"error formatting log message: " messages[0].first.getMessage(),
"invalid format argument {}: argument index out of range, max=2; " MatchesRegex(
"format string: \"param1: {}, param2: {}, {}\", " R"(error formatting log message: invalid format argument \{\}: )"
"arguments: (int: 1234), (ToStringFailure: <error_converting_to_string>)", R"(argument index out of range, max=2; )"
messages[0].first.getMessage()); R"(format string: "param1: \{\}, param2: \{\}, \{\}", )"
R"(arguments: \((.*: )?1234\), )"
R"(\((.*ToStringFailure.*: )?<error_converting_to_string>\))"));
EXPECT_EQ("LoggerTest.cpp", pathBasename(messages[0].first.getFileName())); EXPECT_EQ("LoggerTest.cpp", pathBasename(messages[0].first.getFileName()));
EXPECT_EQ(LogLevel::WARN, messages[0].first.getLevel()); EXPECT_EQ(LogLevel::WARN, messages[0].first.getLevel());
EXPECT_FALSE(messages[0].first.containsNewlines()); EXPECT_FALSE(messages[0].first.containsNewlines());
...@@ -218,13 +225,13 @@ TEST_F(LoggerTest, formatFallbackUnsupported) { ...@@ -218,13 +225,13 @@ TEST_F(LoggerTest, formatFallbackUnsupported) {
auto& messages = handler_->getMessages(); auto& messages = handler_->getMessages();
ASSERT_EQ(1, messages.size()); ASSERT_EQ(1, messages.size());
EXPECT_EQ( EXPECT_THAT(
"error formatting log message: " messages[0].first.getMessage(),
"test; " MatchesRegex(
"format string: \"param1: {}, param2: {}\", " R"(error formatting log message: test; )"
"arguments: (int: 1234), " R"(format string: "param1: \{\}, param2: \{\}", )"
"(FormattableButNoToString: <no_string_conversion>)", R"(arguments: \((.*: )?1234\), )"
messages[0].first.getMessage()); R"(\((.*FormattableButNoToString.*: )?<no_string_conversion>\))"));
EXPECT_EQ("LoggerTest.cpp", pathBasename(messages[0].first.getFileName())); EXPECT_EQ("LoggerTest.cpp", pathBasename(messages[0].first.getFileName()));
EXPECT_EQ(LogLevel::WARN, messages[0].first.getLevel()); EXPECT_EQ(LogLevel::WARN, messages[0].first.getLevel());
EXPECT_FALSE(messages[0].first.containsNewlines()); EXPECT_FALSE(messages[0].first.containsNewlines());
...@@ -334,11 +341,11 @@ TEST_F(LoggerTest, logMacros) { ...@@ -334,11 +341,11 @@ TEST_F(LoggerTest, logMacros) {
// Bad format arguments should not throw // Bad format arguments should not throw
FB_LOGF(footest1234, ERR, "whoops: {}, {}", getValue()); FB_LOGF(footest1234, ERR, "whoops: {}, {}", getValue());
ASSERT_EQ(1, messages.size()); ASSERT_EQ(1, messages.size());
EXPECT_EQ( EXPECT_THAT(
"error formatting log message: " messages[0].first.getMessage(),
"invalid format argument {}: argument index out of range, max=1; " MatchesRegex(
"format string: \"whoops: {}, {}\", " R"(error formatting log message: invalid format argument \{\}: )"
"arguments: (int: 5)", R"(argument index out of range, max=1; )"
messages[0].first.getMessage()); R"(format string: "whoops: \{\}, \{\}", arguments: \((.*: )?5\))"));
messages.clear(); messages.clear();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment