1
1
forked from 0ad/0ad
0ad/source/ps/tests/test_ConfigDB.h
wraitii 66fbc23f2c De-singletonify CConfigDB, make hooks RAII.
CConfigDB is no longer a singleton, and its members are no longer
static.
This makes it possible to have several instances of it, useful for
testing of if we ever want to have different config profiles.

Also updates hooks to delete themselves on destruction, to fit a more
natural RAII pattern. Long-lived objects must take care to destroy hooks
before ConfigDB is reset.

Differential Revision: https://code.wildfiregames.com/D3866
This was SVN commit r25326.
2021-04-27 07:45:40 +00:00

89 lines
2.2 KiB
C++

/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. 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 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/self_test.h"
#include "lib/file/vfs/vfs.h"
#include "ps/ConfigDB.h"
#include <memory>
extern PIVFS g_VFS;
class TestConfigDB : public CxxTest::TestSuite
{
std::unique_ptr<CConfigDB> configDB;
public:
void setUp()
{
g_VFS = CreateVfs();
TS_ASSERT_OK(g_VFS->Mount(L"config", DataDir() / "_testconfig" / ""));
configDB = std::make_unique<CConfigDB>();
}
void tearDown()
{
DeleteDirectory(DataDir()/"_testconfig");
g_VFS.reset();
configDB.reset();
}
void test_setting_int()
{
configDB->SetConfigFile(CFG_SYSTEM, "config/file.cfg");
configDB->WriteFile(CFG_SYSTEM);
configDB->Reload(CFG_SYSTEM);
configDB->SetValueString(CFG_SYSTEM, "test_setting", "5");
configDB->WriteFile(CFG_SYSTEM);
configDB->Reload(CFG_SYSTEM);
{
std::string res;
configDB->GetValue(CFG_SYSTEM, "test_setting", res);
TS_ASSERT_EQUALS(res, "5");
}
{
int res;
configDB->GetValue(CFG_SYSTEM, "test_setting", res);
TS_ASSERT_EQUALS(res, 5);
}
}
void test_setting_empty()
{
configDB->SetConfigFile(CFG_SYSTEM, "config/file.cfg");
configDB->WriteFile(CFG_SYSTEM);
configDB->Reload(CFG_SYSTEM);
configDB->SetValueList(CFG_SYSTEM, "test_setting", {});
configDB->WriteFile(CFG_SYSTEM);
configDB->Reload(CFG_SYSTEM);
{
std::string res = "toto";
configDB->GetValue(CFG_SYSTEM, "test_setting", res);
// Empty config values don't overwrite
TS_ASSERT_EQUALS(res, "toto");
}
{
int res = 3;
configDB->GetValue(CFG_SYSTEM, "test_setting", res);
// Empty config values don't overwrite
TS_ASSERT_EQUALS(res, 3);
}
}
};