#include "cxxtest/TestSuite.h" #include "utility/ConfigManager.h" #include "utility/text/TextAccess.h" class ConfigManagerTestSuite: public CxxTest::TestSuite { public: void test_config_manager_returns_true_when_key_is_found() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); float value; bool success = config->getValue("path/to/single_value", value); TS_ASSERT(success); } void test_config_manager_returns_false_when_key_is_not_found() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); float value; bool success = config->getValue("path/to/nowhere", value); TS_ASSERT(!success); } void test_config_manager_returns_correct_string_for_key() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); std::string value; config->getValue("path/to/single_value", value); TS_ASSERT_EQUALS("42", value); } void test_config_manager_returns_correct_float_for_key() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); float value; config->getValue("path/to/single_value", value); TS_ASSERT_DELTA(42.0f, value, 0.0001f); } void test_config_manager_returns_correct_bool_for_key_if_value_is_true() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); float value; bool success(config->getValue("path/to/bool_that_is_true", value)); TS_ASSERT(success); TS_ASSERT(value); } void test_config_manager_returns_correct_bool_for_key_if_value_is_false() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); float value; bool success(config->getValue("path/to/bool_that_is_false", value)); TS_ASSERT(success); TS_ASSERT(!value); } void test_config_manager_adds_new_key_when_empty() { std::shared_ptr config = ConfigManager::createEmpty(); config->setValue("path/to/true_bool", true); bool value = false; bool success(config->getValue("path/to/true_bool", value)); TS_ASSERT(success); TS_ASSERT(value); } void test_config_manager_adds_new_key_when_not_empty() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); config->setValue("path/to/true_bool", true); bool value = false; bool success(config->getValue("path/to/true_bool", value)); TS_ASSERT(success); TS_ASSERT(value); } void test_config_manager_returns_correct_list_for_key() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); std::vector values; bool success(config->getValues("paths/path", values)); TS_ASSERT(success); TS_ASSERT_EQUALS(values.size(), 3); TS_ASSERT_EQUALS(values[0], 2); TS_ASSERT_EQUALS(values[1], 5); TS_ASSERT_EQUALS(values[2], 8); } void test_config_manager_save_and_load_configuration_and_compare() { std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); config->save("data/temp.xml"); std::shared_ptr config2 = ConfigManager::createAndLoad(TextAccess::createFromFile("data/temp.xml")); TS_ASSERT_EQUALS(config->toString(), config2->toString()); } private: std::shared_ptr getConfigTextAccess() { std::string text = "\n" "\n" "\n" "\n" "0\n" "1\n" "42\n" "\n" "\n" "\n" "4\n" "2\n" "5\n" "8\n" "\n" "\n"; return TextAccess::createFromString(text); } };