From b4291becf0123cadae5779e63bab15d52a24ceb2 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 5 May 2017 09:48:46 +0100 Subject: [PATCH] Add unit test for the SettingsBag. --- src/tawashi_implem/ini_file.cpp | 15 ++++--- src/tawashi_implem/ini_file.hpp | 13 +++++- test/unit/CMakeLists.txt | 4 ++ test/unit/test_settings_bag.cpp | 70 +++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 test/unit/test_settings_bag.cpp diff --git a/src/tawashi_implem/ini_file.cpp b/src/tawashi_implem/ini_file.cpp index 5217da1..290b058 100644 --- a/src/tawashi_implem/ini_file.cpp +++ b/src/tawashi_implem/ini_file.cpp @@ -35,6 +35,7 @@ #include #include #include +#include namespace tawashi { namespace { @@ -89,15 +90,18 @@ namespace tawashi { start = *(section_head >> key_values); } - IniFile::IniMapType parse_ini (const std::string* parIni) { + IniFile::IniMapType parse_ini (const std::string* parIni, bool& parParseOk, int& parParsedCharCount) { using boost::spirit::qi::blank; using boost::spirit::qi::blank_type; IniGrammar gramm(parIni); IniFile::IniMapType result; + parParseOk = false; + parParsedCharCount = 0; + std::string::const_iterator start_it = parIni->cbegin(); - /*const bool parse_ok =*/ boost::spirit::qi::phrase_parse( + const bool parse_ok = boost::spirit::qi::phrase_parse( start_it, parIni->cend(), gramm, @@ -105,7 +109,8 @@ namespace tawashi { result ); - //assert(parse_ok and (parIni->cend() == start_it)); + parParseOk = parse_ok and (parIni->cend() == start_it); + parParsedCharCount = std::distance(parIni->cbegin(), start_it); return result; } } //unnamed namespace @@ -117,7 +122,7 @@ namespace tawashi { IniFile::IniFile (std::string&& parIniData) : m_raw_ini(std::move(parIniData)), - m_map(parse_ini(&m_raw_ini)) + m_map(parse_ini(&m_raw_ini, m_parse_ok, m_parsed_chars)) { } @@ -127,7 +132,7 @@ namespace tawashi { if (m_raw_ini.data() == old_data_ptr) m_map = std::move(parOther.m_map); else - m_map = parse_ini(&m_raw_ini); + m_map = parse_ini(&m_raw_ini, m_parse_ok, m_parsed_chars); } IniFile::~IniFile() noexcept { diff --git a/src/tawashi_implem/ini_file.hpp b/src/tawashi_implem/ini_file.hpp index ec5abcd..82973e0 100644 --- a/src/tawashi_implem/ini_file.hpp +++ b/src/tawashi_implem/ini_file.hpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace tawashi { class IniFile : public Kakoune::SafeCountable { @@ -38,10 +39,20 @@ namespace tawashi { IniFile& operator== (IniFile&&) = delete; IniFile& operator== (const IniFile&) = delete; - const IniMapType& parsed() const { return m_map; } + bool parse_success() const { return m_parse_ok; } + int parsed_characters() const { return m_parsed_chars; } + + const IniMapType& parsed() const; private: std::string m_raw_ini; IniMapType m_map; + int m_parsed_chars; + bool m_parse_ok; }; + + inline const IniFile::IniMapType& IniFile::parsed() const { + assert(parse_success()); + return m_map; + } } //namespace tawashi diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 57d74fb..99b06df 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -1,7 +1,11 @@ project(tawashi_unittest CXX) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + add_executable(${PROJECT_NAME} check.cpp + test_settings_bag.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/test/unit/test_settings_bag.cpp b/test/unit/test_settings_bag.cpp new file mode 100644 index 0000000..149e8e7 --- /dev/null +++ b/test/unit/test_settings_bag.cpp @@ -0,0 +1,70 @@ +/* Copyright 2017, Michele Santullo + * This file is part of "tawashi". + * + * "tawashi" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "tawashi" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "tawashi". If not, see . + */ + +#include "catch.hpp" +#include "settings_bag.hpp" +#include "ini_file.hpp" +#include "safe_stack_object.hpp" +#include +#include +#include +#include + +TEST_CASE ("Add and retrieve values from SettingsBag", "[settings][ini]") { + using tawashi::SettingsBag; + using tawashi::IniFile; + using curry::SafeStackObject; + + auto statuslog = spdlog::stdout_logger_st("statuslog"); + + std::stringstream ss; + ss << std::noskipws; + ss << "[tawashi]\n" << + "redis_server = 127.0.0.1\n" << + "empty=\n" << + "empty_spaces= \n" << + "int_num= 42\n" << + "negative_int_num = -5\n" << + "redis_mode = inet\n" << + "base_uri = http://127.0.0.1:8080\n" + ; + ss.seekg(0); + const int raw_ini_char_count = static_cast(ss.str().size()); + REQUIRE(ss.tellg() == 0); + + auto ini = SafeStackObject(std::istream_iterator(ss), std::istream_iterator()); + REQUIRE(ini->parse_success()); + REQUIRE(ini->parsed_characters() == raw_ini_char_count); + SettingsBag settings(ini); + + CHECK(settings["redis_server"] == "127.0.0.1"); + CHECK(settings["empty"].empty()); + CHECK(settings["empty_spaces"].empty()); + CHECK(settings.as("int_num") == 42); + CHECK(settings.as("redis_mode") == "inet"); + CHECK(settings["base_uri"] == "http://127.0.0.1:8080"); + + settings.add_default("redis_server", "192.168.0.5"); + settings.add_default("not_in_ini_empty", ""); + settings.add_default("not_in_ini", "abcd"); + settings.add_default("not_in_ini_num", "400"); + + CHECK(settings["redis_server"] == "127.0.0.1"); + CHECK(settings["not_in_ini_empty"].empty()); + CHECK(settings["not_in_ini"] == "abcd"); + CHECK(settings.as("not_in_ini_num") == 400); +}