format_test.cxx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // format_test.cxx
  3. // logger_test
  4. //
  5. // Created by Sam Jaffe on 4/4/19.
  6. //
  7. #include <gmock/gmock.h>
  8. #include "logger/exception.h"
  9. #include "logger/format.h"
  10. using namespace logging;
  11. TEST(FormatTest, EmptyFormatterCanParse) {
  12. EXPECT_NO_THROW(format::parse_format_string(""));
  13. }
  14. TEST(FormatTest, ThrowsForEndOfStringAfterPct) {
  15. EXPECT_THROW(format::parse_format_string("%"),
  16. format_parsing_exception);
  17. }
  18. TEST(FormatTest, RawStringFmtReturnsSelf) {
  19. using testing::Eq;
  20. auto fmt = format::parse_format_string("TEST STRING");
  21. EXPECT_THAT(fmt.process({}), Eq("TEST STRING"));
  22. }
  23. TEST(FormatTest, NCharReturnsNewLine) {
  24. using testing::Eq;
  25. auto fmt = format::parse_format_string("%n");
  26. EXPECT_THAT(fmt.process({}), Eq("\n"));
  27. }
  28. TEST(FormatTest, DoublePctIsLiteral) {
  29. using testing::Eq;
  30. auto fmt = format::parse_format_string("%%");
  31. EXPECT_THAT(fmt.process({}), Eq("%"));
  32. }
  33. TEST(FormatTest, CatchesRawContentBeforeFmt) {
  34. using testing::Eq;
  35. auto fmt = format::parse_format_string("TEST%%");
  36. EXPECT_THAT(fmt.process({}), Eq("TEST%"));
  37. }
  38. TEST(FormatTest, CatchesRawContentAfterFmt) {
  39. using testing::Eq;
  40. auto fmt = format::parse_format_string("%%TEST");
  41. EXPECT_THAT(fmt.process({}), Eq("%TEST"));
  42. }
  43. // Thursday, April 4, 2019 6:17:20 PM GMT
  44. constexpr const int NOW = 1554401840;
  45. TEST(FormatTest, HandlesDateFormatter) {
  46. using testing::Eq;
  47. auto fmt = format::parse_format_string("%d");
  48. EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019-04-04 18:17:20,000"));
  49. }
  50. TEST(FormatTest, FormatsMilliseconds) {
  51. using testing::Eq;
  52. auto fmt = format::parse_format_string("%d");
  53. EXPECT_THAT(fmt.process({{NOW,123000}}), Eq("2019-04-04 18:17:20,123"));
  54. }
  55. TEST(FormatTest, ThrowsIfCustomFmtUnterminated) {
  56. using testing::Eq;
  57. EXPECT_THROW(format::parse_format_string("%d{%"),
  58. format_parsing_exception);
  59. }
  60. TEST(FormatTest, SupportsCustomFormatWithBrace) {
  61. using testing::Eq;
  62. auto fmt = format::parse_format_string("%d{%Y}");
  63. EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019"));
  64. }
  65. TEST(FormatTest, FormatsCustomMilliseconds) {
  66. using testing::Eq;
  67. auto fmt = format::parse_format_string("%d{%_ms}");
  68. EXPECT_THAT(fmt.process({{NOW,123000}}), Eq("123"));
  69. }
  70. TEST(FormatTest, SupportsISO8601Format) {
  71. using testing::Eq;
  72. auto fmt = format::parse_format_string("%d{ISO8601}");
  73. EXPECT_THAT(fmt.process({{NOW,0}}), Eq("2019-04-04T18:17:20.000Z"));
  74. }