log_formatter_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <gtest/gtest.h>
  15. #include <log/log_formatter.h>
  16. #include <log/logger_level.h>
  17. #include <vector>
  18. #include <string>
  19. using namespace std;
  20. namespace {
  21. class FormatterTest : public ::testing::Test {
  22. protected:
  23. typedef pair<isc::log::Severity, string> Output;
  24. typedef isc::log::Formatter<FormatterTest> Formatter;
  25. vector<Output> outputs;
  26. public:
  27. void output(const isc::log::Severity& prefix, const string& message) {
  28. outputs.push_back(Output(prefix, message));
  29. }
  30. // Just shortcut for new string
  31. string* s(const char* text) {
  32. return (new string(text));
  33. }
  34. };
  35. // Create an inactive formatter and check it doesn't produce any output
  36. TEST_F(FormatterTest, inactive) {
  37. Formatter();
  38. EXPECT_EQ(0, outputs.size());
  39. }
  40. // Create an active formatter and check it produces output. Does no arg
  41. // substitution yet
  42. TEST_F(FormatterTest, active) {
  43. Formatter(isc::log::INFO, s("Text of message"), this);
  44. ASSERT_EQ(1, outputs.size());
  45. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  46. EXPECT_EQ("Text of message", outputs[0].second);
  47. }
  48. // No output even when we have an arg on the inactive formatter
  49. TEST_F(FormatterTest, inactiveArg) {
  50. Formatter().arg("Hello");
  51. EXPECT_EQ(0, outputs.size());
  52. }
  53. // Create an active formatter and replace a placeholder with string
  54. TEST_F(FormatterTest, stringArg) {
  55. {
  56. SCOPED_TRACE("C++ string");
  57. Formatter(isc::log::INFO, s("Hello %1"), this).arg(string("World"));
  58. ASSERT_EQ(1, outputs.size());
  59. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  60. EXPECT_EQ("Hello World", outputs[0].second);
  61. }
  62. {
  63. SCOPED_TRACE("C++ string");
  64. Formatter(isc::log::INFO, s("Hello %1"), this).arg(string("Internet"));
  65. ASSERT_EQ(2, outputs.size());
  66. EXPECT_EQ(isc::log::INFO, outputs[1].first);
  67. EXPECT_EQ("Hello Internet", outputs[1].second);
  68. }
  69. }
  70. // Can convert to string
  71. TEST_F(FormatterTest, intArg) {
  72. Formatter(isc::log::INFO, s("The answer is %1"), this).arg(42);
  73. ASSERT_EQ(1, outputs.size());
  74. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  75. EXPECT_EQ("The answer is 42", outputs[0].second);
  76. }
  77. // Can use multiple arguments at different places
  78. TEST_F(FormatterTest, multiArg) {
  79. Formatter(isc::log::INFO, s("The %2 are %1"), this).arg("switched").
  80. arg("arguments");
  81. ASSERT_EQ(1, outputs.size());
  82. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  83. EXPECT_EQ("The arguments are switched", outputs[0].second);
  84. }
  85. // Can survive and complains if placeholder is missing
  86. TEST_F(FormatterTest, missingPlace) {
  87. EXPECT_NO_THROW(Formatter(isc::log::INFO, s("Missing the first %2"), this).
  88. arg("missing").arg("argument"));
  89. ASSERT_EQ(1, outputs.size());
  90. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  91. EXPECT_EQ("Missing the first argument "
  92. "@@Missing placeholder %1 for 'missing'@@", outputs[0].second);
  93. }
  94. // Can replace multiple placeholders
  95. TEST_F(FormatterTest, multiPlaceholder) {
  96. Formatter(isc::log::INFO, s("The %1 is the %1"), this).
  97. arg("first rule of tautology club");
  98. ASSERT_EQ(1, outputs.size());
  99. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  100. EXPECT_EQ("The first rule of tautology club is "
  101. "the first rule of tautology club", outputs[0].second);
  102. }
  103. // Test we can cope with replacement containing the placeholder
  104. TEST_F(FormatterTest, noRecurse) {
  105. // If we recurse, this will probably eat all the memory and crash
  106. Formatter(isc::log::INFO, s("%1"), this).arg("%1 %1");
  107. ASSERT_EQ(1, outputs.size());
  108. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  109. EXPECT_EQ("%1 %1", outputs[0].second);
  110. }
  111. // Test it can accept exceptions (which don't have a default conversion
  112. // to string by themself)
  113. TEST_F(FormatterTest, exception) {
  114. class Ex : public std::exception {
  115. public:
  116. virtual const char* what() const throw() {
  117. return "Exception test";
  118. }
  119. };
  120. Formatter(isc::log::INFO, s("%1"), this).arg(Ex());
  121. ASSERT_EQ(1, outputs.size());
  122. EXPECT_EQ(isc::log::INFO, outputs[0].first);
  123. EXPECT_EQ("Exception test", outputs[0].second);
  124. }
  125. }