Add default copy constructor to exception class

The throw statement may copy the exception, since exception are thrown by value
Having const members disables the assignment operator
This commit is contained in:
fekir 2017-08-19 08:53:01 +02:00
parent 670d710f62
commit 94ebe5ced6
3 changed files with 47 additions and 2 deletions

View File

@ -134,6 +134,7 @@ set(SQLITECPP_TESTS
tests/Backup_test.cpp
tests/Transaction_test.cpp
tests/VariadicBind_test.cpp
tests/Exception_test.cpp
)
source_group(tests FILES ${SQLITECPP_TESTS})

View File

@ -91,8 +91,8 @@ public:
const char* getErrorStr() const noexcept; // nothrow
private:
const int mErrcode; ///< Error code value
const int mExtendedErrcode; ///< Detailed error code if any
int mErrcode; ///< Error code value
int mExtendedErrcode; ///< Detailed error code if any
};

44
tests/Exception_test.cpp Normal file
View File

@ -0,0 +1,44 @@
/**
* @file Transaction_test.cpp
* @ingroup tests
* @brief Test of a SQLite Transaction.
*
* Copyright (c) 2012-2016 Sebastien Rombauts (sebastien.rombauts@gmail.com)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#include <SQLiteCpp/Exception.h>
#include <gtest/gtest.h>
TEST(Exception, copy) {
const SQLite::Exception ex1("some error", 2);
const SQLite::Exception ex2 = ex1;
EXPECT_STREQ(ex1.what(), ex2.what());
EXPECT_EQ(ex1.getErrorCode(), ex2.getErrorCode());
EXPECT_EQ(ex1.getExtendedErrorCode(), ex2.getExtendedErrorCode());
}
// see http://eel.is/c++draft/exception#2 or http://www.cplusplus.com/reference/exception/exception/operator=/
// an assignment operator is expected to be avaiable
TEST(Exception, assignment) {
const SQLite::Exception ex1("some error", 2);
SQLite::Exception ex2("some error2", 3);
ex2 = ex1;
EXPECT_STREQ(ex1.what(), ex2.what());
EXPECT_EQ(ex1.getErrorCode(), ex2.getErrorCode());
EXPECT_EQ(ex1.getExtendedErrorCode(), ex2.getExtendedErrorCode());
}
TEST(Exception, throw_catch) {
const char message[] = "some error";
try {
throw SQLite::Exception(message);
} catch (const std::runtime_error& ex) {
EXPECT_STREQ(ex.what(), message);
}
}