1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Settings: Proper priority hierarchy

Remove old defaults system
Introduce priority-based fallback list
Use new functions for map_meta special functions
Change groups to use end tags

Unittest changes:
 * Adapt unittest to the new code
 * Compare Settings objects
This commit is contained in:
SmallJoker 2020-11-22 17:49:30 +01:00 committed by sfan5
parent 5e9dd1667b
commit 37a05ec8d6
21 changed files with 358 additions and 298 deletions

View file

@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cmath>
#include "settings.h"
#include "defaultsettings.h"
#include "noise.h"
class TestSettings : public TestBase {
@ -31,6 +32,7 @@ public:
void runTests(IGameDef *gamedef);
void testAllSettings();
void testDefaults();
void testFlagDesc();
static const char *config_text_before;
@ -42,6 +44,7 @@ static TestSettings g_test_instance;
void TestSettings::runTests(IGameDef *gamedef)
{
TEST(testAllSettings);
TEST(testDefaults);
TEST(testFlagDesc);
}
@ -70,7 +73,8 @@ const char *TestSettings::config_text_before =
" with leading whitespace!\n"
"\"\"\"\n"
"np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.7, 2.4\n"
"zoop = true";
"zoop = true\n"
"[dummy_eof_end_tag]\n";
const std::string TestSettings::config_text_after =
"leet = 1337\n"
@ -111,12 +115,34 @@ const std::string TestSettings::config_text_after =
" animals = cute\n"
" num_apples = 4\n"
" num_oranges = 53\n"
"}\n";
"}\n"
"[dummy_eof_end_tag]";
void compare_settings(const std::string &name, Settings *a, Settings *b)
{
auto keys = a->getNames();
Settings *group1, *group2;
std::string value1, value2;
for (auto &key : keys) {
if (a->getGroupNoEx(key, group1)) {
UASSERT(b->getGroupNoEx(key, group2));
compare_settings(name + "->" + key, group1, group2);
continue;
}
UASSERT(b->getNoEx(key, value1));
// For identification
value1 = name + "->" + key + "=" + value1;
value2 = name + "->" + key + "=" + a->get(key);
UASSERTCMP(std::string, ==, value2, value1);
}
}
void TestSettings::testAllSettings()
{
try {
Settings s;
Settings s("[dummy_eof_end_tag]");
// Test reading of settings
std::istringstream is(config_text_before);
@ -197,21 +223,44 @@ void TestSettings::testAllSettings()
is.clear();
is.seekg(0);
UASSERT(s.updateConfigObject(is, os, "", 0) == true);
//printf(">>>> expected config:\n%s\n", TEST_CONFIG_TEXT_AFTER);
//printf(">>>> actual config:\n%s\n", os.str().c_str());
#if __cplusplus < 201103L
// This test only works in older C++ versions than C++11 because we use unordered_map
UASSERT(os.str() == config_text_after);
#endif
UASSERT(s.updateConfigObject(is, os, 0) == true);
{
// Confirm settings
Settings s2("[dummy_eof_end_tag]");
std::istringstream is(config_text_after, std::ios_base::binary);
s2.parseConfigLines(is);
compare_settings("(main)", &s, &s2);
}
} catch (SettingNotFoundException &e) {
UASSERT(!"Setting not found!");
}
}
void TestSettings::testDefaults()
{
Settings *game = Settings::createLayer(SL_GAME);
Settings *def = Settings::getLayer(SL_DEFAULTS);
def->set("name", "FooBar");
UASSERT(def->get("name") == "FooBar");
UASSERT(game->get("name") == "FooBar");
game->set("name", "Baz");
UASSERT(game->get("name") == "Baz");
delete game;
// Restore default settings
delete Settings::getLayer(SL_DEFAULTS);
set_default_settings();
}
void TestSettings::testFlagDesc()
{
Settings s;
Settings &s = *Settings::createLayer(SL_GAME);
FlagDesc flagdesc[] = {
{ "biomes", 0x01 },
{ "trees", 0x02 },
@ -242,4 +291,6 @@ void TestSettings::testFlagDesc()
// Enabled: tables
s.set("test_flags", "16");
UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == 0x10);
delete &s;
}