LogLevelTest.cpp 5.0 KB

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