From 5aa243f9da1c8ae425ae5b6706fe889f6e301959 Mon Sep 17 00:00:00 2001 From: Marius Hanl Date: Mon, 13 Jun 2022 23:35:02 +0200 Subject: Added the possibility to define a default value in ProjectSettings.get_setting(), which is used when no setting is set. Also added tests for the project settings. Co-authored-by: Yuri Sizov <11782833+YuriSizov@users.noreply.github.com> --- tests/core/config/test_project_settings.h | 102 ++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/core/config/test_project_settings.h (limited to 'tests/core') diff --git a/tests/core/config/test_project_settings.h b/tests/core/config/test_project_settings.h new file mode 100644 index 0000000000..c99ba76a24 --- /dev/null +++ b/tests/core/config/test_project_settings.h @@ -0,0 +1,102 @@ +/*************************************************************************/ +/* test_project_settings.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef TEST_PROJECT_SETTINGS_H +#define TEST_PROJECT_SETTINGS_H + +#include "core/config/project_settings.h" +#include "core/variant/variant.h" +#include "tests/test_macros.h" + +namespace TestProjectSettings { + +TEST_CASE("[ProjectSettings] Get existing setting") { + CHECK(ProjectSettings::get_singleton()->has_setting("application/config/name")); + + Variant variant = ProjectSettings::get_singleton()->get_setting("application/config/name"); + CHECK_EQ(variant.get_type(), Variant::STRING); + + String name = variant; + CHECK_EQ(name, "GDScript Integration Test Suite"); +} + +TEST_CASE("[ProjectSettings] Default value is ignored if setting exists") { + CHECK(ProjectSettings::get_singleton()->has_setting("application/config/name")); + + Variant variant = ProjectSettings::get_singleton()->get_setting("application/config/name", "SomeDefaultValue"); + CHECK_EQ(variant.get_type(), Variant::STRING); + + String name = variant; + CHECK_EQ(name, "GDScript Integration Test Suite"); +} + +TEST_CASE("[ProjectSettings] Non existing setting is null") { + CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting")); + + Variant variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting"); + CHECK_EQ(variant.get_type(), Variant::NIL); +} + +TEST_CASE("[ProjectSettings] Non existing setting should return default value") { + CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting")); + + Variant variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting"); + CHECK_EQ(variant.get_type(), Variant::NIL); + + variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting", "my_nice_default_value"); + CHECK_EQ(variant.get_type(), Variant::STRING); + + String name = variant; + CHECK_EQ(name, "my_nice_default_value"); + + CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting")); +} + +TEST_CASE("[ProjectSettings] Set value should be returned when retrieved") { + CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("my_custom_setting")); + + Variant variant = ProjectSettings::get_singleton()->get_setting("my_custom_setting"); + CHECK_EQ(variant.get_type(), Variant::NIL); + + ProjectSettings::get_singleton()->set_setting("my_custom_setting", true); + CHECK(ProjectSettings::get_singleton()->has_setting("my_custom_setting")); + + variant = ProjectSettings::get_singleton()->get_setting("my_custom_setting"); + CHECK_EQ(variant.get_type(), Variant::BOOL); + + bool value = variant; + CHECK_EQ(true, value); + + CHECK(ProjectSettings::get_singleton()->has_setting("my_custom_setting")); +} + +} // namespace TestProjectSettings + +#endif // TEST_PROJECT_SETTINGS_H -- cgit v1.2.3