mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 14:38:29 +01:00
util: Add ReadSettings and WriteSettings functions
Currently unused, but includes tests.
This commit is contained in:
@@ -12,10 +12,90 @@
|
||||
#include <univalue.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
#include <util/system.h>
|
||||
#include <vector>
|
||||
|
||||
inline bool operator==(const util::SettingsValue& a, const util::SettingsValue& b)
|
||||
{
|
||||
return a.write() == b.write();
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const util::SettingsValue& value)
|
||||
{
|
||||
os << value.write();
|
||||
return os;
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const std::pair<std::string, util::SettingsValue>& kv)
|
||||
{
|
||||
util::SettingsValue out(util::SettingsValue::VOBJ);
|
||||
out.__pushKV(kv.first, kv.second);
|
||||
os << out.write();
|
||||
return os;
|
||||
}
|
||||
|
||||
inline void WriteText(const fs::path& path, const std::string& text)
|
||||
{
|
||||
fsbridge::ofstream file;
|
||||
file.open(path);
|
||||
file << text;
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(settings_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ReadWrite)
|
||||
{
|
||||
fs::path path = GetDataDir() / "settings.json";
|
||||
|
||||
WriteText(path, R"({
|
||||
"string": "string",
|
||||
"num": 5,
|
||||
"bool": true,
|
||||
"null": null
|
||||
})");
|
||||
|
||||
std::map<std::string, util::SettingsValue> expected{
|
||||
{"string", "string"},
|
||||
{"num", 5},
|
||||
{"bool", true},
|
||||
{"null", {}},
|
||||
};
|
||||
|
||||
// Check file read.
|
||||
std::map<std::string, util::SettingsValue> values;
|
||||
std::vector<std::string> errors;
|
||||
BOOST_CHECK(util::ReadSettings(path, values, errors));
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), expected.begin(), expected.end());
|
||||
BOOST_CHECK(errors.empty());
|
||||
|
||||
// Check no errors if file doesn't exist.
|
||||
fs::remove(path);
|
||||
BOOST_CHECK(util::ReadSettings(path, values, errors));
|
||||
BOOST_CHECK(values.empty());
|
||||
BOOST_CHECK(errors.empty());
|
||||
|
||||
// Check duplicate keys not allowed
|
||||
WriteText(path, R"({
|
||||
"dupe": "string",
|
||||
"dupe": "dupe"
|
||||
})");
|
||||
BOOST_CHECK(!util::ReadSettings(path, values, errors));
|
||||
std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", path.string())};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end());
|
||||
|
||||
// Check non-kv json files not allowed
|
||||
WriteText(path, R"("non-kv")");
|
||||
BOOST_CHECK(!util::ReadSettings(path, values, errors));
|
||||
std::vector<std::string> non_kv = {strprintf("Found non-object value \"non-kv\" in settings file %s", path.string())};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), non_kv.begin(), non_kv.end());
|
||||
|
||||
// Check invalid json not allowed
|
||||
WriteText(path, R"(invalid json)");
|
||||
BOOST_CHECK(!util::ReadSettings(path, values, errors));
|
||||
std::vector<std::string> fail_parse = {strprintf("Unable to parse settings file %s", path.string())};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), fail_parse.begin(), fail_parse.end());
|
||||
}
|
||||
|
||||
//! Check settings struct contents against expected json strings.
|
||||
static void CheckValues(const util::Settings& settings, const std::string& single_val, const std::string& list_val)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user