[DEV] correct throw error

This commit is contained in:
Edouard DUPIN 2018-07-16 21:23:28 +02:00
parent a7df1274ee
commit 101f41dad8
6 changed files with 115 additions and 7 deletions

View File

@ -204,10 +204,11 @@ void etest::GenericTest::testResult(bool _result,
void etest::GenericTest::testCatchThrow(const etk::Exception& exeption, uint32_t _line) {
ETEST_ERROR("Detect an error: " << m_file << ":" << _line << ": Catch etk::Exception");
ETEST_ERROR(" What='" << exeption.what() << "'");
ETEST_ERROR(" what='" << exeption.what() << "'");
ETEST_ERROR(" which='" << exeption.which() << "'");
ETEST_ERROR(" file='" << exeption.file() << "'");
ETEST_ERROR(" Line=" << exeption.line());
ETEST_ERROR(" Function='" << exeption.function() << "'");
ETEST_ERROR(" line=" << exeption.line());
ETEST_ERROR(" function='" << exeption.function() << "'");
m_haveError = true;
m_numberCheckFail++;

View File

@ -278,3 +278,41 @@ namespace etest {
#define EXPECT_FLOAT_EQ(element, result) \
EXPECT_FLOAT_EQ_DELTA(element, result, 0.00001f)
#define EXPECT_THROW(element, typeThrow) \
do { \
try { \
etest::g_currentTest->addCheck(); \
ETEST_DEBUG(" [ SUB-RUN ] EXPECT_THROW(" << #element << ", " << #typeThrow << ");"); \
element; \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(false, \
"", \
#element, \
"--- Not throw ---", \
#typeThrow, \
__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
} catch ( typeThrow e ) { \
/*Normale Case ...*/ \
if (etest::g_currentTest == null) { \
ETEST_CRITICAL("Not in a test"); \
} else { \
etest::g_currentTest->testResult(true, \
"", \
#element, \
"--- Have Throw ---", \
#typeThrow, \
__LINE__); \
} \
ETEST_DEBUG(" [ SUB-DONE ]"); \
} catch ( etk::Exception e ) { \
testCatchThrow(e, __LINE__); \
} catch ( ... ) {\
testCatchThrow(__LINE__); \
} \
} while (false)

View File

@ -17,6 +17,12 @@ etk::Exception::Exception(const char* _type, const etk::String& _what):
}
etk::Exception::Exception(const etk::String& _what):
m_type("ETK_EXCEPTION"),
m_what(_what) {
}
const char* etk::Exception::which() const {
return m_type;
}

View File

@ -24,13 +24,15 @@ namespace etk {
* @brief Default constructor.
*/
Exception();
/**
* @brief Generic Constructor (direct generic exception.
* @param[in] _what The explanation of the problem.
*/
Exception(const etk::String& _what);
/**
* @brief Generic Constructor.
* @param[in] _type Type of the exception
* @param[in] _what The explanation of the problem.
* @param[in] _function Function name to find faster the source of the problem.
* @param[in] _file File of the exception throw.
* @param[in] _line Line of the file throw.
*/
Exception(const char* _type, const etk::String& _what);
/**
@ -83,7 +85,13 @@ namespace etk {
*/
etk::String toString() const;
};
#define ETK_THROW_EXCEPTION(ex) throw ex.setFunction(__PRETTY_FUNCTION__).setFile(__FILE__).setLine(__LINE__)
#define ETK_THROW_EXCEPTION(ex) do { \
auto tmp = ex; \
tmp.setFunction(__PRETTY_FUNCTION__); \
tmp.setFile(__FILE__); \
tmp.setLine(__LINE__); \
throw tmp; \
} while(false)
//! @brief Generic
namespace exception {

View File

@ -46,6 +46,7 @@ def configure(target, my_module):
'test/testVector2_f.cpp',
'test/testString.cpp',
'test/testTrait.cpp',
'test/testThrow.cpp',
])
my_module.add_depend([
'etk',

54
test/testThrow.cpp Normal file
View File

@ -0,0 +1,54 @@
/**
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#include <etest/etest.hpp>
#include <etk/Exception.hpp>
#include <test-debug/debug.hpp>
#include "ConstructDestruct.hpp"
TEST(TestThrow, exception_base) {
// Test contructor value
try {
throw etk::Exception("plop");
} catch (etk::Exception ex) {
EXPECT_EQ(true, true);
return;
}
EXPECT_EQ(false, true);
}
TEST(TestThrow, invalidArgument) {
// Test contructor value
try {
throw etk::exception::InvalidArgument("plop");
} catch (etk::exception::InvalidArgument ex) {
EXPECT_EQ(true, true);
return;
} catch (etk::Exception ex) {
EXPECT_EQ(true, false);
return;
}
EXPECT_EQ(false, true);
}
static void tmpThrow() {
throw etk::exception::InvalidArgument("plop");
}
TEST(TestThrow, invalidArgument_etest) {
// Test contructor value
EXPECT_THROW(tmpThrow(), etk::exception::InvalidArgument);
}
static void tmpThrow2() {
ETK_THROW_EXCEPTION(etk::exception::InvalidArgument("plop"));
}
TEST(TestThrow, invalidArgument_etest2) {
// Test contructor value
EXPECT_THROW(tmpThrow2(), etk::exception::InvalidArgument);
}