NullPointerEvaluatorTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-08
  6. * Changed: 2024-05-31
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std/ls-std-core.hpp>
  11. #include <memory>
  12. #include <string>
  13. using ls::std::core::NullPointerEvaluator;
  14. using ls::std::core::NullPointerException;
  15. using std::shared_ptr;
  16. using std::string;
  17. using testing::Test;
  18. namespace
  19. {
  20. class NullPointerEvaluatorTest : public Test
  21. {
  22. public:
  23. NullPointerEvaluatorTest() = default;
  24. ~NullPointerEvaluatorTest() override = default;
  25. };
  26. TEST_F(NullPointerEvaluatorTest, evaluate_raw_pointer)
  27. {
  28. EXPECT_THROW(
  29. {
  30. try
  31. {
  32. NullPointerEvaluator{nullptr}.evaluate();
  33. }
  34. catch (const NullPointerException &_exception)
  35. {
  36. const string actual = _exception.what();
  37. const string expected = _exception.getName() + " thrown - reference in use is null!";
  38. ASSERT_STREQ(expected.c_str(), actual.c_str());
  39. throw;
  40. }
  41. },
  42. NullPointerException);
  43. }
  44. TEST_F(NullPointerEvaluatorTest, evaluate_raw_pointer_dedicated_message)
  45. {
  46. EXPECT_THROW(
  47. {
  48. try
  49. {
  50. NullPointerEvaluator(nullptr, "this reference is not set and causes this exception!").evaluate();
  51. }
  52. catch (const NullPointerException &_exception)
  53. {
  54. const string actual = _exception.what();
  55. const string expected = _exception.getName() + " thrown - this reference is not set and causes this exception!";
  56. ASSERT_STREQ(expected.c_str(), actual.c_str());
  57. throw;
  58. }
  59. },
  60. NullPointerException);
  61. }
  62. TEST_F(NullPointerEvaluatorTest, evaluate)
  63. {
  64. shared_ptr<void> value{};
  65. EXPECT_THROW(
  66. {
  67. try
  68. {
  69. NullPointerEvaluator{value}.evaluate();
  70. }
  71. catch (const NullPointerException &_exception)
  72. {
  73. const string actual = _exception.what();
  74. const string expected = _exception.getName() + " thrown - reference in use is null!";
  75. ASSERT_STREQ(expected.c_str(), actual.c_str());
  76. throw;
  77. }
  78. },
  79. NullPointerException);
  80. }
  81. TEST_F(NullPointerEvaluatorTest, evaluate_dedicated_message)
  82. {
  83. shared_ptr<void> value{};
  84. EXPECT_THROW(
  85. {
  86. try
  87. {
  88. NullPointerEvaluator(value, "this reference is not set and causes this exception!").evaluate();
  89. }
  90. catch (const NullPointerException &_exception)
  91. {
  92. const string actual = _exception.what();
  93. const string expected = _exception.getName() + " thrown - this reference is not set and causes this exception!";
  94. ASSERT_STREQ(expected.c_str(), actual.c_str());
  95. throw;
  96. }
  97. },
  98. NullPointerException);
  99. }
  100. }