LogLevelTest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2021-05-02
  6. * Changed: 2023-02-22
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std/ls-std-core.hpp>
  11. #include <ls-std/ls-std-io.hpp>
  12. using namespace ls::std::core;
  13. using namespace ls::std::io;
  14. using namespace ::testing;
  15. namespace
  16. {
  17. class LogLevelTest : public Test
  18. {
  19. protected:
  20. LogLevelTest() = default;
  21. ~LogLevelTest() override = default;
  22. void SetUp() override
  23. {}
  24. void TearDown() override
  25. {}
  26. };
  27. TEST_F(LogLevelTest, constructor_with_log_level_value_parameter)
  28. {
  29. LogLevel logLevel{LogLevelValue::INFO};
  30. ASSERT_EQ(LogLevelValue::INFO, logLevel);
  31. }
  32. TEST_F(LogLevelTest, constructor_default)
  33. {
  34. LogLevel logLevel{};
  35. ASSERT_EQ(LogLevelValue::FATAL, logLevel);
  36. }
  37. TEST_F(LogLevelTest, operator_assign)
  38. {
  39. LogLevel logLevel{LogLevelValue::DEBUG};
  40. logLevel = LogLevelValue::INFO;
  41. ASSERT_EQ(LogLevelValue::INFO, logLevel);
  42. }
  43. TEST_F(LogLevelTest, operator_lessThan)
  44. {
  45. LogLevel logLevel{}; // default is FATAL
  46. ASSERT_TRUE(logLevel < LogLevelValue::ERR);
  47. ASSERT_TRUE(logLevel < LogLevelValue::WARN);
  48. ASSERT_TRUE(logLevel < LogLevelValue::INFO);
  49. ASSERT_TRUE(logLevel < LogLevelValue::DEBUG);
  50. ASSERT_TRUE(logLevel < LogLevelValue::TRACE);
  51. }
  52. TEST_F(LogLevelTest, operator_lessThan_not_less_than)
  53. {
  54. LogLevel logLevel{LogLevelValue::DEBUG};
  55. ASSERT_FALSE(logLevel < LogLevelValue::ERR);
  56. }
  57. TEST_F(LogLevelTest, operator_less_than_equals)
  58. {
  59. LogLevel logLevel{}; // default is FATAL
  60. ASSERT_TRUE(logLevel <= LogLevelValue::FATAL);
  61. ASSERT_TRUE(logLevel <= LogLevelValue::ERR);
  62. ASSERT_TRUE(logLevel <= LogLevelValue::WARN);
  63. ASSERT_TRUE(logLevel <= LogLevelValue::INFO);
  64. ASSERT_TRUE(logLevel <= LogLevelValue::DEBUG);
  65. ASSERT_TRUE(logLevel <= LogLevelValue::TRACE);
  66. }
  67. TEST_F(LogLevelTest, operator_less_than_equals_not_less_than_equals)
  68. {
  69. LogLevel logLevel{LogLevelValue::DEBUG};
  70. ASSERT_FALSE(logLevel <= LogLevelValue::ERR);
  71. }
  72. TEST_F(LogLevelTest, operator_greater_than)
  73. {
  74. LogLevel logLevel{LogLevelValue::DEBUG};
  75. ASSERT_TRUE(logLevel > LogLevelValue::ERR);
  76. }
  77. TEST_F(LogLevelTest, operator_greater_than_not_greater_than)
  78. {
  79. LogLevel logLevel{LogLevelValue::DEBUG};
  80. ASSERT_FALSE(logLevel > LogLevelValue::DEBUG);
  81. }
  82. TEST_F(LogLevelTest, operator_greater_than_equals)
  83. {
  84. LogLevel logLevel{LogLevelValue::DEBUG};
  85. ASSERT_TRUE(logLevel >= LogLevelValue::ERR);
  86. }
  87. TEST_F(LogLevelTest, operator_greater_than_equals_not_greater_than_equals)
  88. {
  89. LogLevel logLevel{};
  90. ASSERT_FALSE(logLevel >= LogLevelValue::ERR);
  91. }
  92. TEST_F(LogLevelTest, operator_equals)
  93. {
  94. LogLevel logLevel{LogLevelValue::DEBUG};
  95. ASSERT_TRUE(logLevel == LogLevelValue::DEBUG);
  96. }
  97. TEST_F(LogLevelTest, operator_equals_not_equals)
  98. {
  99. LogLevel logLevel{LogLevelValue::TRACE};
  100. ASSERT_FALSE(logLevel == LogLevelValue::DEBUG);
  101. }
  102. TEST_F(LogLevelTest, setLogLevel_with_log_level_value)
  103. {
  104. LogLevel logLevel{};
  105. logLevel.setLogLevel(LogLevelValue::INFO);
  106. ASSERT_EQ(LogLevelValue::INFO, logLevel);
  107. }
  108. TEST_F(LogLevelTest, setLogLevel_with_string)
  109. {
  110. LogLevel logLevel{};
  111. logLevel.setLogLevel("FATAL");
  112. ASSERT_EQ(LogLevelValue::FATAL, logLevel);
  113. logLevel.setLogLevel("ERROR");
  114. ASSERT_EQ(LogLevelValue::ERR, logLevel);
  115. logLevel.setLogLevel("WARN");
  116. ASSERT_EQ(LogLevelValue::WARN, logLevel);
  117. logLevel.setLogLevel("INFO");
  118. ASSERT_EQ(LogLevelValue::INFO, logLevel);
  119. logLevel.setLogLevel("DEBUG");
  120. ASSERT_EQ(LogLevelValue::DEBUG, logLevel);
  121. logLevel.setLogLevel("TRACE");
  122. ASSERT_EQ(LogLevelValue::TRACE, logLevel);
  123. }
  124. TEST_F(LogLevelTest, setLogLevel_string_no_valid_string)
  125. {
  126. LogLevel logLevel{};
  127. EXPECT_THROW(
  128. {
  129. try
  130. {
  131. logLevel.setLogLevel("This is not a valid log level!");
  132. }
  133. catch (const IllegalArgumentException &_exception)
  134. {
  135. throw;
  136. }
  137. },
  138. IllegalArgumentException);
  139. }
  140. TEST_F(LogLevelTest, toString)
  141. {
  142. LogLevel logLevel{};
  143. ASSERT_STREQ("FATAL", logLevel.toString().c_str());
  144. logLevel.setLogLevel(LogLevelValue::ERR);
  145. ASSERT_STREQ("ERROR", logLevel.toString().c_str());
  146. logLevel.setLogLevel(LogLevelValue::WARN);
  147. ASSERT_STREQ("WARN", logLevel.toString().c_str());
  148. logLevel.setLogLevel(LogLevelValue::INFO);
  149. ASSERT_STREQ("INFO", logLevel.toString().c_str());
  150. logLevel.setLogLevel(LogLevelValue::DEBUG);
  151. ASSERT_STREQ("DEBUG", logLevel.toString().c_str());
  152. logLevel.setLogLevel(LogLevelValue::TRACE);
  153. ASSERT_STREQ("TRACE", logLevel.toString().c_str());
  154. }
  155. }