Class User implemented.

This commit is contained in:
Kirill Kirilenko 2016-09-26 19:17:07 +03:00
parent 45cefdcd73
commit 83d330f47a
6 changed files with 183 additions and 1 deletions

View file

@ -0,0 +1,60 @@
#ifndef TELEBOTXX_USER_H
#define TELEBOTXX_USER_H
#include <string>
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

View file

@ -18,6 +18,7 @@ set(LIBRARY_OUTPUT_PATH "../lib")
set(SOURCE_FILES BotApi.cpp
User.cpp
)
add_library(telebotxx SHARED ${SOURCE_FILES})

72
src/User.cpp Normal file
View file

@ -0,0 +1,72 @@
#include <telebotxx/User.hpp>
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;

View file

@ -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})

49
tests/TestTypes.cpp Normal file
View file

@ -0,0 +1,49 @@
#include "TestGlobal.hpp"
#include <telebotxx/User.hpp>
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> 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> 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()