#include <gtest/gtest.h> #include "../../common/compat.hpp" #include "../../common/errors.hpp" #include <type_traits> #if SPRAWL_EXCEPTIONS_ENABLED using sprawl::Exception; #define TEST_EXCEPTION(ExceptionClass, BaseClass, id, name, type) \ TEST(ExceptionTest, Test_##id) \ { \ try \ { \ throw ExceptionClass(); \ } \ catch(BaseClass const& e) \ { \ ASSERT_EQ(sprawl::ExceptionId::id, e.GetId()); \ ASSERT_EQ(0, strcmp(e.what(), name)); \ ASSERT_EQ(e.what(), sprawl::ExceptionString(sprawl::ExceptionId::id)); \ } \ } #define SPRAWL_EXCEPTION(baseId, id, name, type) TEST_EXCEPTION(Exception<sprawl::ExceptionId::id>, Exception<sprawl::ExceptionId::baseId>, id, name, type) #include "../../common/exceptions.hpp" #undef SPRAWL_EXCEPTION #else bool copied = false; bool moved = false; TEST(ErrorStateTest, ErrorStateReferenceDoesNotCopy) { struct CopyTest { CopyTest() {} CopyTest(const CopyTest&) { copied = true; } CopyTest(CopyTest&&) { moved = true; } std::string data_member; }; CopyTest c; sprawl::ErrorState<CopyTest&> copy = c; ASSERT_FALSE(copied); ASSERT_FALSE(moved); } #if SPRAWL_DEBUG TEST(ErrorStateDeathTest, ErrorStateTerminatesOnDestructWithUncheckedError) { EXPECT_DEATH( { sprawl::ErrorState<void> c = sprawl::GeneralException(); }, "Unknown or unspecified error" ); EXPECT_DEATH( { sprawl::ErrorState<bool> c = sprawl::GeneralException(); }, "Unknown or unspecified error" ); EXPECT_DEATH( { sprawl::ErrorState<bool&> c = sprawl::GeneralException(); }, "Unknown or unspecified error" ); } TEST(ErrorStateDeathTest, ErrorStateDoesNotTerminateOnDestructWithCheckedError) { EXPECT_EXIT( { sprawl::ErrorState<void> c = sprawl::GeneralException(); ASSERT_TRUE(c.ErrorCode() == sprawl::ExceptionId::GENERAL_EXCEPTION); fprintf(stderr, "I'm alive!"); exit(0); }, testing::ExitedWithCode(0), "I'm alive!" ); EXPECT_EXIT( { sprawl::ErrorState<bool> c = sprawl::GeneralException(); ASSERT_TRUE(c.ErrorCode() == sprawl::ExceptionId::GENERAL_EXCEPTION); fprintf(stderr, "I'm alive!"); exit(0); }, testing::ExitedWithCode(0), "I'm alive!" ); EXPECT_EXIT( { sprawl::ErrorState<bool&> c = sprawl::GeneralException(); ASSERT_TRUE(c.ErrorCode() == sprawl::ExceptionId::GENERAL_EXCEPTION); fprintf(stderr, "I'm alive!"); exit(0); }, testing::ExitedWithCode(0), "I'm alive!" ); } TEST(ErrorStateDeathTest, ErrorStateTerminatesOnGetWithError) { EXPECT_DEATH( { sprawl::ErrorState<bool> c = sprawl::GeneralException(); printf("%d\n", c.Get()); ASSERT_TRUE(c.ErrorCode() == sprawl::ExceptionId::GENERAL_EXCEPTION); }, "Unknown or unspecified error" ); EXPECT_DEATH( { sprawl::ErrorState<bool&> c = sprawl::GeneralException(); printf("%d\n", c.Get()); ASSERT_TRUE(c.ErrorCode() == sprawl::ExceptionId::GENERAL_EXCEPTION); }, "Unknown or unspecified error" ); } #endif #endif
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 16768 | ShadauxCat |
Improvements to error handling in builds with exceptions disabled: - In debug builds or with SPRAWL_ERRORSTATE_STRICT enabled, ErrorState will output a message to stderr and terminate if Get() is called when an error flag is set. (In release buils or with SPRAWL_ERRORSTATE_PERMISSIVE defined, Get() will return junk memory in this case.) - In debug builds or with SPRAWL_ERRORSTATE_STRICT enabled, ErrorState will output a message to stderr and terminate if its destructor is called without checking the errorstate if an error is present (equivalent to an exception terminating the application if no catch() block is present for it). - On linux builds and when running "Analyze" through visual studio, a warning will be issued if any function returning ErrorState has its return value ignored. (This only applies to builds with exceptions not enabled; when exceptions are enabled no warning is issued) - Many functions that could return ErrorState were having their return values silently ignored in internal sprawl code so the user would not find out about errors if exceptions are disabled; now anything in sprawl code that calls a function returning ErrorState will either handle the error, or (in most cases) surface it back up to the user. - As a positive side-effect of the warnings for ignoring ErrorState, several constructors that were capable of throwing exceptions are no longer capable of doing so. #review-16769 |
||
#2 | 16380 | ShadauxCat |
Missed a header, also corrected missing newline in exceptions.hpp, also corrected exceptions.hpp being exceptions.h. #review-16381 |
||
#1 | 16378 | ShadauxCat |
New exception framework, phase one. Added new class, ErrorState, for handling errors when exceptions are disabled. When exceptions are enabled, ErrorState<T> is an alias for T. When they're disabled, ErrorState<T> additionally encodes an error code, and will have junk data (and probably a crash) if an error is returned and not checked before the data is used. ErrorState<T> is implicitly convertible to and from T, so applications that don't care about errors can code like they don't exist... Phase two will involve overloading operators on ErrorState so that things that return ErrorState can still function as much as possible like they do when exceptions are enabled. #review-16379 |