Adds new Ensure helpers for clearer error messages.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2022-03-20 11:05:44 +01:00
parent be81082588
commit 97babe79e1
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 76 additions and 1 deletions

View File

@ -13,6 +13,41 @@
throw ArbUt::__ErrorHelpers::CreateEnsureError(#expr, std::source_location::current()); \
}
#define EnsureEquals(a, b) \
if ((a) != (b)) { \
std::stringstream error_ss; \
error_ss << a << " was not equal to " << b; \
throw ArbUt::__ErrorHelpers::CreateEnsureError(error_ss.str().c_str(), std::source_location::current()); \
}
#define EnsureGreater(a, b) \
if ((a) <= (b)) { \
std::stringstream error_ss; \
error_ss << a << " was not equal to " << b; \
throw ArbUt::__ErrorHelpers::CreateEnsureError(error_ss.str().c_str(), std::source_location::current()); \
}
#define EnsureGreaterOrEquals(a, b) \
if ((a) < (b)) { \
std::stringstream error_ss; \
error_ss << a << " was not equal to " << b; \
throw ArbUt::__ErrorHelpers::CreateEnsureError(error_ss.str().c_str(), std::source_location::current()); \
}
#define EnsureLess(a, b) \
if ((a) >= (b)) { \
std::stringstream error_ss; \
error_ss << a << " was not equal to " << b; \
throw ArbUt::__ErrorHelpers::CreateEnsureError(error_ss.str().c_str(), std::source_location::current()); \
}
#define EnsureLessOrEquals(a, b) \
if ((a) > (b)) { \
std::stringstream error_ss; \
error_ss << a << " was not equal to " << b; \
throw ArbUt::__ErrorHelpers::CreateEnsureError(error_ss.str().c_str(), std::source_location::current()); \
}
/// @brief Ensures an expression is true for a range. The value in the range can be retrieved using ``item``.
/// @ingroup Ensure
#define EnsureForEach(iterator, assertion) \
@ -23,6 +58,11 @@
#else
// Ensure is empty if NO_ENSURE is defined.
#define Ensure(expr) ;
#define EnsureEquals(a, b) ;
#define EnsureGreater(a, b) ;
#define EnsureGreaterOrEquals(a, b) ;
#define EnsureLess(a, b) ;
#define EnsureLessOrEquals(a, b) ;
#define EnsureForEach(iterator, assertion) ;
#endif

View File

@ -2,6 +2,11 @@
#include "../src/Ensure.hpp"
void TestWrapper(bool wrapperExpression) { Ensure(wrapperExpression) }
void TestWrapperNotNull(void* value) { EnsureNotNull(value) }
void TestWrapperEquals(int32_t a, int32_t b) { EnsureEquals(a, b) }
void TestWrapperGreater(int32_t a, int32_t b) { EnsureGreater(a, b) }
void TestWrapperGreaterEquals(int32_t a, int32_t b) { EnsureGreaterOrEquals(a, b) }
void TestWrapperLess(int32_t a, int32_t b) { EnsureLess(a, b) }
void TestWrapperLessEquals(int32_t a, int32_t b){EnsureLessOrEquals(a, b)}
TEST_CASE("Ensure succeeds if true") {
REQUIRE_NOTHROW(TestWrapper(true));
@ -19,6 +24,36 @@ TEST_CASE("Ensure throws if false with message") {
throw ArbUt::Exception("Didn't throw.");
}
TEST_CASE("Ensure equals true") { REQUIRE_NOTHROW(TestWrapperEquals(10, 10)); }
TEST_CASE("Ensure equals throws if false") { REQUIRE_THROWS(TestWrapperEquals(5, 10)); }
TEST_CASE("Ensure greater true") { REQUIRE_NOTHROW(TestWrapperGreater(10, 5)); }
TEST_CASE("Ensure greater throws if less than") { REQUIRE_THROWS(TestWrapperGreater(5, 10)); }
TEST_CASE("Ensure greater throws if equals") { REQUIRE_THROWS(TestWrapperGreater(10, 10)); }
TEST_CASE("Ensure greater or equals true") {
REQUIRE_NOTHROW(TestWrapperGreaterEquals(10, 5));
REQUIRE_NOTHROW(TestWrapperGreaterEquals(10, 10));
}
TEST_CASE("Ensure greater or equals throws if less than") { REQUIRE_THROWS(TestWrapperGreaterEquals(5, 10)); }
TEST_CASE("Ensure less true") { REQUIRE_NOTHROW(TestWrapperLess(5, 10)); }
TEST_CASE("Ensure less throws if less than") { REQUIRE_THROWS(TestWrapperLess(10, 5)); }
TEST_CASE("Ensure less throws if equals") { REQUIRE_THROWS(TestWrapperLess(10, 10)); }
TEST_CASE("Ensure less or equals true") {
REQUIRE_NOTHROW(TestWrapperLessEquals(5, 10));
REQUIRE_NOTHROW(TestWrapperLessEquals(10, 10));
}
TEST_CASE("Ensure less or equals throws if less than") { REQUIRE_THROWS(TestWrapperLessEquals(10, 5)); }
TEST_CASE("Multiple asserts"){Ensure(true) Ensure(true) Ensure(true)}
TEST_CASE("EnsureNotNull throws if nullptr") {