1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-02-09 09:23:56 +00:00

Add unit test for the SettingsBag.

This commit is contained in:
King_DuckZ 2017-05-05 09:48:46 +01:00
parent 85363e0db1
commit b4291becf0
4 changed files with 96 additions and 6 deletions

View file

@ -35,6 +35,7 @@
#include <boost/phoenix/bind/bind_member_variable.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <type_traits>
#include <iterator>
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<std::string::const_iterator, blank_type> 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 {

View file

@ -22,6 +22,7 @@
#include <iterator>
#include <boost/utility/string_ref.hpp>
#include <string>
#include <cassert>
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

View file

@ -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}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "catch.hpp"
#include "settings_bag.hpp"
#include "ini_file.hpp"
#include "safe_stack_object.hpp"
#include <iterator>
#include <sstream>
#include <cstdint>
#include <spdlog/spdlog.h>
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<int>(ss.str().size());
REQUIRE(ss.tellg() == 0);
auto ini = SafeStackObject<IniFile>(std::istream_iterator<char>(ss), std::istream_iterator<char>());
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<uint32_t>("int_num") == 42);
CHECK(settings.as<std::string>("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<uint32_t>("not_in_ini_num") == 400);
}