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