From 83d330f47a5554b630f0f95694254f7a9cdf04be Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Mon, 26 Sep 2016 19:17:07 +0300 Subject: [PATCH] Class User implemented. --- include/telebotxx/User.hpp | 60 +++++++++++++++++++++++++++ src/CMakeLists.txt | 1 + src/User.cpp | 72 +++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 2 +- tests/{main.cpp => TestApi.cpp} | 0 tests/TestTypes.cpp | 49 ++++++++++++++++++++++ 6 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 include/telebotxx/User.hpp create mode 100644 src/User.cpp rename tests/{main.cpp => TestApi.cpp} (100%) create mode 100644 tests/TestTypes.cpp diff --git a/include/telebotxx/User.hpp b/include/telebotxx/User.hpp new file mode 100644 index 0000000..67af79d --- /dev/null +++ b/include/telebotxx/User.hpp @@ -0,0 +1,60 @@ +#ifndef TELEBOTXX_USER_H +#define TELEBOTXX_USER_H + +#include + +namespace telebotxx +{ + class User + { + public: + User(); + + /// \brief Full constructor + /// \param id id + /// \param firstName first name + /// \param lastName last name + /// \param username username + User(int id, const std::string& firstName, const std::string& lastName, const std::string& username); + + User(const User&); + User(User&&); + + /// \brief Get id + int getId() const; + + /// \brief Set id + void setId(int id); + + /// \brief Get first name + const std::string& getFirstName() const; + + /// \brief Set first name + void setFirstName(const std::string& firstName); + + /// \brief Get last name + const std::string& getLastName() const; + + /// \brief Set last name + void setLastName(const std::string& lastName); + + /// \brief Get username + const std::string& getUsername() const; + + /// \brief Set username + void setUsername(const std::string& username); + + void swap(User&) noexcept; + + const User& operator=(User other); + User& operator=(User&& other); + + private: + int id_; + std::string firstName_; + std::string lastName_; + std::string username_; + }; +} + +#endif // TELEBOTXX_USER_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f1d3b50..0671e1b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,6 +18,7 @@ set(LIBRARY_OUTPUT_PATH "../lib") set(SOURCE_FILES BotApi.cpp + User.cpp ) add_library(telebotxx SHARED ${SOURCE_FILES}) diff --git a/src/User.cpp b/src/User.cpp new file mode 100644 index 0000000..603dc5b --- /dev/null +++ b/src/User.cpp @@ -0,0 +1,72 @@ +#include + +using namespace telebotxx; + +User::User() + : id_(-1) +{ +} + +User::User(int id, const std::string& firstName, const std::string& lastName, const std::string& username) + : id_(id), firstName_(firstName), lastName_(lastName), username_(username) +{ +} + +User::User(const User&) = default; +User::User(User&&) = default; + +int User::getId() const +{ + return id_; +} + +void User::setId(int id) +{ + id_ = id; +} + +const std::string& User::getFirstName() const +{ + return firstName_; +} + +void User::setFirstName(const std::string& firstName) +{ + firstName_ = firstName; +} + +const std::string& User::getLastName() const +{ + return lastName_; +} + +void User::setLastName(const std::string& lastName) +{ + lastName_ = lastName; +} + +const std::string& User::getUsername() const +{ + return username_; +} + +void User::setUsername(const std::string& username) +{ + username_ = username; +} + +void User::swap(User& other) noexcept +{ + std::swap(id_, other.id_); + firstName_.swap(other.firstName_); + lastName_.swap(other.lastName_); + username_.swap(other.username_); +} + +const User& User::operator=(User other) +{ + swap(other); + return *this; +} + +User& User::operator=(User&& other) = default; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b41c194..f2210fb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,7 +19,7 @@ endif(NOT WIN32) set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/tests") -set(SOURCE_FILES Dummy.cpp main.cpp) +set(SOURCE_FILES Dummy.cpp TestTypes.cpp TestApi.cpp) add_executable(telebotxx-test ${SOURCE_FILES}) target_link_libraries(telebotxx-test telebotxx stdc++ ${Boost_LIBRARIES}) diff --git a/tests/main.cpp b/tests/TestApi.cpp similarity index 100% rename from tests/main.cpp rename to tests/TestApi.cpp diff --git a/tests/TestTypes.cpp b/tests/TestTypes.cpp new file mode 100644 index 0000000..009ee20 --- /dev/null +++ b/tests/TestTypes.cpp @@ -0,0 +1,49 @@ +#include "TestGlobal.hpp" + +#include + +using namespace telebotxx; + +BOOST_AUTO_TEST_SUITE(TestUser) + + BOOST_AUTO_TEST_CASE(DefaultConstructor) + { + PRINT_TESTNAME; + BOOST_REQUIRE_NO_THROW(User user); + } + + BOOST_AUTO_TEST_CASE(FullConstructor) + { + PRINT_TESTNAME; + int id(1); + std::string first("John"); + std::string last("Smith"); + std::string username("john_smith"); + BOOST_REQUIRE_NO_THROW(User user(id, first, last, username)); + } + + BOOST_AUTO_TEST_CASE(CopyConstructor) + { + PRINT_TESTNAME; + User other(1, "John", "Smith", "john_smith"); + std::unique_ptr user; + BOOST_REQUIRE_NO_THROW(user.reset(new User(other))); + BOOST_REQUIRE_EQUAL(user->getId(), 1); + BOOST_REQUIRE_EQUAL(user->getFirstName(), "John"); + BOOST_REQUIRE_EQUAL(user->getLastName(), "Smith"); + BOOST_REQUIRE_EQUAL(user->getUsername(), "john_smith"); + } + + BOOST_AUTO_TEST_CASE(MoveConstructor) + { + PRINT_TESTNAME; + User other(1, "John", "Smith", "john_smith"); + std::unique_ptr user; + BOOST_REQUIRE_NO_THROW(user.reset(new User(std::move(other)))); + BOOST_REQUIRE_EQUAL(user->getId(), 1); + BOOST_REQUIRE_EQUAL(user->getFirstName(), "John"); + BOOST_REQUIRE_EQUAL(user->getLastName(), "Smith"); + BOOST_REQUIRE_EQUAL(user->getUsername(), "john_smith"); + } + +BOOST_AUTO_TEST_SUITE_END()