Full constructor removed from class User.

User::toString() implemented.
operator<< (User) implemented.
This commit is contained in:
Kirill Kirilenko 2016-11-09 19:44:19 +03:00
parent 0e896f7066
commit 90d2c2dc96
4 changed files with 37 additions and 30 deletions

View file

@ -10,16 +10,9 @@ namespace telebotxx
{
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&&);
~User();
/// \brief Get id
int getId() const;
@ -45,6 +38,9 @@ namespace telebotxx
/// \brief Set username
void setUsername(const std::string& username);
/// \brief Get string representation of user
const std::string toString() const;
void swap(User&) noexcept;
const User& operator=(User other);
@ -57,6 +53,8 @@ namespace telebotxx
};
using UserPtr = std::shared_ptr<User>;
std::ostream& operator<<(std::ostream& os, const User& user);
}
#endif // TELEBOTXX_USER_H

View file

@ -255,11 +255,12 @@ namespace telebotxx
auto& obj = parseObject(parent, name, required, found);
if (found)
{
int id = parse<int>(obj, "id", REQUIRED);
auto firstName = parse<std::string>(obj, "first_name", REQUIRED);
auto lastName = parse<std::string>(obj, "last_name", OPTIONAL);
auto username = parse<std::string>(obj, "username", OPTIONAL);
return std::make_unique<User>(id, firstName, lastName, username);
auto user = std::make_unique<User>();
user->setId(parse<int>(obj, "id", REQUIRED));
user->setFirstName(parse<std::string>(obj, "first_name", REQUIRED));
user->setLastName(parse<std::string>(obj, "last_name", OPTIONAL));
user->setUsername(parse<std::string>(obj, "username", OPTIONAL));
return user;
}
else
return nullptr;

View file

@ -1,4 +1,5 @@
#include <telebotxx/User.hpp>
#include <sstream>
using namespace telebotxx;
@ -7,13 +8,9 @@ User::User()
{
}
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;
User::~User() = default;
int User::getId() const
{
@ -55,6 +52,13 @@ void User::setUsername(const std::string& username)
username_ = username;
}
const std::string User::toString() const
{
std::stringstream ss;
ss << firstName_ << " " << lastName_ << " (@" << username_ << ")";
return ss.str();
}
void User::swap(User& other) noexcept
{
std::swap(id_, other.id_);
@ -68,3 +72,9 @@ const User& User::operator=(User other)
swap(other);
return *this;
}
std::ostream& telebotxx::operator<<(std::ostream& os, const User& user)
{
os << user.toString();
return os;
}

View file

@ -12,20 +12,14 @@ BOOST_AUTO_TEST_SUITE(TestUser)
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");
User other;
other.setId(1);
other.setFirstName("John");
other.setLastName("Smith");
other.setUsername("john_smith");
std::unique_ptr<User> user;
BOOST_REQUIRE_NO_THROW(user.reset(new User(other)));
BOOST_REQUIRE_EQUAL(user->getId(), 1);
@ -37,7 +31,11 @@ BOOST_AUTO_TEST_SUITE(TestUser)
BOOST_AUTO_TEST_CASE(MoveConstructor)
{
PRINT_TESTNAME;
User other(1, "John", "Smith", "john_smith");
User other;
other.setId(1);
other.setFirstName("John");
other.setLastName("Smith");
other.setUsername("john_smith");
std::unique_ptr<User> user;
BOOST_REQUIRE_NO_THROW(user.reset(new User(std::move(other))));
BOOST_REQUIRE_EQUAL(user->getId(), 1);