aboutsummaryrefslogtreecommitdiff
path: root/zencore/zencore.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2023-04-28 16:42:01 +0200
committerGitHub <[email protected]>2023-04-28 16:42:01 +0200
commit6a44e6c95031179d29fca433851219eef402d71e (patch)
tree870acc699f3d13ea177dbf6eae79cbd0fc78a905 /zencore/zencore.cpp
parent0.2.8-pre0 (diff)
downloadzen-6a44e6c95031179d29fca433851219eef402d71e.tar.xz
zen-6a44e6c95031179d29fca433851219eef402d71e.zip
add customization of assert implementation (#263)
* add customization of assert implementation
Diffstat (limited to 'zencore/zencore.cpp')
-rw-r--r--zencore/zencore.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/zencore/zencore.cpp b/zencore/zencore.cpp
index 8b45d273d..2a7c5755e 100644
--- a/zencore/zencore.cpp
+++ b/zencore/zencore.cpp
@@ -33,6 +33,9 @@
namespace zen {
+AssertImpl AssertImpl::DefaultAssertImpl;
+AssertImpl* AssertImpl::CurrentAssertImpl = &AssertImpl::DefaultAssertImpl;
+
//////////////////////////////////////////////////////////////////////////
bool
@@ -120,6 +123,53 @@ zencore_forcelinktests()
zen::usonpackage_forcelink();
zen::crypto_forcelink();
}
+} // namespace zen
+
+# include <zencore/testing.h>
+
+namespace zen {
+
+TEST_CASE("Assert.Default")
+{
+ bool A = true;
+ bool B = false;
+ CHECK_THROWS_WITH(ZEN_ASSERT(A == B), "A == B");
+}
+
+TEST_CASE("Assert.Custom")
+{
+ struct MyAssertImpl : AssertImpl
+ {
+ ZEN_FORCENOINLINE ZEN_DEBUG_SECTION MyAssertImpl() : PrevAssertImpl(CurrentAssertImpl) { CurrentAssertImpl = this; }
+ virtual ZEN_FORCENOINLINE ZEN_DEBUG_SECTION ~MyAssertImpl() { CurrentAssertImpl = PrevAssertImpl; }
+ virtual void ZEN_FORCENOINLINE ZEN_DEBUG_SECTION OnAssert(const char* Filename,
+ int LineNumber,
+ const char* FunctionName,
+ const char* Msg)
+ {
+ AssertFileName = Filename;
+ Line = LineNumber;
+ FuncName = FunctionName;
+ Message = Msg;
+ }
+ AssertImpl* PrevAssertImpl;
+
+ const char* AssertFileName = nullptr;
+ int Line = -1;
+ const char* FuncName = nullptr;
+ const char* Message = nullptr;
+ };
+
+ MyAssertImpl MyAssert;
+ bool A = true;
+ bool B = false;
+ CHECK_THROWS_WITH(ZEN_ASSERT(A == B), "A == B");
+ CHECK(MyAssert.AssertFileName != nullptr);
+ CHECK(MyAssert.Line != -1);
+ CHECK(MyAssert.FuncName != nullptr);
+ CHECK(strcmp(MyAssert.Message, "A == B") == 0);
+}
+
#endif
} // namespace zen