Implements Assert macro.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
5710329f39
commit
0fe488b1c2
|
@ -0,0 +1,24 @@
|
|||
#ifndef ARBUTILS_ASSERT_HPP
|
||||
#define ARBUTILS_ASSERT_HPP
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string.h>
|
||||
|
||||
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
||||
|
||||
#ifndef NO_ASSERT
|
||||
#define Assert(expr) \
|
||||
{ \
|
||||
if (!expr) { \
|
||||
std::stringstream ss; \
|
||||
ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \
|
||||
ss << #expr << "\""; \
|
||||
throw std::logic_error(ss.str()); \
|
||||
} \
|
||||
}
|
||||
#else
|
||||
// Assert is empty if NO_ASSERT is defined.
|
||||
#define Assert(expr)
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,14 @@
|
|||
#include "../extern/catch.hpp"
|
||||
#include "../src/Assert.hpp"
|
||||
void TestWrapper(bool wrapperExpression){Assert(wrapperExpression)}
|
||||
|
||||
TEST_CASE("Assert succeeds if true", "[Utilities]") {
|
||||
REQUIRE_NOTHROW(TestWrapper(true));
|
||||
}
|
||||
|
||||
TEST_CASE("Assert throws if false", "[Utilities]") { REQUIRE_THROWS(TestWrapper(false)); }
|
||||
|
||||
TEST_CASE("Assert throws if false with message", "[Utilities]") {
|
||||
REQUIRE_THROWS_WITH(TestWrapper(false),
|
||||
Catch::Matchers::Equals("ASSERTION FAILED: [AssertTests.cpp (3)] \"wrapperExpression\""));
|
||||
}
|
Loading…
Reference in New Issue