NullPointerEvaluatorTest.cpp 3.0 KB

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