From 27a32521b7cf6848088412ae38ee8cc62a69fbe6 Mon Sep 17 00:00:00 2001 From: Kacperos155 <56676161+Kacperos155@users.noreply.github.com> Date: Wed, 26 Jan 2022 04:19:10 +0100 Subject: [PATCH] Add test for Column std::shared_ptr; remove noexcept from throwing Column constructor --- include/SQLiteCpp/Column.h | 2 +- src/Column.cpp | 2 +- tests/Column_test.cpp | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/SQLiteCpp/Column.h b/include/SQLiteCpp/Column.h index 057b25f..bc349f9 100644 --- a/include/SQLiteCpp/Column.h +++ b/include/SQLiteCpp/Column.h @@ -54,7 +54,7 @@ public: * @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object. * @param[in] aIndex Index of the column in the row of result, starting at 0 */ - explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept; + explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex); // default destructor: the finalization will be done by the destructor of the last shared pointer // default copy constructor and assignment operator are perfectly suited : diff --git a/src/Column.cpp b/src/Column.cpp index 2edff46..f5dc0d9 100644 --- a/src/Column.cpp +++ b/src/Column.cpp @@ -26,7 +26,7 @@ const int Null = SQLITE_NULL; // Encapsulation of a Column in a row of the result pointed by the prepared Statement. -Column::Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept : +Column::Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) : mStmtPtr(aStmtPtr), mIndex(aIndex) { diff --git a/tests/Column_test.cpp b/tests/Column_test.cpp index bf31c23..adc26bd 100644 --- a/tests/Column_test.cpp +++ b/tests/Column_test.cpp @@ -241,3 +241,39 @@ TEST(Column, stream) std::string content = ss.str(); EXPECT_EQ(content, str); } + +TEST(Column, shared_ptr) +{ + // Create a new database + SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); + EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT)")); + EXPECT_EQ(1, db.exec(R"(INSERT INTO test VALUES (42, "fortytwo"))")); + const char* query_str = "SELECT id, msg FROM test"; + + std::unique_ptr query{ new SQLite::Statement(db, query_str) }; + query->executeStep(); + + auto column0 = query->getColumn(0); + auto column1 = query->getColumn(1); + query.reset(); + + EXPECT_EQ(42, column0.getInt()); + EXPECT_STREQ("fortytwo", column1.getText()); + + query.reset(new SQLite::Statement(db, query_str)); + query->executeStep(); + column0 = query->getColumn(0); + EXPECT_EQ(true, column0.isInteger()); + query->executeStep(); // query is done + + // Undefined behavior + // auto x = column0.getInt(); + + query.reset(); + + // Undefined behavior + // auto x = column0.getInt(); + // bool isInt = column0.isInteger(); + + EXPECT_STREQ("id", column0.getName()); +}