// // format_test.cxx // logger_test // // Created by Sam Jaffe on 4/4/19. // #include #include "logger/exception.h" #include "logger/format.h" #include "logger/logpacket.h" using namespace logging; TEST(FormatTest, EmptyFormatterCanParse) { EXPECT_NO_THROW(format::parse_format_string("")); } TEST(FormatTest, ThrowsForEndOfStringAfterPct) { EXPECT_THROW(format::parse_format_string("%"), format_parsing_exception); } TEST(FormatTest, RawStringFmtReturnsSelf) { using testing::Eq; auto fmt = format::parse_format_string("TEST STRING"); EXPECT_THAT(fmt.process({}), Eq("TEST STRING")); } TEST(FormatTest, NCharReturnsNewLine) { using testing::Eq; auto fmt = format::parse_format_string("%n"); EXPECT_THAT(fmt.process({}), Eq("\n")); } TEST(FormatTest, DoublePctIsLiteral) { using testing::Eq; auto fmt = format::parse_format_string("%%"); EXPECT_THAT(fmt.process({}), Eq("%")); } TEST(FormatTest, CatchesRawContentBeforeFmt) { using testing::Eq; auto fmt = format::parse_format_string("TEST%%"); EXPECT_THAT(fmt.process({}), Eq("TEST%")); } TEST(FormatTest, CatchesRawContentAfterFmt) { using testing::Eq; auto fmt = format::parse_format_string("%%TEST"); EXPECT_THAT(fmt.process({}), Eq("%TEST")); } // Thursday, April 4, 2019 6:17:20 PM GMT namespace { constexpr const int NOW = 1554401840; } TEST(FormatTest, HandlesDateFormatter) { using testing::Eq; auto fmt = format::parse_format_string("%d"); EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019-04-04 18:17:20,000")); } TEST(FormatTest, FormatsMilliseconds) { using testing::Eq; auto fmt = format::parse_format_string("%d"); EXPECT_THAT(fmt.process({{NOW,123000}}), Eq("2019-04-04 18:17:20,123")); } TEST(FormatTest, ThrowsIfCustomFmtUnterminated) { using testing::Eq; EXPECT_THROW(format::parse_format_string("%d{%"), format_parsing_exception); } TEST(FormatTest, SupportsCustomFormatWithBrace) { using testing::Eq; auto fmt = format::parse_format_string("%d{%Y}"); EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019")); } TEST(FormatTest, FormatsCustomMilliseconds) { using testing::Eq; auto fmt = format::parse_format_string("%d{%_ms}"); EXPECT_THAT(fmt.process({{NOW,123000}}), Eq("123")); } TEST(FormatTest, SupportsISO8601Format) { using testing::Eq; auto fmt = format::parse_format_string("%d{ISO8601}"); EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019-04-04T18:17:20.000Z")); } TEST(FormatTest, SupportsSingleDayFormat) { using testing::Eq; auto fmt = format::parse_format_string("%d{ABSOLUTE}"); EXPECT_THAT(fmt.process({{NOW,0}}), Eq("18:17:20,000")); } TEST(FormatTest, SupportsHumanDateFormat) { using testing::Eq; auto fmt = format::parse_format_string("%d{DATE}"); EXPECT_THAT(fmt.process({{NOW,0}}), Eq("04 Apr 2019 18:17:20,000")); } TEST(FormatTest, LoggerIdIsCToken) { using testing::Eq; auto fmt = format::parse_format_string("%c"); EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}), Eq("UNIT_TEST")); } TEST(FormatTest, LogLevelIsPToken) { using testing::Eq; auto fmt = format::parse_format_string("%p"); EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}), Eq("ERROR")); } TEST(FormatTest, LogMessageIsMToken) { using testing::Eq; auto fmt = format::parse_format_string("%m"); EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}), Eq("HELLO")); } TEST(FormatTest, ThrowsOnUnknownToken) { using testing::Eq; EXPECT_THROW(format::parse_format_string("%q"), unknown_format_specifier); } TEST(FormatTest, TokenCanBeTruncatedInFormat) { using testing::Eq; auto fmt = format::parse_format_string("%.3m"); EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}), Eq("HEL")); } TEST(FormatTest, TokenCanBeLeftPadded) { using testing::Eq; auto fmt = format::parse_format_string("%6m"); EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}), Eq(" HELLO")); } TEST(FormatTest, TokenCanBeRightPadded) { using testing::Eq; auto fmt = format::parse_format_string("%-6m"); EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}), Eq("HELLO ")); } TEST(FormatTest, TokenCanBeSizeBound) { using testing::Eq; auto fmt = format::parse_format_string("%6.8m"); EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO"}), Eq(" HELLO")); EXPECT_THAT(fmt.process({{}, level::error, {}, "UNIT_TEST", "HELLO FRIEND"}), Eq("HELLO FR")); }