From 90d2c2dc968b30e2afefd51a38a1130ce2b6b61f Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Wed, 9 Nov 2016 19:44:19 +0300 Subject: [PATCH] Full constructor removed from class User. User::toString() implemented. operator<< (User) implemented. --- include/telebotxx/User.hpp | 14 ++++++-------- src/JsonObjects.cpp | 11 ++++++----- src/User.cpp | 20 +++++++++++++++----- tests/TestTypes.cpp | 22 ++++++++++------------ 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/include/telebotxx/User.hpp b/include/telebotxx/User.hpp index 00bd1fd..375f3b9 100644 --- a/include/telebotxx/User.hpp +++ b/include/telebotxx/User.hpp @@ -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; + + std::ostream& operator<<(std::ostream& os, const User& user); } #endif // TELEBOTXX_USER_H diff --git a/src/JsonObjects.cpp b/src/JsonObjects.cpp index 86a1f2c..b2c82fa 100644 --- a/src/JsonObjects.cpp +++ b/src/JsonObjects.cpp @@ -255,11 +255,12 @@ namespace telebotxx auto& obj = parseObject(parent, name, required, found); if (found) { - int id = parse(obj, "id", REQUIRED); - auto firstName = parse(obj, "first_name", REQUIRED); - auto lastName = parse(obj, "last_name", OPTIONAL); - auto username = parse(obj, "username", OPTIONAL); - return std::make_unique(id, firstName, lastName, username); + auto user = std::make_unique(); + user->setId(parse(obj, "id", REQUIRED)); + user->setFirstName(parse(obj, "first_name", REQUIRED)); + user->setLastName(parse(obj, "last_name", OPTIONAL)); + user->setUsername(parse(obj, "username", OPTIONAL)); + return user; } else return nullptr; diff --git a/src/User.cpp b/src/User.cpp index 9f03891..e8458a2 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -1,4 +1,5 @@ #include +#include 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; +} diff --git a/tests/TestTypes.cpp b/tests/TestTypes.cpp index 009ee20..2574734 100644 --- a/tests/TestTypes.cpp +++ b/tests/TestTypes.cpp @@ -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; 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; BOOST_REQUIRE_NO_THROW(user.reset(new User(std::move(other)))); BOOST_REQUIRE_EQUAL(user->getId(), 1);