mirror of
https://github.com/UltraCoderRU/telebotxx.git
synced 2026-01-28 04:05:13 +00:00
Full constructor removed from class User.
User::toString() implemented. operator<< (User) implemented.
This commit is contained in:
parent
0e896f7066
commit
90d2c2dc96
4 changed files with 37 additions and 30 deletions
|
|
@ -10,16 +10,9 @@ namespace telebotxx
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
User();
|
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(const User&);
|
||||||
User(User&&);
|
User(User&&);
|
||||||
|
~User();
|
||||||
|
|
||||||
/// \brief Get id
|
/// \brief Get id
|
||||||
int getId() const;
|
int getId() const;
|
||||||
|
|
@ -45,6 +38,9 @@ namespace telebotxx
|
||||||
/// \brief Set username
|
/// \brief Set username
|
||||||
void setUsername(const std::string& username);
|
void setUsername(const std::string& username);
|
||||||
|
|
||||||
|
/// \brief Get string representation of user
|
||||||
|
const std::string toString() const;
|
||||||
|
|
||||||
void swap(User&) noexcept;
|
void swap(User&) noexcept;
|
||||||
|
|
||||||
const User& operator=(User other);
|
const User& operator=(User other);
|
||||||
|
|
@ -57,6 +53,8 @@ namespace telebotxx
|
||||||
};
|
};
|
||||||
|
|
||||||
using UserPtr = std::shared_ptr<User>;
|
using UserPtr = std::shared_ptr<User>;
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const User& user);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // TELEBOTXX_USER_H
|
#endif // TELEBOTXX_USER_H
|
||||||
|
|
|
||||||
|
|
@ -255,11 +255,12 @@ namespace telebotxx
|
||||||
auto& obj = parseObject(parent, name, required, found);
|
auto& obj = parseObject(parent, name, required, found);
|
||||||
if (found)
|
if (found)
|
||||||
{
|
{
|
||||||
int id = parse<int>(obj, "id", REQUIRED);
|
auto user = std::make_unique<User>();
|
||||||
auto firstName = parse<std::string>(obj, "first_name", REQUIRED);
|
user->setId(parse<int>(obj, "id", REQUIRED));
|
||||||
auto lastName = parse<std::string>(obj, "last_name", OPTIONAL);
|
user->setFirstName(parse<std::string>(obj, "first_name", REQUIRED));
|
||||||
auto username = parse<std::string>(obj, "username", OPTIONAL);
|
user->setLastName(parse<std::string>(obj, "last_name", OPTIONAL));
|
||||||
return std::make_unique<User>(id, firstName, lastName, username);
|
user->setUsername(parse<std::string>(obj, "username", OPTIONAL));
|
||||||
|
return user;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
||||||
20
src/User.cpp
20
src/User.cpp
|
|
@ -1,4 +1,5 @@
|
||||||
#include <telebotxx/User.hpp>
|
#include <telebotxx/User.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
using namespace telebotxx;
|
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(const User&) = default;
|
||||||
User::User(User&&) = default;
|
User::User(User&&) = default;
|
||||||
|
User::~User() = default;
|
||||||
|
|
||||||
int User::getId() const
|
int User::getId() const
|
||||||
{
|
{
|
||||||
|
|
@ -55,6 +52,13 @@ void User::setUsername(const std::string& username)
|
||||||
username_ = 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
|
void User::swap(User& other) noexcept
|
||||||
{
|
{
|
||||||
std::swap(id_, other.id_);
|
std::swap(id_, other.id_);
|
||||||
|
|
@ -68,3 +72,9 @@ const User& User::operator=(User other)
|
||||||
swap(other);
|
swap(other);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& telebotxx::operator<<(std::ostream& os, const User& user)
|
||||||
|
{
|
||||||
|
os << user.toString();
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,20 +12,14 @@ BOOST_AUTO_TEST_SUITE(TestUser)
|
||||||
BOOST_REQUIRE_NO_THROW(User user);
|
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)
|
BOOST_AUTO_TEST_CASE(CopyConstructor)
|
||||||
{
|
{
|
||||||
PRINT_TESTNAME;
|
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;
|
std::unique_ptr<User> user;
|
||||||
BOOST_REQUIRE_NO_THROW(user.reset(new User(other)));
|
BOOST_REQUIRE_NO_THROW(user.reset(new User(other)));
|
||||||
BOOST_REQUIRE_EQUAL(user->getId(), 1);
|
BOOST_REQUIRE_EQUAL(user->getId(), 1);
|
||||||
|
|
@ -37,7 +31,11 @@ BOOST_AUTO_TEST_SUITE(TestUser)
|
||||||
BOOST_AUTO_TEST_CASE(MoveConstructor)
|
BOOST_AUTO_TEST_CASE(MoveConstructor)
|
||||||
{
|
{
|
||||||
PRINT_TESTNAME;
|
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;
|
std::unique_ptr<User> user;
|
||||||
BOOST_REQUIRE_NO_THROW(user.reset(new User(std::move(other))));
|
BOOST_REQUIRE_NO_THROW(user.reset(new User(std::move(other))));
|
||||||
BOOST_REQUIRE_EQUAL(user->getId(), 1);
|
BOOST_REQUIRE_EQUAL(user->getId(), 1);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue