diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index f21a025..94a98d4 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -72,4 +72,4 @@ jobs: working-directory: ${{ steps.strings.outputs.build-output-dir }} # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest --build-config ${{ matrix.build_type }} + run: ctest --output-on-failure --build-config ${{ matrix.build_type }} diff --git a/.gitignore b/.gitignore index c35d732..eeb996f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ wsjcpp-yaml /tmp +/build .wsjcpp-yaml-logs/* .vscode .wsjcpp-cache/* @@ -51,4 +52,4 @@ __recovery *.ils *.tds *.pdb -*.map \ No newline at end of file +*.map diff --git a/CHANGELOG.md b/CHANGELOG.md index a74226f..b6bf5ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to wsjcpp-yaml project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [v0.1.9] - 2025-12-18 (2025 Dec 18) + +- Migrated unit-tests.wsjcpp to ctest +- Fixed config for .github actions + ## [v0.1.8] - 2025-06-03 (2025 Jun 3) - Fixed #35 Add valFloat / valDouble diff --git a/CMakeLists.txt b/CMakeLists.txt index 2033fa4..3b8e5dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,11 @@ include_directories(${WSJCPP_INCLUDE_DIRS}) add_executable ("wsjcpp-yaml" ${WSJCPP_SOURCES}) -target_link_libraries("wsjcpp-yaml" -lpthread ${WSJCPP_LIBRARIES} ) +if(WIN32) + target_link_libraries("wsjcpp-yaml" ${WSJCPP_LIBRARIES} ) +else() + target_link_libraries("wsjcpp-yaml" -lpthread ${WSJCPP_LIBRARIES} ) +endif() install( TARGETS diff --git a/build_simple.sh b/build_simple.sh index 21ea83d..39c29d3 100755 --- a/build_simple.sh +++ b/build_simple.sh @@ -25,5 +25,5 @@ check_ret $? "configure" cmake --build ./tmp/linux_release --config Release check_ret $? "build" -cd ./tmp/linux_release && ctest --output-on-failure +cd ./tmp/linux_release && ctest --output-on-failure --build-config Release check_ret $? "tests" diff --git a/unit-tests.wsjcpp/parser_yaml.py b/parser_yaml.py similarity index 62% rename from unit-tests.wsjcpp/parser_yaml.py rename to parser_yaml.py index b3f2c03..25dc0e9 100755 --- a/unit-tests.wsjcpp/parser_yaml.py +++ b/parser_yaml.py @@ -3,13 +3,13 @@ import yaml -a_yaml_file = open("./data-tests/read-write-file/docker-compose.yml") +a_yaml_file = open("./data/read-write-file/docker-compose.yml") parsed_yaml_file = yaml.load(a_yaml_file, Loader=yaml.FullLoader) print(parsed_yaml_file) -a_yaml_file2 = open("./data-tests/parser-simple-array/file.yml") +a_yaml_file2 = open("./data/parser-simple-array/file.yml") parsed_yaml_file2 = yaml.load(a_yaml_file2, Loader=yaml.FullLoader) diff --git a/src.wsjcpp/CMakeLists.txt b/src.wsjcpp/CMakeLists.txt index a8bea6d..bcaeaa0 100644 --- a/src.wsjcpp/CMakeLists.txt +++ b/src.wsjcpp/CMakeLists.txt @@ -1,7 +1,7 @@ -# Automaticly generated by wsjcpp@v0.2.4 +# Automaticly generated by wsjcpp@v0.2.5 cmake_minimum_required(VERSION 3.0) -add_definitions(-DWSJCPP_APP_VERSION="v0.1.8") +add_definitions(-DWSJCPP_APP_VERSION="v0.1.9") add_definitions(-DWSJCPP_APP_NAME="wsjcpp-yaml") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") @@ -17,9 +17,5 @@ set (WSJCPP_SOURCES "") find_package(Threads REQUIRED) list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) -# wsjcpp-core:v0.2.1 -list (APPEND WSJCPP_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/./src.wsjcpp/wsjcpp_core/") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/./src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/./src.wsjcpp/wsjcpp_core/wsjcpp_core.h") diff --git a/src.wsjcpp/wsjcpp_core/generate.Class.wsjcpp-script b/src.wsjcpp/wsjcpp_core/generate.Class.wsjcpp-script deleted file mode 100644 index 4a0d11f..0000000 --- a/src.wsjcpp/wsjcpp_core/generate.Class.wsjcpp-script +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/wsjcpp-safe-scripting - -# log_info rootdir -# log_info script_filename - -make_dir "src" - -var user_class_name -set_value user_class_name arg1 -normalize_class_name user_class_name -convert_CamelCase_to_snake_case user_class_name user_class_name - -var class_name -set_value class_name arg1 -normalize_class_name class_name - -var base_filename -convert_CamelCase_to_snake_case class_name base_filename -# log_info base_filename - -var filename_cpp -concat filename_cpp "./src/" base_filename ".cpp" - -var filename_h -concat filename_h "./src/" base_filename ".h" - -var ifndef_header -set_value ifndef_header base_filename -concat ifndef_header "_H" - -to_upper_case ifndef_header - -var content_header -concat content_header "#ifndef " ifndef_header " -#define " ifndef_header " - -#include - -class " class_name " { - public: - " class_name "(); - - private: - std::string TAG; -}; - -#endif // " ifndef_header - - -var content_source -concat content_source " -#include \"" base_filename ".h\" -#include - -// --------------------------------------------------------------------- -// " class_name " - -" class_name "::" class_name "() { - TAG = \"" class_name "\"; -} - -" - -var file_source -concat file_source "src/" filename_cpp - -write_file filename_h content_header -write_file filename_cpp content_source - -log_info " -====== -Generated class: - - " class_name " -Generated files: - - " filename_h " - - " filename_cpp " -====== -" - -cmakelists_txt_append_wsjcpp filename_h -cmakelists_txt_append_wsjcpp filename_cpp \ No newline at end of file diff --git a/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest.wsjcpp-script b/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest.wsjcpp-script deleted file mode 100644 index fdf4902..0000000 --- a/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest.wsjcpp-script +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/wsjcpp-safe-scripting - -# log_info rootdir -# log_info script_filename - -make_dir "./unit-tests.wsjcpp" -make_dir "./unit-tests.wsjcpp/src" - -var class_name -set_value class_name arg1 -normalize_class_name class_name - -var filename_cpp -set_value filename_cpp arg2 - -var content_source -concat content_source " -#include -#include - -// --------------------------------------------------------------------- -// " class_name " - -class " class_name " : public WsjcppUnitTestBase { - public: - " class_name "(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(" class_name ") - -" class_name "::" class_name "() - : WsjcppUnitTestBase(\"" class_name "\") { -} - -// --------------------------------------------------------------------- - -bool " class_name "::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void " class_name "::executeTest() { - compare(\"Not implemented\", true, false); - // TODO unit test source code here -} - -// --------------------------------------------------------------------- - -bool " class_name "::doAfterTest() { - // do somethig after test - return true; -} - -" - -write_file filename_cpp content_source - -log_info " -====== -Generated class: - - " class_name " -Generated files: - - " filename_cpp " -====== -" diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml b/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml deleted file mode 100644 index 67b0e29..0000000 --- a/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml +++ /dev/null @@ -1,98 +0,0 @@ -wsjcpp_version: v0.0.1 -cmake_cxx_standard: 17 -cmake_minimum_required: 3.0 - -name: wsjcpp-core -version: v0.2.1 -description: Basic Utils for wsjcpp -issues: https://github.com/wsjcpp/wsjcpp-core/issues -repositories: - - type: main - url: "https://github.com/wsjcpp/wsjcpp-core" -keywords: - - c++ - - wsjcpp - -authors: - - name: Evgenii Sopov - email: mrseakg@gmail.com - -distribution: - - source-file: src/wsjcpp_core.cpp - target-file: wsjcpp_core.cpp - type: "source-code" - sha1: "00d1395210ee8bad10b2882be331c5ab4a0d1ffb" - - source-file: src/wsjcpp_core.h - target-file: wsjcpp_core.h - type: "source-code" # todo must be header-file - sha1: "a885e73c96bc564158cb97525525c1b7e5b455a6" - - source-file: "src/wsjcpp_unit_tests.cpp" - target-file: "wsjcpp_unit_tests.cpp" - type: "unit-tests" - sha1: "fd5989d1a83c8b90bdc4d5e9bc9c3051eaa1e6d2" - - source-file: "src/wsjcpp_unit_tests.h" - target-file: "wsjcpp_unit_tests.h" - type: "unit-tests" - sha1: "83d4b6e046b6b58c42882ccae4be413e03c401c1" - - source-file: "src/wsjcpp_unit_tests_main.cpp" - target-file: "wsjcpp_unit_tests_main.cpp" - type: "unit-tests" - sha1: "388ae269b325c5e161f6c3a5c598575714a4bffc" - - source-file: "scripts.wsjcpp/generate.WsjcppUnitTest.wsjcpp-script" - target-file: "generate.WsjcppUnitTest.wsjcpp-script" - type: "safe-scripting-generate" - sha1: "a7c9c2d19bf81c5b00e659384b0b92a99319a4c1" - - source-file: "scripts.wsjcpp/generate.Class.wsjcpp-script" - target-file: "generate.Class.wsjcpp-script" - type: "safe-scripting-generate" - - sha1: "de1799907c685d606b93e08b821b540c2faa2db1" -unit-tests: - cases: - - name: CoreNormalizePath - description: Check function normalizePath - - name: CoreExtractFilename - description: Check function extract filenane from path - - name: "ToUpper" - description: "String to upper" - - name: "CreateUuid" - description: "Test generation uuids" - - name: "GetEnv" - description: "Test getEnv function" - - name: "ToLower" - description: "Test toLower" - - name: "ReplaceAll" - description: "Test replace all" - - name: "DecodeUriComponent" - description: "Check decoding" - - name: "EncodeUriComponent" - description: "Check encoding" - - name: "Uint2HexString" - description: "Test convert unsigned int to hex string" - - name: "Split" - description: "Test split function" - - name: "CreateEmptyFile" - description: "Test create empty file" - - name: "ReadFileToBuffer" - description: "test for readFileToBuffer" - - name: "Join" - description: "Test join function" - - name: "getHumanSizeBytes" - description: "Test function get human size in bytes" - - name: "TestResources" - description: "Test basic resources" - - name: "ListOfDirs" - description: "Check list of directories" - - name: "FilePermissions" - description: "" - - name: "StringPadding" - description: "" - - name: "DateTimeFormat" - description: "" - - name: "MakeDirsPath" - description: "" - - name: "ExtractDirpath" - description: "" - - name: "ExtractFilepath" - description: "" - diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp deleted file mode 100644 index 4329de1..0000000 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp +++ /dev/null @@ -1,1329 +0,0 @@ -#include "wsjcpp_core.h" - -#ifndef _MSC_VER - #include - #include - #include - #include -#else - #include - #define PATH_MAX 256 -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -// #include -#include -#include - -// --------------------------------------------------------------------- -// WsjcppFilePermissions - -WsjcppFilePermissions::WsjcppFilePermissions() { - // default permissions - m_bOwnerReadFlag = true; - m_bOwnerWriteFlag = true; - m_bOwnerExecuteFlag = false; - m_bGroupReadFlag = false; - m_bGroupWriteFlag = false; - m_bGroupExecuteFlag = false; - m_bOtherReadFlag = true; - m_bOtherWriteFlag = false; - m_bOtherExecuteFlag = false; -} - -WsjcppFilePermissions::WsjcppFilePermissions( - bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag, - bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag, - bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag -) { - m_bOwnerReadFlag = bOwnerReadFlag; - m_bOwnerWriteFlag = bOwnerWriteFlag; - m_bOwnerExecuteFlag = bOwnerExecuteFlag; - m_bGroupReadFlag = bGroupReadFlag; - m_bGroupWriteFlag = bGroupWriteFlag; - m_bGroupExecuteFlag = bGroupExecuteFlag; - m_bOtherReadFlag = bOtherReadFlag; - m_bOtherWriteFlag = bOtherWriteFlag; - m_bOtherExecuteFlag = bOtherExecuteFlag; -} - -WsjcppFilePermissions::WsjcppFilePermissions(uint16_t nFilePermission) { - - // owner - m_bOwnerReadFlag = nFilePermission & 0x0400; - m_bOwnerWriteFlag = nFilePermission & 0x0200; - m_bOwnerExecuteFlag = nFilePermission & 0x0100; - - // group - m_bGroupReadFlag = nFilePermission & 0x0040; - m_bGroupWriteFlag = nFilePermission & 0x0020; - m_bGroupExecuteFlag = nFilePermission & 0x0010; - - // for other - m_bOtherReadFlag = nFilePermission & 0x0004; - m_bOtherWriteFlag = nFilePermission & 0x0002; - m_bOtherExecuteFlag = nFilePermission & 0x0001; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setOwnerReadFlag(bool bOwnerReadFlag) { - m_bOwnerReadFlag = bOwnerReadFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getOwnerReadFlag() const { - return m_bOwnerReadFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setOwnerWriteFlag(bool bOwnerWriteFlag) { - m_bOwnerWriteFlag = bOwnerWriteFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getOwnerWriteFlag() const { - return m_bOwnerWriteFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setOwnerExecuteFlag(bool bOwnerExecuteFlag) { - m_bOwnerExecuteFlag = bOwnerExecuteFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getOwnerExecuteFlag() const { - return m_bOwnerExecuteFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag) { - m_bOwnerReadFlag = bOwnerReadFlag; - m_bOwnerWriteFlag = bOwnerWriteFlag; - m_bOwnerExecuteFlag = bOwnerExecuteFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setGroupReadFlag(bool bGroupReadFlag) { - m_bGroupReadFlag = bGroupReadFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getGroupReadFlag() const { - return m_bGroupReadFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setGroupWriteFlag(bool bGroupWriteFlag) { - m_bGroupWriteFlag = bGroupWriteFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getGroupWriteFlag() const { - return m_bGroupWriteFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setGroupExecuteFlag(bool bGroupExecuteFlag) { - m_bGroupExecuteFlag = bGroupExecuteFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getGroupExecuteFlag() const { - return m_bGroupExecuteFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag) { - m_bGroupReadFlag = bGroupReadFlag; - m_bGroupWriteFlag = bGroupWriteFlag; - m_bGroupExecuteFlag = bGroupExecuteFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setOtherReadFlag(bool bOtherReadFlag) { - m_bOtherReadFlag = bOtherReadFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getOtherReadFlag() const { - return m_bOtherReadFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setOtherWriteFlag(bool bOtherWriteFlag) { - m_bOtherWriteFlag = bOtherWriteFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getOtherWriteFlag() const { - return m_bOtherWriteFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setOtherExecuteFlag(bool bOtherExecuteFlag) { - m_bOtherExecuteFlag = bOtherExecuteFlag; -} - -// --------------------------------------------------------------------- - -bool WsjcppFilePermissions::getOtherExecuteFlag() const { - return m_bOtherExecuteFlag; -} - -// --------------------------------------------------------------------- - -void WsjcppFilePermissions::setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag) { - m_bOtherReadFlag = bOtherReadFlag; - m_bOtherWriteFlag = bOtherWriteFlag; - m_bOtherExecuteFlag = bOtherExecuteFlag; -} - -// --------------------------------------------------------------------- - -std::string WsjcppFilePermissions::toString() const { - std::string sRet = "-"; - - // owner - sRet += m_bOwnerReadFlag ? "r" : "-"; - sRet += m_bOwnerWriteFlag ? "w" : "-"; - sRet += m_bOwnerExecuteFlag ? "x" : "-"; - - // group - sRet += m_bGroupReadFlag ? "r" : "-"; - sRet += m_bGroupWriteFlag ? "w" : "-"; - sRet += m_bGroupExecuteFlag ? "x" : "-"; - - // for other - sRet += m_bOtherReadFlag ? "r" : "-"; - sRet += m_bOtherWriteFlag ? "w" : "-"; - sRet += m_bOtherExecuteFlag ? "x" : "-"; - - return sRet; -} - -// --------------------------------------------------------------------- - -uint16_t WsjcppFilePermissions::toUInt16() const { - uint16_t nRet = 0x0; - // owner - nRet |= m_bOwnerReadFlag ? 0x0400 : 0x0; - nRet |= m_bOwnerWriteFlag ? 0x0200 : 0x0; - nRet |= m_bOwnerExecuteFlag ? 0x0100 : 0x0; - - // group - nRet += m_bGroupReadFlag ? 0x0040 : 0x0; - nRet += m_bGroupWriteFlag ? 0x0020 : 0x0; - nRet += m_bGroupExecuteFlag ? 0x0010 : 0x0; - - // for other - nRet += m_bOtherReadFlag ? 0x0004 : 0x0; - nRet += m_bOtherWriteFlag ? 0x0002 : 0x0; - nRet += m_bOtherExecuteFlag ? 0x0001 : 0x0; - return nRet; -} - -// --------------------------------------------------------------------- -// WsjcppCore - -bool WsjcppCore::init( - int argc, char** argv, - const std::string &sApplicationName, - const std::string &sApplicationVersion, - const std::string &sApplicationAuthor, - const std::string &sLibraryNameForExports -) { - // init random - // std::srand(std::time(0)); - WsjcppCore::initRandom(); - return true; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::doNormalizePath(const std::string & sPath) { - // split path by / - std::vector vNames; - std::string s = ""; - size_t nStrLen = sPath.length(); - for (size_t i = 0; i < sPath.length(); i++) { - if (sPath[i] == '/') { - vNames.push_back(s); - s = ""; - if (i == nStrLen-1) { - vNames.push_back(""); - } - } else { - s += sPath[i]; - } - } - if (s != "") { - vNames.push_back(s); - } - - // fildered - size_t nLen = vNames.size(); - std::vector vNewNames; - for (size_t i = 0; i < nLen; i++) { - std::string sCurrent = vNames[i]; - if (sCurrent == "" && i == nLen-1) { - vNewNames.push_back(sCurrent); - continue; - } - - if ((sCurrent == "" || sCurrent == ".") && i != 0) { - continue; - } - - if (sCurrent == ".." && vNewNames.size() > 0) { - std::string sPrev = vNewNames[vNewNames.size()-1]; - if (sPrev == "") { - vNewNames.pop_back(); - vNewNames.push_back(sCurrent); - } else if (sPrev != "." && sPrev != "..") { - vNewNames.pop_back(); - } else { - vNewNames.push_back(sCurrent); - } - } else { - vNewNames.push_back(sCurrent); - } - } - std::string sRet = ""; - size_t nNewLen = vNewNames.size(); - size_t nLastNew = nNewLen-1; - for (size_t i = 0; i < nNewLen; i++) { - sRet += vNewNames[i]; - if (i != nLastNew) { - sRet += "/"; - } - } - return sRet; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::extractFilename(const std::string &sPath) { - // split path by / - std::vector vNames; - std::string s = ""; - size_t nStrLen = sPath.length(); - for (size_t i = 0; i < sPath.length(); i++) { - if (sPath[i] == '/') { - vNames.push_back(s); - s = ""; - if (i == nStrLen-1) { - vNames.push_back(""); - } - } else { - s += sPath[i]; - } - } - if (s != "") { - vNames.push_back(s); - } - std::string sRet; - if (vNames.size() > 0) { - sRet = vNames[vNames.size()-1]; - } - return sRet; -} - -std::string WsjcppCore::extractDirpath(const std::string &sFullPath) { - std::vector vDirs = WsjcppCore::split(sFullPath, "/"); - vDirs.pop_back(); - return WsjcppCore::join(vDirs, "/"); -} - -std::string WsjcppCore::getCurrentDirectory() { - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) == NULL) { - WsjcppLog::throw_err("getCurrentDirectory", "Could not get current directory"); - } - return std::string(cwd) + "/"; -} - -long WsjcppCore::getCurrentTimeInMilliseconds() { - long nTimeStart = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); - return nTimeStart; -} - -// --------------------------------------------------------------------- - -long WsjcppCore::getCurrentTimeInSeconds() { - long nTimeStart = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); - return nTimeStart; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::getCurrentTimeForFilename() { - long nTimeStart = WsjcppCore::getCurrentTimeInSeconds(); - return WsjcppCore::formatTimeForFilename(nTimeStart); -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::getCurrentTimeForLogFormat() { - long nTimeStart = WsjcppCore::getCurrentTimeInMilliseconds(); - std::string sMilliseconds = std::to_string(int(nTimeStart % 1000)); - nTimeStart = nTimeStart / 1000; - - std::time_t tm_ = long(nTimeStart); - struct tm tstruct; - gmtime_r(&tm_, &tstruct); - - // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime - // for more information about date/time format - char buf[80]; - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tstruct); - return std::string(buf) + "." + std::string( 3 - sMilliseconds.length(), '0').append(sMilliseconds); -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::getThreadId() { - - static_assert(sizeof(std::thread::id)==sizeof(uint64_t),"this function only works if size of thead::id is equal to the size of uint_64"); - std::thread::id this_id = std::this_thread::get_id(); - uint64_t val = *((uint64_t*) &this_id); - std::stringstream stream; - stream << "0x" << std::setw(16) << std::setfill('0') << std::hex << val; - return std::string(stream.str()); -} - -std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) { - std::time_t tm_ = long(nTimeInSec); - // struct tm tstruct = *localtime(&tm_); - struct tm tstruct; - gmtime_r(&tm_, &tstruct); - - // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime - // for more information about date/time format - char buf[80]; - // Like: Wed, 22 Jul 2009 19:15:56 GMT - strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", &tstruct); - return std::string(buf); -} - -std::string WsjcppCore::formatTimeForFilename(long nTimeInSec) { - std::time_t tm_ = long(nTimeInSec); - // struct tm tstruct = *localtime(&tm_); - struct tm tstruct; - gmtime_r(&tm_, &tstruct); - - // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime - // for more information about date/time format - char buf[80]; - strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", &tstruct); - return std::string(buf); -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::formatTimeUTC(int nTimeInSec) { - // datetime - std::time_t tm_ = long(nTimeInSec); - // struct tm tstruct = *localtime(&tm_); - struct tm tstruct; - gmtime_r(&tm_, &tstruct); - - // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime - // for more information about date/time format - char buf[80]; - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tstruct); - return std::string(buf); -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::fileExists(const std::string &sFilename) { - struct stat st; - bool bExists = (stat(sFilename.c_str(), &st) == 0); - if (bExists) { - return (st.st_mode & S_IFDIR) == 0; - } - return false; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::dirExists(const std::string &sDirname) { - struct stat st; - bool bExists = (stat(sDirname.c_str(), &st) == 0); - if (bExists) { - return (st.st_mode & S_IFDIR) != 0; - } - return false; -} - - -// --------------------------------------------------------------------- - -std::vector WsjcppCore::getListOfDirs(const std::string &sDirname) { - std::vector vDirs; - if (!WsjcppCore::dirExists(sDirname)) { - return vDirs; - } - for (auto& entry : std::filesystem::directory_iterator(sDirname)) { - if (entry.is_directory()) { - std::string sPath = entry.path(); - sPath = sPath.substr(sDirname.length()+1); - vDirs.push_back(sPath); - } - } - std::sort(vDirs.begin(), vDirs.end()); - return vDirs; -} - -// --------------------------------------------------------------------- - -std::vector WsjcppCore::getListOfFiles(const std::string &sDirname) { - std::vector vFiles; - if (!WsjcppCore::dirExists(sDirname)) { - return vFiles; - } - for (auto& entry: std::filesystem::directory_iterator(sDirname)) { - if (!entry.is_directory()) { - std::string sPath = entry.path(); - vFiles.push_back(sPath); - } - } - return vFiles; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::makeDir(const std::string &sDirname) { - struct stat st; - - const std::filesystem::path dir{sDirname}; - std::filesystem::create_directory(dir); - - int nStatus = mkdir(sDirname.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if (nStatus == 0) { - return true; - } - if (nStatus == EACCES) { - std::cout << "FAILED create folder " << sDirname << std::endl; - return false; - } - // std::cout << "nStatus: " << nStatus << std::endl; - return true; -} - -bool WsjcppCore::makeDirsPath(const std::string &sDirname) { - std::string sDirpath = WsjcppCore::doNormalizePath(sDirname); - std::vector vDirs = WsjcppCore::split(sDirpath, "/"); - std::string sDirpath2 = ""; - for (int i = 0; i < vDirs.size(); i++) { - if (vDirs[i] == "") { - continue; - } - sDirpath2 += vDirs[i] + "/"; - if (WsjcppCore::dirExists(sDirpath2)) { - continue; - } else { - if (!WsjcppCore::makeDir(sDirpath2)) { - return false; - } - } - } - return true; -} - -bool WsjcppCore::writeFile(const std::string &sFilename, const std::string &sContent) { - - // std::ofstream f(sFilename, std::ifstream::in); - std::ofstream f(sFilename, std::ios::out); - if (!f) { - WsjcppLog::err("WsjcppCore", "Could not create file to write '" + sFilename + "'"); - return false; - } - f << sContent << std::endl; - f.close(); - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::readTextFile(const std::string &sFilename, std::string &sContent) { - - std::ifstream f(sFilename); - if (!f) { - std::cout << "FAILED could not open file to read " << sFilename << std::endl; - return false; - } - - sContent = std::string( - (std::istreambuf_iterator(f)), - std::istreambuf_iterator() - ); - - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::readFileToBuffer(const std::string &sFilename, char *pBuffer[], int &nBufferSize) { - std::ifstream f(sFilename, std::ifstream::binary); - if (!f) { - return false; - } - - // get length of file: - f.seekg (0, f.end); - nBufferSize = f.tellg(); - f.seekg (0, f.beg); - - *pBuffer = new char [nBufferSize]; - - // read data as a block: - f.read (*pBuffer, nBufferSize); - if (!f) { - WsjcppLog::err("WsjcppCore::readFileToBuffer", "Only " + std::to_string(f.gcount()) + " could be read"); - delete[] pBuffer; - f.close(); - return false; - } - f.close(); - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize) { - std::ofstream f(sFilename, std::ios::out | std::ios::binary); - if (!f) { - std::cout << "FAILED could not create file to wtite " << sFilename << std::endl; - return false; - } - f.write(pBuffer, nBufferSize); - f.close(); - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::removeFile(const std::string &sFilename) { - return remove(sFilename.c_str()) == 0; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::copyFile(const std::string &sSourceFilename, const std::string &sTargetFilename) { - if (!WsjcppCore::fileExists(sSourceFilename)) { - WsjcppLog::err("copyFile", "File '" + sSourceFilename + "' did not exists"); - return false; - } - - if (WsjcppCore::fileExists(sTargetFilename)) { - WsjcppLog::err("copyFile", "File '" + sTargetFilename + "' already exists"); - return false; - } - - std::ifstream src(sSourceFilename, std::ios::binary); - if (!src.is_open()) { - WsjcppLog::err("copyFile", "Could not open file '" + sSourceFilename + "' for read"); - return false; - } - - std::ofstream dst(sTargetFilename, std::ios::binary); - if (!dst.is_open()) { - WsjcppLog::err("copyFile", "Could not open file '" + sTargetFilename + "' for write"); - return false; - } - - dst << src.rdbuf(); - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::createEmptyFile(const std::string &sFilename) { - if (WsjcppCore::fileExists(sFilename)) { - return false; - } - std::ofstream f(sFilename, std::ios::out | std::ios::binary); - if (!f) { - std::cout << "FAILED could not create file to wtite " << sFilename << std::endl; - return false; - } - f.close(); - return true; -} - -// --------------------------------------------------------------------- - -std::string& WsjcppCore::ltrim(std::string& str, const std::string& chars) { - str.erase(0, str.find_first_not_of(chars)); - return str; -} - -// --------------------------------------------------------------------- - -std::string& WsjcppCore::rtrim(std::string& str, const std::string& chars) { - str.erase(str.find_last_not_of(chars) + 1); - return str; -} - -// --------------------------------------------------------------------- - -std::string& WsjcppCore::trim(std::string& str, const std::string& chars) { - return WsjcppCore::ltrim(WsjcppCore::rtrim(str, chars), chars); -} - -// --------------------------------------------------------------------- -// will worked only with latin - -std::string WsjcppCore::toLower(const std::string& str) { - std::string sRet = str; - std::transform(sRet.begin(), sRet.end(), sRet.begin(), ::tolower); - return sRet; -} - -// --------------------------------------------------------------------- -// will worked only with latin - -std::string WsjcppCore::toUpper(const std::string& str) { - std::string sRet = str; - std::transform(sRet.begin(), sRet.end(), sRet.begin(), ::toupper); - return sRet; -} - -// --------------------------------------------------------------------- - -void WsjcppCore::replaceAll(std::string& str, const std::string& sFrom, const std::string& sTo) { - if (sFrom.empty()) { - return; - } - size_t start_pos = 0; - while ((start_pos = str.find(sFrom, start_pos)) != std::string::npos) { - str.replace(start_pos, sFrom.length(), sTo); - start_pos += sTo.length(); // In case 'to' contains 'sFrom', like replacing 'x' with 'yx' - } -} - -// --------------------------------------------------------------------- - -std::vector WsjcppCore::split(const std::string& sWhat, const std::string& sDelim) { - std::vector vRet; - size_t nPos = 0; - size_t nLen = sWhat.length(); - size_t nDelimLen = sDelim.length(); - while (nPos < nLen) { - std::size_t nFoundPos = sWhat.find(sDelim, nPos); - if (nFoundPos != std::string::npos) { - std::string sToken = sWhat.substr(nPos, nFoundPos - nPos); - vRet.push_back(sToken); - nPos = nFoundPos + nDelimLen; - if (nFoundPos + nDelimLen == nLen) { // last delimiter - vRet.push_back(""); - } - } else { - std::string sToken = sWhat.substr(nPos, nLen - nPos); - vRet.push_back(sToken); - break; - } - } - return vRet; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::join(const std::vector &vWhat, const std::string& sDelim) { - std::string sRet; - for (int i = 0; i < vWhat.size(); i++) { - if (i != 0) { - sRet += sDelim; - } - sRet += vWhat[i]; - } - return sRet; -} - -// --------------------------------------------------------------------- - -void WsjcppCore::initRandom() { - time_t t = std::time(0); - std::srand((unsigned int)t); -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::createUuid() { - std::string sRet = "00000000-0000-0000-0000-000000000000"; - const std::string sAlphabet = "0123456789abcdef"; - for (int i = 0; i < 36; i++) { - if (i != 8 && i != 13 && i != 18 && i != 23) { - sRet[i] = sAlphabet[rand() % sAlphabet.length()]; - } - } - return sRet; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::uint2hexString(unsigned int n) { - std::string sRet; - for (int i = 0; i < 8; i++) { - sRet += "0123456789abcdef"[n % 16]; - n >>= 4; - } - return std::string(sRet.rbegin(), sRet.rend()); -} - -// --------------------------------------------------------------------- - -unsigned long WsjcppCore::convertVoidToULong(void *p) { - std::uintptr_t ret = reinterpret_cast(p); - return (unsigned long)ret; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::getPointerAsHex(void *p) { - std::uintptr_t i = reinterpret_cast(p); - std::stringstream stream; - stream << std::hex << i; - return "0x" + std::string(stream.str()); -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::extractURLProtocol(const std::string& sValue) { - std::string sRet = ""; - size_t nPosProtocol = sValue.find("://"); - if (nPosProtocol == std::string::npos) { - return sRet; - } - sRet = sValue.substr(0, nPosProtocol); - return sRet; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::getEnv(const std::string& sName, std::string& sValue) { - if (const char* env_p = std::getenv(sName.c_str())) { - sValue = std::string(env_p); - return true; - } - return false; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::encodeUriComponent(const std::string& sValue) { - std::stringstream ssRet; - for (int i = 0; i < sValue.length(); i++) { - char c = sValue[i]; - if ( - c == '-' || c == '_' || c == '.' || c == '!' - || c == '~' || c == '*' || c == '\'' - || c == '(' || c == ')' || (c >= '0' && c <= '9') - || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') - ) { - ssRet << c; - } else { - ssRet << '%' << std::uppercase << std::hex << (int)c; - } - } - return ssRet.str(); -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::decodeUriComponent(const std::string& sValue) { - std::string sRet = ""; - std::string sHex = ""; - int nLen = sValue.length(); - for (int i = 0; i < sValue.length(); i++) { - char c = sValue[i]; - if (c == '%') { - if (i+2 >= nLen) { - WsjcppLog::throw_err("WsjcppCore::decodeUriElement", "Wrong format of string"); - } - sHex = "0x"; - sHex += sValue[i+1]; - sHex += sValue[i+2]; - i = i + 2; - char c1 = std::stoul(sHex, nullptr, 16); - sRet += c1; - } else { - sRet += c; - } - } - return sRet; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::getHumanSizeBytes(long nBytes) { - if (nBytes == 0) { - return "0B"; - } - std::string arrPrefix[] = {"B", "KB", "MB", "GB", "TB", "PB"}; - long n0 = nBytes; - long n1 = 0; - for (int i = 0; i < 6; i++) { - if (n0 >= 1 && n0 < 1000) { - return std::to_string(n0) + arrPrefix[i]; - } - n0 = nBytes / 1000; - n1 = nBytes - n0 * 1000; - n0 += n1 >= 500 ? 1 : 0; - - nBytes = nBytes / 1000; - if (n0 == 0 && n1 == 0) { - return "fuck"; - } - } - return std::to_string(nBytes) + "PB"; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir) { - if (!WsjcppCore::dirExists(sSourceDir)) { - WsjcppLog::err("recoursiveCopyFiles", "Source Dir '" + sSourceDir + "' did not exists"); - return false; - } - - if (!WsjcppCore::dirExists(sTargetDir)) { - if (!WsjcppCore::makeDir(sTargetDir)) { - WsjcppLog::err("recoursiveCopyFiles", "Could not create target dir '" + sTargetDir + "'"); - return false; - } - } - - std::vector vFiles = WsjcppCore::getListOfFiles(sSourceDir); - for (int i = 0; i < vFiles.size(); i++) { - std::string sSourceFile = sSourceDir + "/" + vFiles[i]; - std::string sTargetFile = sTargetDir + "/" + vFiles[i]; - if (!WsjcppCore::copyFile(sSourceFile, sTargetFile)) { - return false; - } - } - - std::vector vDirs = WsjcppCore::getListOfDirs(sSourceDir); - for (int i = 0; i < vDirs.size(); i++) { - std::string sSourceDir2 = sSourceDir + "/" + vDirs[i]; - std::string sTargetDir2 = sTargetDir + "/" + vDirs[i]; - if (!WsjcppCore::dirExists(sTargetDir2)) { - if (!WsjcppCore::makeDir(sTargetDir2)) { - WsjcppLog::err("recoursiveCopyFiles", "Could not create target subdir '" + sTargetDir2 + "'"); - return false; - } - } - - if (!WsjcppCore::recoursiveCopyFiles(sSourceDir2, sTargetDir2)) { - return false; - } - } - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) { - if (!WsjcppCore::dirExists(sDir)) { - WsjcppLog::err("recoursiveCopyFiles", "Dir '" + sDir + "' did not exists"); - return false; - } - - std::vector vFiles = WsjcppCore::getListOfFiles(sDir); - for (int i = 0; i < vFiles.size(); i++) { - std::string sFile = sDir + "/" + vFiles[i]; - if (!WsjcppCore::removeFile(sFile)) { - return false; - } - } - - std::vector vDirs = WsjcppCore::getListOfDirs(sDir); - for (int i = 0; i < vDirs.size(); i++) { - std::string sDir2 = sDir + "/" + vDirs[i]; - if (!WsjcppCore::recoursiveRemoveDir(sDir2)) { - return false; - } - } - - if (!WsjcppCore::removeFile(sDir)) { - return false; - } - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError) { - - mode_t m = 0x0; - - // owner - m |= filePermissions.getOwnerReadFlag() ? S_IRUSR : 0x0; - m |= filePermissions.getOwnerWriteFlag() ? S_IWUSR : 0x0; - m |= filePermissions.getOwnerExecuteFlag() ? S_IXUSR : 0x0; - - // group - m |= filePermissions.getGroupReadFlag() ? S_IRGRP : 0x0; - m |= filePermissions.getGroupWriteFlag() ? S_IWGRP : 0x0; - m |= filePermissions.getGroupExecuteFlag() ? S_IXGRP : 0x0; - - // for other - m |= filePermissions.getOtherReadFlag() ? S_IROTH : 0x0; - m |= filePermissions.getOtherWriteFlag() ? S_IWOTH : 0x0; - m |= filePermissions.getOtherExecuteFlag() ? S_IXOTH : 0x0; - - if (chmod(sFilePath.c_str(), m) != 0) { - sError = "Could not change permissions for: '" + sFilePath + "'"; - return false; - } - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError) { - if (!WsjcppCore::fileExists(sFilePath)) { - sError = "File '" + sFilePath + "' - not found"; - return false; - } - - struct stat fileStat; - if (stat(sFilePath.c_str(), &fileStat) < 0) { - sError = "Could not get info about file '" + sFilePath + "'."; - return false; - } - - mode_t m = fileStat.st_mode; - - // S_ISDIR(fileStat.st_mode)) ? "d" : "-" - - // owner - filePermissions.setOwnerReadFlag(m & S_IRUSR); - filePermissions.setOwnerWriteFlag(m & S_IWUSR); - filePermissions.setOwnerExecuteFlag(m & S_IXUSR); - - - // group - filePermissions.setGroupReadFlag(m & S_IRGRP); - filePermissions.setGroupWriteFlag(m & S_IWGRP); - filePermissions.setGroupExecuteFlag(m & S_IXGRP); - - // for other - filePermissions.setOtherReadFlag(m & S_IROTH); - filePermissions.setOtherWriteFlag(m & S_IWOTH); - filePermissions.setOtherExecuteFlag(m & S_IXOTH); - - return true; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::doPadLeft(const std::string& sIn, char cWhat, size_t nLength) { - std::string sRet; - size_t nPadLen = nLength - sIn.length(); - for (size_t i = 0; i < nPadLen; i++) { - sRet += cWhat; - } - return sRet + sIn; -} - -// --------------------------------------------------------------------- - -std::string WsjcppCore::doPadRight(const std::string& sIn, char cWhat, size_t nLength) { - std::string sRet; - size_t nPadLen = nLength - sIn.length(); - for (size_t i = 0; i < nPadLen; i++) { - sRet += cWhat; - } - return sIn + sRet; -} - -// --------------------------------------------------------------------- -// WsjcppLog - -WsjcppLogGlobalConf::WsjcppLogGlobalConf() { - // - logDir = "./"; - logPrefixFile = ""; - logFile = ""; - enableLogFile = true; - logStartTime = 0; - logRotationPeriodInSeconds = 51000; -} - -// --------------------------------------------------------------------- - -void WsjcppLogGlobalConf::doLogRotateUpdateFilename(bool bForce) { - long t = WsjcppCore::getCurrentTimeInSeconds(); - long nEverySeconds = logRotationPeriodInSeconds; // rotate log if started now or if time left more then 1 day - if (logStartTime == 0 || t - logStartTime > nEverySeconds || bForce) { - logStartTime = t; - logFile = logDir + "/" - + logPrefixFile + "_" - + WsjcppCore::formatTimeForFilename(logStartTime) + ".log"; - } -} - -WsjcppLogGlobalConf WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF; - -// --------------------------------------------------------------------- - -void WsjcppLog::info(const std::string & sTag, const std::string &sMessage) { - WsjcppColorModifier def(WsjcppColorCode::FG_DEFAULT); - WsjcppLog::add(def, "INFO", sTag, sMessage); -} - -// --------------------------------------------------------------------- - -void WsjcppLog::err(const std::string & sTag, const std::string &sMessage) { - WsjcppColorModifier red(WsjcppColorCode::FG_RED); - WsjcppLog::add(red, "ERR", sTag, sMessage); -} - -// --------------------------------------------------------------------- - -void WsjcppLog::throw_err(const std::string &sTag, const std::string &sMessage) { - WsjcppColorModifier red(WsjcppColorCode::FG_RED); - WsjcppLog::add(red, "ERR", sTag, sMessage); - throw std::runtime_error(sMessage); -} - -// --------------------------------------------------------------------- - -void WsjcppLog::warn(const std::string & sTag, const std::string &sMessage) { - WsjcppColorModifier yellow(WsjcppColorCode::FG_YELLOW); - WsjcppLog::add(yellow, "WARN",sTag, sMessage); -} - -// --------------------------------------------------------------------- - -void WsjcppLog::ok(const std::string &sTag, const std::string &sMessage) { - WsjcppColorModifier green(WsjcppColorCode::FG_GREEN); - WsjcppLog::add(green, "OK", sTag, sMessage); -} - -// --------------------------------------------------------------------- - -std::vector WsjcppLog::getLastLogMessages() { - std::lock_guard lock(WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logMutex); - std::vector vRet; - for (int i = 0; i < WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logLastMessages.size(); i++) { - vRet.push_back(WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logLastMessages[i]); - } - return vRet; -} - -// --------------------------------------------------------------------- - -void WsjcppLog::setLogDirectory(const std::string &sDirectoryPath) { - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logDir = sDirectoryPath; - if (!WsjcppCore::dirExists(sDirectoryPath)) { - if (!WsjcppCore::makeDir(sDirectoryPath)) { - WsjcppLog::err("setLogDirectory", "Could not create log directory '" + sDirectoryPath + "'"); - } - } - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.doLogRotateUpdateFilename(true); -} - -// --------------------------------------------------------------------- - -void WsjcppLog::setPrefixLogFile(const std::string &sPrefixLogFile) { - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logPrefixFile = sPrefixLogFile; - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.doLogRotateUpdateFilename(true); -} - -// --------------------------------------------------------------------- - -void WsjcppLog::setEnableLogFile(bool bEnable) { - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.enableLogFile = bEnable; -} - -// --------------------------------------------------------------------- - -void WsjcppLog::setRotationPeriodInSec(long nRotationPeriodInSec) { - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logRotationPeriodInSeconds = nRotationPeriodInSec; -} - -// --------------------------------------------------------------------- - -void WsjcppLog::add(WsjcppColorModifier &clr, const std::string &sType, const std::string &sTag, const std::string &sMessage) { - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.doLogRotateUpdateFilename(); - - std::lock_guard lock(WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logMutex); - WsjcppColorModifier def(WsjcppColorCode::FG_DEFAULT); - - std::string sLogMessage = WsjcppCore::getCurrentTimeForLogFormat() + ", " + WsjcppCore::getThreadId() - + " [" + sType + "] " + sTag + ": " + sMessage; - std::cout << clr << sLogMessage << def << std::endl; - - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logLastMessages.push_front(sLogMessage); - - - while (WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logLastMessages.size() > 50) { - WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logLastMessages.pop_back(); - } - - // log file - if (WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.enableLogFile) { - std::ofstream logFile(WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logFile, std::ios::app); - if (!logFile) { - std::cout << "Error Opening File" << std::endl; - return; - } - - logFile << sLogMessage << std::endl; - logFile.close(); - } -} - -// --------------------------------------------------------------------- -// WsjcppResourceFile - -WsjcppResourceFile::WsjcppResourceFile() { - WsjcppResourcesManager::add(this); -} - -// --------------------------------------------------------------------- -// WsjcppResourcesManager - -std::vector *g_pWsjcppResourceFiles = nullptr; - -void WsjcppResourcesManager::initGlobalVariables() { - if (g_pWsjcppResourceFiles == nullptr) { - g_pWsjcppResourceFiles = new std::vector(); - } -} - -// --------------------------------------------------------------------- - -void WsjcppResourcesManager::add(WsjcppResourceFile* pStorage) { - WsjcppResourcesManager::initGlobalVariables(); - g_pWsjcppResourceFiles->push_back(pStorage); -} - -// --------------------------------------------------------------------- - -bool WsjcppResourcesManager::has(const std::string &sFilename) { - WsjcppResourcesManager::initGlobalVariables(); - for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) { - if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) { - return true; - } - } - return false; -} - -// --------------------------------------------------------------------- - -WsjcppResourceFile* WsjcppResourcesManager::get(const std::string &sFilename) { - WsjcppResourcesManager::initGlobalVariables(); - for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) { - if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) { - return WsjcppResourcesManager::list()[i]; - } - } - return nullptr; -} - -// --------------------------------------------------------------------- - -const std::vector &WsjcppResourcesManager::list() { - return *g_pWsjcppResourceFiles; -} - -// --------------------------------------------------------------------- - -/* -bool WsjcppResourcesManager::make(const std::string &sWorkspace) { - if (!WsjcppResourcesManager::createFolders(sWorkspace)) { - return false; - } - return WsjcppResourcesManager::extractFiles(sWorkspace); -} - -// --------------------------------------------------------------------- - -bool WsjcppResourcesManager::createFolders(const std::string &sWorkspace) { - // prepare folders - std::vector vCreateDirs; - vCreateDirs.push_back(sWorkspace + "/logs"); - vCreateDirs.push_back(sWorkspace + "/teams"); - vCreateDirs.push_back(sWorkspace + "/checkers"); - vCreateDirs.push_back(sWorkspace + "/html"); - vCreateDirs.push_back(sWorkspace + "/html/css"); - vCreateDirs.push_back(sWorkspace + "/html/js"); - vCreateDirs.push_back(sWorkspace + "/html/images"); - vCreateDirs.push_back(sWorkspace + "/html/images/teams"); - vCreateDirs.push_back(sWorkspace + "/html/images/states"); - - for(int i = 0; i < vCreateDirs.size(); i++) { - std::string sPath = vCreateDirs[i]; - // check dir existing - if (!FS::dirExists(sPath)) { - // try make dir - if (!FS::makeDir(sPath)) { - std::cout << "Could not create folder " << sPath << std::endl; - return false; - } else { - std::cout << "Created folder " << sPath << std::endl; - } - } - } - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppResourcesManager::extractFiles(const std::string &sWorkspace) { - // TODO mkdir -p for files - const std::vector list = WsjcppResourcesManager::list(); - for(int i = 0; i < list.size(); i++) { - std::string sFilename = sWorkspace + "/" + list[i]->filename(); - if (!FS::fileExists(sFilename)) { - if (!FS::writeFile(sFilename, list[i]->buffer(), list[i]->bufferSize())) { - std::cout << "Could not write file " << sFilename << std::endl; - return false; - } else { - std::cout << "Created file " << sFilename << std::endl; - } - } - } - return true; -} -*/ - diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_core.h b/src.wsjcpp/wsjcpp_core/wsjcpp_core.h deleted file mode 100644 index 66c85a3..0000000 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_core.h +++ /dev/null @@ -1,237 +0,0 @@ -#ifndef WSJCPP_CORE_H -#define WSJCPP_CORE_H - -#include -#include -#include -#include -#include -#include - -class WsjcppFilePermissions { - public: - WsjcppFilePermissions(); - WsjcppFilePermissions( - bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag, - bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag, - bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag - ); - WsjcppFilePermissions(uint16_t nFilePermission); - - // owner flags - void setOwnerReadFlag(bool bOwnerReadFlag); - bool getOwnerReadFlag() const; - void setOwnerWriteFlag(bool bOwnerWriteFlag); - bool getOwnerWriteFlag() const; - void setOwnerExecuteFlag(bool bOwnerExecuteFlag); - bool getOwnerExecuteFlag() const; - void setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag); - - // group flags - void setGroupReadFlag(bool bGroupReadFlag); - bool getGroupReadFlag() const; - void setGroupWriteFlag(bool bGroupWriteFlag); - bool getGroupWriteFlag() const; - void setGroupExecuteFlag(bool bGroupExecuteFlag); - bool getGroupExecuteFlag() const; - void setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag); - - // for other flags - void setOtherReadFlag(bool bOtherReadFlag); - bool getOtherReadFlag() const; - void setOtherWriteFlag(bool bOtherWriteFlag); - bool getOtherWriteFlag() const; - void setOtherExecuteFlag(bool bOtherExecuteFlag); - bool getOtherExecuteFlag() const; - void setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag); - - std::string toString() const; - uint16_t toUInt16() const; - - private: - bool m_bOwnerReadFlag; - bool m_bOwnerWriteFlag; - bool m_bOwnerExecuteFlag; - bool m_bGroupReadFlag; - bool m_bGroupWriteFlag; - bool m_bGroupExecuteFlag; - bool m_bOtherReadFlag; - bool m_bOtherWriteFlag; - bool m_bOtherExecuteFlag; -}; - -class WsjcppCore { - public: - static bool init( - int argc, char** argv, - const std::string &sApplicationName, - const std::string &sApplicationVersion, - const std::string &sApplicationAuthor, - const std::string &sLibraryNameForExports - ); - - static std::string doNormalizePath(const std::string &sPath); - static std::string extractFilename(const std::string &sPath); - static std::string extractDirpath(const std::string &sFullPath); - static std::string getCurrentDirectory(); - - static long getCurrentTimeInMilliseconds(); - static long getCurrentTimeInSeconds(); - static std::string getCurrentTimeForFilename(); - static std::string getCurrentTimeForLogFormat(); - static std::string getThreadId(); - static std::string formatTimeForWeb(long nTimeInSec); - static std::string formatTimeForFilename(long nTimeInSec); - static std::string formatTimeUTC(int nTimeInSec); - - static bool dirExists(const std::string &sFilename); - static bool fileExists(const std::string &sFilename); - static std::vector listOfDirs(const std::string &sDirname); - static std::vector getListOfDirs(const std::string &sDirname); - static std::vector listOfFiles(const std::string &sDirname); - static std::vector getListOfFiles(const std::string &sDirname); - static bool makeDir(const std::string &sDirname); - static bool makeDirsPath(const std::string &sDirname); - static bool writeFile(const std::string &sFilename, const std::string &sContent); - static bool readTextFile(const std::string &sFilename, std::string &sOutputContent); - static bool readFileToBuffer(const std::string &sFilename, char *pBuffer[], int &nBufferSize); - static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize); - static bool removeFile(const std::string &sFilename); - static bool copyFile(const std::string &sSourceFilename, const std::string &sTargetFilename); - - static bool createEmptyFile(const std::string &sFilename); - - static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r "); - static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r "); - static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r "); - static std::string toLower(const std::string &str); - static std::string toUpper(const std::string& str); - static void replaceAll(std::string& str, const std::string& from, const std::string& to); - static std::vector split(const std::string& sWhat, const std::string& sDelim); - static std::string join(const std::vector &vWhat, const std::string& sDelim); - - static void initRandom(); - static std::string createUuid(); - static std::string uint2hexString(unsigned int n); - static unsigned long convertVoidToULong(void *p); - static std::string getPointerAsHex(void *p); - static std::string extractURLProtocol(const std::string& sValue); - static bool getEnv(const std::string& sName, std::string& sValue); - - static std::string encodeUriComponent(const std::string& sValue); - static std::string decodeUriComponent(const std::string& sValue); - - static std::string getHumanSizeBytes(long nBytes); - - static bool recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir); - static bool recoursiveRemoveDir(const std::string& sDir); - - static bool setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError); - static bool getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError); - - static std::string doPadLeft(const std::string& sIn, char cWhat, size_t nLength); - static std::string doPadRight(const std::string& sIn, char cWhat, size_t nLength); - -}; - - -// --------------------------------------------------------------------- - -enum WsjcppColorCode { - FG_RED = 31, - FG_GREEN = 32, - FG_YELLOW = 93, - FG_BLUE = 34, - FG_DEFAULT = 39, - BG_RED = 41, - BG_GREEN = 42, - BG_BLUE = 44, - BG_DEFAULT = 49 -}; - -// --------------------------------------------------------------------- - -class WsjcppColorModifier { - WsjcppColorCode code; - public: - WsjcppColorModifier(WsjcppColorCode pCode) : code(pCode) {} - friend std::ostream& - operator<<(std::ostream& os, const WsjcppColorModifier& mod) { - return os << "\033[" << mod.code << "m"; - } -}; - -// --------------------------------------------------------------------- - -class WsjcppLogGlobalConf { - public: - WsjcppLogGlobalConf(); - void doLogRotateUpdateFilename(bool bForce = false); - std::mutex logMutex; - std::string logDir; - std::string logPrefixFile; - std::string logFile; - bool enableLogFile; - long logStartTime; - long logRotationPeriodInSeconds; - std::deque logLastMessages; -}; - -class WsjcppLog { - public: - static WsjcppLogGlobalConf g_WSJCPP_LOG_GLOBAL_CONF; - - static void info(const std::string &sTag, const std::string &sMessage); - static void err(const std::string &sTag, const std::string &sMessage); - static void throw_err(const std::string &sTag, const std::string &sMessage); - static void warn(const std::string &sTag, const std::string &sMessage); - static void ok(const std::string &sTag, const std::string &sMessage); - static std::vector getLastLogMessages(); - static void setLogDirectory(const std::string &sDirectoryPath); - static void setPrefixLogFile(const std::string &sPrefixLogFile); - static void setEnableLogFile(bool bEnable); - static void setRotationPeriodInSec(long nRotationPeriodInSec); - - private: - static void add(WsjcppColorModifier &clr, const std::string &sType, const std::string &sTag, const std::string &sMessage); -}; - -// --------------------------------------------------------------------- -// WsjcppResourceFile - -class WsjcppResourceFile { - public: - WsjcppResourceFile(); - virtual const std::string &getFilename() const = 0; - virtual const std::string &getPackAs() const = 0; - virtual int getBufferSize() const = 0; - virtual const char *getBuffer() const = 0; -}; - - -// --------------------------------------------------------------------- -// WsjcppResourcesManager - -extern std::vector *g_pWsjcppResourceFiles; - -class WsjcppResourcesManager { - public: - static void initGlobalVariables(); - static void add(WsjcppResourceFile*); - static const std::vector &list(); - static bool has(const std::string &sFilename); - static WsjcppResourceFile* get(const std::string &sFilename); - static bool make(const std::string &sWorkspace); - // static bool createFolders(const std::string &sWorkspace); - // static bool extractFiles(const std::string &sWorkspace); -}; - -// --------------------------------------------------------------------- -// Registry WsjcppResourceFile -#define REGISTRY_WSJCPP_RESOURCE_FILE( classname ) \ - static classname * pRegistryWsjcppResourceFile ## classname = new classname(); \ - - -#endif // WSJCPP_CORE_H - - diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp deleted file mode 100644 index bd00383..0000000 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "wsjcpp_unit_tests.h" -#include - -// --------------------------------------------------------------------- -// WsjcppUnitTestBase - -WsjcppUnitTestBase::WsjcppUnitTestBase(const std::string &sTestName) { - m_sTestName = sTestName; - TAG = m_sTestName; - m_bTestResult = true; - WsjcppUnitTests::addUnitTest(sTestName, this); -} - -// --------------------------------------------------------------------- - -std::string WsjcppUnitTestBase::getName() { - return m_sTestName; -} - -// --------------------------------------------------------------------- - -void WsjcppUnitTestBase::ok(const std::string &sSuccessMessage) { - // print obly success message - WsjcppLog::ok(TAG, sSuccessMessage); -} - -// --------------------------------------------------------------------- - -void WsjcppUnitTestBase::fail(const std::string &sFailedMessage) { - WsjcppLog::err(TAG, sFailedMessage); - m_bTestResult = false; -} - -// --------------------------------------------------------------------- - -bool WsjcppUnitTestBase::runTest() { - WsjcppLog::info(TAG, "Start unit-test"); - WsjcppLog::info(TAG, "Do before unit-test"); - if (!doBeforeTest()) { - fail("Problem with before unit-test"); - return false; - } - WsjcppLog::info(TAG, "Execute unit-test"); - try { - executeTest(); - } catch(const std::exception& e) { - fail(e.what()); - } catch(...) { - - } - if (m_bTestResult) { - ok("Test passed."); - } else { - fail("Test failed."); - } - WsjcppLog::info(TAG, "Do after unit-test"); - if (!doAfterTest()) { - fail("Problem with after unit-test"); - } - WsjcppLog::info(TAG, "End unit-test"); - return m_bTestResult; -} - -// --------------------------------------------------------------------- - -bool WsjcppUnitTestBase::compareD(const std::string &sMark, double nValue, double nExpected) { - if (abs(nValue - nExpected) > std::numeric_limits::epsilon()) { - fail(" {" + sMark + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); - return false; - } - return true; -} - -// --------------------------------------------------------------------- - -std::vector *g_pWsjcppUnitTests = nullptr; - -void WsjcppUnitTests::initGlobalVariables() { - if (g_pWsjcppUnitTests == nullptr) { - // WsjcppLog::info(std::string(), "Create handlers map"); - g_pWsjcppUnitTests = new std::vector(); - } -} - -// --------------------------------------------------------------------- - -void WsjcppUnitTests::addUnitTest(const std::string &sTestName, WsjcppUnitTestBase* pUnitTest) { - WsjcppUnitTests::initGlobalVariables(); - bool bFound = false; - for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { - WsjcppUnitTestBase* p = g_pWsjcppUnitTests->at(i); - if (p->getName() == sTestName) { - bFound = true; - } - } - - if (bFound) { - WsjcppLog::err(sTestName, "Already registered"); - } else { - g_pWsjcppUnitTests->push_back(pUnitTest); - // Log::info(sCmd, "Registered"); - } -} - -// --------------------------------------------------------------------- \ No newline at end of file diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h deleted file mode 100644 index df68697..0000000 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef WSJCPP_UNIT_TESTS_H -#define WSJCPP_UNIT_TESTS_H - -#include -#include - -class WsjcppUnitTestBase { - public: - WsjcppUnitTestBase(const std::string &sTestName); - std::string getName(); - void ok(const std::string &sSuccessMessage); - void fail(const std::string &sFailedMessage); - bool runTest(); - - virtual bool doBeforeTest() = 0; - virtual void executeTest() = 0; - virtual bool doAfterTest() = 0; - protected: - std::string TAG; - - bool compareD(const std::string &sMark, double nValue, double nExpected); - template bool compare(const std::string &sMark, T1 tGotValue, T2 tExpectedValue) { - if (tGotValue != tExpectedValue) { - std::stringstream ss; - ss << " {mark: " << sMark << "} Expected '" << tExpectedValue << "', but got '" << tGotValue << "'"; - fail(ss.str()); - return false; - } - return true; - } - private: - bool m_bTestResult; - std::string m_sTestName; -}; - -extern std::vector *g_pWsjcppUnitTests; - -class WsjcppUnitTests { - public: - static void initGlobalVariables(); - static void addUnitTest(const std::string &sTestName, WsjcppUnitTestBase* pUnitTest); -}; - -// RegistryCmdHandler -#define REGISTRY_WSJCPP_UNIT_TEST( classname ) \ - static classname * pRegistryWsjcppUnitTest ## classname = new classname(); \ - - -#endif // WSJCPP_UNIT_TESTS_H diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp deleted file mode 100644 index c66e9d2..0000000 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include -#include -#include - -void printHelp(const std::string &sProgramName) { - std::string sOutput = "\nHelp:\n"; - sOutput += - " '" + sProgramName + "' - run all unit-tests\n" - " '" + sProgramName + " help' - print this help\n" - " '" + sProgramName + " list' - print list of unit-tests\n" - " '" + sProgramName + " run ' - run single unit-test\n" - ; - WsjcppLog::info("UnitTests", sOutput); -} - -int main(int argc, char** argv) { - WsjcppCore::initRandom(); - std::string TAG = "UnitTests"; - WsjcppLog::setPrefixLogFile("unit-tests"); - std::string sLogDir = "./logs"; - if (!WsjcppCore::dirExists(sLogDir)) { - WsjcppCore::makeDir(sLogDir); - } - WsjcppLog::setLogDirectory(sLogDir); - if (!WsjcppCore::dirExists(sLogDir)) { - WsjcppLog::err(TAG, "Directory '" + sLogDir + "' did'not exists"); - return -1; - } - - WsjcppUnitTests::initGlobalVariables(); - std::string sProgramName(argv[0]); - - if (argc == 1) { - int nAll = g_pWsjcppUnitTests->size(); - WsjcppLog::info(TAG, "All tests count " + std::to_string(nAll)); - int nSuccess = 0; - for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { - WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - if (pUnitTest->runTest()) { - nSuccess++; - } - } - if (nSuccess == nAll) { - WsjcppLog::ok(TAG, "All unit-tests passed " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); - } else { - WsjcppLog::warn(TAG, "Passed unit-tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); - WsjcppLog::err(TAG, std::to_string(nAll - nSuccess) + " unit-test(s) failed."); - } - - bool bResult = nSuccess == nAll; - return bResult ? 0 : -1; - } else if (argc == 2) { - std::string sArg2(argv[1]); - if (sArg2 == "list") { - std::string sOutput = "\nList of unit-tests:\n"; - for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { - WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - sOutput += " - " + pUnitTest->getName() + "\n"; - } - WsjcppLog::info(TAG, sOutput); - return -1; - } else if (sArg2 == "help") { - printHelp(sProgramName); - return -1; - } - } else if (argc == 3) { - std::string sArg2(argv[1]); - std::string sArg3(argv[2]); - if (sArg2 == "run") { - int nSuccess = 0; - bool bTestFound = false; - for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { - WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - if (pUnitTest->getName() == sArg3) { - bTestFound = true; - bool bResult = pUnitTest->runTest(); - if (bResult) { - nSuccess++; - } - } - } - if (!bTestFound) { - WsjcppLog::err(TAG, "Test not found try help"); - } - return -1; - } - printHelp(sProgramName); - return -1; - } - - printHelp(sProgramName); - return 0; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6737d5f..2fddc21 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,8 +34,6 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml #include "examples.h" #include "wsjcpp_yaml.h" -// #include "wsjcpp_core.h" - class MyLogger : public IWsjcppYamlLog { public: // IWsjcppYamlLog @@ -76,19 +74,11 @@ int _tmain(int argc, _TCHAR* argv[]) { int main(int argc, char* argv[]) { #endif std::string TAG = "MAIN"; - // WsjcppCore::initRandom(); - // std::string appName = std::string(WSJCPP_APP_NAME); - // std::string appVersion = std::string(WSJCPP_APP_VERSION); - // std::string appLogPath = ".wsjcpp-yaml-logs"; - // WsjcppLog::setLogDirectory(appLogPath); - // if (!WsjcppCore::dirExists(appLogPath)) { - // WsjcppCore::makeDir(appLogPath); - // } MyLogger *pLogger = new MyLogger(); WsjcppYaml yaml; yaml.setLogger(pLogger); - std::string sError; + std::string sError; std::string sFilePath = "./unit-tests.wsjcpp/data-tests/read-file/example-voiting-app/docker-compose.yml"; std::cout << "Reading file " << sFilePath << "..." << std::endl; if (!yaml.loadFromFile(sFilePath, sError)) { @@ -97,7 +87,7 @@ int main(int argc, char* argv[]) { return -1; } std::cout << "Done." << std::endl; - + std::cout << "Saving to file " << sFilePath << "..." << std::endl; if (!yaml.saveToFile(sFilePath, sError)) { std::cerr << "Could not save data to file: " << sFilePath.c_str() << std::endl << "Error: " << sError.c_str() << std::endl; diff --git a/src/tests/data-tests/float-double/example.yml b/src/tests/data/float-double/example.yml similarity index 100% rename from src/tests/data-tests/float-double/example.yml rename to src/tests/data/float-double/example.yml diff --git a/unit-tests.wsjcpp/data-tests/for-memory-leak/some.yml b/src/tests/data/for-memory-leak/some.yml similarity index 100% rename from unit-tests.wsjcpp/data-tests/for-memory-leak/some.yml rename to src/tests/data/for-memory-leak/some.yml diff --git a/unit-tests.wsjcpp/data-tests/parser-simple-array.yml b/src/tests/data/parser-simple-array.yml similarity index 100% rename from unit-tests.wsjcpp/data-tests/parser-simple-array.yml rename to src/tests/data/parser-simple-array.yml diff --git a/src/tests/data-tests/python_yaml_issue_33/example.yaml b/src/tests/data/python_yaml_issue_33/example.yaml similarity index 100% rename from src/tests/data-tests/python_yaml_issue_33/example.yaml rename to src/tests/data/python_yaml_issue_33/example.yaml diff --git a/src/tests/data-tests/python_yaml_issue_33/gen.py b/src/tests/data/python_yaml_issue_33/gen.py similarity index 100% rename from src/tests/data-tests/python_yaml_issue_33/gen.py rename to src/tests/data/python_yaml_issue_33/gen.py diff --git a/unit-tests.wsjcpp/data-tests/read-file/example-voiting-app/docker-compose.yml b/src/tests/data/read-file/example-voiting-app/docker-compose.yml similarity index 100% rename from unit-tests.wsjcpp/data-tests/read-file/example-voiting-app/docker-compose.yml rename to src/tests/data/read-file/example-voiting-app/docker-compose.yml diff --git a/src/tests/data/read-write-file/docker-compose.output.yml b/src/tests/data/read-write-file/docker-compose.output.yml new file mode 100644 index 0000000..64e7f0e --- /dev/null +++ b/src/tests/data/read-write-file/docker-compose.output.yml @@ -0,0 +1,67 @@ +# https://github.com/dockersamples/example-voting-app/blob/master/docker-compose.yml + +version: "3" + +services: + # service vote + vote: + build: ./vote + command: python app.py + volumes: + - ./vote:/app + ports: + - "5000:80" + networks: + - front-tier + - back-tier + + # service result + result: + build: ./result + command: nodemon server.js + volumes: + - ./result:/app + ports: + # ports forward + - "5001:80" # some + + - "5858:5858" + + networks: + - front-tier + - back-tier + + worker: + build: + context: ./worker + depends_on: + - "redis" + - "db" + networks: + - back-tier + + redis: + image: redis:alpine + container_name: redis + ports: ["6379"] + networks: + - back-tier + + db: + image: postgres:9.4 + container_name: db + environment: + POSTGRES_USER: "postgres" + POSTGRES_PASSWORD: "postgres" + volumes: + - "db-data:/var/lib/postgresql/data" + networks: + - back-tier + +volumes: + db-data: + +networks: + front-tier: + back-tier: + diff --git a/unit-tests.wsjcpp/data-tests/read-write-file/docker-compose.yml b/src/tests/data/read-write-file/docker-compose.yml similarity index 100% rename from unit-tests.wsjcpp/data-tests/read-write-file/docker-compose.yml rename to src/tests/data/read-write-file/docker-compose.yml diff --git a/unit-tests.wsjcpp/data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml b/src/tests/data/read-wsjcpp-hold-yml/wsjcpp.hold.yml similarity index 100% rename from unit-tests.wsjcpp/data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml rename to src/tests/data/read-wsjcpp-hold-yml/wsjcpp.hold.yml diff --git a/unit-tests.wsjcpp/data-tests/remove-element-in-array.yml b/src/tests/data/remove-element-from-array.yml similarity index 100% rename from unit-tests.wsjcpp/data-tests/remove-element-in-array.yml rename to src/tests/data/remove-element-from-array.yml diff --git a/unit-tests.wsjcpp/src/get_current_rss.h b/src/tests/get_current_rss.h similarity index 100% rename from unit-tests.wsjcpp/src/get_current_rss.h rename to src/tests/get_current_rss.h diff --git a/src/tests/helpers.h b/src/tests/helpers.h new file mode 100644 index 0000000..1bd3520 --- /dev/null +++ b/src/tests/helpers.h @@ -0,0 +1,62 @@ + +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#pragma once + +#include +#include + +#if defined(_WIN32) +#include +#define GetCurrentDir _getcwd +#else +#include +#define GetCurrentDir getcwd +#endif + +static std::string find_test_data_file(const std::string &sFilePath) { + char buff[FILENAME_MAX]; + char *path = GetCurrentDir(buff, FILENAME_MAX); + if (path == NULL) { + std::cerr << "ERROR: Could not get current directory path" << std::endl; + return "did_not_found_file: " + sFilePath; + } + std::cout << "Current path: " << buff << std::endl; + + std::string sRet = "../../src/tests/" + sFilePath; + + // find path + for (int i = 0; i < 5; i++) { + std::ifstream file_(sRet.c_str()); + if (!file_) { + sRet = "../" + sRet; + } else { + return sRet; + } + } + return sFilePath; +} diff --git a/unit-tests.wsjcpp/src/process_mem_usage.h b/src/tests/process_mem_usage.h similarity index 100% rename from unit-tests.wsjcpp/src/process_mem_usage.h rename to src/tests/process_mem_usage.h diff --git a/src/tests/test_append_elements.cpp b/src/tests/test_append_elements.cpp new file mode 100644 index 0000000..f76fb08 --- /dev/null +++ b/src/tests/test_append_elements.cpp @@ -0,0 +1,86 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + WsjcppYaml yml; + yml.getRoot()->setElementValue("p1", "val1"); + yml.getRoot()->createElementMap("some"); + WsjcppYamlNode *pSome = yml.getRoot()->getElement("some"); + pSome->setElementValue("p2", "val2"); + pSome->createElementMap("sub-some"); + WsjcppYamlNode *pSubSome = pSome->getElement("sub-some"); + pSubSome->setElementValue("p3", "val3"); + pSome->createElementArray("arr-some"); + WsjcppYamlNode *pArrSome = pSome->getElement("arr-some"); + pArrSome->setComment("some array here"); + pArrSome->appendElementValue("1234"); + WsjcppYamlPlaceInFile placeInFile; + WsjcppYamlNode *pItemMap = new WsjcppYamlNode(pArrSome, &yml, placeInFile, WSJCPP_YAML_NODE_MAP); + pItemMap->setComment("some comment for a map"); // why not work ???? + pArrSome->appendElement(pItemMap); + pItemMap->setElementValue("p4", "val4"); + pItemMap->setElementValue("p5", "val5"); + yml.getRoot()->setElementValue("p6", "val6"); + + int ret = 0; + + std::string sExpectedYaml = "" + "p1: val1\n" + "some:\n" + " p2: val2\n" + " sub-some:\n" + " p3: val3\n" + " arr-some: # some array here\n" + " - 1234\n" + " - p4: val4\n" + " p5: val5\n" + "p6: val6"; + + std::string sSaved = yml.getRoot()->toString(); + + if (sSaved != sExpectedYaml) { + std::cerr << "ERROR (1). Not equal. Expected: \n---\n" << sExpectedYaml << "\n---\n, but got \n---\n" << sSaved << "\n---\n" << std::endl; + ret = -1; + } + + std::string sError = ""; + std::string sSaved2 = ""; + std::string sExpectedYaml2 = sExpectedYaml + "\n"; + if (!yml.saveToString(sSaved2, sError)) { + std::cerr << "Error save: " << sError << std::endl; + return -1; + } + + if (sSaved2 != sExpectedYaml2) { + std::cerr << "ERROR (2). Not equal. Expected: \n---\n" << sExpectedYaml2 << "\n---\n, but got \n---\n" << sSaved2 << "\n---\n" << std::endl; + ret = -1; + } + + return ret; +} \ No newline at end of file diff --git a/src/tests/test_cleanup.cpp b/src/tests/test_cleanup.cpp new file mode 100644 index 0000000..4eafe93 --- /dev/null +++ b/src/tests/test_cleanup.cpp @@ -0,0 +1,110 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + std::string sTestYaml1 = + "# Some comment 1\n" + "test10: one\n" + "test20: two # some comment 2\n" + ; + + std::string sTestYaml2 = + "# Some comment 1\n" + "test11: one\n" + "test20: two # some comment 2\n" + "test22: two # some comment 2\n" + ; + + WsjcppYaml yaml; + std::string sError; + int ret = 0; + + if (!yaml.loadFromString("parse1", sTestYaml1, sError)) { + std::cerr << "Error parsing 1 " << sError << std::endl; + return -1; + } + + if (!yaml.getRoot()->hasElement("test10")) { + std::cerr << "Failed check '(1) has test10'" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->hasElement("test11")) { + std::cerr << "Failed check '(1) has test11'" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->hasElement("test20")) { + std::cerr << "Failed check '(1) has test20'" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->hasElement("test22")) { + std::cerr << "Failed check '(1) has test22'" << std::endl; + ret = -1; + } + + // 2 + + if (!yaml.loadFromString("parse2", sTestYaml2, sError)) { + std::cerr << "Error parsing 2 " << sError << std::endl; + return -1; + } + + if (yaml.getRoot()->hasElement("test10")) { + std::cerr << "Failed check '(2) has test10'" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->hasElement("test11")) { + std::cerr << "Failed check '(2) has test11'" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->hasElement("test20")) { + std::cerr << "Failed check '(2) has test20'" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->hasElement("test22")) { + std::cerr << "Failed check '(2) has test22'" << std::endl; + ret = -1; + } + + // 3 + + yaml.clear(); + + if (yaml.getRoot() != nullptr) { + std::cerr << "Failed check '(3) has root'" << std::endl; + ret = -1; + } + + return ret; +} \ No newline at end of file diff --git a/src/tests/test_cursor.cpp b/src/tests/test_cursor.cpp new file mode 100644 index 0000000..1209bad --- /dev/null +++ b/src/tests/test_cursor.cpp @@ -0,0 +1,352 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include + +int main() { + + std::string sTestYaml = + "# Some comment 1\n" + "map1: \n" + " map11: \n" + " sss: \n" + " map111: \n" + " param1111: v1111\n" + " param1112: v1112\n" + " map112: \n" + " param1121: v1121\n" + " param1122: v1122\n" + " map113: \n" + " param1131: v1131\n" + " param1132: v1132\n" + " map12: \n" + " param121: v121\n" + " param122: v122\n" + " map123: \n" + " param1231: v1231\n" + " param1232: v1232\n" + " param1232: v1232\n" + "param2: v2 # some comment 2\n" + "arr1: # some comment array 1\n" + " - some1\n" + " - 3\n" + "\n" + " - Yes\n" + " # empty\n" + " - no\n" + " - true\n" + " - False\n" + "\n" // empty line + ; + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromString("test_cursor", sTestYaml, sError)) { + std::cerr << "Error parsing " << sError << std::endl; + return -1; + } + + if (yaml["map1"].isNull() != false) { + std::cerr << "map1 is null" << std::endl; + return -1; + } + if (yaml["map1"].isUndefined() != false) { + std::cerr << "map1 is undefined" << std::endl; + return -1; + } + if (yaml["map1"].isValue() != false) { + std::cerr << "map1 is value" << std::endl; + return -1; + } + if (yaml["map1"].isArray() != false) { + std::cerr << "map1 is array" << std::endl; + return -1; + } + if (yaml["map1"].isMap() != true) { + std::cerr << "map1 is map" << std::endl; + return -1; + } + + if (yaml["map1-1111"].isNull() != true) { + std::cerr << "map1-1111 is null" << std::endl; + return -1; + } + if (yaml["map1-1111"].isUndefined() != false) { + std::cerr << "map1 is undefined" << std::endl; + return -1; + } + if (yaml["map1-1111"].isValue() != false) { + std::cerr << "map1 is value" << std::endl; + return -1; + } + if (yaml["map1-1111"].isArray() != false) { + std::cerr << "map1-1111 is array" << std::endl; + return -1; + } + if (yaml["map1-1111"].isMap() != false) { + std::cerr << "map1-1111 is map" << std::endl; + return -1; + } + + if (yaml["arr1"].isNull() != false) { + std::cerr << "arr1 is null" << std::endl; + return -1; + } + if (yaml["arr1"].isUndefined() != false) { + std::cerr << "arr1 is undefined" << std::endl; + return -1; + } + if (yaml["arr1"].isValue() != false) { + std::cerr << "arr1 is value" << std::endl; + return -1; + } + if (yaml["arr1"].isArray() != true) { + std::cerr << "arr1 is array" << std::endl; + return -1; + } + if (yaml["arr1"].isMap() != false) { + std::cerr << "arr1 is map" << std::endl; + return -1; + } + + if (yaml["map1"]["map11"]["sss"].isNull() != false) { + std::cerr << "map1.map11.sss is null" << std::endl; + return -1; + } + if (yaml["map1"]["map11"]["sss"].isUndefined() != true) { + std::cerr << "map1.map11.sss is undefined" << std::endl; + return -1; + } + if (yaml["map1"]["map11"]["sss"].isValue() != false) { + std::cerr << "map1.map11.sss is value" << std::endl; + return -1; + } + if (yaml["map1"]["map11"]["sss"].isArray() != false) { + std::cerr << "map1.map11.sss is array" << std::endl; + return -1; + } + if (yaml["map1"]["map11"]["sss"].isMap() != false) { + std::cerr << "map1.map11.sss is map" << std::endl; + return -1; + } + + if (yaml["map1"]["map11"]["map111"]["param1111"].isNull() != false) { + std::cerr << "map1.map11.map111.param1111 is null" << std::endl; + return -1; + } + if (yaml["map1"]["map11"]["map111"]["param1111"].isUndefined() != false) { + std::cerr << "map1.map11.map111.param1111 is undefined" << std::endl; + return -1; + } + if (yaml["map1"]["map11"]["map111"]["param1111"].isValue() != true) { + std::cerr << "map1.map11.map111.param1111 is value" << std::endl; + return -1; + } + if (yaml["map1"]["map11"]["map111"]["param1111"].isArray() != false) { + std::cerr << "map1.map11.map111.param1111 is array" << std::endl; + return -1; + } + if (yaml["map1"]["map11"]["map111"]["param1111"].isMap() != false) { + std::cerr << "map1.map11.map111.param1111 is map" << std::endl; + return -1; + } + + if (yaml["map1"][0].isNull() != true) { + std::cerr << "map use as array" << std::endl; + return -1; + } + if (yaml["arr1"]["0"].isNull() != true) { + std::cerr << "array use as map" << std::endl; + return -1; + } + if (yaml["arr1"][0].isNull() != false) { + std::cerr << "array use as array" << std::endl; + return -1; + } + + if (yaml["arr1"].comment() != "some comment array 1") { + std::cerr << "array 1 comment" << std::endl; + return -1; + } + if (yaml["arr1"].size() != 6) { + std::cerr << "array size 6" << std::endl; + return -1; + } + if (yaml["arr1"][0].valStr() != "some1") { + std::cerr << "array el 0" << std::endl; + return -1; + } + if (yaml["arr1"][1].valStr() != "3") { + std::cerr << "array el 1" << std::endl; + return -1; + } + if (yaml["arr1"][2].valStr() != "Yes") { + std::cerr << "array el 2" << std::endl; + return -1; + } + if (yaml["arr1"][3].valStr() != "no") { + std::cerr << "array el 3" << std::endl; + return -1; + } + if (yaml["arr1"][4].valStr() != "true") { + std::cerr << "array el 4" << std::endl; + return -1; + } + if (yaml["arr1"][5].valStr() != "False") { + std::cerr << "array el 5" << std::endl; + return -1; + } + + if (yaml["arr1"][2].valBool() != true) { + std::cerr << "array bool el 2" << std::endl; + return -1; + } + if (yaml["arr1"][3].valBool() != false) { + std::cerr << "array bool el 3" << std::endl; + return -1; + } + if (yaml["arr1"][4].valBool() != true) { + std::cerr << "array bool el 4" << std::endl; + return -1; + } + if (yaml["arr1"][5].valBool() != false) { + std::cerr << "array bool el 5" << std::endl; + return -1; + } + + // int + if (yaml["arr1"][1].valInt() != 3) { + std::cerr << "array 1 val 3" << std::endl; + return -1; + } + yaml["arr1"][1].val(10); + if (yaml["arr1"][1].valInt() != 10) { + std::cerr << "array 1 val 10" << std::endl; + return -1; + } + + // string + if (yaml["arr1"][0].valStr() != "some1") { + std::cerr << "array el 0 - 1" << std::endl; + return -1; + } + + yaml["arr1"][0].val("different value").comment("1234"); + if (yaml["arr1"][0].valStr() != "different value") { + std::cerr << "array el 0 - 2" << std::endl; + return -1; + } + if (yaml["arr1"][0].comment() != "1234") { + std::cerr << "array el 0 - 3" << std::endl; + return -1; + } + + // bool + if (yaml["arr1"][4].valStr() != "true") { + std::cerr << "array bool el 4 (1)" << std::endl; + return -1; + } + if (yaml["arr1"][4].node()->getValue() != "true") { + std::cerr << "array bool el 4 (2)" << std::endl; + return -1; + } + if (yaml["arr1"][4].valBool() != true) { + std::cerr << "array bool el 4 (3)" << std::endl; + return -1; + } + yaml["arr1"][4].val(true); + if (yaml["arr1"][4].valStr() != "yes") { + std::cerr << "array bool el 4 (4)" << std::endl; + return -1; + } + if (yaml["arr1"][4].valBool() != true) { + std::cerr << "array bool el 4 (5)" << std::endl; + return -1; + } + yaml["arr1"][4].val(false); + if (yaml["arr1"][4].valStr() != "no") { + std::cerr << "array bool el 4 (6)" << std::endl; + return -1; + } + if (yaml["arr1"][4].valBool() != false) { + std::cerr << "array bool el 4 (7)" << std::endl; + return -1; + } + + // map + if (yaml["map1"]["map12"].isMap() != true) { + std::cerr << "map1.map12" << std::endl; + return -1; + } + + std::vector vKeys = yaml["map1"]["map12"].keys(); + if (vKeys.size() != 4) { + std::cerr << "map1.map12 keys size" << std::endl; + return -1; + } + if (vKeys.size() == 4) { + if (vKeys[0] != "param121") { + std::cerr << "map1.map12 keys 0" << std::endl; + return -1; + } + if (vKeys[1] != "param122") { + std::cerr << "map1.map12 keys 1" << std::endl; + return -1; + } + if (vKeys[2] != "map123") { + std::cerr << "map1.map12 keys 2" << std::endl; + return -1; + } + if (vKeys[3] != "param1232") { + std::cerr << "map1.map12 keys 3" << std::endl; + return -1; + } + } + if (yaml["map1"]["map12"].hasKey("some") != false) { + std::cerr << "map1.map12 has key1" << std::endl; + return -1; + } + if (yaml["map1"]["map12"].hasKey("map123") != true) { + std::cerr << "map1.map12 has key2" << std::endl; + return -1; + } + if (yaml["map1"]["map12"]["map123"].isValue() != false) { + std::cerr << "map1.map12 is value" << std::endl; + return -1; + } + if (yaml["map1"]["map12"]["param122"].isValue() != true) { + std::cerr << "map1.map12 is value" << std::endl; + return -1; + } + + + /* WsjcppYamlCursor &push(const std::string &sVal); + WsjcppYamlCursor &push(int nVal); + WsjcppYamlCursor &push(bool bVal); + WsjcppYamlCursor &remove(int nIdx);*/ + + return 0; +} diff --git a/src/tests/test_double_value.cpp b/src/tests/test_double_value.cpp index ad4a9ca..bad453c 100644 --- a/src/tests/test_double_value.cpp +++ b/src/tests/test_double_value.cpp @@ -1,23 +1,40 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + #include #include #include +#include #include - -#ifdef WINDOWS -#include -#define GetCurrentDir _getcwd -#else -#include -#define GetCurrentDir getcwd -#endif +#include "helpers.h" int main() { - char buff[FILENAME_MAX]; - GetCurrentDir(buff, FILENAME_MAX ); - std::cout << "Current path: " << buff << std::endl; + std::string sFilepath = find_test_data_file("data/float-double/example.yml"); WsjcppYaml yaml; - std::string sFilepath = "../../../../src/tests/data-tests/float-double/example.yml"; std::string sError; if (!yaml.loadFromFile(sFilepath, sError)) { std::cerr << "Error parsing: " << sError << std::endl; @@ -75,4 +92,4 @@ int main() { // compareD("has double value is 1.0003 (double 2)", yaml["double"].valDouble(), 1.0003f); return ret; -} \ No newline at end of file +} diff --git a/src/tests/test_float_value.cpp b/src/tests/test_float_value.cpp index 5b665ae..f452a73 100644 --- a/src/tests/test_float_value.cpp +++ b/src/tests/test_float_value.cpp @@ -1,23 +1,40 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + #include #include #include +#include #include - -#ifdef WINDOWS -#include -#define GetCurrentDir _getcwd -#else -#include -#define GetCurrentDir getcwd -#endif +#include "helpers.h" int main() { - char buff[FILENAME_MAX]; - GetCurrentDir(buff, FILENAME_MAX); - std::cout << "Current path: " << buff << std::endl; + std::string sFilepath = find_test_data_file("data/float-double/example.yml"); WsjcppYaml yaml; - std::string sFilepath = "../../../../src/tests/data-tests/float-double/example.yml"; std::string sError; if (!yaml.loadFromFile(sFilepath, sError)) { std::cerr << "Error parsing: " << sError << std::endl; @@ -51,4 +68,4 @@ int main() { } return ret; -} \ No newline at end of file +} diff --git a/src/tests/test_keep_format.cpp b/src/tests/test_keep_format.cpp new file mode 100644 index 0000000..78aa3de --- /dev/null +++ b/src/tests/test_keep_format.cpp @@ -0,0 +1,96 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + // https://github.com/wsjcpp/wsjcpp-yaml/issues/29 + + std::string sInput = + "# commet1:\n" + "\n" + "val1: 1\n" + "\n" + "unit-tests:\n" + " cases:\n" + " - name: \"NormalizeUnitTestName\"\n" + " description: \"Test Normalize UnitTest Name\"\n" + " - name: \"generateFilenameForUnitTest\"\n" + " description: \"Test generate filename for UnitTest\"\n" + " - name: \"InitPackage\"\n" + " description: \"Test for init package\"\n" + " enabled: \"no\"\n" + "\n" + " - name: \"ReadPackage\"\n" + " description: \"Test for read package\"\n" + "\n" + "\n" + "resources:\n" + " - filepath: \"./build_simple.sh\"\n" + " filesize: 77\n" + " pack-as: \"binary\"\n" + " modified: 1600401759\n" + " sha1: \"\"\n" + ; + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromString("input.yml", sInput, sError)) { + std::cerr << sError << std::endl; + return -1; + } + + std::string sOutput = ""; + if (!yaml.saveToString(sOutput, sError)) { + std::cerr << sError << std::endl; + return -1; + } + + if (sOutput != sInput) { + std::cerr << "sOutput != sInput" << std::endl; + return -1; + } + + // try again + + if (!yaml.loadFromString("output.yml", sOutput, sError)) { + std::cerr << sError << std::endl; + return -1; + } + + sOutput = ""; + if (!yaml.saveToString(sOutput, sError)) { + std::cerr << sError << std::endl; + return -1; + } + + if (sOutput != sInput) { + std::cerr << "sOutput != sInput (2)" << std::endl; + return -1; + } + return 0; +} \ No newline at end of file diff --git a/src/tests/test_line_parser.cpp b/src/tests/test_line_parser.cpp new file mode 100644 index 0000000..078c898 --- /dev/null +++ b/src/tests/test_line_parser.cpp @@ -0,0 +1,191 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include +#include +#include + +std::string test_line_parser_WSJCPP_INT_TO_STR(int number) { + #if defined(__CODEGEARC__) && !defined(_WIN64) + // TODO + char buffer[] = { + 0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0, + 0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0, + 0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0, + 0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0 + }; + #if __CODEGEARC__ == 0x0770 + // 12.2 + _itoa(number, buffer, 10); + #else + itoa(number, buffer, 10); + #endif + return std::string(buffer); + #else + return std::to_string(number); + #endif +} + +int main() { + struct LineTest { + LineTest( + int nNumberOfTest, + std::string sLine, + std::string sPrefix, + bool isArrayItem, + std::string sName, + WsjcppYamlQuotes nNameQuotes, + std::string sValue, + WsjcppYamlQuotes nValueQuotes, + std::string sComment + ) : nNumberOfTest(nNumberOfTest), + sLine(sLine), + sPrefix(sPrefix), + isArrayItem(isArrayItem), + sName(sName), + nNameQuotes(nNameQuotes), + sValue(sValue), + nValueQuotes(nValueQuotes), + sComment(sComment) + { + // + }; + int nNumberOfTest; + std::string sLine; + std::string sPrefix; + bool isArrayItem; + std::string sName; + WsjcppYamlQuotes nNameQuotes; + std::string sValue; + WsjcppYamlQuotes nValueQuotes; + std::string sComment; + }; + + std::vector vTestLines; + + vTestLines.push_back(LineTest(1, + "# Some comment 1 ", "", + false, // array node + "", WSJCPP_YAML_QUOTES_NONE, // name + "", WSJCPP_YAML_QUOTES_NONE, // value + "Some comment 1" // comment + )); + vTestLines.push_back(LineTest(2, + " test2: \"t\\\"wo\" # some comment 2 ", " ", + false, // array node + "test2", WSJCPP_YAML_QUOTES_NONE, // name + "t\"wo", WSJCPP_YAML_QUOTES_DOUBLE, // value + "some comment 2" // comment + )); + vTestLines.push_back(LineTest(3, + " test3:", " ", + false, // array node + "test3", WSJCPP_YAML_QUOTES_NONE, // name + "", WSJCPP_YAML_QUOTES_NONE, // value + "" // comment + )); + vTestLines.push_back(LineTest(4, + " - test4", " ", + true, // array node + "", WSJCPP_YAML_QUOTES_NONE, // name + "test4", WSJCPP_YAML_QUOTES_NONE, // value + "" // comment + )); + vTestLines.push_back(LineTest(5, + "", "", + false, // array node + "", WSJCPP_YAML_QUOTES_NONE, // name + "", WSJCPP_YAML_QUOTES_NONE, // value + "" // comment + )); + vTestLines.push_back(LineTest(6, + " - \"test4:111\"", " ", + true, // array node + "", WSJCPP_YAML_QUOTES_NONE, // name + "test4:111", WSJCPP_YAML_QUOTES_DOUBLE, // value + "" // comment + )); + vTestLines.push_back(LineTest(7, + "issues: https://github.com/wsjcpp/wsjcpp-yaml/issues", "", + false, // array node + "issues", WSJCPP_YAML_QUOTES_NONE, // name + "https://github.com/wsjcpp/wsjcpp-yaml/issues", WSJCPP_YAML_QUOTES_NONE, // value + "" // comment + )); + + int ret = 0; + + for (int i = 0; i < vTestLines.size(); i++) { + LineTest test = vTestLines[i]; + std::string tagline = "{line:" + test_line_parser_WSJCPP_INT_TO_STR(test.nNumberOfTest) + ": '" + test.sLine + "'}"; + + WsjcppYamlParsebleLine line(test.nNumberOfTest); + std::string sError; + // compare(tagline + ", parseLine", + if (!line.parseLine(test.sLine, sError)) { + std::cerr << tagline << ", parseLine: " << sError << std::endl; + ret = -1; + continue; + } + + if (line.getPrefix() != test.sPrefix) { + std::cerr << tagline << ", prefix expected '" << test.sPrefix << "', but got '" << line.getPrefix() << "'" << std::endl; + ret = -1; + } + + if (line.isArrayItem() != test.isArrayItem) { + std::cerr << tagline << ", arrayitem expected" << std::endl; + ret = -1; + } + + if (line.getName() != test.sName) { + std::cerr << tagline << ", name expected '" << test.sName << "', but got '" << line.getName() << "'" << std::endl; + ret = -1; + } + + if (line.getValue() != test.sValue) { + std::cerr << tagline << ", value expected '" << test.sValue << "', but got '" << line.getValue() << "'" << std::endl; + ret = -1; + } + + if (line.getComment() != test.sComment) { + std::cerr << tagline << ", comment expected '" << test.sComment << "', but got '" << line.getComment() << "'" << std::endl; + ret = -1; + } + + if (line.getNameQuotes() != test.nNameQuotes) { + std::cerr << tagline << ", name-has-quotes expected" << std::endl; + ret = -1; + } + + if (line.getValueQuotes() != test.nValueQuotes) { + std::cerr << tagline << ", value-quotes expected" << std::endl; + ret = -1; + } + } + return ret; +} \ No newline at end of file diff --git a/src/tests/test_memory_leaks_linux.cpp b/src/tests/test_memory_leaks_linux.cpp new file mode 100644 index 0000000..fcd6465 --- /dev/null +++ b/src/tests/test_memory_leaks_linux.cpp @@ -0,0 +1,98 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include +#include "helpers.h" + +#if defined(_WIN32) +int main() { + // for windows skip test + return 0; +} +#else + +#include "get_current_rss.h" +#include "process_mem_usage.h" + +int createManyTimesObjects() { + std::string sFilepath = find_test_data_file("data/for-memory-leak/some.yml"); + + std::string sError; + for (int i = 0; i < 10000; i++) { + WsjcppYaml yaml; + + if (!yaml.loadFromFile(sFilepath, sError)) { + std::cerr << sError << std::endl; + return -1; + } + } + return 0; +} + +int main() { + double nBeforeVm, nBeforeRss; + + // first use for memory alloc memory for work + if (createManyTimesObjects() != 0) { + return -1; + } + + process_mem_usage(nBeforeVm, nBeforeRss); + if ((int)nBeforeVm <= 0) { + std::cerr << "memory vm could not be null" << std::endl; + return -1; + } + if ((int)nBeforeRss <= 0) { + std::cerr << "memory rss could not be null" << std::endl; + return -1; + } + + // code again check the memoty leak + if (createManyTimesObjects() != 0) { + return -1; + } + + double nAfterVm, nAfterRss; + + process_mem_usage(nAfterVm, nAfterRss); + + std::cerr << "memory vm before " << nBeforeVm << ", after " << nAfterVm << std::endl; + std::cerr << "memory rss before " << nBeforeRss << ", after " << nAfterRss << std::endl; + + if ((int)nBeforeVm != (int)nAfterVm) { + std::cerr << "memory leak found by vm" << std::endl; + return -1; + } + if ((int)nBeforeRss != (int)nAfterRss) { + std::cerr << "memory leak found by rss" << std::endl; + return -1; + } + + return 0; +} + +#endif diff --git a/src/tests/test_parser_comments.cpp b/src/tests/test_parser_comments.cpp index 748be41..6a6ba0d 100644 --- a/src/tests/test_parser_comments.cpp +++ b/src/tests/test_parser_comments.cpp @@ -1,3 +1,29 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + #include #include diff --git a/src/tests/test_python_yaml_issue_33.cpp b/src/tests/test_python_yaml_issue_33.cpp index d4eb4e1..033ffe6 100644 --- a/src/tests/test_python_yaml_issue_33.cpp +++ b/src/tests/test_python_yaml_issue_33.cpp @@ -1,3 +1,29 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + #include #include #include @@ -7,7 +33,7 @@ int main() { // TODO return 0; WsjcppYaml yaml; - std::string sFilepath = "../../../../src/tests/data-tests/python_yaml_issue_33/example.yaml"; + std::string sFilepath = "../../../../src/tests/data/python_yaml_issue_33/example.yaml"; std::string sError; if (!yaml.loadFromFile(sFilepath, sError)) { std::cerr << "Error parsing: " << sError << std::endl; diff --git a/src/tests/test_read_write_file.cpp b/src/tests/test_read_write_file.cpp new file mode 100644 index 0000000..e5e0e7d --- /dev/null +++ b/src/tests/test_read_write_file.cpp @@ -0,0 +1,106 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include +#include +#include +#include "helpers.h" + +int main() { + + std::string sFilepath = find_test_data_file("data/read-write-file/docker-compose.yml"); + std::string sFilepathOutput = find_test_data_file("data/read-write-file/docker-compose.output.yml"); + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromFile(sFilepath, sError)) { + std::cerr << "Error parsing: " << sError << std::endl; + return -1; + } + + int ret = 0; + + if (!yaml.getRoot()->hasElement("version")) { + std::cerr << "Check failed: version" << std::endl; + ret = -1; + } + if (!yaml.getRoot()->hasElement("volumes")) { + std::cerr << "Check failed: volumes" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->hasElement("networks")) { + std::cerr << "Check failed: networks" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->hasElement("services")) { + std::cerr << "Check failed: services" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("version")->getValue() != "3") { + std::cerr << "Check failed: version-value" << std::endl; + ret = -1; + } + + + if (!yaml.saveToFile(sFilepathOutput, sError)) { + std::cerr << "Error save: " << sError << std::endl; + return -1; + } + + std::ifstream f1(sFilepath.c_str()); + if (!f1) { + std::cerr << "FAILED could not open file to read " << sFilepath << std::endl; + return -1; + } + + std::string sOriginalFileContent = std::string( + (std::istreambuf_iterator(f1)), + std::istreambuf_iterator() + ); + + std::ifstream f2(sFilepathOutput.c_str()); + if (!f2) { + std::cerr << "FAILED could not open file to read " << sFilepathOutput << std::endl; + return -1; + } + + std::string sOutputFileContent = std::string( + (std::istreambuf_iterator(f2)), + std::istreambuf_iterator() + ); + + sOriginalFileContent += "\n"; // TODO fix + if (sOutputFileContent != sOriginalFileContent) { + std::cerr << "Check failed: not equal" << std::endl; + ret = -1; + } + + return ret; +} diff --git a/src/tests/test_read_wsjcpp_hold_yaml.cpp b/src/tests/test_read_wsjcpp_hold_yaml.cpp new file mode 100644 index 0000000..923f747 --- /dev/null +++ b/src/tests/test_read_wsjcpp_hold_yaml.cpp @@ -0,0 +1,40 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include +#include "helpers.h" + +int main() { + WsjcppYaml yaml; + std::string sFilepath = find_test_data_file("data/read-wsjcpp-hold-yml/wsjcpp.hold.yml"); + std::string sError; + if (!yaml.loadFromFile(sFilepath, sError)) { + std::cerr << "Error parsing: " << sError << std::endl; + return -1; + } + return 0; +} diff --git a/src/tests/test_read_yaml.cpp b/src/tests/test_read_yaml.cpp new file mode 100644 index 0000000..46d7562 --- /dev/null +++ b/src/tests/test_read_yaml.cpp @@ -0,0 +1,523 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include "helpers.h" + +int main() { + WsjcppYaml yaml; + std::string sFilepath = find_test_data_file("data/read-file/example-voiting-app/docker-compose.yml"); + std::string sError; + if (!yaml.loadFromFile(sFilepath, sError)) { + std::cerr << "Error parsing: " << sError << std::endl; + return -1; + } + + if (yaml.getRoot()->hasElement("version") != true) { + std::cerr << "has version" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("version")->isValue() != true) { + std::cerr << "has version is value" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("version")->getValue() != "3") { + std::cerr << "version-value" << std::endl; + return -1; + } + + if (yaml.getRoot()->hasElement("services") != true) { + std::cerr << "has services" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("services")->isMap() != true) { + std::cerr << "has services is map" << std::endl; + return -1; + } + + WsjcppYamlNode *pServices = yaml.getRoot()->getElement("services"); + + // services.vote + { + if (pServices->hasElement("vote") != true) { + std::cerr << "has services.vote" << std::endl; + return -1; + } + if (pServices->getElement("vote")->isMap() != true) { + std::cerr << "services.vote is map" << std::endl; + return -1; + } + if (pServices->getElement("vote")->getKeys().size() != 5) { + std::cerr << "services.vote keys size 5" << std::endl; + return -1; + } + + WsjcppYamlNode *pVote = pServices->getElement("vote"); + + if (pVote->hasElement("build") != true) { + std::cerr << "has services.vote.build" << std::endl; + return -1; + } + if (pVote->getElement("build")->getValue() != "./vote") { + std::cerr << "services.vote.build val" << std::endl; + return -1; + } + + if (pVote->hasElement("command") != true) { + std::cerr << "has services.vote.command" << std::endl; + return -1; + } + if (pVote->getElement("command")->getValue() != "python app.py") { + std::cerr << "services.vote.command val" << std::endl; + return -1; + } + + WsjcppYamlNode *pVolumes = pVote->getElement("volumes"); + + if (pVolumes->isArray() != true) { + std::cerr << "services.vote.volumes is array" << std::endl; + return -1; + } + if (pVolumes->getLength() != 1) { + std::cerr << "services.vote.volumes size 1" << std::endl; + return -1; + } + if (pVolumes->getElement(0)->getValue() != "./vote:/app") { + std::cerr << "services.vote.volumes val 0" << std::endl; + return -1; + } + + WsjcppYamlNode *pVotePorts = pVote->getElement("ports"); + if (pVotePorts->isArray() != true) { + std::cerr << "services.vote.ports is array" << std::endl; + return -1; + } + if (pVotePorts->getLength() != 1) { + std::cerr << "services.vote.ports size 1" << std::endl; + return -1; + } + if (pVotePorts->getElement(0)->getValue() != "5000:80") { + std::cerr << "services.vote.ports val 0" << std::endl; + return -1; + } + + WsjcppYamlNode *pVoteNetworks = pVote->getElement("networks"); + if (pVoteNetworks->getLength() != 2) { + std::cerr << "services.vote.networks size 2" << std::endl; + return -1; + } + if (pVoteNetworks->getElement(0)->getValue() != "front-tier") { + std::cerr << "services.vote.networks val 0" << std::endl; + return -1; + } + if (pVoteNetworks->getElement(1)->getValue() != "back-tier") { + std::cerr << "services.vote.networks val 1" << std::endl; + return -1; + } + } + + // services.result + { + if (pServices->hasElement("result") != true) { + std::cerr << "has services.result" << std::endl; + return -1; + } + if (pServices->getElement("result")->isMap() != true) { + std::cerr << "services.result is map" << std::endl; + return -1; + } + if (pServices->getElement("result")->getKeys().size() != 5) { + std::cerr << "services.result is map" << std::endl; + return -1; + } + + WsjcppYamlNode *pResult = pServices->getElement("result"); + + if (pResult->hasElement("build") != true) { + std::cerr << "has services.result.build" << std::endl; + return -1; + } + if (pResult->getElement("build")->getValue() != "./result") { + std::cerr << "services.result.build val" << std::endl; + return -1; + } + + if (pResult->hasElement("command") != true) { + std::cerr << "has services.result.command" << std::endl; + return -1; + } + if (pResult->getElement("command")->getValue() != "nodemon server.js") { + std::cerr << "services.result.command val" << std::endl; + return -1; + } + + WsjcppYamlNode *pResultVolumes = pResult->getElement("volumes"); + if (pResultVolumes->isArray() != true) { + std::cerr << "services.result.volumes is array" << std::endl; + return -1; + } + if (pResultVolumes->getLength() != 1) { + std::cerr << "services.result.volumes size 1" << std::endl; + return -1; + } + if (pResultVolumes->getElement(0)->getValue() != "./result:/app") { + std::cerr << "services.result.volumes val 0" << std::endl; return -1; + } + + WsjcppYamlNode *pResultPorts = pResult->getElement("ports"); + if (pResultPorts->isArray() != true) { + std::cerr << "services.result.ports is array" << std::endl; + return -1; + } + if (pResultPorts->getLength() != 2) { + std::cerr << "services.result.ports size 2" << std::endl; + return -1; + } + if (pResultPorts->getElement(0)->getValue() != "5001:80") { + std::cerr << "services.result.ports val 0" << std::endl; + return -1; + } + if (pResultPorts->getElement(1)->getValue() != "5858:5858") { + std::cerr << "services.result.ports val 1" << std::endl; + return -1; + } + + WsjcppYamlNode *pResultNetworks = pResult->getElement("networks"); + if (pResultNetworks->getLength() != 2) { + std::cerr << "services.result.networks size 2" << std::endl; + return -1; + } + if (pResultNetworks->getElement(0)->getValue() != "front-tier") { + std::cerr << "services.result.networks val 0" << std::endl; + return -1; + } + if (pResultNetworks->getElement(1)->getValue() != "back-tier") { + std::cerr << "services.result.networks val 1" << std::endl; + return -1; + } + } + + // std::cerr << << std::endl; return -1; + + // services.worker + { + if (pServices->hasElement("worker") != true) { + std::cerr << "has services.worker" << std::endl; + return -1; + } + if (pServices->getElement("worker")->isMap() != true) { + std::cerr << "has services.worker is map" << std::endl; + return -1; + } + if (pServices->getElement("worker")->getKeys().size() != 3) { + std::cerr << "services.worker keys size 3" << std::endl; + return -1; + } + + WsjcppYamlNode *pWorker = pServices->getElement("worker"); + + if (pWorker->hasElement("build") != true) { + std::cerr << "has services.worker.build" << std::endl; + return -1; + } + if (pWorker->hasElement("depends_on") != true) { + std::cerr << "has services.worker.depends_on" << std::endl; + return -1; + } + if (pWorker->hasElement("networks") != true) { + std::cerr << "has services.worker.networks" << std::endl; + return -1; + } + + WsjcppYamlNode *pWorkerBuild = pWorker->getElement("build"); + if (pWorkerBuild->isMap() != true) { + std::cerr << "services.worker.build is map" << std::endl; + return -1; + } + if (pWorkerBuild->hasElement("context") != true) { + std::cerr << "has services.worker.build.context" << std::endl; + return -1; + } + if (pWorkerBuild->getElement("context")->getValue() != "./worker") { + std::cerr << "services.worker.build.context val" << std::endl; + return -1; + } + + WsjcppYamlNode *pWorkerDependsOn = pWorker->getElement("depends_on"); + if (pWorkerDependsOn->isArray() != true) { + std::cerr << "has services.worker.depends_on" << std::endl; + return -1; + } + if (pWorkerDependsOn->getLength() != 2) { + std::cerr << "services.worker.depends_on size 2" << std::endl; + return -1; + } + if (pWorkerDependsOn->getElement(0)->getValue() != "redis") { + std::cerr << "services.worker.depends_on val 0" << std::endl; + return -1; + } + if (pWorkerDependsOn->getElement(1)->getValue() != "db") { + std::cerr << "services.worker.depends_on val 1" << std::endl; + return -1; + } + + WsjcppYamlNode *pWorkerNetworks = pWorker->getElement("networks"); + if (pWorkerNetworks->getLength() != 1) { + std::cerr << "services.worker.networks size 1" << std::endl; + return -1; + } + if (pWorkerNetworks->getElement(0)->getValue() != "back-tier") { + std::cerr << "services.worker.networks val 0" << std::endl; + return -1; + } + } + + // services.redis + { + if (pServices->hasElement("redis") != true) { + std::cerr << "has services.redis" << std::endl; + return -1; + } + if (pServices->getElement("redis")->isMap() != true) { + std::cerr << "services.redis is map" << std::endl; + return -1; + } + if (pServices->getElement("redis")->getKeys().size() != 4) { + std::cerr << "services.redis keys size 4" << std::endl; + return -1; + } + + WsjcppYamlNode *pRedis = pServices->getElement("redis"); + if (pRedis->hasElement("image") != true) { + std::cerr << "has services.redis.image" << std::endl; + return -1; + } + if (pRedis->hasElement("container_name") != true) { + std::cerr << "has services.redis.container_name" << std::endl; + return -1; + } + if (pRedis->hasElement("ports") != true) { + std::cerr << "has services.redis.ports" << std::endl; + return -1; + } + if (pRedis->hasElement("networks") != true) { + std::cerr << "has services.redis.networks" << std::endl; + return -1; + } + + if (pRedis->getElement("image")->getValue() != "redis:alpine") { + std::cerr << "services.redis.image value" << std::endl; + return -1; + } + if (pRedis->getElement("container_name")->getValue() != "redis") { + std::cerr << "services.redis.container_name" << std::endl; + return -1; + } + + WsjcppYamlNode *pRedisPorts = pRedis->getElement("ports"); + // TODO bug #17 + if (pRedisPorts->isValue() != true) { + std::cerr << "services.redis.ports is value" << std::endl; + return -1; + } + if (pRedisPorts->getValue() != "[\"6379\"]") { + std::cerr << "services.redis.ports value" << std::endl; + return -1; + } + + WsjcppYamlNode *pRedisNetworks = pRedis->getElement("networks"); + if (pRedisNetworks->getLength() != 1) { + std::cerr << "services.redis.networks size 1" << std::endl; + return -1; + } + if (pRedisNetworks->getElement(0)->getValue() != "back-tier") { + std::cerr << "services.redis.networks val 0" << std::endl; + return -1; + } + } + + // services.db + { + if (pServices->hasElement("db") != true) { + std::cerr << "has services.db" << std::endl; + return -1; + } + if (pServices->getElement("db")->isMap() != true) { + std::cerr << "has services.db is map" << std::endl; + return -1; + } + if (pServices->getElement("db")->getKeys().size() != 5) { + std::cerr << "services.db keys size 5" << std::endl; + return -1; + } + + WsjcppYamlNode *pServicesDb = pServices->getElement("db"); + + if (pServicesDb->hasElement("image") != true) { + std::cerr << "has services.db.image" << std::endl; + return -1; + } + if (pServicesDb->getElement("image")->getValue() != "postgres:9.4") { + std::cerr << "services.db.image value" << std::endl; + return -1; + } + if (pServicesDb->getElement("container_name")->getValue() != "db") { + std::cerr << "services.db.container_name" << std::endl; + return -1; + } + + if (pServicesDb->hasElement("environment") != true) { + std::cerr << "has services.db.environment" << std::endl; + return -1; + } + if (pServicesDb->getElement("environment")->isMap() != true) { + std::cerr << "services.db.environment is map" << std::endl; + return -1; + } + + WsjcppYamlNode *pDbEnvironment = pServicesDb->getElement("environment"); + + if (pDbEnvironment->hasElement("POSTGRES_USER") != true) { + std::cerr << "has services.db.environment.POSTGRES_USER" << std::endl; + return -1; + } + if (pDbEnvironment->getElement("POSTGRES_USER")->getValue() != "postgres") { + std::cerr << "services.db.environment.POSTGRES_USER" << std::endl; + return -1; + } + + if (pDbEnvironment->hasElement("POSTGRES_PASSWORD") != true) { + std::cerr << "has services.db.environment.POSTGRES_PASSWORD" << std::endl; + return -1; + } + if (pDbEnvironment->getElement("POSTGRES_PASSWORD")->getValue() != "postgres") { + std::cerr << "services.db.environment.POSTGRES_PASSWORD" << std::endl; + return -1; + } + + if (pServicesDb->hasElement("volumes") != true) { + std::cerr << "has services.db.volumes" << std::endl; + return -1; + } + if (pServicesDb->getElement("volumes")->isArray() != true) { + std::cerr << "services.db.volumes is array" << std::endl; + return -1; + } + + WsjcppYamlNode *pDbVolumes = pServicesDb->getElement("volumes"); + if (pDbVolumes->getLength() != 1) { + std::cerr << "services.db.volumes size 1" << std::endl; + return -1; + } + if (pDbVolumes->getElement(0)->getValue() != "db-data:/var/lib/postgresql/data") { + std::cerr << "services.db.volumes val 0" << std::endl; + return -1; + } + + if (pServicesDb->hasElement("networks") != true) { + std::cerr << "has services.db.networks" << std::endl; + return -1; + } + if (pServicesDb->getElement("networks")->isArray() != true) { + std::cerr << "services.db.networks is array" << std::endl; + return -1; + } + + WsjcppYamlNode *pDbNetworks = pServicesDb->getElement("networks"); + if (pDbNetworks->getLength() != 1) { + std::cerr << "services.db.networks size 1" << std::endl; + return -1; + } + if (pDbNetworks->getElement(0)->getValue() != "back-tier") { + std::cerr << "services.db.networks val 0" << std::endl; + return -1; + } + } + + // volumes + { + if (yaml.getRoot()->hasElement("volumes") != true) { + std::cerr << "has volumes" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("volumes")->isMap() != true) { + std::cerr << "has volumes is map" << std::endl; + return -1; + } + + WsjcppYamlNode *pVolumes = yaml.getRoot()->getElement("volumes"); + + if (pVolumes->hasElement("db-data") != true) { + std::cerr << "has volumes.db-data" << std::endl; + return -1; + } + if (pVolumes->getElement("db-data")->isUndefined() != true) { + std::cerr << "has volumes.db-data is undefined" << std::endl; + return -1; + } + if (pVolumes->getKeys().size() != 1) { + std::cerr << "has volumes keys size 1" << std::endl; + return -1; + } + } + + // networks + { + if (yaml.getRoot()->hasElement("networks") != true) { + std::cerr << "has networks" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("networks")->isMap() != true) { + std::cerr << "has networks is map" << std::endl; + return -1; + } + + WsjcppYamlNode *pNeworks = yaml.getRoot()->getElement("networks"); + if (pNeworks->getKeys().size() != 2) { + std::cerr << "has networks keys size 2" << std::endl; + return -1; + } + + if (pNeworks->hasElement("front-tier") != true) { + std::cerr << "has networks.front-tier" << std::endl; + return -1; + } + if (pNeworks->getElement("front-tier")->isUndefined() != true) { + std::cerr << "has networks.front-tier is undefined" << std::endl; + return -1; + } + + if (pNeworks->hasElement("back-tier") != true) { + std::cerr << "has networks.back-tier" << std::endl; + return -1; + } + if (pNeworks->getElement("back-tier")->isUndefined() != true) { + std::cerr << "has networks.back-tier is undefined" << std::endl; + return -1; + } + } + return 0; +} diff --git a/src/tests/test_remove_element_from_array.cpp b/src/tests/test_remove_element_from_array.cpp new file mode 100644 index 0000000..6d7e5b6 --- /dev/null +++ b/src/tests/test_remove_element_from_array.cpp @@ -0,0 +1,92 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include +#include "helpers.h" + +int main() { + std::string sFilepath = find_test_data_file("data/remove-element-from-array.yml"); + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromFile(sFilepath, sError)) { + std::cerr << "Error parsing: " << sError << std::endl; + return -1; + } + + int ret = 0; + + WsjcppYamlNode *pArr1 = yaml.getRoot()->getElement("arr1"); + if (pArr1->getLength() != 4) { + std::cerr << "Check failed: arr1 len" << std::endl; + ret = -1; + } + + if (pArr1->getElement(0)->getElement("name")->getValue() != "i1") { + std::cerr << "Check failed: arr1 name0" << std::endl; + ret = -1; + } + + if (pArr1->getElement(1)->getElement("name")->getValue() != "i2") { + std::cerr << "Check failed: arr1 name1" << std::endl; + ret = -1; + } + + if (pArr1->getElement(2)->getElement("name")->getValue() != "i3") { + std::cerr << "Check failed: arr1 name2" << std::endl; + ret = -1; + } + + if (pArr1->getElement(3)->getValue() != "very different array items type") { + std::cerr << "Check failed: arr1 name3" << std::endl; + ret = -1; + } + + pArr1->removeElement(1); + + if (pArr1->getLength() != 3) { + std::cerr << "Check failed: arr1 len (2)" << std::endl; + ret = -1; + } + + if (pArr1->getElement(0)->getElement("name")->getValue() != "i1") { + std::cerr << "Check failed: arr1 name0 (2)" << std::endl; + ret = -1; + } + + if (pArr1->getElement(1)->getElement("name")->getValue() != "i3") { + std::cerr << "Check failed: arr1 name1 (2)" << std::endl; + ret = -1; + } + + if (pArr1->getElement(2)->getValue() != "very different array items type") { + std::cerr << "Check failed: arr1 name2 (2)" << std::endl; + ret = -1; + } + + return ret; +} diff --git a/src/tests/test_remove_element_from_map.cpp b/src/tests/test_remove_element_from_map.cpp new file mode 100644 index 0000000..491f1b9 --- /dev/null +++ b/src/tests/test_remove_element_from_map.cpp @@ -0,0 +1,128 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + int ret = 0; + + std::string sTestYaml = + "# Some comment 1\n" + "map1: \n" + " map11: \n" + " map111: \n" + " param1111: v1111\n" + " param1112: v1112\n" + " map112: \n" + " param1121: v1121\n" + " param1122: v1122\n" + " map113: \n" + " param1131: v1131\n" + " param1132: v1132\n" + " map12: \n" + " param121: v121\n" + " param122: v122\n" + " map123: \n" + " param1231: v1231\n" + " param1232: v1232\n" + "param2: v2 # some comment 2\n" + "\n" // empty line + ; + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromString("rm_elem_in_map", sTestYaml, sError)) { + std::cerr << "Error parsing " << sError << std::endl; + return -1; + } + + WsjcppYamlNode *pMap1 = yaml.getRoot()->getElement("map1"); + WsjcppYamlNode *pMap11 = pMap1->getElement("map11"); + + if (!pMap11->hasElement("map111")) { + std::cerr << "Failed check 'has map111'" << std::endl; + ret = -1; + } + + if (!pMap11->hasElement("map112")) { + std::cerr << "Failed check 'has map112'" << std::endl; + ret = -1; + } + + if (!pMap11->hasElement("map113")) { + std::cerr << "Failed check 'has map113'" << std::endl; + ret = -1; + } + + pMap11->removeElement("map112"); + + if (!pMap11->hasElement("map111")) { + std::cerr << "Failed check 'has map111'" << std::endl; + ret = -1; + } + + if (pMap11->hasElement("map112")) { + std::cerr << "Failed check 'has map112'" << std::endl; + ret = -1; + } + + if (!pMap11->hasElement("map113")) { + std::cerr << "Failed check 'has map113'" << std::endl; + ret = -1; + } + + pMap11->removeElement("map111"); + + if (pMap11->hasElement("map111")) { + std::cerr << "Failed check 'has map111'" << std::endl; + ret = -1; + } + + if (pMap11->hasElement("map112")) { + std::cerr << "Failed check 'has map112'" << std::endl; + ret = -1; + } + + if (!pMap11->hasElement("map113")) { + std::cerr << "Failed check 'has map113'" << std::endl; + ret = -1; + } + + if (!pMap1->hasElement("map11")) { + std::cerr << "Failed check 'has map11'" << std::endl; + ret = -1; + } + + yaml.getRoot()->getElement("map1")->removeElement("map11"); + + if (pMap1->hasElement("map11")) { + std::cerr << "Failed check 'has map11'" << std::endl; + ret = -1; + } + + return ret; +} \ No newline at end of file diff --git a/src/tests/test_tag_names.cpp b/src/tests/test_tag_names.cpp new file mode 100644 index 0000000..8fdf690 --- /dev/null +++ b/src/tests/test_tag_names.cpp @@ -0,0 +1,100 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + std::string sTestYaml = "./test10: one"; + WsjcppYaml yaml; + std::string sError; + int ret = 0; + if (yaml.loadFromString("wrong name", "./test10: one", sError)) { + std::cerr << "Failed check 'wrong name'" << std::endl; + ret = -1; + } + + if (!yaml.loadFromString("name use quotes", "\"./test10\": one", sError)) { + std::cerr << "Failed check 'name use quotes 1'" << std::endl; + ret = -1; + } + + if (yaml.loadFromString("name use quotes", "\"./te\"st10\": one", sError)) { + std::cerr << "Failed check 'name use quotes 2 - wrong'" << std::endl; + ret = -1; + } + + if (!yaml.loadFromString("name use quotes", "\"./te\\\"st10\": one", sError)) { + std::cerr << "Failed check 'name use quotes 3'" << std::endl; + ret = -1; + } + + if (!yaml.loadFromString("array", + "arr1: \n" + " - ./te:11\n" + " - \"./te\":11\n" + , sError)) { + std::cerr << "Failed check 'array'" << std::endl; + ret = -1; + }; + + if (!yaml.getRoot()->getElement("arr1")->isArray()) { + std::cerr << "Failed check 'arr1 is array'" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("arr1")->getLength() != 2) { + std::cerr << "Failed check 'arr1 size 2'" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->getElement("arr1")->getElement(0)->isValue()) { + std::cerr << "Failed check 'arr1 el 0 is value'" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("arr1")->getElement(0)->getValue() != "./te:11") { + std::cerr << "Failed check 'arr1 el 0 is value'" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->getElement("arr1")->getElement(1)->isMap()) { + std::cerr << "Failed check 'arr1 el 1 is map'" << std::endl; + ret = -1; + } + + if (!yaml.getRoot()->getElement("arr1")->getElement(1)->hasElement("./te")) { + std::cerr << "Failed check 'arr1 el 1 is map'" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("arr1")->getElement(1)->getElement("./te")->getValue() != "11") { + std::cerr << "Failed check 'arr1 el 1 is map'" << std::endl; + ret = -1; + } + + return ret; +} \ No newline at end of file diff --git a/src/tests/test_yaml_parser_all.cpp b/src/tests/test_yaml_parser_all.cpp new file mode 100644 index 0000000..4d00e2f --- /dev/null +++ b/src/tests/test_yaml_parser_all.cpp @@ -0,0 +1,138 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + std::string sTestYaml = + "# Some comment 1\n" + "test10: one\n" + "test20: two # some comment 2\n" + "\n" // empty line + "array30:\n" + " - one31 # this field for test array30\n" + " - two32\n" + " - three33\n" + "\n" // empty line + "array40: # some comment 3\n" + " - \"one#1\" # some comment\n" + " # some comment\n" + " - \"two#2\"\n" + "array50: # some comment 3\n" + " - p1: test501\n" + " p2: test502\n" + " - p1: test503\n" + " p2: test504\n" + "\n" // empty line + "map60: # some comment 4\n" + " test70: opa1\n" + " test80: opa2" + ; + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromString("parse_all", sTestYaml, sError)) { + std::cerr << sError << std::endl; + return -1; + } + + std::string sSaved1 = ""; + if (!yaml.saveToString(sSaved1, sError)) { + std::cerr << sError << std::endl; + return -1; + } + + WsjcppYamlNode *pItem = nullptr; + if (yaml.getRoot()->getElement("test10")->getValue() != "one") { + std::cerr << "Failed check element 'test10'" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("test20")->getValue() != "two") { + std::cerr << "Failed check element 'test20'" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("array30"); + if (pItem->getLength() != 3) { + std::cerr << "Failed check element 'array30_length'" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("array30")->getElement(0); + if (pItem->getValue() != "one31") { + std::cerr << "Failed check element 'test30_value'" << std::endl; + return -1; + } + if (pItem->getComment() != "this field for test array30") { + std::cerr << "Failed check element 'test30_comment'" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("array30")->getElement(1); + if (pItem->getValue() != "two32") { + std::cerr << "Failed check element 'test40_value'" << std::endl; + return -1; + } + if (pItem->getComment() != "") { + std::cerr << "Failed check element 'test40_comment'" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("array40"); + if (pItem->getLength() != 2) { + std::cerr << "Failed check element 'array40_length'" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("array50"); + if (pItem->getLength() != 2) { + std::cerr << "Failed check element 'array50_length'" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("map60")->getElement("test70"); + if (pItem->getValue() != "opa1") { + std::cerr << "Failed check element 'test70_value'" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("map60")->getElement("test80"); + if (pItem->getValue() != "opa2") { + std::cerr << "Failed check element 'test80_comment'" << std::endl; + return -1; + } + + std::string sSaved2 = ""; + if (!yaml.saveToString(sSaved2, sError)) { + std::cerr << sError << std::endl; + return -1; + } + if (sSaved1 != sSaved2) { + std::cerr << "Expected '" << sSaved2 << "' but got '" << sSaved1 << "'" << std::endl; + return -1; + } +} \ No newline at end of file diff --git a/src/tests/test_yaml_parser_array_included_map.cpp b/src/tests/test_yaml_parser_array_included_map.cpp new file mode 100644 index 0000000..d0af44a --- /dev/null +++ b/src/tests/test_yaml_parser_array_included_map.cpp @@ -0,0 +1,184 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + + std::string sTestYaml = + "#test array included map\n" + "param1: none value1 # it's value for something # olala \n" + "array-test2 : # some comment 2 \n" + " - value21 # comment v21 \n" + " - value22 # comment v22 \n" + " - true # comment true \n" + " # some\n" + "array-and-map:\n" + " - submap-param1: v01\n" + " submap-param2: v02\n" + " - submap-param1: v11\n" + " submap-param2: v12\n" + "param2: v2\n" + ; + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromString("map_in_array", sTestYaml, sError)) { + std::cerr << sError << std::endl; + return -1; + } + + WsjcppYamlNode *pItem = nullptr; + + int ret = 0; + if (yaml.getRoot()->getElement("param1")->getValue() != "none value1") { + std::cerr << "Failed check 'param1-value' " << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param1")->getPlaceInFile().getLine() != "param1: none value1 # it's value for something # olala ") { + std::cerr << "Failed check 'param1-line' " << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param1")->getPlaceInFile().getNumberOfLine() != 1) { + std::cerr << "Failed check 'param1-original-number-of-line' " << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param1")->getComment() != "it's value for something # olala") { + std::cerr << "Failed check 'param1-original-number-of-line' " << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getLength() != 3) { + std::cerr << "Failed check 'array-test2-length' " << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getComment() != "some comment 2") { + std::cerr << "Failed check 'array-test2-comment' " << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("array-test2")->getElement(0); + + if (pItem->getValue() != "value21") { + std::cerr << "Failed check 'array-test2-element0-value' " << std::endl; + ret = -1; + } + + if (pItem->getComment() != "comment v21") { + std::cerr << "Failed check 'array-test2-element0-comment' " << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("array-test2")->getElement(1); + + if (yaml.getRoot()->getElement("array-test2")->getElement(1)->getValue() != "value22") { + std::cerr << "Failed check 'array-test2-element1-value' " << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getElement(1)->getComment() != "comment v22") { + std::cerr << "Failed check 'array-test2-element1-comment' " << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("array-test2")->getElement(2); + + if (pItem->getValue() != "true") { + std::cerr << "Failed check 'array-test2-element2-value' " << std::endl; + ret = -1; + } + + if (pItem->getComment() != "comment true") { + std::cerr << "Failed check 'array-test2-element2-comment' " << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("array-and-map")->getLength() != 2) { + std::cerr << "Failed check 'array-and-map-length' " << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("array-and-map")->getElement(0); + + if (pItem->getElement("submap-param1")->getValue() != "v01") { + std::cerr << "Failed check 'array-and-map-element0-value' " << std::endl; + ret = -1; + } + + if (pItem->getElement("submap-param2")->getValue() != "v02") { + std::cerr << "Failed check 'array-and-map-element0-value' " << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("array-and-map")->getElement(1); + + if (pItem->getElement("submap-param1")->getValue() != "v11") { + std::cerr << "Failed check 'array-and-map-element1-value' " << std::endl; + ret = -1; + } + + if (pItem->getElement("submap-param2")->getValue() != "v12") { + std::cerr << "Failed check 'array-and-map-element1-value' " << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param2")->getValue() != "v2") { + std::cerr << "Failed check 'param2-value' " << std::endl; + ret = -1; + } + + std::string sSaved = ""; + if (!yaml.saveToString(sSaved, sError)) { + std::cerr << "Failed check 'saveToString' " << std::endl; + ret = -1; + } else { + std::string sExpected = + "# test array included map\n" // expected + "param1: none value1 # it's value for something # olala\n" + "array-test2: # some comment 2\n" + " - value21 # comment v21\n" + " - value22 # comment v22\n" + " - true # comment true\n" + " # some\n" + "array-and-map:\n" + " - submap-param1: v01\n" + " submap-param2: v02\n" + " - submap-param1: v11\n" + " submap-param2: v12\n" + "param2: v2\n" + ; + if (sSaved != sExpected) { + std::cerr << "Expected '" << sExpected << "', but got '" << sSaved << "'" << std::endl; + ret = -1; + } + } + return ret; +} \ No newline at end of file diff --git a/src/tests/test_yaml_parser_hierarchical_map.cpp b/src/tests/test_yaml_parser_hierarchical_map.cpp new file mode 100644 index 0000000..29f8690 --- /dev/null +++ b/src/tests/test_yaml_parser_hierarchical_map.cpp @@ -0,0 +1,168 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + std::string sTestYaml = + "# Some comment 1\n" + "map1: \n" + " map11: \n" + " map111: \n" + " param1111: v1111\n" + " param1112: v1112\n" + " map112: \n" + " param1121: v1121\n" + " param1122: v1122\n" + " map113: \n" + " param1131: v1131\n" + " param1132: v1132\n" + " map12: \n" + " param121: v121\n" + " param122: v122\n" + " map123: \n" + " param1231: v1231\n" + " param1232: v1232\n" + "param2: v2 # some comment 2\n" + "\n" // empty line + ; + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromString("hard_map", sTestYaml, sError)) { + std::cerr << "Error parsing: " << sError << std::endl; + return -1; + } + + int ret = 0; + + WsjcppYamlNode *pItem = nullptr; + + pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map111"); + + if (pItem->getElement("param1111")->getValue() != "v1111") { + std::cerr << "Check failed: param1111" << std::endl; + ret = -1; + } + + if (pItem->getElement("param1112")->getValue() != "v1112") { + std::cerr << "Check failed: param1112" << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map112"); + + if (pItem->getElement("param1121")->getValue() != "v1121") { + std::cerr << "Check failed: param1121" << std::endl; + ret = -1; + } + + if (pItem->getElement("param1122")->getValue() != "v1122") { + std::cerr << "Check failed: param1122" << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map113"); + + if (pItem->getElement("param1131")->getValue() != "v1131") { + std::cerr << "Check failed: param1131" << std::endl; + ret = -1; + } + + if (pItem->getElement("param1132")->getValue() != "v1132") { + std::cerr << "Check failed: param1132" << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("map1")->getElement("map12"); + + if (pItem->getElement("param121")->getValue() != "v121") { + std::cerr << "Check failed: param121" << std::endl; + ret = -1; + } + + if (pItem->getElement("param122")->getValue() != "v122") { + std::cerr << "Check failed: param122" << std::endl; + ret = -1; + } + + pItem = yaml.getRoot()->getElement("map1")->getElement("map12")->getElement("map123"); + + if (pItem->getElement("param1231")->getValue() != "v1231") { + std::cerr << "Check failed: param1231" << std::endl; + ret = -1; + } + + if (pItem->getElement("param1232")->getValue() != "v1232") { + std::cerr << "Check failed: param1232" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param2")->getValue() != "v2") { + std::cerr << "Check failed: param2" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param2")->getComment() != "some comment 2") { + std::cerr << "Check failed: param2-comment" << std::endl; + ret = -1; + } + + std::string sSaved = ""; + if (!yaml.saveToString(sSaved, sError)) { + std::cerr << "Error save: " << sError << std::endl; + return -1; + } + + std::string sExpectedYaml = + "# Some comment 1\n" + "map1:\n" + " map11:\n" + " map111:\n" + " param1111: v1111\n" + " param1112: v1112\n" + " map112:\n" + " param1121: v1121\n" + " param1122: v1122\n" + " map113:\n" + " param1131: v1131\n" + " param1132: v1132\n" + " map12:\n" + " param121: v121\n" + " param122: v122\n" + " map123:\n" + " param1231: v1231\n" + " param1232: v1232\n" + "param2: v2 # some comment 2\n" + ; + + if (sSaved != sExpectedYaml) { + std::cerr << " sSaved != sExpectedYaml " << std::endl; + return -1; + } + return ret; +} \ No newline at end of file diff --git a/src/tests/test_yaml_parser_quotes.cpp b/src/tests/test_yaml_parser_quotes.cpp new file mode 100644 index 0000000..e1af434 --- /dev/null +++ b/src/tests/test_yaml_parser_quotes.cpp @@ -0,0 +1,140 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + std::string sTestYaml = + "# Some comment 1\n" + "param1: \"value1\" # v1\n" + "param2: \" #$!!!value2\" # val 2\n" + "\" param3 olala\" : val 3 # val 3*** \n" + "param4: ' #$!!!value4' # val 4\n" + "'param5 aha': ' #$!!!value5' # val 5\n" + "url: \"https://github.com/wsjcpp/wsjcpp-yaml\"\n" + "issues: https://github.com/wsjcpp/wsjcpp-yaml/issues\n" + "empty: \"\"\n" + "array:\n" + " - \"https://github.com/wsjcpp/wsjcpp-core:v0.0.1\"\n" + ; + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromString("parse_quotes", sTestYaml, sError)) { + std::cerr << "Error parsing: " << sError << std::endl; + return -1; + } + + int ret = 0; + + // TODO: .findLine(0) + + if (yaml.getRoot()->getElement("param1")->getValue() != "value1") { + std::cerr << "Check failed: param1" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param1")->getComment() != "v1") { + std::cerr << "Check failed: param1-comment" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param2")->getValue() != " #$!!!value2") { + std::cerr << "Check failed: param2" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param2")->getComment() != "val 2") { + std::cerr << "Check failed: param2-comment" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement(" param3 olala")->getValue() != "val 3") { + std::cerr << "Check failed: param3 olala" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement(" param3 olala")->getComment() != "val 3***") { + std::cerr << "Check failed: param3 olala comment" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param4")->getValue() != " #$!!!value4") { + std::cerr << "Check failed: param4 val" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("param4")->getComment() != "val 4") { + std::cerr << "Check failed: param4 val" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("url")->getValue() != "https://github.com/wsjcpp/wsjcpp-yaml") { + std::cerr << "Check failed: param4 url-value" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("issues")->getValue() != "https://github.com/wsjcpp/wsjcpp-yaml/issues") { + std::cerr << "Check failed: param4 issues-value" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("empty")->getValue() != "") { + std::cerr << "Check failed: param4 empty-value" << std::endl; + ret = -1; + } + + if (yaml.getRoot()->getElement("array")->getElement(0)->getValue() != "https://github.com/wsjcpp/wsjcpp-core:v0.0.1") { + std::cerr << "Check failed: array-element0-value" << std::endl; + ret = -1; + } + + std::string sSaved = ""; + if (!yaml.saveToString(sSaved, sError)) { + std::cerr << "Error save: " << sError << std::endl; + return -1; + } + std::string sExpected = + "# Some comment 1\n" + "param1: \"value1\" # v1\n" + "param2: \" #$!!!value2\" # val 2\n" + "\" param3 olala\": val 3 # val 3***\n" + "param4: ' #$!!!value4' # val 4\n" + "'param5 aha': ' #$!!!value5' # val 5\n" + "url: \"https://github.com/wsjcpp/wsjcpp-yaml\"\n" + "issues: https://github.com/wsjcpp/wsjcpp-yaml/issues\n" + "empty: \"\"\n" + "array:\n" + " - \"https://github.com/wsjcpp/wsjcpp-core:v0.0.1\"\n" + ; + if (sSaved != sExpected) { + std::cerr << "Check failed: not equal" << std::endl; + ret = -1; + } + + return ret; +} \ No newline at end of file diff --git a/src/tests/test_yaml_parser_simple_array.cpp b/src/tests/test_yaml_parser_simple_array.cpp new file mode 100644 index 0000000..ae5ff15 --- /dev/null +++ b/src/tests/test_yaml_parser_simple_array.cpp @@ -0,0 +1,219 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include +#include "helpers.h" + +int main() { + + std::string sFilepath = find_test_data_file("data/parser-simple-array.yml"); + WsjcppYaml yaml; + std::string sError; + + if (!yaml.loadFromFile(sFilepath, sError)) { + std::cerr << "Parsing error " << std::endl; + return -1; + } + + WsjcppYamlNode *pItem = nullptr; + + if (yaml.getRoot()->getElement("param1")->getValue() != "none value1") { + std::cerr << "param1-value" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("param1")->getPlaceInFile().getLine() != "param1: none value1 # it's value for something # olala ") { + std::cerr << "param1-line" << std::endl; + return -1; + } + + if (yaml.getRoot()->getElement("param1")->getPlaceInFile().getNumberOfLine() != 1) { + std::cerr << "param1-original-number-of-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("param1")->getComment() != "it's value for something # olala") { + std::cerr << "param1-comment" << std::endl; + return -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getLength() != 7) { + std::cerr << "array-test2-length" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getComment() != "some comment 2") { + std::cerr << "array-test2-comment" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("array-test2")->getElement(0); + if (pItem->getValue() != "value21") { + std::cerr << "array-test2-element0-value" << std::endl; + return -1; + } + if (pItem->getComment() != "comment v21") { + std::cerr << "array-test2-element0-comment" << std::endl; + return -1; + } + + pItem = yaml.getRoot()->getElement("array-test2")->getElement(1); + if (pItem->getValue() != "value22") { + std::cerr << "array-test2-element1-value" << std::endl; + return -1; + } + if (pItem->getComment() != "comment v22") { + std::cerr << "array-test2-element1-comment" << std::endl; + return -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getElement(2)->getValue() != "true") { + std::cerr << "array-test2-element2-value" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(2)->getPlaceInFile().getLine() != " - true # comment true ") { + std::cerr << "array-test2-element2-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(2)->getPlaceInFile().getNumberOfLine() != 5) { + std::cerr << "array-test2-element2-original-number-of-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(2)->getComment() != "comment true") { + std::cerr << "array-test2-element2-comment" << std::endl; + return -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getElement(3)->getValue() != "falsesome") { + std::cerr << "array-test2-element3-value" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(3)->getPlaceInFile().getLine() != " - falsesome ") { + std::cerr << "array-test2-element3-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(3)->getPlaceInFile().getNumberOfLine() != 7) { + std::cerr << "array-test2-element3-original-number-of-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(3)->getComment() != "") { + std::cerr << "array-test2-element3-comment" << std::endl; + return -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getElement(4)->getValue() != "free@free") { + std::cerr << "array-test2-element4-value" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(4)->getPlaceInFile().getLine() != " - free@free ") { + std::cerr << "array-test2-element4-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(4)->getPlaceInFile().getNumberOfLine() != 8) { + std::cerr << "array-test2-element4-original-number-of-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(4)->getComment() != "") { + std::cerr << "array-test2-element4-comment" << std::endl; + return -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getElement(5)->getValue() != "") { + std::cerr << "array-test2-element5-value" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(5)->getPlaceInFile().getLine() != " - # empty ") { + std::cerr << "array-test2-element5-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(5)->getPlaceInFile().getNumberOfLine() != 9) { + std::cerr << "array-test2-element5-original-number-of-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(5)->getComment() != "empty") { + std::cerr << "array-test2-element5-comment" << std::endl; + return -1; + } + + if (yaml.getRoot()->getElement("array-test2")->getElement(6)->getValue() != "1") { + std::cerr << "array-test2-element6-value" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(6)->getPlaceInFile().getLine() != " - 1") { + std::cerr << "array-test2-element6-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(6)->getPlaceInFile().getNumberOfLine() != 10) { + std::cerr << "array-test2-element6-original-number-of-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("array-test2")->getElement(6)->getComment() != "") { + std::cerr << "array-test2-element6-comment" << std::endl; + return -1; + } + + if (yaml.getRoot()->getElement("param2")->getValue() != "val2") { + std::cerr << "param2-value" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("param2")->getPlaceInFile().getLine() != "param2: val2 # value 2 ") { + std::cerr << "param2-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("param2")->getPlaceInFile().getNumberOfLine() != 11) { + std::cerr << "param2-original-number-of-line" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("param2")->getComment() != "value 2") { + std::cerr << "param2-comment" << std::endl; + return -1; + } + + std::string sSaved = ""; + if (!yaml.saveToString(sSaved, sError)) { + std::cerr << "save yaml" << std::endl; + return -1; + } + + std::string sExpected = "" + "# simple array test\n" // expected + "param1: none value1 # it's value for something # olala\n" + "array-test2: # some comment 2\n" + " - value21 # comment v21\n" + " - value22 # comment v22\n" + " - true # comment true\n" + " # some\n" + " - falsesome\n" + " - free@free\n" + " - # empty\n" + " - 1\n" + "param2: val2 # value 2\n" + ; + + if (sSaved != sExpected) { + std::cerr << "yaml_save unexpected" << std::endl; + return -1; + } + return 0; +} diff --git a/src/tests/test_yaml_parser_simple_map.cpp b/src/tests/test_yaml_parser_simple_map.cpp new file mode 100644 index 0000000..52f1fc4 --- /dev/null +++ b/src/tests/test_yaml_parser_simple_map.cpp @@ -0,0 +1,79 @@ +/* +MIT License + +Copyright (c) 2019-2025 wsjcpp + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml +*/ + +#include +#include + +int main() { + + std::string sTestYaml = + "# Some comment 1\n" + "param1: value1\n" + "param2: value2 # some comment 2\n" + "\n" // empty line + ; + + WsjcppYaml yaml; + std::string sError; + if (!yaml.loadFromString("simple_map", sTestYaml, sError)) { + std::cerr << "Error parsing: " << sError << std::endl; + return -1; + } + + WsjcppYamlNode *pItem = nullptr; + if (yaml.getRoot()->getElement("param1")->getValue() != "value1") { + std::cerr << "param1" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("param2")->getValue() != "value2") { + std::cerr << "param2" << std::endl; + return -1; + } + if (yaml.getRoot()->getElement("param2")->getComment() != "some comment 2") { + std::cerr << "param2" << std::endl; + return -1; + } + + std::string sSaved = ""; + bool bResult = yaml.saveToString(sSaved, sError); + if (bResult != true) { + std::cerr << "save yaml" << std::endl; + return -1; + } + + std::string sExpecetdYaml = "" + "# Some comment 1\n" + "param1: value1\n" + "param2: value2 # some comment 2\n" + ; + + if (sSaved != sExpecetdYaml) { + std::cerr << "yaml_save not equal save and expected" << std::endl; + return -1; + } + + return 0; +} diff --git a/src/wsjcpp_yaml.cpp b/src/wsjcpp_yaml.cpp index bb87695..a28439b 100644 --- a/src/wsjcpp_yaml.cpp +++ b/src/wsjcpp_yaml.cpp @@ -25,7 +25,6 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml */ #include "wsjcpp_yaml.h" -// #include #include #include #include @@ -156,7 +155,6 @@ std::string WsjcppYamlPlaceInFile::getForLogFormat() { // --------------------------------------------------------------------- // WsjcppYamlNode - #if defined(__CODEGEARC__) && !defined(_WIN64) #define WSJCPP_NULL NULL #else @@ -189,8 +187,6 @@ WsjcppYamlNode::WsjcppYamlNode( TAG = "WsjcppYamlNode"; } -// --------------------------------------------------------------------- - WsjcppYamlNode::~WsjcppYamlNode() { for (unsigned int i = 0; i < m_vObjects.size(); i++) { delete m_vObjects[i]; @@ -198,65 +194,45 @@ WsjcppYamlNode::~WsjcppYamlNode() { m_vObjects.clear(); } -// --------------------------------------------------------------------- - WsjcppYamlNode *WsjcppYamlNode::getParent() { return m_pParent; } -// --------------------------------------------------------------------- - WsjcppYamlPlaceInFile WsjcppYamlNode::getPlaceInFile() { return m_placeInFile; } -// --------------------------------------------------------------------- - void WsjcppYamlNode::setPlaceInFile(const WsjcppYamlPlaceInFile &placeInFile) { m_placeInFile.setFilename(placeInFile.getFilename()); m_placeInFile.setLine(placeInFile.getLine()); m_placeInFile.setNumberOfLine(placeInFile.getNumberOfLine()); } -// --------------------------------------------------------------------- - void WsjcppYamlNode::setComment(const std::string &sComment) { m_sComment = sComment; } -// --------------------------------------------------------------------- - std::string WsjcppYamlNode::getComment() { return m_sComment; } -// --------------------------------------------------------------------- - void WsjcppYamlNode::setName(const std::string &sName, WsjcppYamlQuotes nNameQuotes) { m_sName = sName; m_nNameQuotes = nNameQuotes; } -// --------------------------------------------------------------------- - std::string WsjcppYamlNode::getName() { return m_sName; } -// --------------------------------------------------------------------- - WsjcppYamlQuotes WsjcppYamlNode::getNameQuotes() { return m_nNameQuotes; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::isEmpty() { return m_nItemType == WSJCPP_YAML_NODE_EMPTY; } -// --------------------------------------------------------------------- - void WsjcppYamlNode::doEmpty() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_EMPTY; @@ -265,14 +241,10 @@ void WsjcppYamlNode::doEmpty() { } } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::isUndefined() { return m_nItemType == WSJCPP_YAML_NODE_UNDEFINED; } -// --------------------------------------------------------------------- - void WsjcppYamlNode::doArray() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_ARRAY; @@ -281,8 +253,6 @@ void WsjcppYamlNode::doArray() { } } -// --------------------------------------------------------------------- - void WsjcppYamlNode::doMap() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_MAP; @@ -291,8 +261,6 @@ void WsjcppYamlNode::doMap() { } } -// --------------------------------------------------------------------- - void WsjcppYamlNode::doValue() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_VALUE; @@ -301,14 +269,10 @@ void WsjcppYamlNode::doValue() { } } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::isMap() { return m_nItemType == WSJCPP_YAML_NODE_MAP; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::hasElement(const std::string &sName) { if (m_nItemType != WSJCPP_YAML_NODE_MAP) { throw std::runtime_error(TAG + ": hasElement('" + sName + "'): Element must be map"); @@ -321,11 +285,9 @@ bool WsjcppYamlNode::hasElement(const std::string &sName) { return false; } -// --------------------------------------------------------------------- - WsjcppYamlNode *WsjcppYamlNode::getElement(const std::string &sName) { if (m_nItemType != WSJCPP_YAML_NODE_MAP) { - throw std::runtime_error(TAG + ": getElement: Element must be map"); + throw std::runtime_error(TAG + ": getElement (" + sName + "): Element must be map " + this->getForLogFormat()); } for (unsigned int i = 0; i < m_vObjects.size(); i++) { @@ -338,8 +300,6 @@ WsjcppYamlNode *WsjcppYamlNode::getElement(const std::string &sName) { return WSJCPP_NULL; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::setElement(const std::string &sName, WsjcppYamlNode *pItem) { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_MAP; // change item type to map on first element @@ -358,8 +318,6 @@ bool WsjcppYamlNode::setElement(const std::string &sName, WsjcppYamlNode *pItem) return true; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::removeElement(const std::string &sName) { if (m_nItemType != WSJCPP_YAML_NODE_MAP) { throw std::runtime_error(TAG + ": removeElement: Element must be map"); @@ -376,8 +334,6 @@ bool WsjcppYamlNode::removeElement(const std::string &sName) { return false; } -// --------------------------------------------------------------------- - std::vector WsjcppYamlNode::getKeys() { if (m_nItemType != WSJCPP_YAML_NODE_MAP) { throw std::runtime_error(TAG + ": getKeys: Element must be map"); @@ -393,8 +349,6 @@ std::vector WsjcppYamlNode::getKeys() { return vKeys; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::setElementValue( const std::string &sName, const std::string &sValue, @@ -402,13 +356,13 @@ bool WsjcppYamlNode::setElementValue( WsjcppYamlQuotes nValueQuotes ) { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { - m_nItemType = WSJCPP_YAML_NODE_MAP; // change item type to map on first element + m_nItemType = WSJCPP_YAML_NODE_MAP; // change item type to map on first element } if (m_nItemType != WSJCPP_YAML_NODE_MAP) { throw std::runtime_error(TAG + ": setElement, Element must be 'map' for " + this->getPlaceInFile().getForLogFormat()); } - + if (this->hasElement(sName)) { WsjcppYamlNode *pItem = this->getElement(sName); pItem->setValue(sValue, nValueQuotes); @@ -422,8 +376,6 @@ bool WsjcppYamlNode::setElementValue( return true; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::createElementMap(const std::string &sName, WsjcppYamlQuotes nNameQuotes) { if (m_nItemType != WSJCPP_YAML_NODE_MAP ) { throw std::runtime_error(TAG + ": createElementMap, Element must be 'map' for " + this->getPlaceInFile().getForLogFormat()); @@ -439,8 +391,6 @@ bool WsjcppYamlNode::createElementMap(const std::string &sName, WsjcppYamlQuotes return true; } -// --------------------------------------------------------------------- - WsjcppYamlNode *WsjcppYamlNode::createElementMap() { if (m_nItemType != WSJCPP_YAML_NODE_ARRAY ) { throw std::runtime_error(TAG + ": createElementMap, Element must be 'array' for " + this->getPlaceInFile().getForLogFormat()); @@ -451,8 +401,6 @@ WsjcppYamlNode *WsjcppYamlNode::createElementMap() { return pNewItem; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::createElementArray(const std::string &sName, WsjcppYamlQuotes nNameQuotes) { if (m_nItemType != WSJCPP_YAML_NODE_MAP ) { throw std::runtime_error(TAG + ": createElementArray, Element must be 'map' for " + this->getPlaceInFile().getForLogFormat()); @@ -467,14 +415,10 @@ bool WsjcppYamlNode::createElementArray(const std::string &sName, WsjcppYamlQuot return true; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::isArray() { return m_nItemType == WSJCPP_YAML_NODE_ARRAY; } -// --------------------------------------------------------------------- - int WsjcppYamlNode::getLength() { if (m_nItemType != WSJCPP_YAML_NODE_ARRAY) { throw std::runtime_error(TAG + ": getLength, Element must be array for " + this->getForLogFormat()); @@ -488,8 +432,6 @@ int WsjcppYamlNode::getLength() { return nCount; } -// --------------------------------------------------------------------- - WsjcppYamlNode *WsjcppYamlNode::getElement(int i) { if (m_nItemType != WSJCPP_YAML_NODE_ARRAY) { throw std::runtime_error(TAG + ": getElement, Element must be array"); @@ -511,8 +453,6 @@ WsjcppYamlNode *WsjcppYamlNode::getElement(int i) { return pItem; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::appendElement(WsjcppYamlNode *pNode) { if (pNode->isEmpty()) { m_vObjects.push_back(pNode); // TODO clone object @@ -530,8 +470,6 @@ bool WsjcppYamlNode::appendElement(WsjcppYamlNode *pNode) { return true; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::appendElementValue(const std::string &sValue, WsjcppYamlQuotes nValueQuotes) { if (m_nItemType != WSJCPP_YAML_NODE_ARRAY) { throw std::runtime_error(TAG + ": appendElementValue, Element must be array for " + this->getForLogFormat()); @@ -542,8 +480,6 @@ bool WsjcppYamlNode::appendElementValue(const std::string &sValue, WsjcppYamlQuo return this->appendElement(pNewItem); } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::removeElement(int i) { if (m_nItemType != WSJCPP_YAML_NODE_ARRAY) { throw std::runtime_error(TAG + ": appendElement, Element must be array for " + this->getForLogFormat()); @@ -573,14 +509,10 @@ bool WsjcppYamlNode::removeElement(int i) { return false; } -// --------------------------------------------------------------------- - bool WsjcppYamlNode::isValue() { return m_nItemType == WSJCPP_YAML_NODE_VALUE; } -// --------------------------------------------------------------------- - std::string WsjcppYamlNode::getValue() { if (m_nItemType != WSJCPP_YAML_NODE_VALUE) { throw std::runtime_error(TAG + ": getValue, Element must be value for " + this->getForLogFormat()); @@ -588,8 +520,6 @@ std::string WsjcppYamlNode::getValue() { return m_sValue; } -// --------------------------------------------------------------------- - void WsjcppYamlNode::setValue(const std::string &sValue, WsjcppYamlQuotes nQuotes) { if (m_nItemType != WSJCPP_YAML_NODE_VALUE) { throw std::runtime_error(TAG + ": setValue, Element must be value for " + this->getForLogFormat()); @@ -598,14 +528,10 @@ void WsjcppYamlNode::setValue(const std::string &sValue, WsjcppYamlQuotes nQuote m_sValue = sValue; } -// --------------------------------------------------------------------- - WsjcppYamlQuotes WsjcppYamlNode::getValueQuotes() { return m_nValueQuotes; } -// --------------------------------------------------------------------- - std::string WsjcppYamlNode::getSerializedName() { std::string sRet = ""; // TODO escape quotes @@ -619,8 +545,6 @@ std::string WsjcppYamlNode::getSerializedName() { return sRet; } -// --------------------------------------------------------------------- - std::string WsjcppYamlNode::toString(std::string sIndent) { std::string sRet = ""; if (this->isValue()) { @@ -694,7 +618,7 @@ std::string WsjcppYamlNode::toString(std::string sIndent) { sRet += sIndent + pNode->getStringNodeLastIndent() + pNode->getSerializedName() + ":"; if (pNode->getComment().length() > 0) { - sRet += " # " + pNode->getComment(); + sRet += " # " + pNode->getComment(); } std::string s = pNode->toString(sIndent + pNode->getStringNodeLastIndent()); if (pNode->isMap()) { @@ -738,8 +662,6 @@ std::string WsjcppYamlNode::toString(std::string sIndent) { return sRet; } -// --------------------------------------------------------------------- - std::string WsjcppYamlNode::getNodeTypeAsString() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { return "undefined"; @@ -753,26 +675,18 @@ std::string WsjcppYamlNode::getNodeTypeAsString() { return "unknown"; } -// --------------------------------------------------------------------- - std::string WsjcppYamlNode::getForLogFormat() { return m_placeInFile.getForLogFormat(); } -// --------------------------------------------------------------------- - int WsjcppYamlNode::getNodeLastIndent() { return m_nNodeDiffIndent; } -// --------------------------------------------------------------------- - std::string WsjcppYamlNode::getStringNodeLastIndent() { return m_sNodeDiffIndent; } -// --------------------------------------------------------------------- - void WsjcppYamlNode::setNodeIndents(const std::vector & vNodeIndents) { m_nNodeDiffIndent = vNodeIndents.back(); m_sNodeDiffIndent = ""; @@ -785,14 +699,10 @@ void WsjcppYamlNode::setNodeIndents(const std::vector & vNodeIndents) { } } -// --------------------------------------------------------------------- - int WsjcppYamlNode::getNodeIndent() { return m_nNodeIndent; } -// --------------------------------------------------------------------- - int WsjcppYamlNode::getNumberOfLine() const { return m_placeInFile.getNumberOfLine(); } @@ -806,7 +716,7 @@ void WsjcppYamlNode::throw_error(const std::string &sError) { } void WsjcppYamlNode::removeLastCharNewLine(std::string &sLine) { - int nLen = sLine.length(); + size_t nLen = sLine.length(); if (nLen > 0 && sLine[nLen - 1] == '\n') { sLine = sLine.substr(0, nLen - 1); } @@ -851,80 +761,54 @@ int WsjcppYamlParsebleLine::getLineNumber() { return m_nLineNumber; } -// --------------------------------------------------------------------- - std::string WsjcppYamlParsebleLine::getPrefix() { return m_sPrefix; } -// --------------------------------------------------------------------- - int WsjcppYamlParsebleLine::getIndent() { - return m_sPrefix.length(); + return int(m_sPrefix.length()); } -// --------------------------------------------------------------------- - bool WsjcppYamlParsebleLine::isArrayItem() { return m_bArrayItem; } -// --------------------------------------------------------------------- - std::string WsjcppYamlParsebleLine::getComment() { return m_sComment; } -// --------------------------------------------------------------------- - bool WsjcppYamlParsebleLine::hasComment() { return m_bHasComment; } -// --------------------------------------------------------------------- - std::string WsjcppYamlParsebleLine::getName() { return m_sTagName; } -// --------------------------------------------------------------------- - WsjcppYamlQuotes WsjcppYamlParsebleLine::getNameQuotes() { return m_nNameQuotes; } -// --------------------------------------------------------------------- - bool WsjcppYamlParsebleLine::isEmptyName() { return m_sTagName.length() == 0; } -// --------------------------------------------------------------------- - std::string WsjcppYamlParsebleLine::getValue() { return m_sValue; } -// --------------------------------------------------------------------- - WsjcppYamlQuotes WsjcppYamlParsebleLine::getValueQuotes() { return m_nValueQuotes; } -// --------------------------------------------------------------------- - bool WsjcppYamlParsebleLine::isEmptyValue() { return m_sValue.length() == 0; } -// --------------------------------------------------------------------- - bool WsjcppYamlParsebleLine::isEmptyLine() { return m_bEmptyLine; } -// --------------------------------------------------------------------- - enum WsjcppYamlParserLineStates { WSJCPP_YAML_PARSER_LINE_STATE_NO, WSJCPP_YAML_PARSER_LINE_STATE_VALUE, @@ -1070,12 +954,10 @@ bool WsjcppYamlParsebleLine::parseLine(const std::string &sLine, std::string &sE return true; } -// --------------------------------------------------------------------- - bool WsjcppYamlParsebleLine::canTagName(const std::string &sVal) { std::string sTrim = sVal; sTrim = WsjcppYaml::trim(sTrim); - int nLen = sTrim.length(); + size_t nLen = sTrim.length(); if (nLen == 0) { return false; } @@ -1100,14 +982,12 @@ bool WsjcppYamlParsebleLine::canTagName(const std::string &sVal) { return true; } -// --------------------------------------------------------------------- - std::string WsjcppYamlParsebleLine::removeStringDoubleQuotes(const std::string &sValue) { if (sValue.size() > 0 && sValue[0] != '"') { return sValue; } int nStartPos = 1; - int nEndPos = sValue.size()-1; + size_t nEndPos = sValue.size() - 1; std::string sRet = ""; bool bEscape = false; for (int i = nStartPos; i < nEndPos; i++) { @@ -1129,15 +1009,12 @@ std::string WsjcppYamlParsebleLine::removeStringDoubleQuotes(const std::string & return sRet; } - -// --------------------------------------------------------------------- - std::string WsjcppYamlParsebleLine::removeStringSingleQuotes(const std::string &sValue) { if (sValue.size() > 0 && sValue[0] != '\'') { return sValue; } int nStartPos = 1; - int nEndPos = sValue.size()-1; + size_t nEndPos = sValue.size() - 1; std::string sRet = ""; bool bEscape = false; for (int i = nStartPos; i < nEndPos; i++) { @@ -1199,56 +1076,38 @@ size_t WsjcppYamlCursor::size() const { return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isArray() ? m_pCurrentNode->getLength() : -1; } -// --------------------------------------------------------------------- - bool WsjcppYamlCursor::isMap() const { return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isMap(); } -// --------------------------------------------------------------------- - std::vector WsjcppYamlCursor::keys() const { return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isMap() ? m_pCurrentNode->getKeys() : std::vector(); } -// --------------------------------------------------------------------- - bool WsjcppYamlCursor::hasKey(const std::string &sKey) const { return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sKey); } -// --------------------------------------------------------------------- - // WsjcppYamlCursor &WsjcppYamlCursor::set(const std::string &sName, const std::string &sValue) { // return *this; // } // -// // --------------------------------------------------------------------- -// // WsjcppYamlCursor &WsjcppYamlCursor::set(const std::string &sName, int nValue) { // return *this; // } // -// // --------------------------------------------------------------------- -// // WsjcppYamlCursor &WsjcppYamlCursor::set(const std::string &sName, bool bValue) { // return *this; // } // -// // --------------------------------------------------------------------- -// // WsjcppYamlCursor &WsjcppYamlCursor::remove(const std::string &sKey) { // return *this; // } -// --------------------------------------------------------------------- - std::string WsjcppYamlCursor::comment() { return m_pCurrentNode != WSJCPP_NULL ? m_pCurrentNode->getComment() : std::string(""); } -// --------------------------------------------------------------------- - WsjcppYamlCursor &WsjcppYamlCursor::comment(const std::string& sComment) { if (m_pCurrentNode != WSJCPP_NULL) { m_pCurrentNode->setComment(sComment); @@ -1256,8 +1115,6 @@ WsjcppYamlCursor &WsjcppYamlCursor::comment(const std::string& sComment) { return *this; } -// --------------------------------------------------------------------- - std::string WsjcppYamlCursor::valStr() const { return m_pCurrentNode != WSJCPP_NULL ? m_pCurrentNode->getValue() : std::string(""); } @@ -1431,8 +1288,6 @@ bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError) return parse(sFileName, sTextContent, sError); } -// --------------------------------------------------------------------- - bool WsjcppYaml::saveToFile(const std::string &sFileName, std::string &sError) { this->info(TAG, "Saving to " + sFileName); std::string sBuffer = m_pRoot->toString() + "\n"; // last empty line must be always @@ -1449,34 +1304,24 @@ bool WsjcppYaml::loadFromString(const std::string &sBufferName, const std::strin return parse(sBufferName, sBuffer, sError); } -// --------------------------------------------------------------------- - bool WsjcppYaml::saveToString(std::string &sBuffer, std::string &sError) { // WsjcppLog::warn(TAG, "WsjcppYaml::saveToString"); sBuffer = m_pRoot->toString() + "\n"; // last empty line must be always return true; } -// --------------------------------------------------------------------- - WsjcppYamlNode *WsjcppYaml::getRoot() { return m_pRoot; } -// --------------------------------------------------------------------- - WsjcppYamlCursor WsjcppYaml::getCursor() const { return WsjcppYamlCursor(m_pRoot); } -// --------------------------------------------------------------------- - WsjcppYamlCursor WsjcppYaml::operator[](int idx) const { return this->getCursor()[idx]; } -// --------------------------------------------------------------------- - WsjcppYamlCursor WsjcppYaml::operator[](const std::string &sName) const { return this->getCursor()[sName]; } @@ -1563,12 +1408,10 @@ void WsjcppYaml::info(const std::string &TAG, const std::string &sMessage) { } } -// --------------------------------------------------------------------- - std::vector WsjcppYaml::splitToLines(const std::string &sBuffer) { std::vector vLines; std::string sLine = ""; - int nSize = sBuffer.length(); + size_t nSize = sBuffer.length(); for (int i = 0; i < nSize; i++) { char c = sBuffer[i]; if (c == '\n') { @@ -1584,8 +1427,6 @@ std::vector WsjcppYaml::splitToLines(const std::string &sBuffer) { return vLines; } -// --------------------------------------------------------------------- - bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer, std::string &sError) { this->clear(); if (m_pRoot == WSJCPP_NULL) { @@ -1717,15 +1558,11 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer, return true; } -// --------------------------------------------------------------------- - void WsjcppYaml::process_hasName_emptyValue_arrayItem() { this->warn(TAG, "process_hasName_emptyValue_arrayItem"); this->logUnknownParseLine(); } -// --------------------------------------------------------------------- - void WsjcppYaml::process_hasName_emptyValue_noArrayItem() { // std::cout << "process_hasName_emptyValue_noArrayItem" << std::endl; if (m_parseLine.getIndent() == m_pParseCurrentParentNode->getNodeIndent()) { @@ -1755,8 +1592,6 @@ void WsjcppYaml::process_hasName_emptyValue_noArrayItem() { } } -// --------------------------------------------------------------------- - void WsjcppYaml::process_hasName_hasValue_arrayItem() { // std::cout << "process_hasName_hasValue_arrayItem " << std::endl; if (m_pParseCurrentParentNode->isUndefined()) { @@ -1795,8 +1630,6 @@ void WsjcppYaml::process_hasName_hasValue_arrayItem() { m_nParseCurrentIndent += 2; } -// --------------------------------------------------------------------- - void WsjcppYaml::process_hasName_hasValue_noArrayItem() { // std::cout << "process_hasName_hasValue_noArrayItem" << std::endl; WsjcppYamlNode *pNode = new WsjcppYamlNode( @@ -1816,8 +1649,6 @@ void WsjcppYaml::process_hasName_hasValue_noArrayItem() { // m_pParseCurrentParentNode = pItem; } -// --------------------------------------------------------------------- - void WsjcppYaml::process_emptyName_hasValue_arrayItem() { // std::cout << "process_emptyName_hasValue_arrayItem " << std::endl; if (m_pParseCurrentParentNode->isUndefined()) { @@ -1836,15 +1667,11 @@ void WsjcppYaml::process_emptyName_hasValue_arrayItem() { pNode->setNodeIndents(m_vStackDiffNodeIndents); } -// --------------------------------------------------------------------- - void WsjcppYaml::process_emptyName_hasValue_noArrayItem() { this->warn(TAG, "TODO process_emptyName_hasValue_noArrayItem"); this->logUnknownParseLine(); } -// --------------------------------------------------------------------- - void WsjcppYaml::process_emptyName_emptyValue_arrayItem() { // std::cout << "process_emptyName_emptyValue_arrayItem" << std::endl; if (m_pParseCurrentParentNode->isUndefined()) { @@ -1862,8 +1689,6 @@ void WsjcppYaml::process_emptyName_emptyValue_arrayItem() { m_pParseCurrentParentNode->appendElement(pNode); } -// --------------------------------------------------------------------- - void WsjcppYaml::process_emptyName_emptyValue_noArrayItem() { // std::cout << "process_emptyName_emptyValue_noArrayItem " << std::endl; WsjcppYamlNode *pNode = new WsjcppYamlNode( diff --git a/src/wsjcpp_yaml.h b/src/wsjcpp_yaml.h index e1e05a6..cdcca75 100644 --- a/src/wsjcpp_yaml.h +++ b/src/wsjcpp_yaml.h @@ -35,8 +35,6 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml #include #include -// --------------------------------------------------------------------- - enum WsjcppYamlNodeType { WSJCPP_YAML_NODE_UNDEFINED = 0, WSJCPP_YAML_NODE_EMPTY = 1, @@ -45,7 +43,11 @@ enum WsjcppYamlNodeType { WSJCPP_YAML_NODE_VALUE = 4 }; -// --------------------------------------------------------------------- +enum WsjcppYamlQuotes { + WSJCPP_YAML_QUOTES_NONE, + WSJCPP_YAML_QUOTES_DOUBLE, + WSJCPP_YAML_QUOTES_SINGLE +}; class WsjcppYamlPlaceInFile { public: @@ -71,12 +73,6 @@ class WsjcppYamlPlaceInFile { // --------------------------------------------------------------------- // WsjcppYamlQuotes -enum WsjcppYamlQuotes { - WSJCPP_YAML_QUOTES_NONE, - WSJCPP_YAML_QUOTES_DOUBLE, - WSJCPP_YAML_QUOTES_SINGLE -}; - class IWsjcppYamlLog { public: virtual void err(const std::string &TAG, const std::string &sMessage) = 0; diff --git a/unit-tests.wsjcpp/.gitignore b/unit-tests.wsjcpp/.gitignore deleted file mode 100644 index 0a460e8..0000000 --- a/unit-tests.wsjcpp/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -tmp/* -logs/* -unit-tests -.wsjcpp/* - -# Vim temp files -*.swp - diff --git a/unit-tests.wsjcpp/CMakeLists.txt b/unit-tests.wsjcpp/CMakeLists.txt deleted file mode 100644 index 2138ce9..0000000 --- a/unit-tests.wsjcpp/CMakeLists.txt +++ /dev/null @@ -1,63 +0,0 @@ -# Automaticly generated by wsjcpp@v0.2.4 -cmake_minimum_required(VERSION 3.0) - -project(unit-tests C CXX) -add_definitions(-DWSJCPP_APP_VERSION="ut-v0.1.8") -add_definitions(-DWSJCPP_APP_NAME="unit-tests-wsjcpp-yaml") - -if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - set(MACOSX TRUE) -endif() - -set(CMAKE_CXX_STANDARD 17) -set(EXECUTABLE_OUTPUT_PATH ${unit-tests_SOURCE_DIR}) - -set (WSJCPP_LIBRARIES "") -set (WSJCPP_INCLUDE_DIRS "") -set (WSJCPP_SOURCES "") - -find_package(Threads REQUIRED) -list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) - -# wsjcpp-core:v0.2.1 -list (APPEND WSJCPP_INCLUDE_DIRS "../src.wsjcpp/wsjcpp_core/") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../src.wsjcpp/wsjcpp_core/wsjcpp_core.h") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp") - -# wsjcpp-yaml:v0.1.8 -list (APPEND WSJCPP_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/../src") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../src/wsjcpp_yaml.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../src/wsjcpp_yaml.h") - -# unit-tests -list (APPEND WSJCPP_INCLUDE_DIRS "src") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_line_parser.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_remove_element_in_array.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_memory_leaks.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_read_yaml.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_read_write_file.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_cursor.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_tag_names.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_cleanup.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_append_elements.cpp") -list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_keep_format.cpp") - -include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt) - - -include_directories(${WSJCPP_INCLUDE_DIRS}) - -add_executable ("unit-tests" ${WSJCPP_SOURCES}) - -target_link_libraries("unit-tests" -lpthread ${WSJCPP_LIBRARIES} ) - diff --git a/unit-tests.wsjcpp/CMakeLists.user-custom.txt b/unit-tests.wsjcpp/CMakeLists.user-custom.txt deleted file mode 100644 index 127f538..0000000 --- a/unit-tests.wsjcpp/CMakeLists.user-custom.txt +++ /dev/null @@ -1,2 +0,0 @@ -## You can here write some custom includes - diff --git a/unit-tests.wsjcpp/build_simple.sh b/unit-tests.wsjcpp/build_simple.sh deleted file mode 100755 index aef6538..0000000 --- a/unit-tests.wsjcpp/build_simple.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash - -check_ret() { - if [ $1 -ne 0 ]; then - echo "" - echo "!!! FAIL: $2" - echo "********************************************************************************" - echo "" - exit $1 - else - echo "" - echo "*** SUCCESS: $2" - echo "********************************************************************************" - echo "" - fi -} - -if [ ! -d tmp ]; then - mkdir -p tmp -fi - -cd tmp -cmake .. -check_ret $? "configure" - -make -check_ret $? "make" - diff --git a/unit-tests.wsjcpp/src/unit_test_append_elements.cpp b/unit-tests.wsjcpp/src/unit_test_append_elements.cpp deleted file mode 100644 index 0d29051..0000000 --- a/unit-tests.wsjcpp/src/unit_test_append_elements.cpp +++ /dev/null @@ -1,73 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestAppendElements - -class UnitTestAppendElements : public WsjcppUnitTestBase { - public: - UnitTestAppendElements(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestAppendElements) - -UnitTestAppendElements::UnitTestAppendElements() - : WsjcppUnitTestBase("UnitTestAppendElements") { -} - -// --------------------------------------------------------------------- - -bool UnitTestAppendElements::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestAppendElements::executeTest() { - - WsjcppYaml yml; - yml.getRoot()->setElementValue("p1", "val1"); - yml.getRoot()->createElementMap("some"); - WsjcppYamlNode *pSome = yml.getRoot()->getElement("some"); - pSome->setElementValue("p2", "val2"); - pSome->createElementMap("sub-some"); - WsjcppYamlNode *pSubSome = pSome->getElement("sub-some"); - pSubSome->setElementValue("p3", "val3"); - pSome->createElementArray("arr-some"); - WsjcppYamlNode *pArrSome = pSome->getElement("arr-some"); - pArrSome->appendElementValue("1234"); - WsjcppYamlPlaceInFile placeInFile; - WsjcppYamlNode *pItemMap = new WsjcppYamlNode(pArrSome, &yml, placeInFile, WSJCPP_YAML_NODE_MAP); - pArrSome->appendElement(pItemMap); - pItemMap->setElementValue("p4", "val4"); - pItemMap->setElementValue("p5", "val5"); - yml.getRoot()->setElementValue("p6", "val6"); - - compare("created yaml 1", yml.getRoot()->toString(), - "p1: val1\n" - "some:\n" - " p2: val2\n" - " sub-some:\n" - " p3: val3\n" - " arr-some:\n" - " - 1234\n" - " - p4: val4\n" - " p5: val5\n" - "p6: val6" - ); -} - -// --------------------------------------------------------------------- - -bool UnitTestAppendElements::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_cleanup.cpp b/unit-tests.wsjcpp/src/unit_test_cleanup.cpp deleted file mode 100644 index d713084..0000000 --- a/unit-tests.wsjcpp/src/unit_test_cleanup.cpp +++ /dev/null @@ -1,81 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestCleanup - -class UnitTestCleanup : public WsjcppUnitTestBase { - public: - UnitTestCleanup(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestCleanup) - -UnitTestCleanup::UnitTestCleanup() - : WsjcppUnitTestBase("UnitTestCleanup") { -} - -// --------------------------------------------------------------------- - -bool UnitTestCleanup::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestCleanup::executeTest() { - std::string sTestYaml1 = - "# Some comment 1\n" - "test10: one\n" - "test20: two # some comment 2\n" - ; - - std::string sTestYaml2 = - "# Some comment 1\n" - "test11: one\n" - "test20: two # some comment 2\n" - "test22: two # some comment 2\n" - ; - - WsjcppYaml yaml; - std::string sError; - - if (!compare("Error parsing 1", yaml.loadFromString("parse1", sTestYaml1, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - compare("(1) has test10", yaml.getRoot()->hasElement("test10"), true); - compare("(1) has test11", yaml.getRoot()->hasElement("test11"), false); - compare("(1) has test20", yaml.getRoot()->hasElement("test20"), true); - compare("(1) has test22", yaml.getRoot()->hasElement("test22"), false); - - if (!compare("Error parsing 2", yaml.loadFromString("parse2", sTestYaml2, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - compare("(2) has test10", yaml.getRoot()->hasElement("test10"), false); - compare("(2) has test11", yaml.getRoot()->hasElement("test11"), true); - compare("(2) has test20", yaml.getRoot()->hasElement("test20"), true); - compare("(2) has test22", yaml.getRoot()->hasElement("test22"), true); - - yaml.clear(); - - compare("(3) has root", yaml.getRoot() == nullptr, true); -} - -// --------------------------------------------------------------------- - -bool UnitTestCleanup::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_cursor.cpp b/unit-tests.wsjcpp/src/unit_test_cursor.cpp deleted file mode 100644 index 0fb4e31..0000000 --- a/unit-tests.wsjcpp/src/unit_test_cursor.cpp +++ /dev/null @@ -1,177 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestCursor - -class UnitTestCursor : public WsjcppUnitTestBase { - public: - UnitTestCursor(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestCursor) - -UnitTestCursor::UnitTestCursor() - : WsjcppUnitTestBase("UnitTestCursor") { -} - -// --------------------------------------------------------------------- - -bool UnitTestCursor::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestCursor::executeTest() { - - std::string sTestYaml = - "# Some comment 1\n" - "map1: \n" - " map11: \n" - " sss: \n" - " map111: \n" - " param1111: v1111\n" - " param1112: v1112\n" - " map112: \n" - " param1121: v1121\n" - " param1122: v1122\n" - " map113: \n" - " param1131: v1131\n" - " param1132: v1132\n" - " map12: \n" - " param121: v121\n" - " param122: v122\n" - " map123: \n" - " param1231: v1231\n" - " param1232: v1232\n" - " param1232: v1232\n" - "param2: v2 # some comment 2\n" - "arr1: # some comment array 1\n" - " - some1\n" - " - 3\n" - "\n" - " - Yes\n" - " # empty\n" - " - no\n" - " - true\n" - " - False\n" - "\n" // empty line - ; - - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromString("test_cursor", sTestYaml, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - compare("map1 is null", yaml["map1"].isNull(), false); - compare("map1 is undefined", yaml["map1"].isUndefined(), false); - compare("map1 is value", yaml["map1"].isValue(), false); - compare("map1 is array", yaml["map1"].isArray(), false); - compare("map1 is map", yaml["map1"].isMap(), true); - - compare("map1-1111 is null", yaml["map1-1111"].isNull(), true); - compare("map1 is undefined", yaml["map1-1111"].isUndefined(), false); - compare("map1 is value", yaml["map1-1111"].isValue(), false); - compare("map1-1111 is array", yaml["map1-1111"].isArray(), false); - compare("map1-1111 is map", yaml["map1-1111"].isMap(), false); - - compare("arr1 is null", yaml["arr1"].isNull(), false); - compare("arr1 is undefined", yaml["arr1"].isUndefined(), false); - compare("arr1 is value", yaml["arr1"].isValue(), false); - compare("arr1 is array", yaml["arr1"].isArray(), true); - compare("arr1 is map", yaml["arr1"].isMap(), false); - - compare("map1.map11.sss is null", yaml["map1"]["map11"]["sss"].isNull(), false); - compare("map1.map11.sss is undefined", yaml["map1"]["map11"]["sss"].isUndefined(), true); - compare("map1.map11.sss is value", yaml["map1"]["map11"]["sss"].isValue(), false); - compare("map1.map11.sss is array", yaml["map1"]["map11"]["sss"].isArray(), false); - compare("map1.map11.sss is map", yaml["map1"]["map11"]["sss"].isMap(), false); - - compare("map1.map11.map111.param1111 is null", yaml["map1"]["map11"]["map111"]["param1111"].isNull(), false); - compare("map1.map11.map111.param1111 is undefined", yaml["map1"]["map11"]["map111"]["param1111"].isUndefined(), false); - compare("map1.map11.map111.param1111 is value", yaml["map1"]["map11"]["map111"]["param1111"].isValue(), true); - compare("map1.map11.map111.param1111 is array", yaml["map1"]["map11"]["map111"]["param1111"].isArray(), false); - compare("map1.map11.map111.param1111 is map", yaml["map1"]["map11"]["map111"]["param1111"].isMap(), false); - - compare("map use as array", yaml["map1"][0].isNull(), true); - compare("array use as map", yaml["arr1"]["0"].isNull(), true); - compare("array use as array", yaml["arr1"][0].isNull(), false); - - compare("array size", yaml["arr1"].comment(), "some comment array 1"); - compare("array size", yaml["arr1"].size(), 6); - compare("array el 0", yaml["arr1"][0].valStr(), "some1"); - compare("array el 1", yaml["arr1"][1].valStr(), "3"); - compare("array el 2", yaml["arr1"][2].valStr(), "Yes"); - compare("array el 3", yaml["arr1"][3].valStr(), "no"); - compare("array el 4", yaml["arr1"][4].valStr(), "true"); - compare("array el 5", yaml["arr1"][5].valStr(), "False"); - - compare("array bool el 2", yaml["arr1"][2].valBool(), true); - compare("array bool el 3", yaml["arr1"][3].valBool(), false); - compare("array bool el 4", yaml["arr1"][4].valBool(), true); - compare("array bool el 5", yaml["arr1"][5].valBool(), false); - - // int - compare("array size", yaml["arr1"][1].valInt(), 3); - yaml["arr1"][1].val(10); - compare("array size", yaml["arr1"][1].valInt(), 10); - - // string - compare("array el 0 - 1", yaml["arr1"][0].valStr(), "some1"); - yaml["arr1"][0].val("different value").comment("1234"); - compare("array el 0 - 2", yaml["arr1"][0].valStr(), "different value"); - compare("array el 0 - 3", yaml["arr1"][0].comment(), "1234"); - - // bool - compare("array bool el 4", yaml["arr1"][4].valStr(), "true"); - compare("array bool el 4", yaml["arr1"][4].node()->getValue(), "true"); - compare("array bool el 4", yaml["arr1"][4].valBool(), true); - yaml["arr1"][4].val(true); - compare("array bool el 4", yaml["arr1"][4].valStr(), "yes"); - compare("array bool el 4", yaml["arr1"][4].valBool(), true); - yaml["arr1"][4].val(false); - compare("array bool el 4", yaml["arr1"][4].valStr(), "no"); - compare("array bool el 4", yaml["arr1"][4].valBool(), false); - - // map - compare("map1.map12", yaml["map1"]["map12"].isMap(), true); - - std::vector vKeys = yaml["map1"]["map12"].keys(); - compare("map1.map12 keys size", vKeys.size(), 4); - if (vKeys.size() == 4) { - compare("map1.map12 keys 0", vKeys[0], "param121"); - compare("map1.map12 keys 1", vKeys[1], "param122"); - compare("map1.map12 keys 2", vKeys[2], "map123"); - compare("map1.map12 keys 3", vKeys[3], "param1232"); - } - compare("map1.map12 has key1", yaml["map1"]["map12"].hasKey("some"), false); - compare("map1.map12 has key2", yaml["map1"]["map12"].hasKey("map123"), true); - compare("map1.map12 is value", yaml["map1"]["map12"]["map123"].isValue(), false); - compare("map1.map12 is value", yaml["map1"]["map12"]["param122"].isValue(), true); - - - /* WsjcppYamlCursor &push(const std::string &sVal); - WsjcppYamlCursor &push(int nVal); - WsjcppYamlCursor &push(bool bVal); - WsjcppYamlCursor &remove(int nIdx);*/ - - -} - -// --------------------------------------------------------------------- - -bool UnitTestCursor::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_keep_format.cpp b/unit-tests.wsjcpp/src/unit_test_keep_format.cpp deleted file mode 100644 index 10db2a0..0000000 --- a/unit-tests.wsjcpp/src/unit_test_keep_format.cpp +++ /dev/null @@ -1,103 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestKeepFormat - -class UnitTestKeepFormat : public WsjcppUnitTestBase { - public: - UnitTestKeepFormat(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestKeepFormat) - -UnitTestKeepFormat::UnitTestKeepFormat() - : WsjcppUnitTestBase("UnitTestKeepFormat") { -} - -// --------------------------------------------------------------------- - -bool UnitTestKeepFormat::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestKeepFormat::executeTest() { - // https://github.com/wsjcpp/wsjcpp-yaml/issues/29 - - std::string sInput = - "# commet1:\n" - "\n" - "val1: 1\n" - "\n" - "unit-tests:\n" - " cases:\n" - " - name: \"NormalizeUnitTestName\"\n" - " description: \"Test Normalize UnitTest Name\"\n" - " - name: \"generateFilenameForUnitTest\"\n" - " description: \"Test generate filename for UnitTest\"\n" - " - name: \"InitPackage\"\n" - " description: \"Test for init package\"\n" - " enabled: \"no\"\n" - "\n" - " - name: \"ReadPackage\"\n" - " description: \"Test for read package\"\n" - "\n" - "\n" - "resources:\n" - " - filepath: \"./build_simple.sh\"\n" - " filesize: 77\n" - " pack-as: \"binary\"\n" - " modified: 1600401759\n" - " sha1: \"\"\n" - ; - - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromString("input.yml", sInput, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - std::string sOutput = ""; - if (!compare("Error parsing", yaml.saveToString(sOutput, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - if (!compare("Keep formating", sOutput, sInput)) { - return; - } - - // try again - if (!compare("Error parsing", yaml.loadFromString("output.yml", sOutput, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - sOutput = ""; - if (!compare("Error parsing", yaml.saveToString(sOutput, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - if (!compare("Keep formating", sOutput, sInput)) { - return; - } -} - -// --------------------------------------------------------------------- - -bool UnitTestKeepFormat::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_line_parser.cpp b/unit-tests.wsjcpp/src/unit_test_line_parser.cpp deleted file mode 100644 index 9dca9ca..0000000 --- a/unit-tests.wsjcpp/src/unit_test_line_parser.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestLineParser - -class UnitTestLineParser : public WsjcppUnitTestBase { - public: - UnitTestLineParser(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestLineParser) - -UnitTestLineParser::UnitTestLineParser() - : WsjcppUnitTestBase("UnitTestLineParser") { - // -} - -// --------------------------------------------------------------------- - -bool UnitTestLineParser::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestLineParser::executeTest() { - struct LineTest { - LineTest( - int nNumberOfTest, - std::string sLine, - std::string sPrefix, - bool isArrayItem, - std::string sName, - WsjcppYamlQuotes nNameQuotes, - std::string sValue, - WsjcppYamlQuotes nValueQuotes, - std::string sComment - ) : nNumberOfTest(nNumberOfTest), - sLine(sLine), - sPrefix(sPrefix), - isArrayItem(isArrayItem), - sName(sName), - nNameQuotes(nNameQuotes), - sValue(sValue), - nValueQuotes(nValueQuotes), - sComment(sComment) - { - // - }; - int nNumberOfTest; - std::string sLine; - std::string sPrefix; - bool isArrayItem; - std::string sName; - WsjcppYamlQuotes nNameQuotes; - std::string sValue; - WsjcppYamlQuotes nValueQuotes; - std::string sComment; - }; - - std::vector vTestLines; - - vTestLines.push_back(LineTest(1, - "# Some comment 1 ", "", - false, // array node - "", WSJCPP_YAML_QUOTES_NONE, // name - "", WSJCPP_YAML_QUOTES_NONE, // value - "Some comment 1" // comment - )); - vTestLines.push_back(LineTest(2, - " test2: \"t\\\"wo\" # some comment 2 ", " ", - false, // array node - "test2", WSJCPP_YAML_QUOTES_NONE, // name - "t\"wo", WSJCPP_YAML_QUOTES_DOUBLE, // value - "some comment 2" // comment - )); - vTestLines.push_back(LineTest(3, - " test3:", " ", - false, // array node - "test3", WSJCPP_YAML_QUOTES_NONE, // name - "", WSJCPP_YAML_QUOTES_NONE, // value - "" // comment - )); - vTestLines.push_back(LineTest(4, - " - test4", " ", - true, // array node - "", WSJCPP_YAML_QUOTES_NONE, // name - "test4", WSJCPP_YAML_QUOTES_NONE, // value - "" // comment - )); - vTestLines.push_back(LineTest(5, - "", "", - false, // array node - "", WSJCPP_YAML_QUOTES_NONE, // name - "", WSJCPP_YAML_QUOTES_NONE, // value - "" // comment - )); - vTestLines.push_back(LineTest(6, - " - \"test4:111\"", " ", - true, // array node - "", WSJCPP_YAML_QUOTES_NONE, // name - "test4:111", WSJCPP_YAML_QUOTES_DOUBLE, // value - "" // comment - )); - vTestLines.push_back(LineTest(7, - "issues: https://github.com/wsjcpp/wsjcpp-yaml/issues", "", - false, // array node - "issues", WSJCPP_YAML_QUOTES_NONE, // name - "https://github.com/wsjcpp/wsjcpp-yaml/issues", WSJCPP_YAML_QUOTES_NONE, // value - "" // comment - )); - - for (int i = 0; i < vTestLines.size(); i++) { - LineTest test = vTestLines[i]; - std::string tagline = "{line:" + std::to_string(test.nNumberOfTest) + ": '" + test.sLine + "'}"; - - WsjcppYamlParsebleLine line(test.nNumberOfTest); - std::string sError; - if (!compare(tagline + ", parseLine", line.parseLine(test.sLine, sError), true)) { - WsjcppLog::err(tagline + ", parseLine", sError); - return; - } - - compare(tagline + ", prefix", line.getPrefix(), test.sPrefix); - compare(tagline + ", arrayitem", line.isArrayItem(), test.isArrayItem); - compare(tagline + ", name", line.getName(), test.sName); - compare(tagline + ", name-has-quotes", line.getNameQuotes(), test.nNameQuotes); - compare(tagline + ", value", line.getValue(), test.sValue); - compare(tagline + ", value-quotes", line.getValueQuotes(), test.nValueQuotes); - compare(tagline + ", comment", line.getComment(), test.sComment); - } -} - -// --------------------------------------------------------------------- - -bool UnitTestLineParser::doAfterTest() { - // do somethig after test - return true; -} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_memory_leaks.cpp b/unit-tests.wsjcpp/src/unit_test_memory_leaks.cpp deleted file mode 100644 index 43657b9..0000000 --- a/unit-tests.wsjcpp/src/unit_test_memory_leaks.cpp +++ /dev/null @@ -1,79 +0,0 @@ - -#include -#include -#include -#include "get_current_rss.h" -#include "process_mem_usage.h" - - -// --------------------------------------------------------------------- -// UnitTestMemoryLeaks - -class UnitTestMemoryLeaks : public WsjcppUnitTestBase { - public: - UnitTestMemoryLeaks(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; - - private: - void createManyTimesObjects(); -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestMemoryLeaks) - -UnitTestMemoryLeaks::UnitTestMemoryLeaks() - : WsjcppUnitTestBase("UnitTestMemoryLeaks") { -} - -// --------------------------------------------------------------------- - -bool UnitTestMemoryLeaks::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestMemoryLeaks::createManyTimesObjects() { - std::string sFilepath = "./data-tests/for-memory-leak/some.yml"; - std::string sError; - for (int i = 0; i < 10000; i++) { - WsjcppYaml yaml; - if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - } -} - -// --------------------------------------------------------------------- - -void UnitTestMemoryLeaks::executeTest() { - double nBeforeVm, nBeforeRss; - double nAfterVm, nAfterRss; - std::string sFilepath = "./data-tests/for-memory-leak/some.yml"; - - // first use for memory alloc memory for work - createManyTimesObjects(); - - process_mem_usage(nBeforeVm, nBeforeRss); - compare("memory vm not null", (int)nBeforeVm > 0, true); - compare("memory vm not null", (int)nBeforeRss > 0, true); - - // code again check the memoty leak - createManyTimesObjects(); - - process_mem_usage(nAfterVm, nAfterRss); - compare("memory vm", (int)nAfterVm, (int)nBeforeVm); - compare("memory rss", (int)nAfterRss, (int)nBeforeRss); -} - -// --------------------------------------------------------------------- - -bool UnitTestMemoryLeaks::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_read_write_file.cpp b/unit-tests.wsjcpp/src/unit_test_read_write_file.cpp deleted file mode 100644 index 65f3d40..0000000 --- a/unit-tests.wsjcpp/src/unit_test_read_write_file.cpp +++ /dev/null @@ -1,71 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestReadWriteFile - -class UnitTestReadWriteFile : public WsjcppUnitTestBase { - public: - UnitTestReadWriteFile(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestReadWriteFile) - -UnitTestReadWriteFile::UnitTestReadWriteFile() - : WsjcppUnitTestBase("UnitTestReadWriteFile") { -} - -// --------------------------------------------------------------------- - -bool UnitTestReadWriteFile::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestReadWriteFile::executeTest() { - WsjcppYaml yaml; - std::string sFilepath = "./data-tests/read-write-file/docker-compose.yml"; - std::string sFilepathOutput = "./data-tests/read-write-file/docker-compose.output.yml"; - std::string sError; - if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - compare("has version", yaml.getRoot()->hasElement("version"), true); - compare("has volumes", yaml.getRoot()->hasElement("volumes"), true); - compare("has networks", yaml.getRoot()->hasElement("networks"), true); - compare("has services", yaml.getRoot()->hasElement("services"), true); - - compare("version-value", yaml.getRoot()->getElement("version")->getValue(), "3"); - - if (!compare("Error parsing", yaml.saveToFile(sFilepathOutput, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - std::string sOriginalFileContent; - WsjcppCore::readTextFile(sFilepath, sOriginalFileContent); - std::string sOutputFileContent; - WsjcppCore::readTextFile(sFilepathOutput, sOutputFileContent); - - sOriginalFileContent += "\n"; // TODO fix - compare("compare conteent ", sOutputFileContent, sOriginalFileContent); - -} - -// --------------------------------------------------------------------- - -bool UnitTestReadWriteFile::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_read_wsjcpp_hold_yaml.cpp b/unit-tests.wsjcpp/src/unit_test_read_wsjcpp_hold_yaml.cpp deleted file mode 100644 index 930f3c2..0000000 --- a/unit-tests.wsjcpp/src/unit_test_read_wsjcpp_hold_yaml.cpp +++ /dev/null @@ -1,49 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestReadWsjcppHoldYml - -class UnitTestReadWsjcppHoldYml : public WsjcppUnitTestBase { - public: - UnitTestReadWsjcppHoldYml(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestReadWsjcppHoldYml) - -UnitTestReadWsjcppHoldYml::UnitTestReadWsjcppHoldYml() - : WsjcppUnitTestBase("UnitTestReadWsjcppHoldYml") { -} - -// --------------------------------------------------------------------- - -bool UnitTestReadWsjcppHoldYml::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestReadWsjcppHoldYml::executeTest() { - WsjcppYaml yaml; - std::string sFilepath = "./data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml"; - std::string sError; - if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } -} - -// --------------------------------------------------------------------- - -bool UnitTestReadWsjcppHoldYml::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_read_yaml.cpp b/unit-tests.wsjcpp/src/unit_test_read_yaml.cpp deleted file mode 100644 index 017b15a..0000000 --- a/unit-tests.wsjcpp/src/unit_test_read_yaml.cpp +++ /dev/null @@ -1,238 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestReadYaml - -class UnitTestReadYaml : public WsjcppUnitTestBase { - public: - UnitTestReadYaml(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestReadYaml) - -UnitTestReadYaml::UnitTestReadYaml() - : WsjcppUnitTestBase("UnitTestReadYaml") { -} - -// --------------------------------------------------------------------- - -bool UnitTestReadYaml::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestReadYaml::executeTest() { - WsjcppYaml yaml; - std::string sFilepath = "./data-tests/read-file/example-voiting-app/docker-compose.yml"; - std::string sError; - if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - compare("has version", yaml.getRoot()->hasElement("version"), true); - compare("has version is value", yaml.getRoot()->getElement("version")->isValue(), true); - compare("version-value", yaml.getRoot()->getElement("version")->getValue(), "3"); - - compare("has services", yaml.getRoot()->hasElement("services"), true); - compare("has services is map", yaml.getRoot()->getElement("services")->isMap(), true); - - - WsjcppYamlNode *pServices = yaml.getRoot()->getElement("services"); - - // services.vote - { - compare("has services.vote", pServices->hasElement("vote"), true); - compare("services.vote is map", pServices->getElement("vote")->isMap(), true); - compare("services.vote keys size 5", pServices->getElement("vote")->getKeys().size(), 5); - - WsjcppYamlNode *pVote = pServices->getElement("vote"); - - compare("has services.vote.build", pVote->hasElement("build"), true); - compare("services.vote.build val", pVote->getElement("build")->getValue(), "./vote"); - - compare("has services.vote.command", pVote->hasElement("command"), true); - compare("services.vote.command val", pVote->getElement("command")->getValue(), "python app.py"); - - WsjcppYamlNode *pVolumes = pVote->getElement("volumes"); - compare("services.vote.volumes is array", pVolumes->isArray(), true); - compare("services.vote.volumes size 1", pVolumes->getLength(), 1); - compare("services.vote.volumes val 0", pVolumes->getElement(0)->getValue(), "./vote:/app"); - - WsjcppYamlNode *pVotePorts = pVote->getElement("ports"); - compare("services.vote.ports is array", pVotePorts->isArray(), true); - compare("services.vote.ports size 1", pVotePorts->getLength(), 1); - compare("services.vote.ports val 0", pVotePorts->getElement(0)->getValue(), "5000:80"); - - WsjcppYamlNode *pVoteNetworks = pVote->getElement("networks"); - compare("services.vote.networks size 2", pVoteNetworks->getLength(), 2); - compare("services.vote.networks val 0", pVoteNetworks->getElement(0)->getValue(), "front-tier"); - compare("services.vote.networks val 1", pVoteNetworks->getElement(1)->getValue(), "back-tier"); - } - - // services.result - { - compare("has services.result", pServices->hasElement("result"), true); - compare("services.result is map", pServices->getElement("result")->isMap(), true); - compare("services.result keys size 5", pServices->getElement("result")->getKeys().size(), 5); - - WsjcppYamlNode *pResult = pServices->getElement("result"); - - compare("has services.result.build", pResult->hasElement("build"), true); - compare("services.result.build val", pResult->getElement("build")->getValue(), "./result"); - - compare("has services.result.command", pResult->hasElement("command"), true); - compare("services.result.command val", pResult->getElement("command")->getValue(), "nodemon server.js"); - - WsjcppYamlNode *pResultVolumes = pResult->getElement("volumes"); - compare("services.result.volumes is array", pResultVolumes->isArray(), true); - compare("services.result.volumes size 1", pResultVolumes->getLength(), 1); - compare("services.result.volumes val 0", pResultVolumes->getElement(0)->getValue(), "./result:/app"); - - WsjcppYamlNode *pResultPorts = pResult->getElement("ports"); - compare("services.result.ports is array", pResultPorts->isArray(), true); - compare("services.result.ports size 2", pResultPorts->getLength(), 2); - compare("services.result.ports val 0", pResultPorts->getElement(0)->getValue(), "5001:80"); - compare("services.result.ports val 1", pResultPorts->getElement(1)->getValue(), "5858:5858"); - - WsjcppYamlNode *pResultNetworks = pResult->getElement("networks"); - compare("services.result.networks size 2", pResultNetworks->getLength(), 2); - compare("services.result.networks val 0", pResultNetworks->getElement(0)->getValue(), "front-tier"); - compare("services.result.networks val 1", pResultNetworks->getElement(1)->getValue(), "back-tier"); - } - - // services.worker - { - compare("has services.worker", pServices->hasElement("worker"), true); - compare("has services.worker is map", pServices->getElement("worker")->isMap(), true); - compare("services.worker keys size 3", pServices->getElement("worker")->getKeys().size(), 3); - WsjcppYamlNode *pWorker = pServices->getElement("worker"); - - compare("has services.worker.build", pWorker->hasElement("build"), true); - - compare("has services.worker.depends_on", pWorker->hasElement("depends_on"), true); - compare("has services.worker.networks", pWorker->hasElement("networks"), true); - - WsjcppYamlNode *pWorkerBuild = pWorker->getElement("build"); - compare("services.worker.build is map", pWorkerBuild->isMap(), true); - compare("has services.worker.build.context", pWorkerBuild->hasElement("context"), true); - compare("services.worker.build.context val", pWorkerBuild->getElement("context")->getValue(), "./worker"); - - WsjcppYamlNode *pWorkerDependsOn = pWorker->getElement("depends_on"); - compare("has services.worker.depends_on", pWorkerDependsOn->isArray(), true); - compare("services.worker.depends_on size 2", pWorkerDependsOn->getLength(), 2); - compare("services.worker.depends_on val 0", pWorkerDependsOn->getElement(0)->getValue(), "redis"); - compare("services.worker.depends_on val 1", pWorkerDependsOn->getElement(1)->getValue(), "db"); - - WsjcppYamlNode *pWorkerNetworks = pWorker->getElement("networks"); - compare("services.worker.networks size 1", pWorkerNetworks->getLength(), 1); - compare("services.worker.networks val 0", pWorkerNetworks->getElement(0)->getValue(), "back-tier"); - } - - // services.redis - { - compare("has services.redis", pServices->hasElement("redis"), true); - compare("services.redis is map", pServices->getElement("redis")->isMap(), true); - compare("services.redis keys size 4", pServices->getElement("redis")->getKeys().size(), 4); - - WsjcppYamlNode *pRedis = pServices->getElement("redis"); - compare("has services.redis.image", pRedis->hasElement("image"), true); - compare("has services.redis.container_name", pRedis->hasElement("container_name"), true); - compare("has services.redis.ports", pRedis->hasElement("ports"), true); - compare("has services.redis.networks", pRedis->hasElement("networks"), true); - - compare("services.redis.image value", pRedis->getElement("image")->getValue(), "redis:alpine"); - compare("services.redis.container_name", pRedis->getElement("container_name")->getValue(), "redis"); - - WsjcppYamlNode *pRedisPorts = pRedis->getElement("ports"); - // TODO bug #17 - compare("services.redis.ports is value", pRedisPorts->isValue(), true); - compare("services.redis.ports value", pRedisPorts->getValue(), "[\"6379\"]"); - - WsjcppYamlNode *pRedisNetworks = pRedis->getElement("networks"); - compare("services.redis.networks size 1", pRedisNetworks->getLength(), 1); - compare("services.redis.networks val 0", pRedisNetworks->getElement(0)->getValue(), "back-tier"); - } - - // services.db - { - compare("has services.db", pServices->hasElement("db"), true); - compare("has services.db is map", pServices->getElement("db")->isMap(), true); - compare("services.db keys size 5", pServices->getElement("db")->getKeys().size(), 5); - - WsjcppYamlNode *pServicesDb = pServices->getElement("db"); - - compare("has services.db.image", pServicesDb->hasElement("image"), true); - compare("services.db.image value", pServicesDb->getElement("image")->getValue(), "postgres:9.4"); - compare("services.db.container_name", pServicesDb->getElement("container_name")->getValue(), "db"); - - compare("has services.db.environment", pServicesDb->hasElement("environment"), true); - compare("services.db.environment is map", pServicesDb->getElement("environment")->isMap(), true); - - WsjcppYamlNode *pDbEnvironment = pServicesDb->getElement("environment"); - - compare("has services.db.environment.POSTGRES_USER", pDbEnvironment->hasElement("POSTGRES_USER"), true); - compare("services.db.environment.POSTGRES_USER", pDbEnvironment->getElement("POSTGRES_USER")->getValue(), "postgres"); - - compare("has services.db.environment.POSTGRES_PASSWORD", pDbEnvironment->hasElement("POSTGRES_PASSWORD"), true); - compare("services.db.environment.POSTGRES_PASSWORD", pDbEnvironment->getElement("POSTGRES_PASSWORD")->getValue(), "postgres"); - - compare("has services.db.volumes", pServicesDb->hasElement("volumes"), true); - compare("services.db.volumes is array", pServicesDb->getElement("volumes")->isArray(), true); - - WsjcppYamlNode *pDbVolumes = pServicesDb->getElement("volumes"); - compare("services.db.volumes size 1", pDbVolumes->getLength(), 1); - compare("services.db.volumes val 0", pDbVolumes->getElement(0)->getValue(), "db-data:/var/lib/postgresql/data"); - - compare("has services.db.networks", pServicesDb->hasElement("networks"), true); - compare("services.db.networks is array", pServicesDb->getElement("networks")->isArray(), true); - - WsjcppYamlNode *pDbNetworks = pServicesDb->getElement("networks"); - compare("services.db.networks size 1", pDbNetworks->getLength(), 1); - compare("services.db.networks val 0", pDbNetworks->getElement(0)->getValue(), "back-tier"); - } - - // volumes - { - compare("has volumes", yaml.getRoot()->hasElement("volumes"), true); - compare("has volumes is map", yaml.getRoot()->getElement("volumes")->isMap(), true); - - WsjcppYamlNode *pVolumes = yaml.getRoot()->getElement("volumes"); - - compare("has volumes.db-data", pVolumes->hasElement("db-data"), true); - compare("has volumes.db-data is undefined", pVolumes->getElement("db-data")->isUndefined(), true); - compare("has volumes keys size 1", pVolumes->getKeys().size(), 1); - } - - // networks - { - compare("has networks", yaml.getRoot()->hasElement("networks"), true); - compare("has networks is map", yaml.getRoot()->getElement("networks")->isMap(), true); - - WsjcppYamlNode *pNeworks = yaml.getRoot()->getElement("networks"); - compare("has networks keys size 2", pNeworks->getKeys().size(), 2); - - compare("has networks.front-tier", pNeworks->hasElement("front-tier"), true); - compare("has networks.front-tier is undefined", pNeworks->getElement("front-tier")->isUndefined(), true); - - compare("has networks.back-tier", pNeworks->hasElement("back-tier"), true); - compare("has networks.back-tier is undefined", pNeworks->getElement("back-tier")->isUndefined(), true); - } -} - -// --------------------------------------------------------------------- - -bool UnitTestReadYaml::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp b/unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp deleted file mode 100644 index 4029047..0000000 --- a/unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp +++ /dev/null @@ -1,92 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestRemoveElementForMap - -class UnitTestRemoveElementForMap : public WsjcppUnitTestBase { - public: - UnitTestRemoveElementForMap(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestRemoveElementForMap) - -UnitTestRemoveElementForMap::UnitTestRemoveElementForMap() - : WsjcppUnitTestBase("UnitTestRemoveElementForMap") { -} - -// --------------------------------------------------------------------- - -bool UnitTestRemoveElementForMap::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestRemoveElementForMap::executeTest() { - std::string sTestYaml = - "# Some comment 1\n" - "map1: \n" - " map11: \n" - " map111: \n" - " param1111: v1111\n" - " param1112: v1112\n" - " map112: \n" - " param1121: v1121\n" - " param1122: v1122\n" - " map113: \n" - " param1131: v1131\n" - " param1132: v1132\n" - " map12: \n" - " param121: v121\n" - " param122: v122\n" - " map123: \n" - " param1231: v1231\n" - " param1232: v1232\n" - "param2: v2 # some comment 2\n" - "\n" // empty line - ; - - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromString("rm_elem_in_map", sTestYaml, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - WsjcppYamlNode *pMap1 = yaml.getRoot()->getElement("map1"); - WsjcppYamlNode *pMap11 = pMap1->getElement("map11"); - - compare("has map111", pMap11->hasElement("map111"), true); - compare("has map112", pMap11->hasElement("map112"), true); - compare("has map113", pMap11->hasElement("map113"), true); - pMap11->removeElement("map112"); - - compare("has map111", pMap11->hasElement("map111"), true); - compare("has map112", pMap11->hasElement("map112"), false); - compare("has map113", pMap11->hasElement("map113"), true); - pMap11->removeElement("map111"); - - compare("has map111", pMap11->hasElement("map111"), false); - compare("has map112", pMap11->hasElement("map112"), false); - compare("has map113", pMap11->hasElement("map113"), true); - - compare("has map11", pMap1->hasElement("map11"), true); - yaml.getRoot()->getElement("map1")->removeElement("map11"); - compare("has map11", pMap1->hasElement("map11"), false); -} - -// --------------------------------------------------------------------- - -bool UnitTestRemoveElementForMap::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_remove_element_in_array.cpp b/unit-tests.wsjcpp/src/unit_test_remove_element_in_array.cpp deleted file mode 100644 index 28f95de..0000000 --- a/unit-tests.wsjcpp/src/unit_test_remove_element_in_array.cpp +++ /dev/null @@ -1,65 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestRemoveElementInArray - -class UnitTestRemoveElementInArray : public WsjcppUnitTestBase { - public: - UnitTestRemoveElementInArray(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestRemoveElementInArray) - -UnitTestRemoveElementInArray::UnitTestRemoveElementInArray() - : WsjcppUnitTestBase("UnitTestRemoveElementInArray") { -} - -// --------------------------------------------------------------------- - -bool UnitTestRemoveElementInArray::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestRemoveElementInArray::executeTest() { - - std::string sFilepath = "./data-tests/remove-element-in-array.yml"; - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - - WsjcppYamlNode *pArr1 = yaml.getRoot()->getElement("arr1"); - compare("arr1 len", pArr1->getLength(), 4); - compare("arr1 name0 ", pArr1->getElement(0)->getElement("name")->getValue(), "i1"); - compare("arr1 name1 ", pArr1->getElement(1)->getElement("name")->getValue(), "i2"); - compare("arr1 name2 ", pArr1->getElement(2)->getElement("name")->getValue(), "i3"); - compare("arr1 name3 ", pArr1->getElement(3)->getValue(), "very different array items type"); - - pArr1->removeElement(1); - - compare("arr1 len", pArr1->getLength(), 3); - compare("arr1 name0 ", pArr1->getElement(0)->getElement("name")->getValue(), "i1"); - compare("arr1 name1 ", pArr1->getElement(1)->getElement("name")->getValue(), "i3"); - compare("arr1 name2 ", pArr1->getElement(2)->getValue(), "very different array items type"); -} - -// --------------------------------------------------------------------- - -bool UnitTestRemoveElementInArray::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_tag_names.cpp b/unit-tests.wsjcpp/src/unit_test_tag_names.cpp deleted file mode 100644 index 0770534..0000000 --- a/unit-tests.wsjcpp/src/unit_test_tag_names.cpp +++ /dev/null @@ -1,65 +0,0 @@ - -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestTagNames - -class UnitTestTagNames : public WsjcppUnitTestBase { - public: - UnitTestTagNames(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestTagNames) - -UnitTestTagNames::UnitTestTagNames() - : WsjcppUnitTestBase("UnitTestTagNames") { -} - -// --------------------------------------------------------------------- - -bool UnitTestTagNames::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestTagNames::executeTest() { - - std::string sTestYaml = "./test10: one"; - WsjcppYaml yaml; - std::string sError; - // wrong - compare("wrong name", yaml.loadFromString("wrong name", "./test10: one", sError), false); - compare("name use quotes 1", yaml.loadFromString("name use quotes", "\"./test10\": one", sError), true); - compare("name use quotes 2 - wrong", yaml.loadFromString("name use quotes", "\"./te\"st10\": one", sError), false); - compare("name use quotes 3", yaml.loadFromString("name use quotes", "\"./te\\\"st10\": one", sError), true); - - compare("array", yaml.loadFromString("array", - "arr1: \n" - " - ./te:11\n" - " - \"./te\":11\n" - , sError), true); - - compare("arr1 is array", yaml.getRoot()->getElement("arr1")->isArray(), true); - compare("arr1 size 2", yaml.getRoot()->getElement("arr1")->getLength(), 2); - compare("arr1 el 0 is value", yaml.getRoot()->getElement("arr1")->getElement(0)->isValue(), true); - compare("arr1 el 0 is value", yaml.getRoot()->getElement("arr1")->getElement(0)->getValue(), "./te:11"); - compare("arr1 el 1 is map", yaml.getRoot()->getElement("arr1")->getElement(1)->isMap(), true); - compare("arr1 el 1 is map", yaml.getRoot()->getElement("arr1")->getElement(1)->hasElement("./te"), true); - compare("arr1 el 1 is map", yaml.getRoot()->getElement("arr1")->getElement(1)->getElement("./te")->getValue(), "11"); -} - -// --------------------------------------------------------------------- - -bool UnitTestTagNames::doAfterTest() { - // do somethig after test - return true; -} - - diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp deleted file mode 100644 index 309962e..0000000 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp +++ /dev/null @@ -1,111 +0,0 @@ -#include -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestYamlParserAll - -class UnitTestYamlParserAll : public WsjcppUnitTestBase { - public: - UnitTestYamlParserAll(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestYamlParserAll) - -UnitTestYamlParserAll::UnitTestYamlParserAll() - : WsjcppUnitTestBase("UnitTestYamlParserAll") { - // -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserAll::doBeforeTest() { - // do something before test - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestYamlParserAll::executeTest() { - - std::string sTestYaml = - "# Some comment 1\n" - "test10: one\n" - "test20: two # some comment 2\n" - "\n" // empty line - "array30:\n" - " - one31 # this field for test array30\n" - " - two32\n" - " - three33\n" - "\n" // empty line - "array40: # some comment 3\n" - " - \"one#1\" # some comment\n" - " # some comment\n" - " - \"two#2\"\n" - "array50: # some comment 3\n" - " - p1: test501\n" - " p2: test502\n" - " - p1: test503\n" - " p2: test504\n" - "\n" // empty line - "map60: # some comment 4\n" - " test70: opa1\n" - " test80: opa2" - ; - - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromString("parse_all", sTestYaml, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - std::string sSaved1 = ""; - if (!compare("Error saving", yaml.saveToString(sSaved1, sError), true)) { - compare("yaml_saved 2-test", sSaved1, sTestYaml); - return; - } - - WsjcppYamlNode *pItem = nullptr; - compare("test10", yaml.getRoot()->getElement("test10")->getValue(), "one"); - compare("test20", yaml.getRoot()->getElement("test20")->getValue(), "two"); - - pItem = yaml.getRoot()->getElement("array30"); - compare("array30_length", pItem->getLength(), 3); - - pItem = yaml.getRoot()->getElement("array30")->getElement(0); - compare("test30_value", pItem->getValue(), "one31"); - compare("test30_comment", pItem->getComment(), "this field for test array30"); - pItem = yaml.getRoot()->getElement("array30")->getElement(1); - compare("test40_value", pItem->getValue(), "two32"); - compare("test40_comment", pItem->getComment(), ""); - - pItem = yaml.getRoot()->getElement("array40"); - compare("array40_length", pItem->getLength(), 2); - - pItem = yaml.getRoot()->getElement("array50"); - compare("array50_length", pItem->getLength(), 2); - - pItem = yaml.getRoot()->getElement("map60")->getElement("test70"); - compare("test70_value", pItem->getValue(), "opa1"); - - pItem = yaml.getRoot()->getElement("map60")->getElement("test80"); - compare("test80_comment", pItem->getValue(), "opa2"); - - std::string sSaved2 = ""; - if (compare("saving yaml", yaml.saveToString(sSaved2, sError), true)) { - compare("yaml_saved 1-2", sSaved1, sSaved2); - } -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserAll::doAfterTest() { - // do something after test - return true; -} diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp deleted file mode 100644 index 9bcd8a1..0000000 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestYamlParserArrayIncludedMap - -class UnitTestYamlParserArrayIncludedMap : public WsjcppUnitTestBase { - public: - UnitTestYamlParserArrayIncludedMap(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestYamlParserArrayIncludedMap) - -UnitTestYamlParserArrayIncludedMap::UnitTestYamlParserArrayIncludedMap() - : WsjcppUnitTestBase("UnitTestYamlParserArrayIncludedMap") { - // -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserArrayIncludedMap::doBeforeTest() { - // nothing - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestYamlParserArrayIncludedMap::executeTest() { - - std::string sTestYaml = - "#test array included map\n" - "param1: none value1 # it's value for something # olala \n" - "array-test2 : # some comment 2 \n" - " - value21 # comment v21 \n" - " - value22 # comment v22 \n" - " - true # comment true \n" - " # some\n" - "array-and-map:\n" - " - submap-param1: v01\n" - " submap-param2: v02\n" - " - submap-param1: v11\n" - " submap-param2: v12\n" - "param2: v2\n" - ; - - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromString("map_in_array", sTestYaml, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - WsjcppYamlNode *pItem = nullptr; - - compare("param1-value", yaml.getRoot()->getElement("param1")->getValue(), "none value1"); - compare("param1-line", yaml.getRoot()->getElement("param1")->getPlaceInFile().getLine(), "param1: none value1 # it's value for something # olala "); - compare("param1-original-number-of-line", yaml.getRoot()->getElement("param1")->getPlaceInFile().getNumberOfLine(), 1); - compare("param1-comment", yaml.getRoot()->getElement("param1")->getComment(), "it's value for something # olala"); - - compare("array-test2-length", yaml.getRoot()->getElement("array-test2")->getLength(), 3); - compare("array-test2-comment", yaml.getRoot()->getElement("array-test2")->getComment(), "some comment 2"); - - pItem = yaml.getRoot()->getElement("array-test2")->getElement(0); - compare("array-test2-element0-value", pItem->getValue(), "value21"); - compare("array-test2-element0-comment", pItem->getComment(), "comment v21"); - - pItem = yaml.getRoot()->getElement("array-test2")->getElement(1); - compare("array-test2-element1-value", yaml.getRoot()->getElement("array-test2")->getElement(1)->getValue(), "value22"); - compare("array-test2-element1-comment", yaml.getRoot()->getElement("array-test2")->getElement(1)->getComment(), "comment v22"); - - pItem = yaml.getRoot()->getElement("array-test2")->getElement(2); - compare("array-test2-element2-value", pItem->getValue(), "true"); - compare("array-test2-element2-comment", pItem->getComment(), "comment true"); - - compare("array-and-map-length", yaml.getRoot()->getElement("array-and-map")->getLength(), 2); - - pItem = yaml.getRoot()->getElement("array-and-map")->getElement(0); - compare("array-and-map-element0-value", pItem->getElement("submap-param1")->getValue(), "v01"); - compare("array-and-map-element0-value", pItem->getElement("submap-param2")->getValue(), "v02"); - - pItem = yaml.getRoot()->getElement("array-and-map")->getElement(1); - compare("array-and-map-element1-value", pItem->getElement("submap-param1")->getValue(), "v11"); - compare("array-and-map-element1-value", pItem->getElement("submap-param2")->getValue(), "v12"); - - compare("param2-value", yaml.getRoot()->getElement("param2")->getValue(), "v2"); - - std::string sSaved = ""; - if (compare("save yaml", yaml.saveToString(sSaved, sError), true)) { - compare("yaml_save", sSaved, - "# test array included map\n" // expected - "param1: none value1 # it's value for something # olala\n" - "array-test2: # some comment 2\n" - " - value21 # comment v21\n" - " - value22 # comment v22\n" - " - true # comment true\n" - " # some\n" - "array-and-map:\n" - " - submap-param1: v01\n" - " submap-param2: v02\n" - " - submap-param1: v11\n" - " submap-param2: v12\n" - "param2: v2\n" - ); - } -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserArrayIncludedMap::doAfterTest() { - // nothing - return true; -} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp deleted file mode 100644 index ef72763..0000000 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestYamlParserHierarchicalMap - -class UnitTestYamlParserHierarchicalMap : public WsjcppUnitTestBase { - public: - UnitTestYamlParserHierarchicalMap(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestYamlParserHierarchicalMap) - -UnitTestYamlParserHierarchicalMap::UnitTestYamlParserHierarchicalMap() - : WsjcppUnitTestBase("UnitTestYamlParserHierarchicalMap") { - // -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserHierarchicalMap::doBeforeTest() { - // nothing - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestYamlParserHierarchicalMap::executeTest() { - - std::string sTestYaml = - "# Some comment 1\n" - "map1: \n" - " map11: \n" - " map111: \n" - " param1111: v1111\n" - " param1112: v1112\n" - " map112: \n" - " param1121: v1121\n" - " param1122: v1122\n" - " map113: \n" - " param1131: v1131\n" - " param1132: v1132\n" - " map12: \n" - " param121: v121\n" - " param122: v122\n" - " map123: \n" - " param1231: v1231\n" - " param1232: v1232\n" - "param2: v2 # some comment 2\n" - "\n" // empty line - ; - - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromString("hard_map", sTestYaml, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - WsjcppYamlNode *pItem = nullptr; - - pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map111"); - compare("param1111", pItem->getElement("param1111")->getValue(), "v1111"); - compare("param1112", pItem->getElement("param1112")->getValue(), "v1112"); - - pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map112"); - compare("param1121", pItem->getElement("param1121")->getValue(), "v1121"); - compare("param1122", pItem->getElement("param1122")->getValue(), "v1122"); - - pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map113"); - compare("param1131", pItem->getElement("param1131")->getValue(), "v1131"); - compare("param1132", pItem->getElement("param1132")->getValue(), "v1132"); - - pItem = yaml.getRoot()->getElement("map1")->getElement("map12"); - compare("param121", pItem->getElement("param121")->getValue(), "v121"); - compare("param122", pItem->getElement("param122")->getValue(), "v122"); - - pItem = yaml.getRoot()->getElement("map1")->getElement("map12")->getElement("map123"); - compare("param1231", pItem->getElement("param1231")->getValue(), "v1231"); - compare("param1232", pItem->getElement("param1232")->getValue(), "v1232"); - - compare("param2", yaml.getRoot()->getElement("param2")->getValue(), "v2"); - compare("param2", yaml.getRoot()->getElement("param2")->getComment(), "some comment 2"); - - std::string sSaved = ""; - bool bResult = yaml.saveToString(sSaved, sError); - if (compare("save yaml", bResult, true)) { - compare("yaml_save", sSaved, - "# Some comment 1\n" - "map1:\n" - " map11:\n" - " map111:\n" - " param1111: v1111\n" - " param1112: v1112\n" - " map112:\n" - " param1121: v1121\n" - " param1122: v1122\n" - " map113:\n" - " param1131: v1131\n" - " param1132: v1132\n" - " map12:\n" - " param121: v121\n" - " param122: v122\n" - " map123:\n" - " param1231: v1231\n" - " param1232: v1232\n" - "param2: v2 # some comment 2\n" - ); - } -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserHierarchicalMap::doAfterTest() { - // nothing - return true; -} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp deleted file mode 100644 index 9fc2e8f..0000000 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestYamlParserQuotes - -class UnitTestYamlParserQuotes : public WsjcppUnitTestBase { - public: - UnitTestYamlParserQuotes(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestYamlParserQuotes) - -UnitTestYamlParserQuotes::UnitTestYamlParserQuotes() - : WsjcppUnitTestBase("UnitTestYamlParserQuotes") { - // -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserQuotes::doBeforeTest() { - // nothing - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestYamlParserQuotes::executeTest() { - - std::string sTestYaml = - "# Some comment 1\n" - "param1: \"value1\" # v1\n" - "param2: \" #$!!!value2\" # val 2\n" - "\" param3 olala\" : val 3 # val 3*** \n" - "param4: ' #$!!!value4' # val 4\n" - "'param5 aha': ' #$!!!value5' # val 5\n" - "url: \"https://github.com/wsjcpp/wsjcpp-yaml\"\n" - "issues: https://github.com/wsjcpp/wsjcpp-yaml/issues\n" - "empty: \"\"\n" - "array:\n" - " - \"https://github.com/wsjcpp/wsjcpp-core:v0.0.1\"\n" - ; - - - - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromString("parse_quotes", sTestYaml, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - // TODO: .findLine(0) - - compare("param1", yaml.getRoot()->getElement("param1")->getValue(), "value1"); - compare("param1", yaml.getRoot()->getElement("param1")->getComment(), "v1"); - - compare("param2", yaml.getRoot()->getElement("param2")->getValue(), " #$!!!value2"); - compare("param2", yaml.getRoot()->getElement("param2")->getComment(), "val 2"); - - compare(" param3 olala", yaml.getRoot()->getElement(" param3 olala")->getValue(), "val 3"); - compare(" param3 olala", yaml.getRoot()->getElement(" param3 olala")->getComment(), "val 3***"); - - compare("param4 val", yaml.getRoot()->getElement("param4")->getValue(), " #$!!!value4"); - compare("param4 comment", yaml.getRoot()->getElement("param4")->getValue(), " #$!!!value4"); - - compare("url-value", yaml.getRoot()->getElement("url")->getValue(), "https://github.com/wsjcpp/wsjcpp-yaml"); - compare("issues-value", yaml.getRoot()->getElement("issues")->getValue(), "https://github.com/wsjcpp/wsjcpp-yaml/issues"); - compare("empty-value", yaml.getRoot()->getElement("empty")->getValue(), ""); - - compare("array-element0-value", yaml.getRoot()->getElement("array")->getElement(0)->getValue(), "https://github.com/wsjcpp/wsjcpp-core:v0.0.1"); - - std::string sSaved = ""; - compare("save yaml", yaml.saveToString(sSaved, sError), true); - compare("yaml_save", sSaved, - "# Some comment 1\n" - "param1: \"value1\" # v1\n" - "param2: \" #$!!!value2\" # val 2\n" - "\" param3 olala\": val 3 # val 3***\n" - "param4: ' #$!!!value4' # val 4\n" - "'param5 aha': ' #$!!!value5' # val 5\n" - "url: \"https://github.com/wsjcpp/wsjcpp-yaml\"\n" - "issues: https://github.com/wsjcpp/wsjcpp-yaml/issues\n" - "empty: \"\"\n" - "array:\n" - " - \"https://github.com/wsjcpp/wsjcpp-core:v0.0.1\"\n" - ); -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserQuotes::doAfterTest() { - // nothing - return true; -} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp deleted file mode 100644 index d396519..0000000 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestYamlParserSimpleArray - -class UnitTestYamlParserSimpleArray : public WsjcppUnitTestBase { - public: - UnitTestYamlParserSimpleArray(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestYamlParserSimpleArray) - -UnitTestYamlParserSimpleArray::UnitTestYamlParserSimpleArray() - : WsjcppUnitTestBase("UnitTestYamlParserSimpleArray") { - // -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserSimpleArray::doBeforeTest() { - // nothing - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestYamlParserSimpleArray::executeTest() { - - std::string sFilepath = "./data-tests/parser-simple-array.yml"; - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - WsjcppYamlNode *pItem = nullptr; - - compare("param1-value", yaml.getRoot()->getElement("param1")->getValue(), "none value1"); - compare("param1-line", yaml.getRoot()->getElement("param1")->getPlaceInFile().getLine(), "param1: none value1 # it's value for something # olala "); - compare("param1-original-number-of-line", yaml.getRoot()->getElement("param1")->getPlaceInFile().getNumberOfLine(), 1); - compare("param1-comment", yaml.getRoot()->getElement("param1")->getComment(), "it's value for something # olala"); - - compare("array-test2-length", yaml.getRoot()->getElement("array-test2")->getLength(), 7); - compare("array-test2-comment", yaml.getRoot()->getElement("array-test2")->getComment(), "some comment 2"); - - pItem = yaml.getRoot()->getElement("array-test2")->getElement(0); - compare("array-test2-element0-value", pItem->getValue(), "value21"); - compare("array-test2-element0-comment", pItem->getComment(), "comment v21"); - - pItem = yaml.getRoot()->getElement("array-test2")->getElement(1); - compare("array-test2-element1-value", pItem->getValue(), "value22"); - compare("array-test2-element1-comment", pItem->getComment(), "comment v22"); - - compare("array-test2-element2-value", yaml.getRoot()->getElement("array-test2")->getElement(2)->getValue(), "true"); - compare("array-test2-element2-line", yaml.getRoot()->getElement("array-test2")->getElement(2)->getPlaceInFile().getLine(), " - true # comment true "); - compare("array-test2-element2-original-number-of-line", yaml.getRoot()->getElement("array-test2")->getElement(2)->getPlaceInFile().getNumberOfLine(), 5); - compare("array-test2-element2-comment", yaml.getRoot()->getElement("array-test2")->getElement(2)->getComment(), "comment true"); - - compare("array-test2-element3-value", yaml.getRoot()->getElement("array-test2")->getElement(3)->getValue(), "falsesome"); - compare("array-test2-element3-line", yaml.getRoot()->getElement("array-test2")->getElement(3)->getPlaceInFile().getLine(), " - falsesome "); - compare("array-test2-element3-original-number-of-line", yaml.getRoot()->getElement("array-test2")->getElement(3)->getPlaceInFile().getNumberOfLine(), 7); - compare("array-test2-element3-comment", yaml.getRoot()->getElement("array-test2")->getElement(3)->getComment(), ""); - - compare("array-test2-element4-value", yaml.getRoot()->getElement("array-test2")->getElement(4)->getValue(), "free@free"); - compare("array-test2-element4-line", yaml.getRoot()->getElement("array-test2")->getElement(4)->getPlaceInFile().getLine(), " - free@free "); - compare("array-test2-element4-original-number-of-line", yaml.getRoot()->getElement("array-test2")->getElement(4)->getPlaceInFile().getNumberOfLine(), 8); - compare("array-test2-element4-comment", yaml.getRoot()->getElement("array-test2")->getElement(4)->getComment(), ""); - - compare("array-test2-element5-value", yaml.getRoot()->getElement("array-test2")->getElement(5)->getValue(), ""); - compare("array-test2-element5-line", yaml.getRoot()->getElement("array-test2")->getElement(5)->getPlaceInFile().getLine(), " - # empty "); - compare("array-test2-element5-original-number-of-line", yaml.getRoot()->getElement("array-test2")->getElement(5)->getPlaceInFile().getNumberOfLine(), 9); - compare("array-test2-element5-comment", yaml.getRoot()->getElement("array-test2")->getElement(5)->getComment(), "empty"); - - compare("array-test2-element6-value", yaml.getRoot()->getElement("array-test2")->getElement(6)->getValue(), "1"); - compare("array-test2-element6-line", yaml.getRoot()->getElement("array-test2")->getElement(6)->getPlaceInFile().getLine(), " - 1"); - compare("array-test2-element6-original-number-of-line", yaml.getRoot()->getElement("array-test2")->getElement(6)->getPlaceInFile().getNumberOfLine(), 10); - compare("array-test2-element6-comment", yaml.getRoot()->getElement("array-test2")->getElement(6)->getComment(), ""); - - compare("param2-value", yaml.getRoot()->getElement("param2")->getValue(), "val2"); - compare("param2-line", yaml.getRoot()->getElement("param2")->getPlaceInFile().getLine(), "param2: val2 # value 2 "); - compare("param2-original-number-of-line", yaml.getRoot()->getElement("param2")->getPlaceInFile().getNumberOfLine(), 11); - compare("param2-comment", yaml.getRoot()->getElement("param2")->getComment(), "value 2"); - - std::string sSaved = ""; - bool bResult = yaml.saveToString(sSaved, sError); - compare("save yaml", bResult, true); - compare("yaml_save", sSaved, - "# simple array test\n" // expected - "param1: none value1 # it's value for something # olala\n" - "array-test2: # some comment 2\n" - " - value21 # comment v21\n" - " - value22 # comment v22\n" - " - true # comment true\n" - " # some\n" - " - falsesome\n" - " - free@free\n" - " - # empty\n" - " - 1\n" - "param2: val2 # value 2\n" - ); -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserSimpleArray::doAfterTest() { - // nothing - return true; -} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp deleted file mode 100644 index c8778bf..0000000 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include - -// --------------------------------------------------------------------- -// UnitTestYamlParserSimpleMap - -class UnitTestYamlParserSimpleMap : public WsjcppUnitTestBase { - public: - UnitTestYamlParserSimpleMap(); - virtual bool doBeforeTest() override; - virtual void executeTest() override; - virtual bool doAfterTest() override; -}; - -REGISTRY_WSJCPP_UNIT_TEST(UnitTestYamlParserSimpleMap) - -UnitTestYamlParserSimpleMap::UnitTestYamlParserSimpleMap() - : WsjcppUnitTestBase("UnitTestYamlParserSimpleMap") { - // -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserSimpleMap::doBeforeTest() { - // nothing - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestYamlParserSimpleMap::executeTest() { - - std::string sTestYaml = - "# Some comment 1\n" - "param1: value1\n" - "param2: value2 # some comment 2\n" - "\n" // empty line - ; - - WsjcppYaml yaml; - std::string sError; - if (!compare("Error parsing", yaml.loadFromString("simple_map", sTestYaml, sError), true)) { - WsjcppLog::err(TAG, sError); - return; - } - - WsjcppYamlNode *pItem = nullptr; - compare("param1", yaml.getRoot()->getElement("param1")->getValue(), "value1"); - compare("param2", yaml.getRoot()->getElement("param2")->getValue(), "value2"); - compare("param2", yaml.getRoot()->getElement("param2")->getComment(), "some comment 2"); - - std::string sSaved = ""; - bool bResult = yaml.saveToString(sSaved, sError); - if (compare("save yaml", bResult, true)) { - compare("yaml_save", sSaved, - "# Some comment 1\n" - "param1: value1\n" - "param2: value2 # some comment 2\n" - ); - } -} - -// --------------------------------------------------------------------- - -bool UnitTestYamlParserSimpleMap::doAfterTest() { - // nothing - return true; -} \ No newline at end of file diff --git a/wsjcpp-yaml.cbproj b/wsjcpp-yaml.cbproj index fcc1e6e..eb0d31c 100644 --- a/wsjcpp-yaml.cbproj +++ b/wsjcpp-yaml.cbproj @@ -175,944 +175,6 @@ Embarcadero C++Builder Office XP Servers Package - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - wsjcpp-yaml.exe - true - - - - - wsjcpp-yaml.tds - true - - - - - 1 - - - 0 - - - - - classes - 64 - - - classes - 64 - - - - - res\xml - 1 - - - res\xml - 1 - - - - - library\lib\armeabi - 1 - - - library\lib\armeabi - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\mips - 1 - - - library\lib\mips - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable-anydpi-v21 - 1 - - - res\drawable-anydpi-v21 - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\values-v21 - 1 - - - res\values-v21 - 1 - - - - - res\values-v31 - 1 - - - res\values-v31 - 1 - - - - - res\drawable-anydpi-v26 - 1 - - - res\drawable-anydpi-v26 - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable-anydpi-v33 - 1 - - - res\drawable-anydpi-v33 - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\values-night-v21 - 1 - - - res\values-night-v21 - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-ldpi - 1 - - - res\drawable-ldpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-small - 1 - - - res\drawable-small - 1 - - - - - res\drawable-normal - 1 - - - res\drawable-normal - 1 - - - - - res\drawable-large - 1 - - - res\drawable-large - 1 - - - - - res\drawable-xlarge - 1 - - - res\drawable-xlarge - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\drawable-anydpi-v24 - 1 - - - res\drawable-anydpi-v24 - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable-night-anydpi-v21 - 1 - - - res\drawable-night-anydpi-v21 - 1 - - - - - res\drawable-anydpi-v31 - 1 - - - res\drawable-anydpi-v31 - 1 - - - - - res\drawable-night-anydpi-v31 - 1 - - - res\drawable-night-anydpi-v31 - 1 - - - - - 1 - - - 1 - - - 0 - - - - - 1 - .framework - - - 1 - .framework - - - 1 - .framework - - - 0 - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 0 - .dll;.bpl - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 0 - .bpl - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - 1 - - - 1 - - - - - - - - Contents\Resources - 1 - - - Contents\Resources - 1 - - - Contents\Resources - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 0 - - - - - library\lib\armeabi-v7a - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - - - - 1 - - - 1 - - - 1 - - - - - Assets - 1 - - - Assets - 1 - - - Assets - 1 - - - - - Assets - 1 - - - Assets - 1 - - - Assets - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - - - - - - - - - - - True True diff --git a/wsjcpp.yml b/wsjcpp.yml index e2aaf51..edfcec5 100644 --- a/wsjcpp.yml +++ b/wsjcpp.yml @@ -2,7 +2,7 @@ wsjcpp_version: v0.0.1 cmake_minimum_required: 3.0 cmake_cxx_standard: 17 name: wsjcpp-yaml -version: v0.1.8 +version: v0.1.9 description: Read/Write yaml files issues: https://github.com/wsjcpp/wsjcpp-yaml/issues @@ -20,14 +20,6 @@ authors: - name: Evgenii Sopov email: mrseakg@gmail.com -dependencies: - - name: "wsjcpp-core" - version: "v0.2.1" - url: "https://github.com/wsjcpp/wsjcpp-core:master" - origin: "https://github.com/" - installation-dir: "./src.wsjcpp/wsjcpp_core" - installation-datetime: "Wed, 12 Jan 2022 14:25:07 GMT" - distribution: - source-file: src/wsjcpp_yaml.cpp target-file: wsjcpp_yaml.cpp @@ -36,41 +28,3 @@ distribution: target-file: wsjcpp_yaml.h type: "source-code" -unit-tests: - cases: - - name: LineParser - description: Test Parse YAML Line - - name: YamlParserSimpleArray - description: Test Parse Array - - name: YamlParserSimpleMap - description: Test Parse Map - - name: YamlParserHierarchicalMap - description: Test Hierarchical Map - - name: YamlParserQuotes - description: YamlParserQuotes - - name: YamlParserAll - description: YamlParserAll - - name: YamlParserArrayIncludedMap - description: YamlParserArrayIncludedMap - - name: "RemoveElementForMap" - description: "" - - name: "RemoveElementInArray" - description: "" - - name: "MemoryLeaks" - description: "" - - name: "ReadYaml" - description: "" - - name: "ReadWriteFile" - description: "" - - name: "Cursor" - description: "" - - name: "TagNames" - description: "" - - name: "Cleanup" - description: "" - - name: "AppendElements" - description: "" - - name: "ReadWsjcppHoldYml" - description: "" - - name: "KeepFormat" - description: ""