From fcc18ee3cfeb1cd2ebc69ae7b6e402e9d296f8e4 Mon Sep 17 00:00:00 2001 From: Evgenii Sopov Date: Mon, 25 May 2020 00:20:37 +0700 Subject: [PATCH] Prepare first version --- .travis.yml | 1 + README.md | 21 +++++----- src/main.cpp | 12 +++--- src/wsjcpp_dto.cpp | 22 +++++----- src/wsjcpp_dto.h | 7 ++-- unit-tests.wsjcpp/CMakeLists.txt | 4 ++ .../src/unit_test_dto_person.cpp | 40 +++++++++++++++++++ unit-tests.wsjcpp/src/unit_test_dto_person.h | 15 +++++++ wsjcpp.yml | 17 ++++++++ 9 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 unit-tests.wsjcpp/src/unit_test_dto_person.cpp create mode 100644 unit-tests.wsjcpp/src/unit_test_dto_person.h diff --git a/.travis.yml b/.travis.yml index 933ea81..58e0159 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ addons: # Build steps script: - ./build_simple.sh + - ./wsjcpp-dto - cd unit-tests.wsjcpp - ./build_simple.sh - ./unit-tests diff --git a/README.md b/README.md index f2566ac..ebb3ba3 100644 --- a/README.md +++ b/README.md @@ -6,25 +6,24 @@ Helper classes for create C++ Data Transfer Object ## Integrate to your project -Include this files: - -* src.wsjcpp/nlohmann_json/json.hpp -* src/wsjcpp_dto.h -* src/wsjcpp_dto.cpp - -Or use a `wsjcpp`: +Use a `wsjcpp`: ``` $ wsjcpp install https://github.com/wsjcpp/wsjcpp-dto:master ``` -## How to use: +Or include this files: + +* src.wsjcpp/nlohmann_json/json.hpp +* src/wsjcpp_dto.h +* src/wsjcpp_dto.cpp + +## How to use ``` -class WsjcppDtoPerson : public WsjcppDefineDto { +class WsjcppDtoPerson : public WsjcppDto { public: - WsjcppDtoPerson() : WsjcppDefineDto("person", "Person") { - // requireField("name", "Name of person"); + WsjcppDtoPerson() : WsjcppDto("person", "Person") { requireField("name", "Name of person"); optionalField("age", "Age of person"); }; diff --git a/src/main.cpp b/src/main.cpp index 4031b4d..ea48597 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,18 +4,18 @@ #include #include -class WsjcppDtoPerson : public WsjcppDefineDto { +class WsjcppDtoPerson : public WsjcppDto { public: - WsjcppDtoPerson() : WsjcppDefineDto("person", "Person") { + WsjcppDtoPerson() : WsjcppDto("person", "Person") { // requireField("name", "Name of person"); requireField("name", "Name of person"); optionalField("age", "Age of person"); }; }; -class WsjcppDtoUserProfile : public WsjcppDefineDto { +class WsjcppDtoUserProfile : public WsjcppDto { public: - WsjcppDtoUserProfile() : WsjcppDefineDto("user_profile", "User Profile") { + WsjcppDtoUserProfile() : WsjcppDto("user_profile", "User Profile") { // requireField("name", "Name of person"); requireField("userid", "User Id"); requireField("", "Current Page"); @@ -26,9 +26,9 @@ class WsjcppDtoUserProfile : public WsjcppDefineDto { }; }; -class WsjcppDtoPersonsPage : public WsjcppDefineDto { +class WsjcppDtoPersonsPage : public WsjcppDto { public: - WsjcppDtoPersonsPage() : WsjcppDefineDto("persons_page", "Persons page") { + WsjcppDtoPersonsPage() : WsjcppDto("persons_page", "Persons page") { // requireField("name", "Name of person"); requireField("page", "Current Page"); requireField("onpage", "Nomebr per page"); diff --git a/src/wsjcpp_dto.cpp b/src/wsjcpp_dto.cpp index b970e93..3fdd541 100644 --- a/src/wsjcpp_dto.cpp +++ b/src/wsjcpp_dto.cpp @@ -80,9 +80,9 @@ WsjcppDtoInteger::WsjcppDtoInteger(const std::string &sName, const std::string & } // --------------------------------------------------------------------- -// WsjcppDefineDto +// WsjcppDto -WsjcppDefineDto::WsjcppDefineDto(const std::string &sTypeName, const std::string &sDescription) { +WsjcppDto::WsjcppDto(const std::string &sTypeName, const std::string &sDescription) { TAG = "DTO-" + sTypeName; m_sTypeName = sTypeName; m_sDescription = sDescription; @@ -90,19 +90,19 @@ WsjcppDefineDto::WsjcppDefineDto(const std::string &sTypeName, const std::string // --------------------------------------------------------------------- -const std::string &WsjcppDefineDto::getTypeName() { +const std::string &WsjcppDto::getTypeName() { return m_sTypeName; } // --------------------------------------------------------------------- -const std::string &WsjcppDefineDto::getDescription() { +const std::string &WsjcppDto::getDescription() { return m_sDescription; } // --------------------------------------------------------------------- -bool WsjcppDefineDto::setFieldStringValue(const std::string &sFieldName, const std::string &sFieldValue) { +bool WsjcppDto::setFieldStringValue(const std::string &sFieldName, const std::string &sFieldValue) { WsjcppDefineFieldDto *pField = this->findFieldByName(sFieldName); if (pField == nullptr) { WsjcppLog::throw_err(TAG, "Object '" + m_sTypeName + "' has not field '" + sFieldName + "'"); @@ -117,7 +117,7 @@ bool WsjcppDefineDto::setFieldStringValue(const std::string &sFieldName, const s // --------------------------------------------------------------------- -bool WsjcppDefineDto::setFieldIntegerValue(const std::string &sFieldName, int nValue) { +bool WsjcppDto::setFieldIntegerValue(const std::string &sFieldName, int nValue) { WsjcppDefineFieldDto *pField = this->findFieldByName(sFieldName); if (pField == nullptr) { WsjcppLog::throw_err(TAG, "Object '" + m_sTypeName + "' has not field '" + sFieldName + "'"); @@ -130,7 +130,7 @@ bool WsjcppDefineDto::setFieldIntegerValue(const std::string &sFieldName, int nV // --------------------------------------------------------------------- -bool WsjcppDefineDto::setFieldBooleanValue(const std::string &sFieldName, bool bValue) { +bool WsjcppDto::setFieldBooleanValue(const std::string &sFieldName, bool bValue) { WsjcppDefineFieldDto *pField = this->findFieldByName(sFieldName); if (pField == nullptr) { WsjcppLog::throw_err(TAG, "Object '" + m_sTypeName + "' has not field '" + sFieldName + "'"); @@ -143,7 +143,7 @@ bool WsjcppDefineDto::setFieldBooleanValue(const std::string &sFieldName, bool b // --------------------------------------------------------------------- -bool WsjcppDefineDto::setFieldJsonValue(const std::string &sFieldName, const nlohmann::json &jsonFieldValue) { +bool WsjcppDto::setFieldJsonValue(const std::string &sFieldName, const nlohmann::json &jsonFieldValue) { WsjcppDefineFieldDto *pField = this->findFieldByName(sFieldName); if (pField == nullptr) { WsjcppLog::throw_err(TAG, "Object '" + m_sTypeName + "' has not field '" + sFieldName + "'"); @@ -156,19 +156,19 @@ bool WsjcppDefineDto::setFieldJsonValue(const std::string &sFieldName, const nlo // --------------------------------------------------------------------- -bool WsjcppDefineDto::fillFromJson(nlohmann::json &jsonData, std::string &sError) { +bool WsjcppDto::fillFromJson(nlohmann::json &jsonData, std::string &sError) { return false; } // --------------------------------------------------------------------- -nlohmann::json WsjcppDefineDto::toJson() { +nlohmann::json WsjcppDto::toJson() { return m_jsonReadyObject; } // --------------------------------------------------------------------- -WsjcppDefineFieldDto *WsjcppDefineDto::findFieldByName(const std::string &sFieldName) { +WsjcppDefineFieldDto *WsjcppDto::findFieldByName(const std::string &sFieldName) { for (int i = 0; i < m_vFields.size(); i++) { if (m_vFields[i]->getFieldName() == sFieldName) { return m_vFields[i]; diff --git a/src/wsjcpp_dto.h b/src/wsjcpp_dto.h index 5c4464e..57f2b16 100644 --- a/src/wsjcpp_dto.h +++ b/src/wsjcpp_dto.h @@ -63,11 +63,11 @@ class WsjcppDtoBoolean : public WsjcppDefineFieldDto { }; // --------------------------------------------------------------------- -// WsjcppDefineDto - parent class for Data Transfer Objects +// WsjcppDto - parent class for Data Transfer Objects -class WsjcppDefineDto { +class WsjcppDto { public: - WsjcppDefineDto(const std::string &sTypeName, const std::string &sDescription); + WsjcppDto(const std::string &sTypeName, const std::string &sDescription); const std::string &getTypeName(); const std::string &getDescription(); @@ -115,7 +115,6 @@ class WsjcppDefineDto { std::string m_sTypeName; std::string m_sDescription; - std::vector m_vFields; nlohmann::json m_jsonReadyObject; diff --git a/unit-tests.wsjcpp/CMakeLists.txt b/unit-tests.wsjcpp/CMakeLists.txt index e6ea093..9f97599 100644 --- a/unit-tests.wsjcpp/CMakeLists.txt +++ b/unit-tests.wsjcpp/CMakeLists.txt @@ -35,9 +35,13 @@ list (APPEND WSJCPP_SOURCES "../src.wsjcpp/nlohmann_json/json.hpp") # wsjcpp-dto:v0.0.1 list (APPEND WSJCPP_INCLUDE_DIRS "../src") +list (APPEND WSJCPP_SOURCES "../src/wsjcpp_dto.h") +list (APPEND WSJCPP_SOURCES "../src/wsjcpp_dto.cpp") # unit-tests list (APPEND WSJCPP_INCLUDE_DIRS "src") +list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_dto_person.h") +list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_dto_person.cpp") include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt) diff --git a/unit-tests.wsjcpp/src/unit_test_dto_person.cpp b/unit-tests.wsjcpp/src/unit_test_dto_person.cpp new file mode 100644 index 0000000..ff5741c --- /dev/null +++ b/unit-tests.wsjcpp/src/unit_test_dto_person.cpp @@ -0,0 +1,40 @@ +#include "unit_test_dto_person.h" +#include +#include +#include + +REGISTRY_WSJCPP_UNIT_TEST(UnitTestDtoPerson) + +UnitTestDtoPerson::UnitTestDtoPerson() + : WsjcppUnitTestBase("UnitTestDtoPerson") { +} + +// --------------------------------------------------------------------- + +void UnitTestDtoPerson::init() { + // nothing +} + +// --------------------------------------------------------------------- + +class WsjcppDtoPerson : public WsjcppDto { + public: + WsjcppDtoPerson() : WsjcppDto("person", "Person") { + requireField("name", "Name of person"); + optionalField("age", "Age of person"); + }; +}; + +// --------------------------------------------------------------------- + +bool UnitTestDtoPerson::run() { + bool bTestSuccess = true; + WsjcppDtoPerson p; + p.setFieldStringValue("name", "John Smith"); + p.setFieldIntegerValue("age", 21); + std::string sJsonGot = p.toJson().dump(); + std::string sJsonExpected = "{\"age\":21,\"name\":\"John Smith\"}"; + compareS(bTestSuccess, "WsjcppDtoPerson", sJsonGot, sJsonExpected); + return bTestSuccess; +} + diff --git a/unit-tests.wsjcpp/src/unit_test_dto_person.h b/unit-tests.wsjcpp/src/unit_test_dto_person.h new file mode 100644 index 0000000..5259ebe --- /dev/null +++ b/unit-tests.wsjcpp/src/unit_test_dto_person.h @@ -0,0 +1,15 @@ +#ifndef UNIT_TEST_DTO_PERSON_H +#define UNIT_TEST_DTO_PERSON_H + +#include + +// Description: TODO +class UnitTestDtoPerson : public WsjcppUnitTestBase { + public: + UnitTestDtoPerson(); + virtual void init(); + virtual bool run(); +}; + +#endif // UNIT_TEST_DTO_PERSON_H + diff --git a/wsjcpp.yml b/wsjcpp.yml index a021c60..2701ec8 100644 --- a/wsjcpp.yml +++ b/wsjcpp.yml @@ -3,14 +3,18 @@ version: "v0.0.1" cmake_minimum_required: "3.0" cmake_cxx_standard: "11" description: "Helper classes for create C++ Data Transfer Object " + authors: - name: "Evgenii Sopov" email: "mrseakg@gmail.com" + origins: - address: "https://sea-kg.com/wsjcpp-package-registry/" type: "package-registry" + keywords: - "c++" + dependencies: - name: "wsjcpp-core" version: "v0.1.5" @@ -22,3 +26,16 @@ dependencies: url: "https://github.com/nlohmann/json:develop" origin: "https://github.com/" installation-dir: "./src.wsjcpp/nlohmann_json" + +unit-tests: + cases: + - name: "DtoPerson" + description: "Test DtoPerson" + +distribution: + - source-file: "src/wsjcpp_dto.h" + target-file: "wsjcpp_dto.h" + type: "source-code" + - source-file: "src/wsjcpp_dto.cpp" + target-file: "wsjcpp_dto.cpp" + type: "source-code"