LogLevelTest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.getValue());
  31. }
  32. TEST_F(LogLevelTest, constructor_default)
  33. {
  34. LogLevel logLevel{};
  35. ASSERT_EQ(LogLevelValue::FATAL, logLevel.getValue());
  36. }
  37. TEST_F(LogLevelTest, operator_assign)
  38. {
  39. LogLevel logLevel{LogLevelValue::DEBUG};
  40. logLevel = LogLevelValue::INFO;
  41. ASSERT_EQ(LogLevelValue::INFO, logLevel.getValue());
  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, getLogLevel)
  103. {
  104. ASSERT_EQ(LogLevelValue::FATAL, LogLevel{}.getValue());
  105. }
  106. TEST_F(LogLevelTest, setLogLevel_with_log_level_value)
  107. {
  108. LogLevel logLevel{};
  109. logLevel.setLogLevel(LogLevelValue::INFO);
  110. ASSERT_EQ(LogLevelValue::INFO, logLevel.getValue());
  111. }
  112. TEST_F(LogLevelTest, setLogLevel_with_string)
  113. {
  114. LogLevel logLevel{};
  115. logLevel.setLogLevel("FATAL");
  116. ASSERT_EQ(LogLevelValue::FATAL, logLevel.getValue());
  117. logLevel.setLogLevel("ERROR");
  118. ASSERT_EQ(LogLevelValue::ERR, logLevel.getValue());
  119. logLevel.setLogLevel("WARN");
  120. ASSERT_EQ(LogLevelValue::WARN, logLevel.getValue());
  121. logLevel.setLogLevel("INFO");
  122. ASSERT_EQ(LogLevelValue::INFO, logLevel.getValue());
  123. logLevel.setLogLevel("DEBUG");
  124. ASSERT_EQ(LogLevelValue::DEBUG, logLevel.getValue());
  125. logLevel.setLogLevel("TRACE");
  126. ASSERT_EQ(LogLevelValue::TRACE, logLevel.getValue());
  127. }
  128. TEST_F(LogLevelTest, setLogLevel_string_no_valid_string)
  129. {
  130. LogLevel logLevel{};
  131. EXPECT_THROW(
  132. {
  133. try
  134. {
  135. logLevel.setLogLevel("This is not a valid log level!");
  136. }
  137. catch (const IllegalArgumentException &_exception)
  138. {
  139. throw;
  140. }
  141. },
  142. IllegalArgumentException);
  143. }
  144. TEST_F(LogLevelTest, toString)
  145. {
  146. LogLevel logLevel{};
  147. ASSERT_STREQ("FATAL", logLevel.toString().c_str());
  148. logLevel.setLogLevel(LogLevelValue::ERR);
  149. ASSERT_STREQ("ERROR", logLevel.toString().c_str());
  150. logLevel.setLogLevel(LogLevelValue::WARN);
  151. ASSERT_STREQ("WARN", logLevel.toString().c_str());
  152. logLevel.setLogLevel(LogLevelValue::INFO);
  153. ASSERT_STREQ("INFO", logLevel.toString().c_str());
  154. logLevel.setLogLevel(LogLevelValue::DEBUG);
  155. ASSERT_STREQ("DEBUG", logLevel.toString().c_str());
  156. logLevel.setLogLevel(LogLevelValue::TRACE);
  157. ASSERT_STREQ("TRACE", logLevel.toString().c_str());
  158. }
  159. }